Skip to content

Commit

Permalink
backup of code
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-gray-tangent committed Jul 10, 2024
1 parent 1046333 commit 4c0db2f
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 11 deletions.
3 changes: 0 additions & 3 deletions app/app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,13 @@ def search(request):

paginator = Paginator(f.qs, 5) # 5 documents per page

print(paginator)

try:
page_obj = paginator.page(page_number)
except PageNotAnInteger:
page_obj = paginator.page(1)
except EmptyPage:
page_obj = paginator.page(paginator.num_pages)

# Update the context with the page object
context = {
"search_results": paginator.page(page_obj.number),
"filter": f,
Expand Down
1 change: 1 addition & 0 deletions app/general/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def filter_search(self, queryset, name, value):
queue = SearchQuery(value.strip())
search_rank = SearchRank(F("search_vector"), queue)
search_headline = SearchHeadline("document_data", queue)

queryset = (
queryset.annotate(
rank=search_rank,
Expand Down
93 changes: 93 additions & 0 deletions app/general/tests/test_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import unittest

from django.contrib.postgres.search import SearchVector
from django.test import TestCase

from general.filters import DocumentFileFilter
from general.models import DocumentFile, Institution, Language, Subject


class TestSearchFilter(TestCase):
def setUp(self):
# Create institutions
self.institution1 = Institution.objects.create(name="Institution 1")
self.institution2 = Institution.objects.create(name="Institution 2")

# Create languages
self.language1 = Language.objects.create(name="English", iso_code="EN")
self.language2 = Language.objects.create(name="Afrikaans", iso_code="AF")

# Create subjects
self.subject1 = Subject.objects.create(name="Science")
self.subject2 = Subject.objects.create(name="Math")

# Create DocumentFiles
self.doc1 = DocumentFile.objects.create(
title="Document 1",
document_data="Document 1 content",
institution=self.institution1,
document_type="glossary",
)
self.doc1.subjects.add(self.subject1)
self.doc1.languages.add(self.language1)

self.doc2 = DocumentFile.objects.create(
title="Document 2",
document_data="Document 2 content",
institution=self.institution2,
document_type="glossary",
)
self.doc2.subjects.add(self.subject2)
self.doc2.languages.add(self.language2)

def test_institution_filter(self):
data = {"institution": [self.institution1.id]}
filter = DocumentFileFilter(data=data)
self.assertEqual(len(filter.qs), 1)
self.assertIn(self.doc1, filter.qs)

def test_document_type_filter(self):
data = {"document_type": ["glossary"]}
filter = DocumentFileFilter(data=data)
self.assertEqual(len(filter.qs), 2)
self.assertIn(self.doc1, filter.qs)
self.assertIn(self.doc2, filter.qs)

def test_subjects_filter(self):
data = {"subjects": [self.subject1.id]}
filter = DocumentFileFilter(data=data)
self.assertEqual(len(filter.qs), 1)
self.assertIn(self.doc1, filter.qs)

def test_languages_filter(self):
data = {"languages": [self.language1.id]}
filter = DocumentFileFilter(data=data)
self.assertEqual(len(filter.qs), 1)
self.assertIn(self.doc1, filter.qs)

def test_combined_filters(self):
data = {
"institution": [self.institution1.id],
"document_type": ["glossary"],
"subjects": [self.subject1.id],
"languages": [self.language1.id],
}
filter = DocumentFileFilter(data=data)
self.assertEqual(len(filter.qs), 1)
self.assertIn(self.doc1, filter.qs)

def test_search_filter(self):
data = {"search": "Document"}
filter = DocumentFileFilter(data=data)
self.assertEqual(len(filter.qs), 2)
self.assertIn(self.doc1, filter.qs)
self.assertIn(self.doc2, filter.qs)

data = {"search": "Document 1"}
filter = DocumentFileFilter(data=data)
self.assertEqual(len(filter.qs), 1)
self.assertIn(self.doc1, filter.qs)


if __name__ == "__main__":
unittest.main()
8 changes: 0 additions & 8 deletions app/templates/app/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,4 @@ <h5 class="card-title">{% trans "Search a term" %}</h5>
</div>
</div>

<script>
function resetForm() {
// Assuming your form has an id, replace 'formId' with your actual form id
{#document.getElementById('formId').reset();#}
document.location.href = "/search/"
}
</script>

{% endblock content %}

0 comments on commit 4c0db2f

Please sign in to comment.