Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

re #1633: finish preprint->repository refactor for searching preprints #1763

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
49 changes: 26 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,52 @@ 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))
# TODO: replicate the ability to search by author and institution

from_author = core_models.Account.objects.filter(
(Q(first_name__in=split_search_term) |
Q(last_name__in=split_search_term) |
institution_query)
)
# institution_query = reduce(operator.and_, (Q(institution__icontains=x) for x 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())]
from_author = models.Author.objects.filter(
(Q(last_name__icontains=split_search_term) |
Q(first_name__icontains=split_search_term) |
Q(middle_name__icontains=split_search_term) )
).values('id')

articles = set(article_search + articles_from_author)
preprints_from_author = [preprint for preprint in models.Preprint.objects.filter(
preprintauthor__author_id__in=from_author,
stage=models.STAGE_PREPRINT_PUBLISHED,
date_published__lte=timezone.now())]

preprints = set(repository_search + preprints_from_author)

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 +264,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