blob: b5575dc82c30714e12fdb2804a1cfbceb161f295 [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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Module for the GCI Organization application."""
from django.utils.translation import ugettext
from codein.views.helper import urls
from melange.request import access
from melange.request import exception
from melange.request import links
from soc.views import org_app
from soc.views.helper import access_checker
from soc.views.helper import url_patterns
from soc.modules.gci.views import forms
from soc.modules.gci.views.base import GCIRequestHandler
from soc.modules.gci.views.helper import url_patterns as gci_url_patterns
_BASE_FORM_TEMPLATE_PATH = 'modules/gci/_form.html'
class OrgAppEditForm(org_app.OrgAppEditForm):
"""Form to create/edit GCI organization application survey."""
Meta = org_app.OrgAppEditForm.Meta
def __init__(self, **kwargs):
super(OrgAppEditForm, self).__init__(forms.GCIBoundField, **kwargs)
def templatePath(self):
return _BASE_FORM_TEMPLATE_PATH
class OrgAppTakeForm(org_app.OrgAppTakeForm):
"""Form for would-be organization admins to apply for a GCI program."""
CHECKBOX_SELECT_MULTIPLE = forms.CheckboxSelectMultiple
RADIO_FIELD_RENDERER = forms.RadioFieldRenderer
Meta = org_app.OrgAppTakeForm.Meta
def __init__(self, request_data=None, **kwargs):
super(OrgAppTakeForm, self).__init__(
forms.GCIBoundField, request_data=request_data, **kwargs)
def templatePath(self):
return _BASE_FORM_TEMPLATE_PATH
def _getCreateProfileURL(self, redirector):
"""Returns the full secure URL of the GCI create profile page."""
return links.ABSOLUTE_LINKER.program(
self.data.program, urls.UrlNames.PROFILE_REGISTER_AS_ORG_MEMBER,
secure=True)
class GCIOrgAppEditPage(GCIRequestHandler):
"""View for creating/editing organization application."""
access_checker = access.PROGRAM_ADMINISTRATOR_ACCESS_CHECKER
def djangoURLPatterns(self):
return [
gci_url_patterns.url(
r'org/application/edit/%s$' % url_patterns.PROGRAM,
self, name='gci_edit_org_app'),
]
def templatePath(self):
return 'modules/gci/org_app/edit.html'
def context(self, data, check, mutator):
if data.org_app:
form = OrgAppEditForm(data=data.POST or None, instance=data.org_app)
else:
form = OrgAppEditForm(data=data.POST or None)
if data.org_app:
page_name = ugettext('Edit - %s' % data.org_app.title)
else:
page_name = 'Create new organization application'
context = {
'page_name': page_name,
'post_url': self.linker.program(data.program, 'gci_edit_org_app'),
'forms': [form],
'error': bool(form.errors),
}
return context
def orgAppFromForm(self, data):
"""Create/edit the organization application entity from form.
Args:
data: A RequestData describing the current request.
Returns:
a newly created or updated organization application entity or None.
"""
if data.org_app:
form = OrgAppEditForm(data=data.POST, instance=data.org_app)
else:
form = OrgAppEditForm(data=data.POST)
if not form.is_valid():
return None
form.cleaned_data['modified_by'] = data.ndb_user.key.to_old_key()
if not data.org_app:
form.cleaned_data['created_by'] = data.ndb_user.key.to_old_key()
form.cleaned_data['program'] = data.program
key_name = 'gci_program/%s/orgapp' % data.program.key().name()
entity = form.create(key_name=key_name, commit=True)
else:
entity = form.save(commit=True)
return entity
def post(self, data, check, mutator):
if self.orgAppFromForm(data):
# TODO(nathaniel): make unnecessary this .program() call.
data.redirect.program()
return data.redirect.to('gci_edit_org_app', validated=True)
else:
# TODO(nathaniel): problematic self-call.
return self.get(data, check, mutator)
class GCIOrgAppPreviewPage(GCIRequestHandler):
"""Organization Application preview page.
View for Organization Administrators to preview the organization
application for the program specified in the URL.
"""
access_checker = access.PROGRAM_ADMINISTRATOR_ACCESS_CHECKER
def djangoURLPatterns(self):
return [
gci_url_patterns.url(
r'org/application/preview/%s$' % url_patterns.PROGRAM,
self, name='gci_preview_org_app'),
]
def templatePath(self):
return 'modules/gci/org_app/take.html'
def context(self, data, check, mutator):
form = OrgAppTakeForm(request_data=data)
context = {
'page_name': '%s' % data.org_app.title,
'description': data.org_app.content,
'forms': [form],
'error': bool(form.errors),
'hide_buttons': True,
}
return context
class OrgAppReadOnlyTemplate(org_app.OrgAppReadOnlyTemplate):
"""Template to construct readonly organization application record."""
template_path = 'modules/gci/org_app/readonly_template.html'
class GCIOrgAppShowPage(GCIRequestHandler):
"""View to display the readonly page for organization application."""
def djangoURLPatterns(self):
return [
gci_url_patterns.url(r'org/application/show/%s$' % url_patterns.ID,
self, name='gci_show_org_app'),
]
def checkAccess(self, data, check, mutator):
if not data.org_app:
raise exception.NotFound(
message=access_checker.DEF_NO_ORG_APP % data.program.name)
mutator.orgAppRecordIfIdInKwargs()
assert access_checker.isSet(data.org_app_record)
check.canViewOrgApp()
def templatePath(self):
return 'modules/gci/org_app/show.html'
def context(self, data, check, mutator):
record = data.org_app_record
context = {
'page_name': 'Organization application - %s' % (record.name),
'organization': record.name,
'css_prefix': OrgAppReadOnlyTemplate.Meta.css_prefix,
}
if record:
context['record'] = OrgAppReadOnlyTemplate(record)
return context