blob: ee82be142e8c734e3c5f5f53e79f576339ca8ce3 [file] [log] [blame]
# Copyright 2014 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 with generalized student forms related views.
The classes defined here are supposed to be instantiated with dependencies that
are specific to actual programs.
"""
import logging
from django import http
from google.appengine.ext import blobstore
from melange.logic import profile as profile_logic
from melange.models import profile as profile_model
from melange.request import access
from melange.request import exception
from soc.views import base
from soc.views.helper import blobstore as bs_helper
from soc.views.helper import url_patterns
# GET parameter to specify the type of form.
FORM_TYPE_GET_PARAM = 'form_type'
# form type corresponding to parental consent form.
CONSENT_FORM_TYPE = 'consent_form'
# form type corresponding to student enrollment form.
ENROLLMENT_FORM_TYPE = 'enrollment_form'
# form type corresponding to awaiting forms profile flag.
AWAITING_FORMS_TYPE = 'awaiting_forms'
# regex pattern that contains all types of forms supported by this module.
FORM_TYPES_REGEX = '|'.join([CONSENT_FORM_TYPE, ENROLLMENT_FORM_TYPE,
AWAITING_FORMS_TYPE])
# action type corresponding to verifying a form.
VERIFY_ACTION_TYPE = 'verify'
# action type corresponding to un-verifying a form.
UNVERIFY_ACTION_TYPE = 'unverify'
# regex pattern that contains all types of actions supported by the view.
ACTION_TYPES_REGEX = '|'.join([UNVERIFY_ACTION_TYPE, VERIFY_ACTION_TYPE])
class StudentFormVerify(base.RequestHandler):
"""Handler to verify student forms."""
access_checker = access.ConjuctionAccessChecker([
access.PROGRAM_ADMINISTRATOR_ACCESS_CHECKER,
access.STUDENT_URL_PROFILE_ACCESS_CHECKER])
def __init__(self, initializer, linker, renderer, error_handler,
url_pattern_constructor, url_names):
"""Initializes a new instance of the request handler for the specified
parameters.
Args:
initializer: Implementation of initialize.Initializer interface.
linker: Instance of links.Linker class.
renderer: Implementation of render.Renderer interface.
error_handler: Implementation of error.ErrorHandler interface.
url_pattern_constructor:
Implementation of url_patterns.UrlPatternConstructor.
url_names: Instance of url_names.UrlNames.
"""
super(StudentFormVerify, self).__init__(
initializer, linker, renderer, error_handler)
self.url_pattern_constructor = url_pattern_constructor
self.url_names = url_names
def djangoURLPatterns(self):
"""See base.RequestHandler.djangoURLPatterns for specification."""
return [
self.url_pattern_constructor.construct(
r'profile/forms/(?P<action>%s)/(?P<form_type>%s)/%s$' % (
ACTION_TYPES_REGEX, FORM_TYPES_REGEX, url_patterns.PROFILE),
self, name=self.url_names.STUDENT_FORM_VERIFY)]
def post(self, data, check, mutator):
"""See base.RequestHandler.post for specification."""
action = data.kwargs['action']
if action == VERIFY_ACTION_TYPE:
is_verified = True
elif action == UNVERIFY_ACTION_TYPE:
is_verified = False
else:
# This should never happen
logging.error('Unknown action state: %s', action)
raise exception.BadRequest('Unknown action state: %s' % action)
form_type = data.kwargs['form_type']
if form_type == CONSENT_FORM_TYPE:
prop = profile_model.StudentData.is_consent_form_verified
profile_prop = 'student_data'
elif form_type == ENROLLMENT_FORM_TYPE:
prop = profile_model.StudentData.is_enrollment_form_verified
profile_prop = 'student_data'
elif form_type == AWAITING_FORMS_TYPE:
prop = prop = profile_model.Flags.awaiting_forms
profile_prop = 'flags'
else:
# This should never happen
logging.error('Unknown form type: %s', form_type)
raise exception.BadRequest('Unknown form type: %s' % form_type)
profile_logic.editProfile(
data.url_ndb_profile.key, {profile_prop: {prop._name: is_verified}})
return http.HttpResponse()
class StudentFormDownload(base.RequestHandler):
"""Handler to download uploaded student forms."""
access_checker = access.PROGRAM_ADMINISTRATOR_ACCESS_CHECKER
def __init__(self, initializer, linker, renderer, error_handler,
url_pattern_constructor, url_names):
"""Initializes a new instance of the request handler for the specified
parameters.
Args:
initializer: Implementation of initialize.Initializer interface.
linker: Instance of links.Linker class.
renderer: Implementation of render.Renderer interface.
error_handler: Implementation of error.ErrorHandler interface.
url_pattern_constructor:
Implementation of url_patterns.UrlPatternConstructor.
url_names: Instance of url_names.UrlNames.
"""
super(StudentFormDownload, self).__init__(
initializer, linker, renderer, error_handler)
self.url_pattern_constructor = url_pattern_constructor
self.url_names = url_names
def djangoURLPatterns(self):
"""See base.RequestHandler.djangoURLPatterns for specification."""
return [
self.url_pattern_constructor.construct(
r'profile/forms/%s$' % url_patterns.PROFILE, self,
name=self.url_names.STUDENT_FORM_DOWNLOAD)]
def get(self, data, check, mutator):
"""See base.RequestHandler.get for specification."""
form_type = data.GET.get(FORM_TYPE_GET_PARAM)
if form_type == CONSENT_FORM_TYPE:
uploaded_form = data.url_ndb_profile.student_data.consent_form
elif form_type == ENROLLMENT_FORM_TYPE:
uploaded_form = data.url_ndb_profile.student_data.enrollment_form
else:
raise exception.BadRequest(message='No file requested')
# download has been requested
if uploaded_form:
return bs_helper.sendBlobZip(blobstore.BlobInfo(uploaded_form))
else:
raise exception.NotFound(message='File not found')