diff --git a/core/admin.py b/core/admin.py index a1753690..f3163ff5 100644 --- a/core/admin.py +++ b/core/admin.py @@ -24,6 +24,7 @@ TermAdminForm, UserAdminForm, UserCreationAdminForm, + LateStartEventForm ) from .models import Comment, StaffMember from .utils.actions import ( @@ -48,6 +49,12 @@ PostTypeFilter, ) +from django.template.response import TemplateResponse +from django.shortcuts import redirect +from django.urls import path +from datetime import datetime, time +from django.core.exceptions import PermissionDenied + User = get_user_model() # Register your models here. @@ -546,12 +553,93 @@ def formfield_for_manytomany(self, db_field, request, **kwargs): kwargs["queryset"] = models.Tag.objects.all().order_by("name") return super().formfield_for_manytomany(db_field, request, **kwargs) - class EventAdmin(CustomTimeMixin, admin.ModelAdmin): list_display = ["name", "organization", "start_date", "end_date"] list_filter = [OrganizationListFilter] ordering = ["-start_date", "-end_date"] search_fields = ["name"] + change_list_template = 'admin/change_list_buttons.html' + + def changelist_view(self, request, extra_context=None): + extra_context = extra_context or {} + extra_context['buttons'] = [ + { + 'name': 'Add Late Start', + 'url': reverse('admin:late_start'), + }, + ] + return super().changelist_view(request, extra_context=extra_context) + + def get_urls(self): + return [ + path( + "createLateStart/", + self.admin_site.admin_view(self.late_start_view), + name="late_start" + ), + *super().get_urls(), + ] + + def late_start_view(self, request): + + if not request.user.has_perm('core.add_event'): + raise PermissionDenied() + + url = request.get_full_path() + + context = dict( + self.admin_site.each_context(request), + form=LateStartEventForm, + url=url, + title='Add Late Start', + media=LateStartEventForm().media + ) + + if request.method == 'POST': + form = LateStartEventForm(request.POST) + if form.is_valid(): + start_date_value = form.cleaned_data.get('start_date') + start_date = datetime.combine(start_date_value, time(hour=10)) + end_date = datetime.combine(start_date_value, time(hour=10, second=1)) + + data = { + 'name': 'Late Start', + 'term': models.Term.get_current(start_date), + 'schedule_format': 'late-start', + 'start_date': start_date, + 'end_date': end_date + } + + try: + data["organization"] = models.Organization.objects.get(name='SAC') + except models.Organization.DoesNotExist: + + if not request.user.has_perm('core.add_organization'): + raise PermissionDenied() + + earliest_superuser = models.User.objects.filter(is_superuser=True).earliest("date_joined") + + organization_data = { + 'bio': 'WLMAC Student Activity Council', + 'is_open': False, + 'name': 'SAC', + 'slug': 'wlmac', + 'owner': earliest_superuser + } + + sac_org = models.Organization.objects.create(**organization_data) + sac_org.execs.add(earliest_superuser) + sac_org.save() + + data["organization"] = sac_org + + models.Event.objects.create(**data) + return redirect("/admin/core/event") + else: + context['form'] = form + return TemplateResponse(request, "admin/custom_form.html", context) + else: + return TemplateResponse(request, "admin/custom_form.html", context) def get_queryset(self, request): qs = super().get_queryset(request) diff --git a/core/forms.py b/core/forms.py index d7642116..5ebc0db7 100644 --- a/core/forms.py +++ b/core/forms.py @@ -5,6 +5,7 @@ from django.contrib.auth.forms import ( AdminUserCreationForm as ContribAdminUserCreationForm, ) +from django.contrib.admin.widgets import AdminDateWidget from django.utils import timezone from django_select2 import forms as s2forms from martor.widgets import AdminMartorWidget @@ -13,6 +14,7 @@ from core.views.mixins import CaseInsensitiveUsernameMixin + class MetropolisSignupForm(SignupForm, CaseInsensitiveUsernameMixin): first_name = forms.CharField( max_length=30, @@ -295,3 +297,13 @@ class UserAdminForm(CaseInsensitiveUsernameMixin, ContribUserChangeForm): class UserCreationAdminForm(CaseInsensitiveUsernameMixin, ContribAdminUserCreationForm): pass + +class LateStartEventForm(forms.Form): + start_date = forms.DateField(widget=AdminDateWidget()) + + def clean(self): + cleaned_data = super().clean() + if cleaned_data.get('start_date') != None and models.Term.get_current(cleaned_data['start_date']) == None: + raise forms.ValidationError( + {'start_date': 'No Term Found For Date'} + ) \ No newline at end of file diff --git a/templates/admin/change_list_buttons.html b/templates/admin/change_list_buttons.html new file mode 100644 index 00000000..8b8573b3 --- /dev/null +++ b/templates/admin/change_list_buttons.html @@ -0,0 +1,10 @@ +{% extends "admin/change_list.html" %} +{% load i18n admin_urls static admin_list %} +{% block object-tools-items %} + {% for button in buttons %} +
  • + {{ button.name }} +
  • + {% endfor %} + {{ block.super }} +{% endblock %} diff --git a/templates/admin/custom_form.html b/templates/admin/custom_form.html new file mode 100644 index 00000000..a184d19c --- /dev/null +++ b/templates/admin/custom_form.html @@ -0,0 +1,49 @@ +{% extends "admin/base_site.html" %} +{% load i18n admin_urls static admin_modify %} + +{% block extrahead %} + {{ block.super }} + + + {{ media }} +{% endblock %} + +{% block extrastyle %} + {{ block.super }} + +{% endblock %} + +{% if not is_popup %} + {% block breadcrumbs %}{% endblock %} +{% endif %} + +{% block coltype %}colM{% endblock %} +{% block content %} +
    + {% csrf_token %} + {% if form.errors %} +

    + {% if form.errors|length == 1 %} + {% translate "Please correct the error below." %} + {% else %} + {% translate "Please correct the errors below." %} + {% endif %} +

    + {% endif %} +
    + {% for field in form %} +
    + {{ field.errors }} +
    +
    + {{ field.label_tag }} {{ field }} +
    +
    +
    + {% endfor %} +
    +
    + +
    +
    +{% endblock %}