Skip to content

Commit

Permalink
feat(sections): add/change faculty for section universities
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoeejoee committed Nov 15, 2023
1 parent 89db2a3 commit 0d39dd4
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{# TODO: with support for title it's best to generalize it #}
{# TODO: support for title #}
<form hx-post="{{ form_url }}"
hx-swap="outerHTML"
{% if form.is_multipart %}enctype="multipart/form-data" hx-encoding="multipart/form-data"{% endif %}>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.2.7 on 2023-11-15 12:45

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('universities', '0003_alter_faculty_created_alter_faculty_university_and_more'),
('sections', '0020_alter_section_created_and_more'),
]

operations = [
migrations.AlterField(
model_name='sectionuniversity',
name='section',
field=models.ForeignKey(db_index=False, on_delete=django.db.models.deletion.RESTRICT, related_name='section_universities', to='sections.section', verbose_name='ESN section'),
),
migrations.AlterField(
model_name='sectionuniversity',
name='university',
field=models.ForeignKey(db_index=False, on_delete=django.db.models.deletion.CASCADE, related_name='university_sections', to='universities.university', verbose_name='university'),
),
]
2 changes: 2 additions & 0 deletions fiesta/apps/sections/models/section.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,14 @@ class SectionUniversity(BaseTimestampedModel):
"sections.Section",
on_delete=models.RESTRICT,
verbose_name=_("ESN section"),
related_name="section_universities",
db_index=False,
)
university = models.ForeignKey(
"universities.University",
on_delete=models.CASCADE,
verbose_name=_("university"),
related_name="university_sections",
db_index=False,
)

Expand Down
13 changes: 12 additions & 1 deletion fiesta/apps/sections/tables/faculties.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from __future__ import annotations

from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from django_tables2 import Column, tables

from apps.universities.models import Faculty


class UniversityFacultiesTable(tables.Table):
name = Column()
name = Column(
linkify=("sections:update-section-faculty", {"pk": tables.Accessor("pk")}),
# TODO: probably extract to constant in fiestatables
attrs={"a": {"x-data": lambda: "modal($el.href)", "x-bind": "bind"}},
)
abbr = Column()

members_count = Column(
Expand All @@ -16,6 +21,10 @@ class UniversityFacultiesTable(tables.Table):
internationals_count = Column(
linkify=lambda record: reverse("sections:section-internationals") + f"?user__profile__faculty={record.pk}",
)
users_count = Column(
verbose_name=_("Total count*"),
attrs={"th": {"title": _("Including people from other sections.")}},
)

class Meta:
model = Faculty
Expand All @@ -29,3 +38,5 @@ class Meta:
)

attrs = dict(tbody={"hx-disable": True})

empty_text = _("No related faculties.")
16 changes: 11 additions & 5 deletions fiesta/apps/sections/templates/sections/universities.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@
{% for university, table in object_list|zip:tables %}

<div class="mb-4 flex flex-row justify-between items-center">
<h2 class="text-2xl font-bold">{{ university.name }}</h2>
{# <button class="btn btn-sm btn-outline btn-success">#}
{# {% translate "Add faculty" %}#}
{# </button>#}

<h2 class="text-2xl font-bold flex flex-row items-center gap-x-4">
{{ university.name }}
<a href="{% url "sections:update-section-university" university.pk %}"
x-data="modal($el.href)"
x-bind="bind"
class="btn btn-xs sm:btn-sm btn-outline btn-warning">change</a>
</h2>
<a class="btn btn-success btn-xs sm:btn-sm btn-outline"
href="{% url "sections:new-section-faculty" university.pk %}"
x-data="modal($el.href)"
x-bind="bind">{% translate "Add faculty" %}</a>
</div>

<div>{% render_table table %}</div>
Expand Down
11 changes: 10 additions & 1 deletion fiesta/apps/sections/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,21 @@
SetupPluginFormView,
)
from apps.sections.views.stats import SectionStatsView
from apps.sections.views.universities import NewSectionUniversityView, SectionUniversitiesView
from apps.sections.views.universities import (
NewSectionFacultyView,
NewSectionUniversityView,
SectionUniversitiesView,
UpdateSectionFacultyView,
UpdateSectionUniversityView,
)

register_model_converter(Section, field="space_slug", base=SlugConverter)
urlpatterns = [
path("universities", SectionUniversitiesView.as_view(), name="section-universities"),
path("universities/new", NewSectionUniversityView.as_view(), name="new-section-university"),
path("universities/<uuid:pk>", UpdateSectionUniversityView.as_view(), name="update-section-university"),
path("universities/<uuid:pk>/faculties/new", NewSectionFacultyView.as_view(), name="new-section-faculty"),
path("universities/faculties/<uuid:pk>", UpdateSectionFacultyView.as_view(), name="update-section-faculty"),
path("section-members", SectionMembersView.as_view(), name="section-members"),
path("section-internationals", SectionInternationalsView.as_view(), name="section-internationals"),
path("section-stats", SectionStatsView.as_view(), name="section-stats"),
Expand Down
93 changes: 89 additions & 4 deletions fiesta/apps/sections/views/universities.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
from django.contrib.messages.views import SuccessMessageMixin
from django.db import transaction
from django.db.models import Count
from django.urls import reverse_lazy
from django.shortcuts import get_object_or_404
from django.urls import reverse, reverse_lazy
from django.utils.translation import gettext_lazy as _
from django.views.generic import CreateView
from django.views.generic import CreateView, UpdateView

from apps.fiestaforms.views.htmx import HtmxFormMixin
from apps.fiestatables.views.tables import FiestaMultiTableView
from apps.sections.models import SectionMembership, SectionUniversity
from apps.sections.tables.faculties import UniversityFacultiesTable
from apps.sections.views.mixins.membership import EnsurePrivilegedUserViewMixin
from apps.sections.views.mixins.section_space import EnsureInSectionSpaceViewMixin
from apps.universities.forms import UniversityForm
from apps.universities.forms import FacultyForm, UniversityForm
from apps.universities.models import Faculty, University
from apps.utils.models.query import Q
from apps.utils.views import AjaxViewMixin

Expand All @@ -28,7 +30,7 @@ class NewSectionUniversityView(
):
form_class = UniversityForm
template_name = "fiestaforms/pages/card_page_for_ajax_form.html"
ajax_template_name = "sections/university_form.html"
ajax_template_name = "fiestaforms/parts/ajax-form-container.html"

success_url = reverse_lazy("sections:section-universities")
success_message = _("University created successfully")
Expand All @@ -50,6 +52,85 @@ def form_valid(self, form):
return response


class UpdateSectionUniversityView(
EnsureInSectionSpaceViewMixin,
EnsurePrivilegedUserViewMixin,
HtmxFormMixin,
AjaxViewMixin,
SuccessMessageMixin,
UpdateView,
):
form_class = UniversityForm
template_name = "fiestaforms/pages/card_page_for_ajax_form.html"
ajax_template_name = "fiestaforms/parts/ajax-form-container.html"

success_url = reverse_lazy("sections:section-universities")
success_message = _("University changed successfully")

def get_queryset(self):
return University.objects.filter(university_sections__section=self.request.in_space_of_section)

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["form_url"] = reverse("sections:update-section-university", kwargs={"pk": self.object.pk})
return context


class NewSectionFacultyView(
EnsureInSectionSpaceViewMixin,
EnsurePrivilegedUserViewMixin,
HtmxFormMixin,
AjaxViewMixin,
SuccessMessageMixin,
CreateView,
):
form_class = FacultyForm
template_name = "fiestaforms/pages/card_page_for_ajax_form.html"
ajax_template_name = "fiestaforms/parts/ajax-form-container.html"

success_url = reverse_lazy("sections:section-universities")
success_message = _("Faculty created successfully")

university: University = None

def dispatch(self, request, *args, **kwargs):
self.university = get_object_or_404(University, pk=kwargs.get("pk"))
return super().dispatch(request, *args, **kwargs)

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["form_url"] = reverse("sections:new-section-faculty", kwargs={"pk": self.university.pk})
return context

def form_valid(self, form):
form.instance.university = self.university
return super().form_valid(form)


class UpdateSectionFacultyView(
EnsureInSectionSpaceViewMixin,
EnsurePrivilegedUserViewMixin,
HtmxFormMixin,
AjaxViewMixin,
SuccessMessageMixin,
UpdateView,
):
form_class = FacultyForm
template_name = "fiestaforms/pages/card_page_for_ajax_form.html"
ajax_template_name = "fiestaforms/parts/ajax-form-container.html"

success_url = reverse_lazy("sections:section-universities")
success_message = _("Faculty changed successfully")

def get_queryset(self):
return Faculty.objects.filter(university__university_sections__section=self.request.in_space_of_section)

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["form_url"] = reverse("sections:update-section-faculty", kwargs={"pk": self.object.pk})
return context


class SectionUniversitiesView(
EnsureInSectionSpaceViewMixin,
EnsurePrivilegedUserViewMixin,
Expand Down Expand Up @@ -89,6 +170,10 @@ def get_tables(self):
faculty_user_profiles__user__memberships__role=SectionMembership.Role.INTERNATIONAL,
),
),
users_count=Count(
"faculty_user_profiles",
distinct=True,
),
),
)
for university in self.object_list
Expand Down
7 changes: 0 additions & 7 deletions fiesta/apps/universities/forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

from django.forms import HiddenInput

from apps.fiestaforms.forms import BaseModelForm
from apps.universities.models import Faculty, University

Expand All @@ -22,9 +20,4 @@ class Meta:
fields = (
"name",
"abbr",
"university",
)

widgets = {
"university": HiddenInput,
}

0 comments on commit 0d39dd4

Please sign in to comment.