Skip to content

Commit

Permalink
Merge pull request #285 from imaegg11/late-start-form
Browse files Browse the repository at this point in the history
Faster Way To Add Late Starts
  • Loading branch information
JasonLovesDoggo authored Nov 11, 2024
2 parents 790fd11 + 3bc9373 commit 87628d5
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 1 deletion.
90 changes: 89 additions & 1 deletion core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
TermAdminForm,
UserAdminForm,
UserCreationAdminForm,
LateStartEventForm
)
from .models import Comment, StaffMember
from .utils.actions import (
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions core/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -13,6 +14,7 @@
from core.views.mixins import CaseInsensitiveUsernameMixin



class MetropolisSignupForm(SignupForm, CaseInsensitiveUsernameMixin):
first_name = forms.CharField(
max_length=30,
Expand Down Expand Up @@ -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'}
)
10 changes: 10 additions & 0 deletions templates/admin/change_list_buttons.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "admin/change_list.html" %}
{% load i18n admin_urls static admin_list %}
{% block object-tools-items %}
{% for button in buttons %}
<li>
<a href="{{ button.url }}" class="grp-state-focus addlink">{{ button.name }}</a>
</li>
{% endfor %}
{{ block.super }}
{% endblock %}
49 changes: 49 additions & 0 deletions templates/admin/custom_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{% extends "admin/base_site.html" %}
{% load i18n admin_urls static admin_modify %}

{% block extrahead %}
{{ block.super }}
<script src="{% url 'admin:jsi18n' %}"></script>
<script src="{% static "admin/js/core.js" %}"></script>
{{ media }}
{% endblock %}

{% block extrastyle %}
{{ block.super }}
<link rel="stylesheet" type="text/css" href="{% static "admin/css/forms.css" %}">
{% endblock %}

{% if not is_popup %}
{% block breadcrumbs %}<div class="breadcrumbs" style="height:0px;padding:0px"></div>{% endblock %}
{% endif %}

{% block coltype %}colM{% endblock %}
{% block content %}
<form action="{{ url }}" method="post">
{% csrf_token %}
{% if form.errors %}
<p class="errornote">
{% if form.errors|length == 1 %}
{% translate "Please correct the error below." %}
{% else %}
{% translate "Please correct the errors below." %}
{% endif %}
</p>
{% endif %}
<fieldset class="module aligned ">
{% for field in form %}
<div class="form-row field-name">
{{ field.errors }}
<div>
<div class="flex-container">
{{ field.label_tag }} {{ field }}
</div>
</div>
</div>
{% endfor %}
</fieldset>
<div class="submit-row">
<input class="default" type="submit" value="Save">
</div>
</form>
{% endblock %}

0 comments on commit 87628d5

Please sign in to comment.