|
| 1 | +from django.views.generic import TemplateView |
| 2 | +from django.db.models import Prefetch |
| 3 | +from django_email_learning.models import Organization, Course |
| 4 | +from django.http import Http404 |
| 5 | +from django_email_learning.public.serializers import ( |
| 6 | + OrganizationSerializer, |
| 7 | + PublicCourseSerializer, |
| 8 | +) |
| 9 | + |
| 10 | + |
| 11 | +class OrganizationView(TemplateView): |
| 12 | + template_name = "public/organization.html" |
| 13 | + |
| 14 | + def get_context_data(self, **kwargs) -> dict: # type: ignore[no-untyped-def] |
| 15 | + organization_id: int = kwargs.get("organization_id") # type: ignore[assignment] |
| 16 | + context = super().get_context_data(**kwargs) |
| 17 | + # Add any additional context if needed |
| 18 | + organization_details = Organization.objects.filter( |
| 19 | + id=organization_id |
| 20 | + ).prefetch_related( |
| 21 | + Prefetch( |
| 22 | + "course_set", |
| 23 | + queryset=Course.objects.filter(enabled=True), |
| 24 | + to_attr="courses", |
| 25 | + ), |
| 26 | + ) |
| 27 | + if organization_details.exists(): |
| 28 | + organization = organization_details.first() |
| 29 | + if not organization: |
| 30 | + raise Http404("Organization does not exist") |
| 31 | + courses = [] |
| 32 | + for course in organization.courses: |
| 33 | + course_data = PublicCourseSerializer( |
| 34 | + id=course.id, |
| 35 | + title=course.title, |
| 36 | + slug=course.slug, |
| 37 | + description=course.description, |
| 38 | + imap_email=course.imap_connection.email |
| 39 | + if course.imap_connection |
| 40 | + else None, |
| 41 | + ) |
| 42 | + courses.append(course_data) |
| 43 | + organization_data = OrganizationSerializer( |
| 44 | + id=organization.id, |
| 45 | + name=organization.name, |
| 46 | + logo_url=organization.logo.url if organization.logo else None, |
| 47 | + description=organization.description, |
| 48 | + courses=courses, |
| 49 | + ) |
| 50 | + context["organization_json"] = organization_data.model_dump_json() |
| 51 | + context["organization"] = organization_data.model_dump() |
| 52 | + context["page_title"] = organization.name |
| 53 | + return context |
| 54 | + |
| 55 | + # If organization not found, raise 404 |
| 56 | + raise Http404("Organization does not exist") |
0 commit comments