-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathstaff_views.py
128 lines (115 loc) · 4.03 KB
/
staff_views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from django.contrib import messages
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth.models import User
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from apps.ldap.utils import add_officer, is_officer, is_root
from .forms import OfficerCreationForm
from .models import (
Officer,
Officership,
Person,
Politburo,
PolitburoMembership,
Semester,
)
@staff_member_required
def update_or_create_officer(request):
semester = Semester.objects.filter(current=True).get()
if request.method == "POST":
form = OfficerCreationForm(request.POST, request.FILES)
if form.is_valid():
username = form.cleaned_data.get("username")
blurb = form.cleaned_data.get("blurb")
office_hours = form.cleaned_data.get("office_hours")
photo = form.cleaned_data.get("photo")
photo_url = form.cleaned_data.get("photo_url")
photo2 = form.cleaned_data.get("photo2")
photo2_url = form.cleaned_data.get("photo2_url")
officer_since = form.cleaned_data.get("officer_since")
_update_or_create_officer(
request,
username,
photo,
photo_url,
photo2,
photo2_url,
officer_since,
blurb,
office_hours,
semester,
)
return HttpResponseRedirect(reverse("add-officer"))
else:
form = OfficerCreationForm()
return render(request, "add_officer.html", {"form": form, "semester": semester})
def _update_or_create_officer(
request,
username,
photo,
photo_url,
photo2,
photo2_url,
officer_since,
blurb,
office_hours,
semester,
):
user, created = User.objects.get_or_create(username=username)
if created:
messages.info(request, f"User {username} created")
defaults = {}
if photo:
messages.info(request, f"Updated photo1 for {username}")
defaults.update(photo1=photo)
elif photo_url:
...
# TODO: download photo
messages.warning(request, "Using photo_url is not yet supported")
if photo2:
messages.info(request, f"Updated photo2 for {username}")
defaults.update(photo2=photo)
elif photo2_url:
...
# TODO: download photo
messages.warning(request, "Using photo2_url is not yet supported")
person, created = Person.objects.update_or_create(user=user, defaults=defaults)
person.save()
if created:
messages.info(request, f"Person {username} created")
root_staff = is_root(username)
defaults = {"root_staff": root_staff}
if officer_since:
defaults.update(officer_since=officer_since)
messages.info(
request, f"Officer {username} start date updated to {officer_since}"
)
officer, created = Officer.objects.update_or_create(
person=person, defaults=defaults
)
if created:
messages.info(request, f"Officer {username} created")
defaults = {}
if blurb:
defaults.update(blurb=blurb)
messages.info(request, f"Updated blurb of {username}")
if office_hours:
defaults.update(office_hours=office_hours)
messages.info(request, f"Office hour updated to {office_hours}")
officership = Officership.objects.update_or_create(
officer=officer, semester=semester, defaults=defaults
)
if not is_officer(username):
success, msg = add_officer(username)
if success:
messages.info(request, f"Added {username} to officers LDAP group")
else:
messages.error(
request, f"Failed to add {username} to officers LDAP group: {msg}"
)
messages.info(request, f"{username} updated")
@staff_member_required
def update_semester(request):
semesters = Semester.objects.all()
return render(request, "update_semester.html", {"semesters": semesters})