From 3080e5b5b661ed92aeeec1b5b728d94e77c2ff35 Mon Sep 17 00:00:00 2001 From: Denis Istratov Date: Thu, 28 May 2026 17:51:07 +0300 Subject: [PATCH] hw 6-7 some fixes --- questions/static/core/css/style.css | 7 ++- questions/static/core/js/script.js | 52 +++++++++++------------ questions/templates/core/layout/base.html | 2 +- questions/templates/core/profile.html | 2 +- questions/urls.py | 1 + questions/views.py | 9 ++-- static/core/css/style.css | 7 ++- static/core/js/script.js | 52 +++++++++++------------ 8 files changed, 67 insertions(+), 65 deletions(-) diff --git a/questions/static/core/css/style.css b/questions/static/core/css/style.css index c9ae6df..8b7009b 100644 --- a/questions/static/core/css/style.css +++ b/questions/static/core/css/style.css @@ -221,15 +221,18 @@ a:hover { .search-suggestions { position: absolute; + top: calc(100% + 0.4rem); + left: 0; + right: 0; background: #ffffff; border: 1px solid #1f2d3a; border-radius: 0.5rem; - width: calc(100% - 2rem); + width: 100%; + box-sizing: border-box; max-height: 18rem; overflow: auto; display: none; z-index: 100; - margin-top: 0.4rem; } .search-suggestion { diff --git a/questions/static/core/js/script.js b/questions/static/core/js/script.js index 15c5fc3..ca829ab 100644 --- a/questions/static/core/js/script.js +++ b/questions/static/core/js/script.js @@ -55,6 +55,30 @@ if (searchInput) { }); } +function askItSearch(event) { + event.preventDefault(); + const query = searchInput ? searchInput.value.trim() : ''; + if (!query) { + return false; + } + + fetch(`/search?q=${encodeURIComponent(query)}`) + .then(response => response.json()) + .then(data => { + const results = data.results || []; + if (results.length) { + window.location.href = results[0].url; + } else { + window.location.href = `/ask?q=${encodeURIComponent(query)}`; + } + }) + .catch(() => { + window.location.href = `/ask?q=${encodeURIComponent(query)}`; + }); + + return false; +} + function postReaction(url, data, onSuccess) { $.ajax({ url, @@ -167,31 +191,3 @@ $(document).on('click', '.correct-answer-btn', function () { console.error('centrifuge init error', e); } })(); -// Handle ASK IT button - transfer search text to ask page -function redirectToAsk(event) { - event.preventDefault(); - const searchInput = document.getElementById('search-input'); - const query = searchInput ? searchInput.value.trim() : ''; - const url = query ? `/ask?q=${encodeURIComponent(query)}` : '/ask'; - window.location.href = url; -} - -// Populate title field from URL parameter on ask page -function initializeTitleFromQuery() { - const params = new URLSearchParams(window.location.search); - const query = params.get('q'); - if (query) { - const titleField = document.querySelector('[name="title"]'); - if (titleField) { - titleField.value = query; - titleField.focus(); - } - } -} - -// Run immediately (script.js loads at end of page, DOMContentLoaded may have already fired) -if (document.readyState === 'loading') { - document.addEventListener('DOMContentLoaded', initializeTitleFromQuery); -} else { - initializeTitleFromQuery(); -} \ No newline at end of file diff --git a/questions/templates/core/layout/base.html b/questions/templates/core/layout/base.html index b920c6e..daa5999 100644 --- a/questions/templates/core/layout/base.html +++ b/questions/templates/core/layout/base.html @@ -24,7 +24,7 @@
diff --git a/questions/templates/core/profile.html b/questions/templates/core/profile.html index f0459ce..6f97a4d 100644 --- a/questions/templates/core/profile.html +++ b/questions/templates/core/profile.html @@ -3,7 +3,7 @@ {% block content %}
-

Profile: Dr. Pepper

+

Profile:

diff --git a/questions/urls.py b/questions/urls.py index 9daace6..3e7a422 100644 --- a/questions/urls.py +++ b/questions/urls.py @@ -7,6 +7,7 @@ from questions import views urlpatterns = [ path('', views.index, name='index'), path('login', views.login, name='login'), + path('login/', views.login), path('ask', views.ask, name='ask'), path('profile', views.profile, name='profile'), path('question', views.question, name='question'), diff --git a/questions/views.py b/questions/views.py index 0635ca7..2703de1 100644 --- a/questions/views.py +++ b/questions/views.py @@ -112,11 +112,14 @@ def search(request): if not query_text: return JsonResponse({'results': []}) - vector = SearchVector('title', 'body') - query = SearchQuery(query_text) + vector = ( + SearchVector('title', weight='A') + + SearchVector('body', weight='B') + ) + query = SearchQuery(query_text, search_type='websearch') questions = Question.objects.annotate( rank=SearchRank(vector, query) - ).filter(rank__gte=0.1).order_by('-rank')[:10] + ).filter(rank__gt=0).order_by('-rank')[:10] results = [ { diff --git a/static/core/css/style.css b/static/core/css/style.css index c9ae6df..8b7009b 100644 --- a/static/core/css/style.css +++ b/static/core/css/style.css @@ -221,15 +221,18 @@ a:hover { .search-suggestions { position: absolute; + top: calc(100% + 0.4rem); + left: 0; + right: 0; background: #ffffff; border: 1px solid #1f2d3a; border-radius: 0.5rem; - width: calc(100% - 2rem); + width: 100%; + box-sizing: border-box; max-height: 18rem; overflow: auto; display: none; z-index: 100; - margin-top: 0.4rem; } .search-suggestion { diff --git a/static/core/js/script.js b/static/core/js/script.js index 15c5fc3..ca829ab 100644 --- a/static/core/js/script.js +++ b/static/core/js/script.js @@ -55,6 +55,30 @@ if (searchInput) { }); } +function askItSearch(event) { + event.preventDefault(); + const query = searchInput ? searchInput.value.trim() : ''; + if (!query) { + return false; + } + + fetch(`/search?q=${encodeURIComponent(query)}`) + .then(response => response.json()) + .then(data => { + const results = data.results || []; + if (results.length) { + window.location.href = results[0].url; + } else { + window.location.href = `/ask?q=${encodeURIComponent(query)}`; + } + }) + .catch(() => { + window.location.href = `/ask?q=${encodeURIComponent(query)}`; + }); + + return false; +} + function postReaction(url, data, onSuccess) { $.ajax({ url, @@ -167,31 +191,3 @@ $(document).on('click', '.correct-answer-btn', function () { console.error('centrifuge init error', e); } })(); -// Handle ASK IT button - transfer search text to ask page -function redirectToAsk(event) { - event.preventDefault(); - const searchInput = document.getElementById('search-input'); - const query = searchInput ? searchInput.value.trim() : ''; - const url = query ? `/ask?q=${encodeURIComponent(query)}` : '/ask'; - window.location.href = url; -} - -// Populate title field from URL parameter on ask page -function initializeTitleFromQuery() { - const params = new URLSearchParams(window.location.search); - const query = params.get('q'); - if (query) { - const titleField = document.querySelector('[name="title"]'); - if (titleField) { - titleField.value = query; - titleField.focus(); - } - } -} - -// Run immediately (script.js loads at end of page, DOMContentLoaded may have already fired) -if (document.readyState === 'loading') { - document.addEventListener('DOMContentLoaded', initializeTitleFromQuery); -} else { - initializeTitleFromQuery(); -} \ No newline at end of file