Skip to content

Commit

Permalink
Improved readability
Browse files Browse the repository at this point in the history
  • Loading branch information
david-okeke1337 committed Dec 17, 2024
1 parent 5c300e4 commit d644bb3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 35 deletions.
65 changes: 32 additions & 33 deletions src/search/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.conf import settings
from django.urls import reverse
from wagtail.search.query import Fuzzy, Or, Phrase, PlainText
from django.http import HttpRequest, HttpResponse

from extended_search import settings as search_settings
from extended_search.index import Indexed
Expand Down Expand Up @@ -250,51 +251,49 @@ def has_only_bad_results(query, category, pinned_results, search_results):
#


def get_edit_page_url(base_url, page) -> str:
return f"{base_url}{reverse('wagtailadmin_pages:edit', args=[page.id])}"


def get_content_owner(page) -> dict:
content_owner = {}
if hasattr(page, "content_owner"):
content_owner["name"] = page.content_owner.full_name
content_owner["email"] = page.content_owner.email
else:
content_owner["name"] = ""
content_owner["email"] = ""
content_owner = {
"name": "",
"email": "",
}
if page_content_owner := getattr(page, "content_owner", None):
content_owner["name"] = page_content_owner.full_name
content_owner["email"] = page_content_owner.email
return content_owner


def get_content_author(page) -> dict:
content_author = {}
content_author = {
"name": "",
"email": "",
}
perm_sec_as_author = (
page.perm_sec_as_author if hasattr(page, "perm_sec_as_author") else False
)
if perm_sec_as_author:
content_author["name"] = settings.PERM_SEC_NAME
content_author["email"] = ""
elif issubclass(page.__class__, NewsPage):
if hasattr(page, "get_first_publisher"):
content_author["name"] = page.get_first_publisher().get_full_name()
content_author["email"] = page.get_first_publisher().email
else:
latest_revision_user = page.get_latest_revision().user
if latest_revision_user:
content_author["name"] = latest_revision_user.get_full_name()
content_author["email"] = latest_revision_user.email
else:
content_author["name"] = ""
content_author["email"] = ""
return content_author

if issubclass(page.__class__, NewsPage) and hasattr(page, "get_first_publisher"):
first_publisher = page.get_first_publisher()
content_author["name"] = first_publisher.get_full_name()
content_author["email"] = first_publisher.email
return content_author

latest_revision_user = page.get_latest_revision().user
if latest_revision_user:
content_author["name"] = latest_revision_user.get_full_name()
content_author["email"] = latest_revision_user.email
return content_author


def get_page_export_row(page_result: "BasePage", base_url: str) -> list[str]:
def get_page_export_row(page_result: "BasePage", request: HttpRequest) -> list[str]:
content_owner = get_content_owner(page_result)
content_author = get_content_author(page_result)
return [
page_result.title,
page_result.get_full_url(),
get_edit_page_url(base_url, page_result),
request.build_absolute_uri(page_result.get_url()),
request.build_absolute_uri(reverse('wagtailadmin_pages:edit', args=[page_result.id])),
content_owner["name"],
content_owner["email"],
content_author["name"],
Expand All @@ -305,19 +304,19 @@ def get_page_export_row(page_result: "BasePage", base_url: str) -> list[str]:
]


def get_person_export_row(person_result: "Person", base_url: str) -> list[str]:
def get_person_export_row(person_result: "Person", request: HttpRequest) -> list[str]:
return [
f"{person_result.first_name} {person_result.last_name}",
person_result.email,
person_result.primary_phone_number,
f"{base_url}{person_result.get_absolute_url()}",
request.build_absolute_uri(person_result.get_absolute_url()),
{role.job_title: role.team.name for role in person_result.roles.all()},
]


def get_team_export_row(team_result: "Team", base_url: str) -> list[str]:
def get_team_export_row(team_result: "Team", request: HttpRequest) -> list[str]:
return [
team_result.name,
f"{base_url}{team_result.get_absolute_url()}",
f"{base_url}{team_result.get_absolute_url()}edit",
request.build_absolute_uri(team_result.get_absolute_url()),
request.build_absolute_uri(reverse('team-edit', args=[team_result.slug])),
]
4 changes: 2 additions & 2 deletions src/search/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ def export_search(request: HttpRequest, category: str) -> HttpResponse:
for k, v in SEARCH_EXPORT_MAPPINGS.items():
if issubclass(search_model, k):
export_mapping = v
break

if not export_mapping:
raise TypeError(
Expand All @@ -205,13 +206,12 @@ def export_search(request: HttpRequest, category: str) -> HttpResponse:
content_type="text/csv",
headers={"Content-Disposition": f'attachment; filename="{filename}"'},
)
base_url = f"{request.scheme}://{request.get_host()}"

writer = csv.writer(response)
writer.writerow(export_mapping["header"])

for result in search_results:
row = export_mapping["item_to_row_function"](result, base_url)
row = export_mapping["item_to_row_function"](result, request)
writer.writerow(row)

return response

0 comments on commit d644bb3

Please sign in to comment.