Skip to content

Commit

Permalink
refactor(source list view): minor source list view refactoring
Browse files Browse the repository at this point in the history
- remove unused variables
- reduce number of statements with assignment operator
  • Loading branch information
dchiller committed Aug 9, 2024
1 parent c36febf commit a206538
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions django/cantusdb_project/main_app/views/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def get_context_data(self, **kwargs):
return context


class SourceListView(ListView):
class SourceListView(ListView): # type: ignore
model = Source
paginate_by = 100
context_object_name = "sources"
Expand All @@ -232,41 +232,39 @@ def get_queryset(self) -> QuerySet[Source]:
"segment", "provenance", "holding_institution"
)

display_unpublished: bool = self.request.user.is_authenticated
if display_unpublished:
if self.request.user.is_authenticated:
q_obj_filter = Q()
else:
q_obj_filter = Q(published=True)

if self.request.GET.get("century"):
century_name = Century.objects.get(id=self.request.GET.get("century")).name
if century_id := self.request.GET.get("century"):
century_name = Century.objects.get(id=century_id).name
q_obj_filter &= Q(century__name__icontains=century_name)

if self.request.GET.get("provenance"):
provenance_id = int(self.request.GET.get("provenance"))
q_obj_filter &= Q(provenance__id=provenance_id)
if self.request.GET.get("segment"):
segment_id = int(self.request.GET.get("segment"))
q_obj_filter &= Q(segment__id=segment_id)
if self.request.GET.get("fullSource") in ["true", "false"]:
full_source_str = self.request.GET.get("fullSource")
if provenance_id := self.request.GET.get("provenance"):
q_obj_filter &= Q(provenance__id=int(provenance_id))
if segment_id := self.request.GET.get("segment"):
q_obj_filter &= Q(segment__id=int(segment_id))
if (full_source_str := self.request.GET.get("fullSource")) in ["true", "false"]:
if full_source_str == "true":
full_source_q = Q(full_source=True) | Q(full_source=None)
q_obj_filter &= full_source_q
else:
q_obj_filter &= Q(full_source=False)

if self.request.GET.get("general"):
if general_str := self.request.GET.get("general"):
# Strip spaces at the beginning and end. Then make list of terms split on spaces
general_search_terms = self.request.GET.get("general").strip(" ").split(" ")
general_search_terms = general_str.strip(" ").split(" ")
# We need a Q Object for each field we're gonna look into
shelfmark_q = Q()
siglum_q = Q()
holding_institution_q = Q()
holding_institution_city_q = Q()
description_q = Q()
# it seems that old cantus don't look into title and provenance for the general search terms
# cantus.uwaterloo.ca/source/123901 this source cannot be found by searching its provenance 'Kremsmünster' in the general search field
# it seems that old cantus don't look into title and provenance
# for the general search terms
# cantus.uwaterloo.ca/source/123901 this source cannot be found by searching
# its provenance 'Kremsmünster' in the general search field
# provenance_q = Q()
summary_q = Q()

Expand Down Expand Up @@ -302,9 +300,9 @@ def get_queryset(self) -> QuerySet[Source]:

# For the indexing notes search we follow the same procedure as above but with
# different fields
if self.request.GET.get("indexing"):
if indexing_str := self.request.GET.get("indexing"):
# Make list of terms split on spaces
indexing_search_terms = self.request.GET.get("indexing").split(" ")
indexing_search_terms = indexing_str.strip(" ").split(" ")
# We need a Q Object for each field we're gonna look into
inventoried_by_q = Q()
full_text_entered_by_q = Q()
Expand Down

0 comments on commit a206538

Please sign in to comment.