app/soc/views/models/survey.py
changeset 2442 dd1f94c3594c
parent 2440 05c430d1c147
child 2447 dae6715a2f19
equal deleted inserted replaced
2441:a24b2b4af87d 2442:dd1f94c3594c
    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 
    31 
       
    32 from google.appengine.ext import db
       
    33 
    32 from django import forms
    34 from django import forms
    33 from django import http
    35 from django import http
    34 from django.utils import simplejson
    36 from django.utils import simplejson
    35 
       
    36 from google.appengine.ext import db
       
    37 
    37 
    38 from soc.cache import home
    38 from soc.cache import home
    39 from soc.logic import cleaning
    39 from soc.logic import cleaning
    40 from soc.logic import dicts
    40 from soc.logic import dicts
    41 from soc.logic.models.survey import GRADES
    41 from soc.logic.models.survey import GRADES
    42 from soc.logic.models.survey import logic as survey_logic
    42 from soc.logic.models.survey import logic as survey_logic
       
    43 from soc.logic.models.survey import project_logic
       
    44 from soc.logic.models.survey import grading_logic
    43 from soc.logic.models.survey_record import logic as results_logic
    45 from soc.logic.models.survey_record import logic as results_logic
       
    46 from soc.logic.models.survey_record import updateSurveyRecord
    44 from soc.logic.models.user import logic as user_logic
    47 from soc.logic.models.user import logic as user_logic
    45 from soc.models.survey import Survey
    48 from soc.models.survey import Survey
    46 from soc.models.survey_record import SurveyRecord
    49 from soc.models.survey_record import SurveyRecord
    47 from soc.models.user import User
    50 from soc.models.user import User
    48 from soc.views import out_of_band
    51 from soc.views import out_of_band
   106     rights['list'] = ['checkDocumentList']
   109     rights['list'] = ['checkDocumentList']
   107     rights['pick'] = ['checkDocumentPick']
   110     rights['pick'] = ['checkDocumentPick']
   108     rights['grade'] = ['checkIsSurveyGradable']
   111     rights['grade'] = ['checkIsSurveyGradable']
   109 
   112 
   110     new_params = {}
   113     new_params = {}
       
   114     # TODO(ajaksu) pass logic in a way views can use them
   111     new_params['logic'] = survey_logic
   115     new_params['logic'] = survey_logic
   112     new_params['rights'] = rights
   116     new_params['rights'] = rights
   113 
   117 
   114     new_params['name'] = "Survey"
   118     new_params['name'] = "Survey"
   115     new_params['pickable'] = True
   119     new_params['pickable'] = True
   266          # not submitting completed survey OR we're ignoring late submission
   270          # not submitting completed survey OR we're ignoring late submission
   267         pass
   271         pass
   268       else:
   272       else:
   269         # save/update the submitted survey
   273         # save/update the submitted survey
   270         context['notice'] = "Survey Submission Saved"
   274         context['notice'] = "Survey Submission Saved"
   271         survey_record = survey_logic.updateSurveyRecord(user, survey,
   275         survey_record = updateSurveyRecord(user, survey, survey_record,
   272         survey_record, request.POST)
   276                                            request.POST)
   273     survey_content = survey.survey_content
   277     survey_content = survey.survey_content
   274 
   278 
   275     if not survey_record and read_only:
   279     if not survey_record and read_only:
   276       # no recorded answers, we're either past deadline or want to see answers
   280       # no recorded answers, we're either past deadline or want to see answers
   277       is_same_user = user.key() == user_logic.getForCurrentAccount().key()
   281       is_same_user = user.key() == user_logic.getForCurrentAccount().key()
   389 
   393 
   390     context['survey_records'] = results.render(self._entity, self._params,
   394     context['survey_records'] = results.render(self._entity, self._params,
   391                                                filter={})
   395                                                filter={})
   392 
   396 
   393     super(View, self)._editContext(request, context)
   397     super(View, self)._editContext(request, context)
       
   398 
       
   399   def createPost(self, request, context, params):
       
   400 
       
   401     # TODO(ajaksu) create new View class for other surveys
       
   402     survey_type = request.POST.get('survey_type')
       
   403     if not survey_type:
       
   404       self._logic = params['logic'] = survey_logic
       
   405     elif survey_type == 'project':
       
   406       self._logic =  params['logic'] = project_logic
       
   407     elif survey_type == 'grading':
       
   408       self._logic = params['logic'] = grading_logic
       
   409 
       
   410     return super(View, self).createPost(request, context, params)
   394 
   411 
   395   def _editPost(self, request, entity, fields):
   412   def _editPost(self, request, entity, fields):
   396     """See base.View._editPost().
   413     """See base.View._editPost().
   397 
   414 
   398     Processes POST request items to add new dynamic field names,
   415     Processes POST request items to add new dynamic field names,
   906   header = _get_csv_header(survey)
   923   header = _get_csv_header(survey)
   907   leading = ['user', 'created', 'modified']
   924   leading = ['user', 'created', 'modified']
   908   properties = leading + survey.survey_content.orderedProperties()
   925   properties = leading + survey.survey_content.orderedProperties()
   909 
   926 
   910   try:
   927   try:
   911     first = survey.survey_records.run().next()
   928     first = survey.getRecords().run().next()
   912   except StopIteration:
   929   except StopIteration:
   913     # bail out early if survey_records.run() is empty
   930     # bail out early if survey_records.run() is empty
   914     return header, survey.link_id
   931     return header, survey.link_id
   915 
   932 
   916   # generate results list
   933   # generate results list
   917   recs = survey.survey_records.run()
   934   recs = survey.getRecords().run()
   918   recs = _get_records(recs, properties)
   935   recs = _get_records(recs, properties)
   919 
   936 
   920   # write results to CSV
   937   # write results to CSV
   921   output = StringIO.StringIO()
   938   output = StringIO.StringIO()
   922   writer = csv.writer(output)
   939   writer = csv.writer(output)