From 7454fb745456f6704cc7554b4a3c6fbf03a718a2 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 3 May 2020 21:13:43 -0700 Subject: [PATCH] Search is a view not an action --- fedireads/urls.py | 3 ++- fedireads/view_actions.py | 19 +------------------ fedireads/views.py | 22 ++++++++++++++++++++-- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/fedireads/urls.py b/fedireads/urls.py index f6a02bc8..bd43a72d 100644 --- a/fedireads/urls.py +++ b/fedireads/urls.py @@ -68,13 +68,14 @@ urlpatterns = [ re_path(r'^shelf/%s/(?P[\w-]+)(.json)?/?$' % username_regex, views.shelf_page), re_path(r'^shelf/%s/(?P[\w-]+)(.json)?/?$' % localname_regex, views.shelf_page), + re_path(r'^search/?$', views.search), + # internal action endpoints re_path(r'^logout/?$', actions.user_logout), re_path(r'^user-login/?$', actions.user_login), re_path(r'^register/?$', actions.register), re_path(r'^edit_profile/?$', actions.edit_profile), - re_path(r'^search/?$', actions.search), re_path(r'^import_data/?', actions.import_data), re_path(r'^edit_book/(?P\d+)/?', actions.edit_book), re_path(r'^upload_cover/(?P\d+)/?', actions.upload_cover), diff --git a/fedireads/view_actions.py b/fedireads/view_actions.py index 98c71a9f..4c7e65b9 100644 --- a/fedireads/view_actions.py +++ b/fedireads/view_actions.py @@ -1,6 +1,5 @@ ''' views for actions you can take in the application ''' from io import BytesIO, TextIOWrapper -import re from PIL import Image from django.contrib.auth import authenticate, login, logout @@ -10,7 +9,7 @@ from django.http import HttpResponseBadRequest, HttpResponseNotFound from django.shortcuts import redirect from django.template.response import TemplateResponse -from fedireads import forms, models, books_manager, outgoing +from fedireads import forms, models, outgoing from fedireads import goodreads_import from fedireads.settings import DOMAIN from fedireads.views import get_user_from_username @@ -346,22 +345,6 @@ def unfollow(request): return redirect('/user/%s' % user_slug) -@login_required -def search(request): - ''' that search bar up top ''' - query = request.GET.get('q') - if re.match(r'\w+@\w+.\w+', query): - # if something looks like a username, search with webfinger - results = [outgoing.handle_account_search(query)] - template = 'user_results.html' - else: - # just send the question over to book search - results = books_manager.search(query) - template = 'book_results.html' - - return TemplateResponse(request, template, {'results': results}) - - @login_required def clear_notifications(request): ''' permanently delete notification for user ''' diff --git a/fedireads/views.py b/fedireads/views.py index 0bd3b7df..02013740 100644 --- a/fedireads/views.py +++ b/fedireads/views.py @@ -1,4 +1,6 @@ ''' views for pages you can go to in the application ''' +import re + from django.contrib.auth.decorators import login_required from django.db.models import Avg, Q from django.http import HttpResponseBadRequest, HttpResponseNotFound,\ @@ -8,8 +10,8 @@ from django.shortcuts import redirect from django.template.response import TemplateResponse from django.views.decorators.csrf import csrf_exempt -from fedireads import activitypub -from fedireads import forms, models +from fedireads import activitypub, outgoing +from fedireads import forms, models, books_manager from fedireads import goodreads_import from fedireads.books_manager import get_or_create_book from fedireads.tasks import app @@ -141,6 +143,22 @@ def get_activity_feed(user, filter_level, model=models.Status): return activities +@login_required +def search(request): + ''' that search bar up top ''' + query = request.GET.get('q') + if re.match(r'\w+@\w+.\w+', query): + # if something looks like a username, search with webfinger + results = [outgoing.handle_account_search(query)] + template = 'user_results.html' + else: + # just send the question over to book search + results = books_manager.search(query) + template = 'book_results.html' + + return TemplateResponse(request, template, {'results': results}) + + def books_page(request): ''' discover books ''' recent_books = models.Work.objects