Commit c3304159 authored by Daniel W Bond's avatar Daniel W Bond
Browse files

relying more heavily on django's built-in cleaning; fixes the error where you...

relying more heavily on django's built-in cleaning; fixes the error where you aren't able to clear out your graduation year
parent 552c5ff2
...@@ -183,19 +183,15 @@ class UpdateStudent(LoginRequiredMixin, FormValidMessageMixin, FormView): ...@@ -183,19 +183,15 @@ class UpdateStudent(LoginRequiredMixin, FormValidMessageMixin, FormView):
if me.recent_changes() > 2: if me.recent_changes() > 2:
form_room = current_room form_room = current_room
else: else:
try: form_room = form.cleaned_data.get('room')
form_room = Room.objects.get(pk=form.data['room'])
except:
form_room = None
# casts to an integer, 0 or 1
on_campus = strtobool(form.data.get('on_campus', 'True'))
# no room if you move off campus # no room if you move off campus
on_campus = form.cleaned_data.get('on_campus')
if not on_campus: if not on_campus:
form_room = None form_room = None
# note this is after the 'on campus' check # note this is after the 'on campus' check
# remove any flags you've received
if current_room != form_room: if current_room != form_room:
me.times_changed_room += 1 me.times_changed_room += 1
Confirmation.objects.filter(student=me).delete() Confirmation.objects.filter(student=me).delete()
...@@ -208,7 +204,7 @@ class UpdateStudent(LoginRequiredMixin, FormValidMessageMixin, FormView): ...@@ -208,7 +204,7 @@ class UpdateStudent(LoginRequiredMixin, FormValidMessageMixin, FormView):
if not(me.on_campus): if not(me.on_campus):
me.privacy = 'students' me.privacy = 'students'
else: else:
me.privacy = form.data['privacy'] me.privacy = form.cleaned_data.get('privacy')
# if you are an RA or an RD, you live on campus # if you are an RA or an RD, you live on campus
if me.is_staff(): if me.is_staff():
...@@ -217,11 +213,10 @@ class UpdateStudent(LoginRequiredMixin, FormValidMessageMixin, FormView): ...@@ -217,11 +213,10 @@ class UpdateStudent(LoginRequiredMixin, FormValidMessageMixin, FormView):
try: try:
# in case someone disabled the js, limit processing to only the first # in case someone disabled the js, limit processing to only the first
# two majors passed by the user # two majors passed by the user
#
# we also eliminate the potential a student manipulates the form to # we also eliminate the potential a student manipulates the form to
# pass in two majors of the same type by casting to a set # pass in two majors of the same type by casting to a set
form_major_pks = set(form.data.getlist('major')[:2]) form_majors = set(form.cleaned_data.get('major')[:2])
# retrieve the major objects from the list of pk strings
form_majors = [Major.objects.get(pk=pk) for pk in form_major_pks]
# print(form_majors) # print(form_majors)
# iterate over a student's current majors # iterate over a student's current majors
for current_major in me.major.all(): for current_major in me.major.all():
...@@ -235,15 +230,15 @@ class UpdateStudent(LoginRequiredMixin, FormValidMessageMixin, FormView): ...@@ -235,15 +230,15 @@ class UpdateStudent(LoginRequiredMixin, FormValidMessageMixin, FormView):
me.major.add(form_major) me.major.add(form_major)
except: except:
# don't change majors # don't change majors
# if someone is selecting a major pk that does not exist, don't do anything
pass pass
# replicate the same thing for the other m2m field # replicate the same thing for the other m2m field
try: try:
form_blocked_pks = set(form.data.getlist('blocked_kids')) form_blocked = set(form.cleaned_data.get('blocked_kids'))
current_blocked = me.blocked_kids.all() current_blocked = me.blocked_kids.all()
# most people will not being blocking other students # most people will not being blocking other students
if form_blocked_pks or current_blocked: if form_blocked or current_blocked:
form_blocked = [Student.objects.get(pk=pk) for pk in form_blocked_pks]
for current_block in current_blocked: for current_block in current_blocked:
if current_block not in form_blocked: if current_block not in form_blocked:
me.blocked_kids.remove(current_block) me.blocked_kids.remove(current_block)
...@@ -253,11 +248,14 @@ class UpdateStudent(LoginRequiredMixin, FormValidMessageMixin, FormView): ...@@ -253,11 +248,14 @@ class UpdateStudent(LoginRequiredMixin, FormValidMessageMixin, FormView):
except: except:
pass pass
me.user.first_name = no_nums(form.data['first_name']) # silently remove numbers from names
me.user.last_name = no_nums(form.data['last_name']) me.user.first_name = no_nums(form.cleaned_data.get('first_name'))
me.gender = form.data.getlist('gender') me.user.last_name = no_nums(form.cleaned_data.get('last_name'))
me.show_gender = strtobool(form.data.get('show_gender', 'False'))
me.graduating_year = form.data['graduating_year'] me.gender = form.cleaned_data.get('gender')
me.show_gender = form.cleaned_data.get('show_gender')
me.graduating_year = form.cleaned_data.get('graduating_year')
me.user.save() me.user.save()
me.save() me.save()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment