blob: 0d545a61184622df3042240e40e3f6c21044b7bb [file] [log] [blame]
# Copyright 2011 the Melange authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.
"""Module for the org applications."""
from google.appengine.ext import ndb
from django import forms as django_forms
from django.utils.translation import ugettext
from soc.models import org_app_record
from soc.views import forms
from soc.logic import cleaning
from soc.models.org_app_record import OrgAppRecord
from soc.models.org_app_survey import OrgAppSurvey
from soc.views.readonly_template import SurveyRecordReadOnlyTemplate
'Backup admin does not have an org admin profile for the program. Please '
'ask your backup admin to register a profile for %s at %s')
'Finalize decisions and send acceptance/rejection emails'
NEW_ORG_CHOICES = [('Veteran', 'Veteran'), ('New', 'New')]
class OrgAppEditForm(forms.SurveyEditForm):
"""Form to create/edit organization application survey.
class Meta:
model = OrgAppSurvey
css_prefix = 'org-app-edit'
exclude = ['scope', 'author', 'program', 'created_by', 'modified_by']
class OrgAppTakeForm(forms.SurveyTakeForm):
"""Form for would-be organization admins to apply for the program.
backup_admin_id = django_forms.CharField(
label=ugettext('Backup Admin'), required=True,
help_text=ugettext('The username of the user who will serve as the '
'backup admin for this organization.'))
# We render this field as a select field instead of a checkbox because
# of the visibility on the form. The checkbox field because of its location
# is not correctly visible to the person who fills the form, so we may
# have trouble later. As a precaution, we display this field as a choice
# field and then convert the data back to boolean value in the corresponding
# field cleaner.
new_org = forms.ChoiceField(choices=NEW_ORG_CHOICES)
def __init__(self, bound_class_field, request_data=None, **kwargs):
self.request_data = request_data
# Workaround for Django's limitation of not being able to set initial value
# for ChoiceField after calling super.
if 'instance' in kwargs:
kwargs['initial'] = {
'new_org': 'New' if kwargs['instance'].new_org else 'Veteran'
super(OrgAppTakeForm, self).__init__(
bound_class_field, survey=self.request_data.org_app, **kwargs)
if self.instance:
backup_admin_key = ndb.Key.from_old_key(
self.fields['backup_admin_id'].initial = backup_admin_key.get().user_id
# not marked required by data model for backwards compatibility
self.fields['org_id'].required = True
class Meta:
model = OrgAppRecord
css_prefix = 'org-app-record'
exclude = ['main_admin', 'backup_admin', 'status', 'user', 'survey',
'created', 'modified', 'program', 'agreed_to_admin_agreement']
widgets = forms.choiceWidgets(model,
def clean_org_id(self):
org_id = cleaning.clean_link_id('org_id')(self)
if not org_id:
# manual required check, see Issue 1291
raise django_forms.ValidationError('This field is required.')
q = OrgAppRecord.all()
q.filter('survey', self.survey)
q.filter('org_id', org_id)
org_app = q.get()
if org_app:
# If we are creating a new org app it is a duplicate, if we are editing
# an org app we must check if the one we found has a different key.
if (not self.instance) or (org_app.key() != self.instance.key()):
raise django_forms.ValidationError('This ID has already been taken.')
return org_id
def clean_backup_admin_id(self):
backup_admin = cleaning.clean_existing_user('backup_admin_id')(self)
if not self.instance:
main_admin_key = ndb.Key.from_old_key(
if main_admin_key == backup_admin.key:
raise django_forms.ValidationError(
'You cannot enter the person who created the application here.')
self.cleaned_data['backup_admin'] = backup_admin.key.to_old_key()
return backup_admin
def clean_new_org(self):
"""Converts the select widget value of the new_org field from the form to
the boolean value required by the backing data model.
return self.cleaned_data['new_org'] == 'New'
def clean(self):
cleaned_data = self.cleaned_data
# pop our custom id field if exists
if 'backup_admin_id' in cleaned_data:
return cleaned_data
def _getCreateProfileURL(self, redirector):
"""Returns the full secure URL of the create profile page."""
raise NotImplementedError
class OrgAppReadOnlyTemplate(SurveyRecordReadOnlyTemplate):
"""Template to construct readonly organization application record.
class Meta:
model = OrgAppRecord
css_prefix = 'org-app-show'
fields = ['org_id', 'name', 'description', 'home_page', 'license',
renderers = {
'new_org': lambda instance: 'New' if instance.new_org else 'Veteran'
survey_name = 'Organization Application'