Skip to content

Commit

Permalink
Added remaining fields
Browse files Browse the repository at this point in the history
  • Loading branch information
david-okeke1337 committed Dec 12, 2024
1 parent 8e3198c commit 16649b0
Showing 1 changed file with 87 additions and 30 deletions.
117 changes: 87 additions & 30 deletions src/search/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
from django.urls import reverse
from django.views.decorators.http import require_http_methods

from config import settings
from content.models import BasePage, ContentPage
from extended_search.models import Setting as SearchSetting
from extended_search.settings import settings_singleton
from news.models import NewsPage
from peoplefinder.models import Person, Team
from search.search import AllPagesSearchVector
from search.templatetags import search as search_template_tag
from search.utils import get_query_info_for_model

Expand Down Expand Up @@ -146,22 +147,31 @@ def export_search(request: HttpRequest, category: str) -> HttpResponse:
Administrative view for exporting search results as csv
"""
query = request.GET.get("query", "")
if category == "all": category = "all_pages"
if category == "all":
category = "all_pages"
search_vector = search_template_tag.SEARCH_VECTORS[category](request)
search_results = search_vector.search(query)
search_model = search_vector.model

def build_search_export_csv_response(category, search_model, search_results) -> HttpResponse:
def build_search_export_csv_response(
request: HttpRequest, category, search_model, search_results
) -> HttpResponse:
header = build_export_search_csv_header(search_model)

if issubclass(search_model, BasePage):
return build_page_export_csv_response(category, header, search_results)
return build_page_export_csv_response(
request, category, header, search_results
)

if issubclass(search_model, Person):
return build_people_export_csv_response(category, header, search_results)
return build_people_export_csv_response(
request, category, header, search_results
)

if issubclass(search_model, Team):
return build_teams_export_csv_response(category, header, search_results)
return build_teams_export_csv_response(
request, category, header, search_results
)

def build_export_search_csv_header(search_model) -> list[str]:
if issubclass(search_model, BasePage):
Expand All @@ -184,7 +194,7 @@ def build_export_search_csv_header(search_model) -> list[str]:
"Email",
"Phone",
"Profile-URL",
"Roles(Job Title|Team Name)"
"Roles{'Job-Title':'Team-Name'}",
]

if issubclass(search_model, Team):
Expand All @@ -193,7 +203,6 @@ def build_export_search_csv_header(search_model) -> list[str]:
"URL",
"Edit-URL",
]


def build_search_export_response_header(category: str) -> HttpResponse:
filename = f"search_export_{category}.csv"
Expand All @@ -203,30 +212,72 @@ def build_search_export_response_header(category: str) -> HttpResponse:
)
return response


def build_page_export_csv_response(category, header, search_results) -> HttpResponse:
def build_page_export_csv_response(
request, category, header, search_results
) -> HttpResponse:
response = build_search_export_response_header(category)
base_url = get_base_url(request)

writer = csv.writer(response)
writer.writerow(header)

for result in search_results:
content_owner = get_content_owner(result)
content_author = get_content_author(result)
row = [
result.title,
result.get_full_url(),
"Edit-URL",
result.content_owner.full_name if result.content_owner else "",#tbc
"Content-Owner-Email",
"Content-Author-Name",
"Content-Author-Email",
"First-Published",
"Last-Updated",
category,
]
get_edit_page_url(base_url, result),
content_owner["name"],
content_owner["email"],
content_author["name"],
content_author["email"],
result.first_published_at,
result.last_published_at,
type(result).__name__,
]
writer.writerow(row)
return response


def build_people_export_csv_response(category, header, search_results) -> HttpResponse:
def get_base_url(request: HttpRequest) -> str:
return f"{request.scheme}://{request.get_host()}"

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"] = ""
return content_owner

def get_content_author(page) -> dict:
content_author = {}
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:
content_author["name"] = page.get_latest_revision().user.get_full_name()
content_author["email"] = page.get_latest_revision().user.email
return content_author

def build_people_export_csv_response(
request, category, header, search_results
) -> HttpResponse:
response = build_search_export_response_header(category)
base_url = get_base_url(request)

writer = csv.writer(response)
writer.writerow(header)

Expand All @@ -235,24 +286,30 @@ def build_people_export_csv_response(category, header, search_results) -> HttpRe
f"{result.first_name} {result.last_name}",
result.email,
result.primary_phone_number,
result.get_absolute_url(),#tbc
"Roles(Job Title|Team Name)",
]
f"{base_url}{result.get_absolute_url()}",
{role.job_title: role.team.name for role in result.roles.all()},
]
writer.writerow(row)
return response

def build_teams_export_csv_response(category, header, search_results) -> HttpResponse:
def build_teams_export_csv_response(
request, category, header, search_results
) -> HttpResponse:
response = build_search_export_response_header(category)
base_url = get_base_url(request)

writer = csv.writer(response)
writer.writerow(header)

for result in search_results:
row = [
result.name,
result.get_absolute_url(),#tbc
result.get_edit_url(),#tbc
]
f"{base_url}{result.get_absolute_url()}",
f"{base_url}{result.get_absolute_url()}edit",
]
writer.writerow(row)
return response

return build_search_export_csv_response(category, search_model, search_results)

return build_search_export_csv_response(
request, category, search_model, search_results
)

0 comments on commit 16649b0

Please sign in to comment.