This repository has been archived by the owner on Jun 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
168 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from wagtail.wagtailadmin.forms import WagtailAdminPageForm | ||
|
||
|
||
class StaffPageForm(WagtailAdminPageForm): | ||
def clean(self): | ||
cleaned_data = super(StaffPageForm, self).clean() | ||
|
||
for form in self.formsets.get('roles', []): | ||
self.clean_staff_page_role_form(form, cleaned_data) | ||
|
||
def clean_staff_page_role_form(self, form, staff_page_cleaned_data): | ||
if not form.is_valid(): | ||
return | ||
|
||
area = form.cleaned_data.get('area') | ||
location = form.cleaned_data.get('location') | ||
programme = form.cleaned_data.get('programme') | ||
school = form.cleaned_data.get('school') | ||
staff_type = staff_page_cleaned_data.get('staff_type') | ||
|
||
# Will display all errors at the same time, so need to create a dict | ||
errors = {x: [] for x in ('area', 'location', 'programme', 'school')} | ||
|
||
# Staff location must be only for technical staff | ||
if staff_type != 'technical' and location: | ||
errors['location'].append('Location can be assigned only to technical staff') | ||
|
||
# School and programme must be only for academic staff | ||
if staff_type != 'academic': | ||
if school: | ||
errors['school'].append('School can be assigned only to academic staff.') | ||
|
||
if programme: | ||
errors['programme'].append('Programme can be only assigned to academic staff.') | ||
|
||
# Area cannot be filled in when staff is non-academic/administrative | ||
if staff_type not in ('academic', 'administrative') and area: | ||
errors['area'].append('Area can be only assigned to academic or administrative staff.') | ||
|
||
# If there are any errors in our dict, raise them. | ||
if any(errors.itervalues()): | ||
for field_name, field_errors in errors.items(): | ||
for field_error in field_errors: | ||
form.add_error(field_name, field_error) |
65 changes: 43 additions & 22 deletions
65
django-verdant/rca/migrations/0087_staffpage_migrate_area_data_to_roles.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
django-verdant/rca/migrations/0089_staffpage_move_location_data.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.9.13 on 2017-07-06 21:26 | ||
from __future__ import unicode_literals | ||
import json | ||
|
||
from django.db import migrations | ||
|
||
|
||
def move_location_data(apps, schema_editor): | ||
""" | ||
Move StaffPage.location to StaffPageRole.location if the user is | ||
technical. | ||
""" | ||
StaffPage = apps.get_model('rca', 'StaffPage') | ||
|
||
# Move location data to roles | ||
records = StaffPage.objects \ | ||
.prefetch_related('roles') \ | ||
.exclude(staff_location='') \ | ||
.filter(staff_type='technical') \ | ||
|
||
print('Found {} records of technical staff with location data ' | ||
'to migrate.'.format(records.count())) | ||
|
||
for staff in records: | ||
# Update revisions | ||
if staff.has_unpublished_changes: | ||
# Can't use get_latest_revision() as I am unable to call | ||
# model methods in migrations. | ||
revision = staff.revisions.order_by('-created_at', '-id').first() | ||
|
||
if revision: | ||
revision_json = json.loads(revision.content_json) | ||
|
||
if revision_json['roles']: | ||
print('Updating draft of {} (#{}).'.format(staff, staff.pk)) | ||
|
||
revision_json['roles'][0]['location'] = staff.staff_location | ||
|
||
revision.content_json = json.dumps(revision_json) | ||
revision.save() | ||
|
||
first_role = staff.roles.first() | ||
|
||
if first_role is not None: | ||
print('Updating live version of {} (#{}).'.format(staff, staff.pk)) | ||
|
||
first_role.location = staff.staff_location | ||
first_role.save() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
('rca', '0088_staffpage_move_area_and_location_field_to_roles'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(move_location_data) | ||
] |
19 changes: 19 additions & 0 deletions
19
django-verdant/rca/migrations/0090_staffpage_remove_location.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.9.13 on 2017-07-06 21:28 | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('rca', '0089_staffpage_move_location_data'), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name='staffpage', | ||
name='staff_location', | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters