Skip to content

Commit

Permalink
Merge pull request #109 from SADiLaR/ui-rework2
Browse files Browse the repository at this point in the history
UI rework2
  • Loading branch information
friedelwolff authored Aug 14, 2024
2 parents 0d52ebd + 5b503f3 commit 1f1dfa0
Show file tree
Hide file tree
Showing 54 changed files with 1,484 additions and 1,938 deletions.
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ repos:
hooks:
# Run the linter.
- id: ruff
args: [ --diff ]
# Run the formatter.
- id: ruff-format
args: [ --diff ]

# Automatically sort python imports
- repo: https://github.com/PyCQA/isort
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ check:
@docker compose run --rm web python manage.py check

make-messages:
@docker compose run --rm web python manage.py makemessages --all
@docker compose run --rm web python manage.py makemessages --all -e html,txt,py,js

compile-messages:
@docker compose run --rm web python manage.py compilemessages
9 changes: 8 additions & 1 deletion app/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"simple_history",
"accounts",
"django_filters",
"django_htmx",
]
if DEBUG:
INSTALLED_APPS += [
Expand All @@ -75,9 +76,9 @@
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"simple_history.middleware.HistoryRequestMiddleware",
"django_htmx.middleware.HtmxMiddleware",
]

# Add debug toolbar middleware
if DEBUG and DEBUG_TOOLBAR:
MIDDLEWARE.insert(2, "debug_toolbar.middleware.DebugToolbarMiddleware")

Expand All @@ -96,6 +97,7 @@
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
"general.context_processors.template_vars",
],
},
},
Expand All @@ -121,6 +123,11 @@
}

if DEBUG:
DEBUG_TOOLBAR_CONFIG = {
"ROOT_TAG_EXTRA_ATTRS": "hx-preserve",
"UPDATE_ON_FETCH": True,
}

# Some things rely on the setting INTERNAL_IPS:
# - debug_toolbar.middleware.show_toolbar
# - django.template.context_processors.debug
Expand Down
120 changes: 52 additions & 68 deletions app/app/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.db.models import Count, Prefetch
from django.core.paginator import Paginator
from django.db.models import Count, F, Func, OuterRef, Prefetch, Subquery
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render
from django.utils.http import urlencode
Expand Down Expand Up @@ -41,13 +41,14 @@ def get_date_range(project):
start_date = project.start_date
end_date = project.end_date

if (start_date is not None) and (end_date is not None) and (start_date.year != end_date.year):
date = f"{start_date.year}{end_date.year}"
elif (start_date is not None) and (end_date is not None) and (start_date.year == end_date.year):
date = start_date.year
elif start_date is not None:
if start_date and end_date:
if start_date.year != end_date.year:
date = f"{start_date.year}{end_date.year}"
else:
date = start_date.year
elif start_date:
date = _("Since {year}").format(year=start_date.year)
elif end_date is not None:
elif end_date:
date = _("Until {year}").format(year=end_date.year)
else:
date = None
Expand Down Expand Up @@ -83,21 +84,17 @@ def get_subjects(subjects):
def projects(request):
template = "app/projects.html"

subject_id = request.GET.get("subject")
language_id = request.GET.get("language")
institution_id = request.GET.get("institution")

projects = (
Project.objects.select_related("institution")
.prefetch_related("subjects", "languages")
.order_by("name")
)

if subject_id:
if subject_id := request.GET.get("subject"):
projects = projects.filter(subjects__id=subject_id)
if language_id:
if language_id := request.GET.get("language"):
projects = projects.filter(languages__id=language_id)
if institution_id:
if institution_id := request.GET.get("institution"):
projects = projects.filter(institution__id=institution_id)

subjects = Subject.objects.order_by("name")
Expand All @@ -108,24 +105,14 @@ def projects(request):
for project in projects:
project_subjects = project.subjects.all()
project_languages = project.languages.all()

languages_data = get_languages(project_languages)
subjects_data = get_subjects(project_subjects)

logo = get_logo(project)

institution_name = project.institution.name

date = get_date_range(project)

project_data.append(
{
"project": project,
"logo": logo,
"subjects": subjects_data,
"languages": languages_data,
"date": date,
"institution_name": institution_name,
"logo": get_logo(project),
"subjects": get_subjects(project_subjects),
"languages": get_languages(project_languages),
"date": get_date_range(project),
"institution_name": project.institution.name,
"description": project.description,
}
)
Expand All @@ -145,16 +132,17 @@ def project_detail(request, project_id):
template = "app/project_detail.html"

project = get_object_or_404(
Project.objects.select_related("institution").prefetch_related("subjects", "languages"),
Project.objects.select_related("institution").prefetch_related(
Prefetch("subjects", queryset=Subject.objects.order_by("name")),
Prefetch("languages", queryset=Language.objects.order_by("name")),
),
id=project_id,
)

logo = get_logo(project)

context = {
"current_page": "project_detail",
"project": project,
"logo": logo,
"logo": get_logo(project),
"subjects": project.subjects.all(),
"languages": project.languages.all(),
}
Expand All @@ -168,14 +156,12 @@ def institution_detail(request, institution_id):
projects = Project.objects.filter(institution=institution).order_by("name")
documents = DocumentFile.objects.filter(institution=institution).order_by("title")

logo = institution.logo

context = {
"current_page": "institution_detail",
"institution": institution,
"projects": projects,
"documents": documents,
"logo": logo,
"logo": institution.logo,
}

return render(request, template_name=template, context=context)
Expand All @@ -184,29 +170,24 @@ def institution_detail(request, institution_id):
def documents(request):
template = "app/documents.html"

url_params = {}
subject_id = request.GET.get("subject")
language_id = request.GET.get("language")
institution_id = request.GET.get("institution")

documents = (
DocumentFile.objects.select_related("institution")
.prefetch_related("subjects", "languages")
.order_by("title")
)

if subject_id:
url_params = {}
if subject_id := request.GET.get("subject"):
documents = documents.filter(subjects__id=subject_id)
url_params["subject"] = subject_id
if language_id:
if language_id := request.GET.get("language_id"):
documents = documents.filter(languages__id=language_id)
url_params["language"] = language_id
if institution_id:
if institution_id := request.GET.get("institution"):
documents = documents.filter(institution__id=institution_id)
url_params["institution"] = institution_id

paginator = Paginator(documents, 10)

page_number = request.GET.get("page")
page_obj = paginator.get_page(page_number)

Expand All @@ -218,15 +199,11 @@ def documents(request):
for document in page_obj:
document_subjects = document.subjects.all()
document_languages = document.languages.all()

languages_data = get_languages(document_languages)
subjects_data = get_subjects(document_subjects)

document_data.append(
{
"document": document,
"subjects": subjects_data,
"languages": languages_data,
"subjects": get_subjects(document_subjects),
"languages": get_languages(document_languages),
"institution_name": document.institution.name,
"description": document.description,
"url": document.url,
Expand Down Expand Up @@ -283,7 +260,6 @@ def languages(request):
)

language_data = []

for language in languages:
documents = language.documentfile_set.all()
projects = language.project_set.all()
Expand Down Expand Up @@ -351,7 +327,17 @@ def institutions(request):
template = "app/institutions.html"
context = {}

institutions = Institution.objects.annotate(project_count=Count("project")).order_by("name")
# https://docs.djangoproject.com/en/stable/topics/db/aggregation/#combining-multiple-aggregations
subquery = (
DocumentFile.objects.filter(institution=OuterRef("id"))
.order_by()
.annotate(count=Func(F("id"), function="Count"))
)

institutions = Institution.objects.annotate(
project_count=Count("project"),
document_count=Subquery(subquery.values("count")),
).order_by("name")

institutions_array = []

Expand All @@ -375,6 +361,7 @@ def institutions(request):
rating = (completed_fields_count / len(institution_dict)) * 100
institution_dict["rating"] = round(rating)
institution_dict["project_count"] = institution.project_count
institution_dict["document_count"] = institution.document_count
institution_dict["id"] = institution.id
institutions_array.append(institution_dict)

Expand All @@ -384,28 +371,25 @@ def institutions(request):


def search(request):
page_number = request.GET.get("page", "1")
if not page_number.isdigit():
page_number = "1"

f = DocumentFileFilter(request.GET, queryset=DocumentFile.objects.all())

template = "app/search.html"

paginator = Paginator(f.qs, 5) # 5 documents per page
page_number = request.GET.get("page")
page_obj = paginator.get_page(page_number)

try:
page_obj = paginator.page(page_number)
except PageNotAnInteger:
page_obj = paginator.page(1)
except EmptyPage:
page_obj = paginator.page(paginator.num_pages)
for result in page_obj:
# Remove headline that is identical to description
if headline := result.get("search_headline", ""):
if result["extra"].startswith(headline):
del result["search_headline"]

context = {
"search_results": paginator.page(page_obj.number),
"current_page": "search",
"filter": f,
"documents": page_obj,
"search_params": pagination_url(request),
"page_obj": page_obj,
"url_params": pagination_url(request),
}

return render(request, template_name=template, context=context)
Expand All @@ -420,4 +404,4 @@ def pagination_url(request):
"languages": request.GET.getlist("languages", []),
}

return "?" + urlencode(url_params, doseq=True)
return urlencode(url_params, doseq=True)
8 changes: 8 additions & 0 deletions app/general/context_processors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.conf import settings


def template_vars(request):
return {
"debug_toolbar": settings.DEBUG and settings.DEBUG_TOOLBAR,
"BASE_TEMPLATE": "base_htmx.html" if request.htmx else "base.html",
}
Loading

0 comments on commit 1f1dfa0

Please sign in to comment.