app/soc/views/models/survey.py
changeset 2475 dfb9249d91b6
parent 2474 b8e25769880d
child 2476 c27c21beba35
equal deleted inserted replaced
2474:b8e25769880d 2475:dfb9249d91b6
    26 import csv
    26 import csv
    27 import datetime
    27 import datetime
    28 import re
    28 import re
    29 import StringIO
    29 import StringIO
    30 import string
    30 import string
       
    31 
       
    32 from google.appengine.ext import db
       
    33 
    31 from django import forms
    34 from django import forms
    32 from django import http
    35 from django import http
    33 from django.utils import simplejson
    36 from django.utils import simplejson
    34 
    37 
    35 from google.appengine.ext import db
       
    36 
       
    37 from soc.cache import home
       
    38 from soc.logic import cleaning
    38 from soc.logic import cleaning
    39 from soc.logic import dicts
    39 from soc.logic import dicts
       
    40 from soc.logic.models.survey import GRADES
    40 from soc.logic.models.survey import logic as survey_logic
    41 from soc.logic.models.survey import logic as survey_logic
    41 from soc.logic.models.survey import GRADES
       
    42 from soc.logic.models.user import logic as user_logic
    42 from soc.logic.models.user import logic as user_logic
    43 from soc.models.survey import Survey
    43 from soc.models.survey import Survey
    44 from soc.models.survey_record import SurveyRecord
    44 from soc.models.survey_record import SurveyRecord
    45 from soc.models.user import User
    45 from soc.models.user import User
    46 from soc.views import out_of_band
    46 from soc.views import out_of_band
   101     rights['create'] = ['checkIsUser']
   101     rights['create'] = ['checkIsUser']
   102     rights['edit'] = [('checkIsSurveyWritable', survey_logic)]
   102     rights['edit'] = [('checkIsSurveyWritable', survey_logic)]
   103     rights['delete'] = ['checkIsDeveloper'] # TODO: fix deletion of Surveys
   103     rights['delete'] = ['checkIsDeveloper'] # TODO: fix deletion of Surveys
   104     rights['list'] = ['checkDocumentList']
   104     rights['list'] = ['checkDocumentList']
   105     rights['pick'] = ['checkDocumentPick']
   105     rights['pick'] = ['checkDocumentPick']
       
   106     rights['take'] = ['checkIsDeveloper'] # TODO(ljvderijk) test proper check
   106 
   107 
   107     new_params = {}
   108     new_params = {}
   108     new_params['logic'] = survey_logic
   109     new_params['logic'] = survey_logic
   109     new_params['rights'] = rights
   110     new_params['rights'] = rights
   110 
   111 
   111     new_params['name'] = "Survey"
   112     new_params['name'] = "Survey"
   112     new_params['pickable'] = True
   113     new_params['pickable'] = True
   113 
   114 
   114     new_params['extra_django_patterns'] = [
   115     new_params['extra_django_patterns'] = [
       
   116          (r'^%(url_name)s/(?P<access_type>take)/%(key_fields)s$',
       
   117          'soc.views.models.%(module_name)s.take',
       
   118          'Take %(module_name)s'),
   115          (r'^%(url_name)s/(?P<access_type>json)/%(scope)s$',
   119          (r'^%(url_name)s/(?P<access_type>json)/%(scope)s$',
   116          'soc.views.models.%(module_name)s.json',
   120          'soc.views.models.%(module_name)s.json',
   117          'Export %(name)s as JSON'),
   121          'Export %(name)s as JSON'),
   118         (r'^%(url_name)s/(?P<access_type>results)/%(scope)s$',
   122         (r'^%(url_name)s/(?P<access_type>results)/%(scope)s$',
   119          'soc.views.models.%(module_name)s.results',
   123          'soc.views.models.%(module_name)s.results',
   131         'link_id', 'scope_path', 'name', 'short_name', 'title',
   135         'link_id', 'scope_path', 'name', 'short_name', 'title',
   132         'content', 'prefix','read_access','write_access']
   136         'content', 'prefix','read_access','write_access']
   133 
   137 
   134     new_params['edit_template'] = 'soc/survey/edit.html'
   138     new_params['edit_template'] = 'soc/survey/edit.html'
   135     new_params['create_template'] = 'soc/survey/edit.html'
   139     new_params['create_template'] = 'soc/survey/edit.html'
       
   140     new_params['take_template'] = 'soc/survey/take.html'
   136 
   141 
   137     # TODO which one of these are leftovers from Document?
   142     # TODO which one of these are leftovers from Document?
   138     new_params['no_create_raw'] = True
   143     new_params['no_create_raw'] = True
   139     new_params['no_create_with_scope'] = True
   144     new_params['no_create_with_scope'] = True
   140     new_params['no_create_with_key_fields'] = True
   145     new_params['no_create_with_key_fields'] = True
   484     if entity.survey_end and datetime.datetime.now() > entity.survey_end:
   489     if entity.survey_end and datetime.datetime.now() > entity.survey_end:
   485       # are we already passed the survey_end?
   490       # are we already passed the survey_end?
   486       context["passed_survey_end"] = True
   491       context["passed_survey_end"] = True
   487 
   492 
   488     return super(View, self).editGet(request, entity, context, params=params)
   493     return super(View, self).editGet(request, entity, context, params=params)
       
   494 
       
   495   @decorators.merge_params
       
   496   @decorators.check_access
       
   497   def take(self, request, access_type, page_name=None,
       
   498            params=None, **kwargs):
       
   499     """View for taking a Survey.
       
   500 
       
   501     For Args see base.View().public().
       
   502     """
       
   503 
       
   504     try:
       
   505       entity = self._logic.getFromKeyFieldsOr404(kwargs)
       
   506     except out_of_band.Error, error:
       
   507       return responses.errorResponse(
       
   508           error, request, template=params['error_public'])
       
   509 
       
   510     template = params['take_template']
       
   511 
       
   512     # get the context for this webpage
       
   513     context = responses.getUniversalContext(request)
       
   514     responses.useJavaScript(context, params['js_uses_all'])
       
   515     context['page_name'] = "%s titled '%s'" % (page_name, entity.title)
       
   516     context['entity'] = entity
       
   517 
       
   518     if request.POST:
       
   519       return self.takePost(request, template, context, params, entity,
       
   520                            **kwargs)
       
   521     else: #request.GET
       
   522       return self.takeGet(request, template, context, params, entity,
       
   523                           **kwargs)
       
   524 
       
   525   def takeGet(self, request, template, context, params, entity, **kwargs):
       
   526     """Handles the GET request for the Survey's take page.
       
   527 
       
   528     Args:
       
   529         template: the template used for this view
       
   530         entity: the student project entity
       
   531         rest: see base.View.public()
       
   532     """
       
   533 
       
   534     # TODO(ljvderijk) implement takeGet
       
   535 
       
   536     return http.HttpResponse("Work in Progress")
       
   537 
       
   538   def takePost(self, request, template, context, params, entity, **kwargs):
       
   539     """Handles the POST request for the Survey's take page.
       
   540 
       
   541     Args:
       
   542         template: the template used for this view
       
   543         entity: the student project entity
       
   544         rest: see base.View.public()
       
   545     """
       
   546 
       
   547     # TODO(ljvderijk) implement takePost
       
   548 
       
   549     return httpHttpResponse("Work in Progress")
   489 
   550 
   490   def activate(self, request, **kwargs):
   551   def activate(self, request, **kwargs):
   491     """This is a hack to support the 'Enable grades' button.
   552     """This is a hack to support the 'Enable grades' button.
   492     """
   553     """
   493     self.activateGrades(request)
   554     self.activateGrades(request)
   785 view = View()
   846 view = View()
   786 
   847 
   787 admin = decorators.view(view.admin)
   848 admin = decorators.view(view.admin)
   788 create = decorators.view(view.create)
   849 create = decorators.view(view.create)
   789 edit = decorators.view(view.edit)
   850 edit = decorators.view(view.edit)
       
   851 export = decorators.view(view.export)
   790 delete = decorators.view(view.delete)
   852 delete = decorators.view(view.delete)
       
   853 json = decorators.view(view.exportSerialized)
   791 list = decorators.view(view.list)
   854 list = decorators.view(view.list)
   792 public = decorators.view(view.public)
   855 public = decorators.view(view.public)
   793 export = decorators.view(view.export)
       
   794 pick = decorators.view(view.pick)
   856 pick = decorators.view(view.pick)
   795 results = decorators.view(view.viewResults)
   857 results = decorators.view(view.viewResults)
   796 json = decorators.view(view.exportSerialized)
   858 take = decorators.view(view.take)