def updateSurveyRecord(self, user, survey, survey_record, fields): """ Create a new survey record, or get an existing one. params: user = user taking survey survey = survey entity survey_record = existing record, if one exists fields = submitted responses to survey fields """ if survey_record: create = False for prop in survey_record.dynamic_properties(): delattr(survey_record, prop) else: create = True survey_record = SurveyRecord(user=user, survey=survey) schema = eval(survey.survey_content.schema) for name, value in fields.items(): if name == 'project': project = student_project.StudentProject.get(value) survey_record.project = project elif name == 'grade': survey_record.grade = GRADES[value] else: pick_multi = name in schema and schema[name]['type'] == 'pick_multi' if pick_multi and hasattr(fields, 'getlist'): # it's a multidict setattr(survey_record, name, ','.join(fields.getlist(name))) else: setattr(survey_record, name, value) # if creating evaluation record, set SurveyRecordGroup db.put(survey_record) if 'evaluation' in survey.taking_access and create: if not project: return False role = self.getUserRole(user, survey, project) survey_record_group = self.setSurveyRecordGroup(survey, survey_record, project) if survey_record_group: db.put(survey_record_group) return survey_record
def clear(*args, **kwargs): """Removes all entities from the datastore. """ # there no explicit ranker model anywhere, so make one for # our own convenience to delete all rankers class ranker(db.Model): """ranker model used with ranklist module. """ pass # TODO(dbentley): If there are more than 1000 instances of any model, # this method will not clear all instances. Instead, it should continually # call .all(), delete all those, and loop until .all() is empty. entities = itertools.chain(*[ Notification.all(), GSoCMentor.all(), GHOPMentor.all(), GSoCStudent.all(), GHOPStudent.all(), Survey.all(), SurveyContent.all(), SurveyRecord.all(), GSoCOrgAdmin.all(), GHOPOrgAdmin.all(), ranker.all(), RankerRoot.all(), StudentProposal.all(), GSoCOrganization.all(), GHOPOrganization.all(), OrgApplication.all(), GSoCTimeline.all(), GHOPTimeline.all(), GSoCProgram.all(), GHOPProgram.all(), Host.all(), Sponsor.all(), User.all(), Site.all(), Document.all(), ]) try: for entity in entities: entity.delete() except db.Timeout: return http.HttpResponseRedirect('#') # pylint: disable-msg=E1101 memcache.flush_all() return http.HttpResponse('Done')
def clear(*args, **kwargs): """Removes all entities from the datastore. """ # there no explicit ranker model anywhere, so make one for # our own convenience to delete all rankers class ranker(db.Model): """ranker model used with ranklist module. """ pass # TODO(dbentley): If there are more than 1000 instances of any model, # this method will not clear all instances. Instead, it should continually # call .all(), delete all those, and loop until .all() is empty. entities = itertools.chain(*[ Notification.all(), GSoCMentor.all(), GCIMentor.all(), GSoCStudent.all(), GCIStudent.all(), Survey.all(), SurveyContent.all(), SurveyRecord.all(), GSoCOrgAdmin.all(), GCIOrgAdmin.all(), ranker.all(), RankerRoot.all(), StudentProposal.all(), GSoCOrganization.all(), GCIOrganization.all(), GSoCTimeline.all(), GCITimeline.all(), GSoCProgram.all(), GCIProgram.all(), Host.all(), Sponsor.all(), User.all(), Site.all(), Document.all(), ]) try: for entity in entities: entity.delete() except db.Timeout: return http.HttpResponseRedirect('#') # pylint: disable=E1101 memcache.flush_all() return http.HttpResponse('Done')
def clear(*args, **kwargs): """Removes all entities from the datastore. """ # TODO(dbentley): If there are more than 1000 instances of any model, # this method will not clear all instances. Instead, it should continually # call .all(), delete all those, and loop until .all() is empty. entities = itertools.chain(*[ Survey.all(), SurveyRecord.all(), GCIOrganization.all(), GSoCTimeline.all(), GCITimeline.all(), GSoCProgram.all(), GSoCProject.all(), GSoCProposal.all(), GCIProgram.all(), GCIScore.all(), GSoCStudentInfo.all(), GCIStudentInfo.all(), GCITask.all(), Sponsor.all(), Site.all(), Document.all(), # The below models are all subclasses of ndb.Model and therefore must # use .query() to return all instances instead of .all(). soc_org_model.SOCOrganization.query(), profile_model.Profile.query(), soc_profile.SOCStudentData.query(), user.User.query(), address.Address.query(), contact.Contact.query() ]) try: for entity in entities: if isinstance(entity, ndb.Model): entity.key.delete() else: entity.delete() except db.Timeout: return http.HttpResponseRedirect('#') memcache.flush_all() return http.HttpResponse('Done')
def clear(*args, **kwargs): """Removes all entities from the datastore. """ # TODO(dbentley): If there are more than 1000 instances of any model, # this method will not clear all instances. Instead, it should continually # call .all(), delete all those, and loop until .all() is empty. entities = itertools.chain(*[ Notification.all(), GCIStudent.all(), Survey.all(), SurveyRecord.all(), StudentProposal.all(), GSoCOrganization.all(), GCIOrganization.all(), GSoCTimeline.all(), GCITimeline.all(), GSoCProgram.all(), GSoCProfile.all(), GCIProfile.all(), GSoCProposal.all(), GCIProgram.all(), GCIScore.all(), GSoCStudentInfo.all(), GCIStudentInfo.all(), GCITask.all(), Host.all(), Sponsor.all(), User.all(), Site.all(), Document.all(), ]) try: for entity in entities: entity.delete() except db.Timeout: return http.HttpResponseRedirect('#') # pylint: disable=E1101 memcache.flush_all() return http.HttpResponse('Done')
def _public(self, request, entity, context): """Survey taking and result display handler. Args: request: the django request object entity: the entity to make public context: the context object -- Taking Survey Pages Are Not 'Public' -- For surveys, the "public" page is actually the access-protected survey-taking page. --- Deadlines --- A survey_end can also be used as a conditional for updating values, we have a special read_only UI and a check on the POST handler for this. Passing read_only=True here allows one to fetch the read_only view. """ # check ACL rights = self._params['rights'] rights.checkIsSurveyReadable({'key_name': entity.key().name(), 'prefix': entity.prefix, 'scope_path': entity.scope_path, 'link_id': entity.link_id,}, 'key_name') survey = entity user = user_logic.getForCurrentAccount() status = self.getStatus(request, context, user, survey) read_only, can_write, not_ready = status # If user can edit this survey and is requesting someone else's results, # in a read-only request, we fetch them. if can_write and read_only and 'user_results' in request.GET: user = user_logic.getFromKeyNameOr404(request.GET['user_results']) if not_ready and not can_write: context['notice'] = "No survey available." return False elif not_ready: return False else: # check for existing survey_record record_query = SurveyRecord.all( ).filter("user ="******"survey =", survey) survey_record = record_query.get() if len(request.POST) < 1 or read_only or not_ready: # not submitting completed survey OR we're ignoring late submission pass else: # save/update the submitted survey context['notice'] = "Survey Submission Saved" record_logic = self._logic.getRecordLogic() survey_record = record_logic.updateSurveyRecord(user, survey, survey_record, request.POST) survey_content = survey.survey_content if not survey_record and read_only: # no recorded answers, we're either past survey_end or want to see answers is_same_user = user.key() == user_logic.getForCurrentAccount().key() if not can_write or not is_same_user: # If user who can edit looks at her own taking page, show the default # form as readonly. Otherwise, below, show nothing. context["notice"] = "There are no records for this survey and user." return False survey_form = surveys.SurveyForm(survey_content=survey_content, this_user=user, project=None, survey_logic=self._params['logic'], survey_record=survey_record, read_only=read_only, editing=False) survey_form.getFields() # set help and status text self.setHelpStatus(context, read_only, survey_record, survey_form, survey) if not context['survey_form']: access_tpl = "Access Error: This Survey Is Limited To %s" context["notice"] = access_tpl % string.capwords(survey.taking_access) context['read_only'] = read_only context['project'] = None return True
def seed_many(request, *args, **kwargs): """Seeds many instances of the specified type. Understands the following GET args: start: where to start adding new users at end: where to stop adding new users at goal: how many users to add in total, implies user_only step: how many users to add per request, defaults to 15 seed_type: the type of entity to seed, should be one of: user, org, org_app, mentor, student_proposal Redirects if end < goal, incrementing both start and end with step. """ get_args = request.GET if not dicts.containsAll(get_args, ['goal', 'start', 'end', 'seed_type']): return http.HttpResponse('Missing get args.') seed_types = { 'user': (seed_user, User), 'org': (seed_org, GSoCOrganization), 'org_app': (seed_org_app, OrgApplication), 'mentor': (seed_mentor, GSoCMentor), 'student': (seed_student, GSoCStudent), 'student_proposal': (seed_student_proposal, StudentProposal), 'survey': (seed_survey, Survey), 'survey_answer': (seed_survey_answer, SurveyRecord), } goal = int(get_args['goal']) start = int(get_args['start']) end = int(get_args['end']) step = int(get_args.get('step', '15')) seed_type = get_args['seed_type'] if not seed_type in seed_types: return http.HttpResponse('Unknown seed_type: "%s".' % seed_type) action, model = seed_types[seed_type] for i in range(start, end): try: props = action(request, i) except Error, error: return http.HttpResponse(error.message) for properties in props if isinstance(props, list) else [props]: entity = model(**properties) if seed_type == 'survey': survey_content = survey_logic.createSurvey( properties['fields'], properties['schema'], this_survey=None) entity.this_survey = survey_content elif seed_type == 'survey_answer': record = SurveyRecord.gql( "WHERE user = :1 AND this_survey = :2", properties['user'], properties['_survey']).get() entity = survey_logic.updateSurveyRecord( properties['user'], properties['_survey'], record, properties['_fields']) entity.put() if seed_type == 'survey': survey_logic._onCreate(entity)