app/soc/views/models/survey.py
changeset 2543 4c95d717a976
parent 2542 a9dec4763c6b
child 2546 df7e4291c23c
equal deleted inserted replaced
2542:a9dec4763c6b 2543:4c95d717a976
    33 from django import http
    33 from django import http
    34 from django.utils import simplejson
    34 from django.utils import simplejson
    35 
    35 
    36 from soc.logic import cleaning
    36 from soc.logic import cleaning
    37 from soc.logic import dicts
    37 from soc.logic import dicts
       
    38 from soc.logic.helper import timeline
    38 from soc.logic.models.survey import logic as survey_logic
    39 from soc.logic.models.survey import logic as survey_logic
    39 from soc.logic.models.user import logic as user_logic
    40 from soc.logic.models.user import logic as user_logic
    40 from soc.models.survey import Survey
    41 from soc.models.survey import Survey
    41 from soc.models.survey_record import SurveyRecord
    42 from soc.models.survey_record import SurveyRecord
    42 from soc.models.user import User
    43 from soc.models.user import User
    91 
    92 
    92     Params:
    93     Params:
    93       params: a dict with params for this View
    94       params: a dict with params for this View
    94     """
    95     """
    95 
    96 
    96     # TODO: read/write access needs to match survey
       
    97     # TODO: usage requirements
       
    98 
       
    99     rights = access.Checker(params)
    97     rights = access.Checker(params)
   100     rights['any_access'] = ['allow']
    98     rights['any_access'] = ['allow']
   101     rights['show'] = [('checkIsSurveyWritable', survey_logic)]
    99     rights['show'] = [('checkIsSurveyWritable', survey_logic)]
   102     rights['create'] = ['checkIsUser']
   100     rights['create'] = ['checkIsUser']
   103     rights['edit'] = [('checkIsSurveyWritable', survey_logic)]
   101     rights['edit'] = [('checkIsSurveyWritable', survey_logic)]
   283       db.put(entity)
   281       db.put(entity)
   284     else:
   282     else:
   285       fields['survey_content'] = survey_content
   283       fields['survey_content'] = survey_content
   286 
   284 
   287     fields['modified_by'] = user
   285     fields['modified_by'] = user
       
   286 
   288     super(View, self)._editPost(request, entity, fields)
   287     super(View, self)._editPost(request, entity, fields)
   289 
   288 
   290   def loadSurveyContent(self, schema, survey_fields, entity):
   289   def loadSurveyContent(self, schema, survey_fields, entity):
   291     """Populate the schema dict and get text survey questions.
   290     """Populate the schema dict and get text survey questions.
   292     """
   291     """
   636         entity: a Survey entity
   635         entity: a Survey entity
   637         record: a SurveyRecord entity
   636         record: a SurveyRecord entity
   638         properties: properties to be stored in the SurveyRecord entity
   637         properties: properties to be stored in the SurveyRecord entity
   639     """
   638     """
   640     pass
   639     pass
       
   640 
   641 
   641 
   642   def setHelpAndStatus(self, context, survey, survey_record):
   642   def setHelpAndStatus(self, context, survey, survey_record):
   643     """Get help_text and status for template use.
   643     """Get help_text and status for template use.
   644 
   644 
   645     Args:
   645     Args:
   805           error, request, template=params['error_public'], context=context)
   805           error, request, template=params['error_public'], context=context)
   806       return error, None
   806       return error, None
   807 
   807 
   808     return entity, context
   808     return entity, context
   809 
   809 
       
   810   def getMenusForScope(self, entity, params, id, user):
       
   811     """List featured surveys if after the survey_start date 
       
   812     and before survey_end an iff the current user has the right taking access.
       
   813 
       
   814     Args:
       
   815       entity: entity which is the scope for a Survey
       
   816       params: params from the requesting View
       
   817       id: GAE user instance for the current user
       
   818       user: User entity from the current user
       
   819     """
       
   820 
       
   821     # only list surveys for registered users
       
   822     if not user:
       
   823       return []
       
   824 
       
   825     survey_params = self.getParams().copy()
       
   826     survey_logic = survey_params['logic']
       
   827     record_logic = survey_logic.getRecordLogic()
       
   828 
       
   829     # filter all featured surveys for the given entity
       
   830     filter = {
       
   831         'prefix' : params['url_name'],
       
   832         'scope_path': entity.key().id_or_name(),
       
   833         'is_featured': True,
       
   834         }
       
   835 
       
   836     survey_entities = survey_logic.getForFields(filter)
       
   837     submenus = []
       
   838 
       
   839     # get the rights checker
       
   840     rights = self._params['rights']
       
   841     rights.setCurrentUser(id, user)
       
   842 
       
   843     # cache ACL
       
   844     survey_rights = {}
       
   845 
       
   846     # add a link to all featured active surveys the user can take
       
   847     for survey_entity in survey_entities:
       
   848 
       
   849       if survey_entity.taking_access not in survey_rights:
       
   850         # we have not determined if this user has the given type of access
       
   851 
       
   852         # check if the current user is allowed to visit the take Survey page
       
   853         allowed_to_take = False
       
   854 
       
   855         rights.checkIsSurveyTakeable(
       
   856             {'key_name': survey_entity.key().name(),
       
   857              'prefix': survey_entity.prefix,
       
   858              'scope_path': survey_entity.scope_path,
       
   859              'link_id': survey_entity.link_id,
       
   860              'user': user},
       
   861             survey_logic,
       
   862             check_time=False)
       
   863         allowed_to_take = True
       
   864         # cache ACL for a given entity.taking_access
       
   865         survey_rights[survey_entity.taking_access] = allowed_to_take
       
   866 
       
   867         if not allowed_to_take:
       
   868           # not allowed to take this survey
       
   869           continue
       
   870 
       
   871       elif not survey_rights[survey_entity.taking_access]:
       
   872         # we already determined that the user doens't have access to this type
       
   873         continue
       
   874 
       
   875       if not timeline.isActivePeriod(survey_entity, 'survey'):
       
   876         # this survey is not active right now
       
   877         continue
       
   878 
       
   879       # check if any SurveyRecord is available for this survey
       
   880       filter = {'survey': survey_entity,
       
   881                 'user': user}
       
   882 
       
   883       survey_record = record_logic.getForFields(filter, unique=True)
       
   884 
       
   885       if survey_record:
       
   886         taken_status = ""
       
   887       else:
       
   888         # no SurveyRecord available so we mark the survey as new
       
   889         taken_status = "(new)"
       
   890 
       
   891       submenu = (redirects.getTakeSurveyRedirect(survey_entity, survey_params),
       
   892                  'Survey ' + taken_status + ': ' + survey_entity.short_name,
       
   893                  'show')
       
   894 
       
   895       submenus.append(submenu)
       
   896     return submenus
       
   897 
   810 
   898 
   811 view = View()
   899 view = View()
   812 
   900 
   813 admin = decorators.view(view.admin)
   901 admin = decorators.view(view.admin)
   814 create = decorators.view(view.create)
   902 create = decorators.view(view.create)