Skip to content

Commit

Permalink
re openlibhums#1633: finish preprint->repository refactor for searchi…
Browse files Browse the repository at this point in the history
…ng preprints

- use correct object models throughout
- tweak search form to use a name for the search_term input
- tweak list page to indicate the search_term if it's present
  • Loading branch information
hardyoyo committed Aug 10, 2020
1 parent 43e0efd commit 2e9031b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 30 deletions.
8 changes: 4 additions & 4 deletions src/repository/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
name='repository_about'),

url(r'^search/$',
views.preprints_search,
name='preprints_search'),
views.repository_search,
name='repository_search'),

url(r'^search/(?P<search_term>.*)/$',
views.preprints_search,
name='preprints_search_with_term'),
views.repository_search,
name='repository_search_with_term'),

url(r'^view/(?P<preprint_id>\d+)/$',
views.repository_preprint,
Expand Down
45 changes: 22 additions & 23 deletions src/repository/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ def repository_home(request):
:param request: HttpRequest object
:return: HttpResponse
"""

preprints = models.Preprint.objects.filter(
repository=request.repository,
date_published__lte=timezone.now(),
stage=models.STAGE_PREPRINT_PUBLISHED
)
)[:6]
subjects = models.Subject.objects.filter(
repository=request.repository,
).prefetch_related(
Expand Down Expand Up @@ -207,50 +208,48 @@ def repository_list(request, subject_slug=None):


# TODO: Re-implement
def preprints_search(request, search_term=None):
def repository_search(request, search_term=None):
"""
Searches through preprints based on their titles and authors
Searches through repository content based on titles and authors
:param request: HttpRequest
:param search_term: Optional string
:return: HttpResponse
"""
if search_term:
split_search_term = search_term.split(' ')

article_search = submission_models.Article.preprints.filter(
repository_search = models.Preprint.objects.filter(
(Q(title__icontains=search_term) |
Q(subtitle__icontains=search_term) |
Q(abstract__icontains=search_term) |
Q(keywords__word__in=split_search_term)),
stage=submission_models.STAGE_PREPRINT_PUBLISHED, date_published__lte=timezone.now()
stage=models.STAGE_PREPRINT_PUBLISHED, date_published__lte=timezone.now()
)
article_search = [article for article in article_search]
repository_search = [preprint for preprint in repository_search]

institution_query = reduce(operator.and_, (Q(institution__icontains=x) for x in split_search_term))
# institution_query = reduce(operator.and_, (Q(institution__icontains=x) for x in split_search_term))

from_author = core_models.Account.objects.filter(
(Q(first_name__in=split_search_term) |
Q(last_name__in=split_search_term) |
institution_query)
)
# from_author = models.Preprint.objects.filter(
# (Q(preprintauthor__in=split_search_term))
# )

articles_from_author = [article for article in submission_models.Article.preprints.filter(
authors__in=from_author,
stage=submission_models.STAGE_PREPRINT_PUBLISHED,
date_published__lte=timezone.now())]
# preprints_from_author = [preprint for preprint in models.Preprint.objects.filter(
# preprintauthor__in=from_author,
# stage=models.STAGE_PREPRINT_PUBLISHED,
# date_published__lte=timezone.now())]

articles = set(article_search + articles_from_author)
preprints = set(repository_search)

else:
articles = submission_models.Article.preprints.all()
preprints = models.Preprint.objects.all()

if request.POST:
search_term = request.POST.get('search_term')
return redirect(reverse('preprints_search_with_term', kwargs={'search_term': search_term}))
return redirect(reverse('repository_search_with_term', kwargs={'search_term': search_term}))

template = 'preprints/list.html'
template = 'repository/list.html'
context = {
'search_term': search_term,
'articles': articles,
'preprints': preprints,
}

return render(request, template, context)
Expand All @@ -261,7 +260,7 @@ def repository_preprint(request, preprint_id):
"""
Fetches a single article and displays its metadata
:param request: HttpRequest
:param preprint_id: integer, PK of an Article object
:param preprint_id: integer, PK of an Preprint object
:return: HttpResponse or Http404 if object not found
"""
preprint = get_object_or_404(
Expand Down
4 changes: 2 additions & 2 deletions src/themes/material/templates/repository/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ <h1 class="center">{{ request.repository.name }}</h1>


<div class="input-field col s12 m6 offset-m3 spacer">
<form method="POST" action="">
<form method="POST" action="{% url 'repository_search' %}">
{% csrf_token %}

<i class="material-icons prefix">search</i>
<input id="icon_prefix" type="text" class="validate">
<input id="icon_prefix" type="text" class="validate" name="search_term">
<label for="icon_prefix">Search {{ request.repository.object_name_plural }}</label>
</form>

Expand Down
2 changes: 1 addition & 1 deletion src/themes/material/templates/repository/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<div class="row">

<div class="col m8 spacer">
<h1>{{ request.repository.object_name_plural }}</h1>
<h1>{{ request.repository.object_name_plural }} {%if search_term %} {% trans 'matching' %}: {{search_term}} {% endif %}</h1>
{% include "repository/elements/preprint_listing.html" with preprints=preprints %}
</div>
</div>
Expand Down

0 comments on commit 2e9031b

Please sign in to comment.