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 4, 2024
1 parent 982c72e commit b6762c4
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 108 deletions.
102 changes: 69 additions & 33 deletions app/app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,43 +230,79 @@ def institutions(request):


def search(request):
q = request.GET.get("q")
languages = request.GET.getlist("languages")
print(languages)

if q:
queue = SearchQuery(q)
search_headline = SearchHeadline("document_data", queue)

documents = (
DocumentFile.objects.annotate(rank=SearchRank("search_vector", queue))
.select_related("institution")
.annotate(search_headline=search_headline)
.filter(search_vector=queue)
# .only('title', 'description', 'institution__name','institution__abbreviation', 'search_headline', 'rank')
.order_by("-rank")
)
# project_subjects = Subject.objects.all()
if request.method == "POST":
search_request = request.POST.get("search")
subjects_request = request.POST.get("subjects")

print(search_request)
print(subjects_request)

project_subjects = Subject.objects.all()
documents = DocumentFile.objects.all()

selected_subjects = []
if search_request:
queue = SearchQuery(search_request)
search_headline = SearchHeadline("document_data", queue)

documents = (
DocumentFile.objects.annotate(rank=SearchRank("search_vector", queue))
.select_related("institution")
.annotate(search_headline=search_headline)
.filter(search_vector=queue)
# .only('title', 'description', 'institution__name','institution__abbreviation', 'search_headline', 'rank')
.order_by("-rank")
)

# if subjects_request:
# # Split the subjects_request string into a list of subject IDs
# subject_ids = subjects_request.split(',')
# print(subject_ids)
# # Filter documents by those subject IDs
# documents = documents.filter(subjects__id__in=subject_ids).distinct()

if subjects_request:
subject_ids = subjects_request.split(",")
documents = documents.filter(subjects__id__in=subject_ids).distinct()

# Query the Subject model to get the names of the selected subjects
selected_subjects = Subject.objects.filter(id__in=subject_ids).values_list(
"name", flat=True
)

else:
documents = None
else:
documents = None

# Create a Paginator instance with the documents and the number of items per page
paginator = Paginator(documents, 10) if documents else None # Show 10 documents per page
# Create a Paginator instance with the documents and the number of items per page
paginator = Paginator(documents, 10) if documents else None # Show 10 documents per page

# Get the page number from the request's GET parameters
page_number = request.GET.get("page")
# Get the page number from the request's GET parameters
page_number = request.GET.get("page")

# Use the get_page method to get the Page object for that page number
page_obj = paginator.get_page(page_number) if paginator else None
# Use the get_page method to get the Page object for that page number
page_obj = paginator.get_page(page_number) if paginator else None

feature_flag = os.getenv("FEATURE_FLAG", False)
feature_flag = os.getenv("FEATURE_FLAG", False)

template = "app/search.html"
context = {
"documents": page_obj,
"current_page": "search",
"document_count": len(documents) if documents else 0,
"feature_flag": feature_flag,
}
template = "app/search.html"
context = {
"search": search_request,
"documents": page_obj,
"project_subjects": project_subjects,
"selected_subjects": selected_subjects,
"current_page": "search",
"document_count": len(documents) if documents else 0,
"feature_flag": feature_flag,
}

return render(request, template_name=template, context=context)
return render(request, template_name=template, context=context)

else:
feature_flag = os.getenv("FEATURE_FLAG", False)
template = "app/search.html"
context = {
"project_subjects": Subject.objects.all(),
"feature_flag": feature_flag,
}
return render(request, template_name=template, context=context)
146 changes: 71 additions & 75 deletions app/templates/app/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,76 +4,72 @@

{% block content %}

<div class="body">
<div class="section mt-3 mb-3">
<div class="card content-card">
<div class="card-body">
<h5 class="card-title">{% trans "Search a term" %}</h5>
{% if feature_flag == "search_feature" %}
{# test section start#}
<form>
<div class="input-group mb-3">
<input type="search" class="form-control" name="q" placeholder="Search">
{# <div class="input-group-append">#}
{# <button class="btn btn-outline-secondary">{% trans "Search" %}</button>#}
{# </div>#}
</div>
<div class="body">
<div class="section mt-3 mb-3">
<div class="card content-card">
<div class="card-body">
<h5 class="card-title">{% trans "Search a term" %}</h5>
{% if feature_flag == "search_feature" %}
{# test section start#}
<form method="post" >
{% csrf_token %}
<div class="input-group mb-3">
<input type="search" class="form-control" name="search" placeholder="Search" value="{{ search }}">
</div>
<hr>

{# </form>#}
<hr>
<select multiple="multiple" name="languages">
<option value="java">Java</option>
<option value="python">Python</option>
<option value="javascript">JavaScript</option>
<option value="rust">Rust</option>
<option value="go">Go</option>
</select>
<hr>
<div class="input-group-append">
<select multiple="multiple" name="subjects">
{% for project_subject in project_subjects %}
<option value="{{ project_subject.id }}">{{ project_subject.name }}</option>
{% endfor %}

</select>
<hr>
<div class="input-group-append">
<button class="btn btn-outline-secondary">{% trans "Search" %}</button>
</div>
</form>
<div class="mb-3">
<span class="mr-5"><b>{% trans "Results:" %}</b></span>
<span>{{ documents|length }}</span>
</div>
<div>
{% for document in documents %}
<div>
<ul>
<li>
<span class="mr-5 text-primary"><b>{% trans "Title:" %}</b></span>
<span>{{ document.title }}</span>
</li>
<li class="mb-2">
<span class="mr-5"><b>{% trans "Institution:" %}</b></span>
<span>{{ document.institution }}</span>
</li>
<li class="mb-2">
<span class="mr-5"><b>{% trans "Headline:" %}</b></span>
<span>{{ document.search_headline|safe }}</span>
</li>
<li>
<span class="mr-5"><b>{% trans "File:" %}</b></span>
<span>{{ document.uploaded_file }}</span>
</li>
<li>
<span class="mr-5"><b>{% trans "License:" %}</b></span>
<span>{{ document.license }}</span>
</li>
<li>
<span class="mr-5"><b>{% trans "Mime Type:" %}</b></span>
<span>{{ document.mime_type }}</span>
</li>
<li class="mt-2">
<span class="mr-5"><b>{% trans "Rank:" %}</b></span>
<span><i>{{ document.rank }}</i></span>
</li>
</ul>
</div>
{% endfor %}
</div>
<div class="pagination">
</form>
<div class="mb-3">
<span class="mr-5"><b>{% trans "Results:" %}</b></span>
<span>{{ documents|length }}</span>
</div>
<div>
{% for document in documents %}
<div>
<ul>
<li>
<span class="mr-5 text-primary"><b>{% trans "Title:" %}</b></span>
<span>{{ document.title }}</span>
</li>
<li class="mb-2">
<span class="mr-5"><b>{% trans "Institution:" %}</b></span>
<span>{{ document.institution }}</span>
</li>
<li class="mb-2">
<span class="mr-5"><b>{% trans "Headline:" %}</b></span>
<span>{{ document.search_headline|safe }}</span>
</li>
<li>
<span class="mr-5"><b>{% trans "File:" %}</b></span>
<span>{{ document.uploaded_file }}</span>
</li>
<li>
<span class="mr-5"><b>{% trans "License:" %}</b></span>
<span>{{ document.license }}</span>
</li>
<li>
<span class="mr-5"><b>{% trans "Mime Type:" %}</b></span>
<span>{{ document.mime_type }}</span>
</li>
<li class="mt-2">
<span class="mr-5"><b>{% trans "Rank:" %}</b></span>
<span><i>{{ document.rank }}</i></span>
</li>
</ul>
</div>
{% endfor %}
</div>
<div class="pagination">
<span class="step-links">
{% if documents.has_previous %}
<a href="?page=1">&laquo; {% trans "first" %}</a>
Expand All @@ -87,15 +83,15 @@ <h5 class="card-title">{% trans "Search a term" %}</h5>
<a href="?page={{ documents.paginator.num_pages }}">{% trans "last" %} &raquo;</a>
{% endif %}
</span>
<hr>
</div>
</div>
{# test section end#}
{% else %}
<p>{% trans "Functionality of this page coming soon" %}</p>
{% endif %}
<hr>
</div>
</div>
{# test section end#}
{% else %}
<p>{% trans "Functionality of this page coming soon" %}</p>
{% endif %}
</div>
</div>
</div>
</div>

{% endblock content %}

0 comments on commit b6762c4

Please sign in to comment.