def testCreateUser(self): self.timeline.studentSignup() self.default_props.update({ 'link_id': 'test', }) response = self.post(self.student_url, self.default_props) self.assertResponseRedirect(response, self.validated_url) self.assertEqual(1, GCIProfile.all().count()) student = GCIProfile.all().get() self.assertEqual(self.birth_date, str(student.birth_date))
def clean_email(self): email_cleaner = cleaning.clean_email('email') try: email_address = email_cleaner(self) except djangoforms.ValidationError as e: if e.code != 'invalid': raise msg = ugettext(u'Enter a valid email address.') raise djangoforms.ValidationError(msg, code='invalid') account = users.User(email_address) user_account = accounts.normalizeAccount(account) user = User.all().filter('account', user_account).get() if not user: raise djangoforms.ValidationError( "There is no user with that email address") self.cleaned_data['user'] = user q = GCIProfile.all() q.filter('program', self.request_data.program) q.ancestor(user) self.cleaned_data['profile'] = q.get()
def testCreateProfile(self): from soc.modules.gci.models.profile import GCIStudentInfo self.timeline.studentSignup() self.data.createUser() self._updateDefaultProps(self.data) postdata = self.default_props response = self.post(self.student_url, postdata) self.assertResponseRedirect(response, self.validated_url) # hacky profile = GCIProfile.all().get() profile.delete() postdata.update({ 'email': 'somerandominvalid@emailid', }) response = self.post(self.student_url, postdata) # yes! this is the protocol for form posts. We get an OK response # with the response containing the form's GET request page whenever # the form has an error and could not be posted. This is the architecture # chosen in order to save the form error state's while rendering the # error fields. self.assertResponseOK(response) error_dict = response.context['error'] self.assertTrue('email' in error_dict)
def recalculateForStudent(self, request, *args, **kwargs): """Recalculates GCI Student Ranking for the specified student. Args in POST: key: The string version of the key for the GCIProfile entity representing the student. """ post_dict = request.POST key = db.Key(post_dict['key']) student = GCIProfile.get(key) if not student: logging.warning('Enqueued task to recalculate ranking for ' 'non-existent student %s' %(key)) return responses.terminateTask() # get all the tasks that the student has completed q = GCITask.all() q.filter('student', student) q.filter('status', 'Closed') tasks = q.fetch(1000) ranking_logic.calculateRankingForStudent(student, tasks) return responses.terminateTask()
def recalculateForStudent(self, request, *args, **kwargs): """Recalculates GCI Student Ranking for the specified student. Args in POST: key: The string version of the key for the GCIProfile entity representing the student. """ post_dict = request.POST key = db.Key(post_dict['key']) student = GCIProfile.get(key) if not student: logging.warning( 'Enqueued task to recalculate ranking for ' 'non-existent student %s', key) return responses.terminateTask() # get all the tasks that the student has completed q = GCITask.all() q.filter('student', student) q.filter('status', 'Closed') tasks = q.fetch(1000) score_logic.calculateRankingForStudent(student, tasks) return responses.terminateTask()
def validate(self): """Validates the form data. Returns a newly created request entity or None if an error occurs. """ assert isSet(self.data.organization) request_form = RequestForm(self.data.POST) if not request_form.is_valid(): return None request_form.cleaned_data['org'] = self.data.organization request_form.cleaned_data['role'] = self.data.kwargs['role'] request_form.cleaned_data['type'] = 'Request' request_form.cleaned_data['user'] = self.data.user q = GCIProfile.all().filter('org_admin_for', self.data.organization) q = q.filter('status', 'active').filter('notify_new_requests', True) admins = q.fetch(1000) admin_emails = [i.email for i in admins] def create_request_txn(): request = request_form.create(commit=True) context = notifications.requestContext(self.data, request, admin_emails) sub_txn = mailer.getSpawnMailTaskTxn(context, parent=request) sub_txn() return request return db.run_in_transaction(create_request_txn)
def queryProfileForUserAndProgram(user, program): """Returns the query to fetch GCIProfile entity for the specified user and program. Args: user: User entity for which the profile should be found program: GCIProgram entity for which the profile should be found """ return GCIProfile.all().ancestor(user).filter('scope = ', program)
def orgAdminsForOrg(org, limit=1000): """Returns the organization administrators for the given GCI Organization. Args: org: The GCIOrganization entity for which the admins should be found. """ query = GCIProfile.all() query.filter('org_admin_for', org) return query.fetch(limit)
def assertMailSentToSubscribers(self, comment): """Check if a notification email sent to the subscribers of the task. """ subscribers = GCIProfile.get(self.task.subscribers) subject = DEF_NEW_TASK_COMMENT_SUBJECT % { 'commented_by': comment.created_by.name, 'program_name': self.task.program.name, 'task_title': self.task.title } for subscriber in subscribers: self.assertEmailSent(bcc=subscriber.email, subject=subject)
def assertMailSentToSubscribers(self, comment): """Check if a notification email sent to the subscribers of the task. """ subscribers = GCIProfile.get(self.task.subscribers) subject = DEF_NEW_TASK_COMMENT_SUBJECT % { "commented_by": comment.created_by.name, "program_name": self.task.program.name, "task_title": self.task.title, } for subscriber in subscribers: self.assertEmailSent(bcc=subscriber.email, subject=subject)
def recalculateGCIRanking(self, request, *args, **kwargs): """Recalculates student ranking for the entire program. Args in POST dict: cursor: Query cursor to figure out where we need to start processing """ key_name = '%s/%s' % (kwargs['sponsor'], kwargs['program']) cursor = request.POST.get('cursor') program = GCIProgram.get_by_key_name(key_name) if not program: logging.warning( 'Enqueued recalculate ranking task for non-existing program: %s', key_name) return responses.terminateTask() # Retrieve the students for the program q = GCIProfile.all() q.filter('program', program) q.filter('is_student', True) if cursor: q.with_cursor(cursor) students = q.fetch(25) for student in students: # get all the tasks that the student has completed task_q = GCITask.all() task_q.filter('student', student) task_q.filter('status', 'Closed') tasks = task_q.fetch(1000) # calculate score with all the tasks score_logic.calculateScore(student, tasks, program) # calculate org score with all the tasks db.run_in_transaction(org_score_logic.updateOrgScoresTxn(tasks)) if students: # schedule task to do the rest of the students params = { 'cursor': q.cursor(), } taskqueue.add(queue_name='gci-update', url=request.path, params=params) return responses.terminateTask()
def recalculateGCIRanking(self, request, *args, **kwargs): """Recalculates student ranking for the entire program. Args in POST dict: cursor: Query cursor to figure out where we need to start processing """ key_name = '%s/%s' % (kwargs['sponsor'], kwargs['program']) cursor = request.POST.get('cursor') program = GCIProgram.get_by_key_name(key_name) if not program: logging.warning( 'Enqueued recalculate ranking task for non-existing ' 'program: %s' %key_name) return responses.terminateTask() # Retrieve the students for the program q = GCIProfile.all() q.filter('scope', program) q.filter('is_student', True) if cursor: q.with_cursor(cursor) students = q.fetch(25) for student in students: # get all the tasks that the student has completed task_q = GCITask.all() task_q.filter('student', student) task_q.filter('status', 'Closed') tasks = task_q.fetch(1000) # calculate ranking with all the tasks # ranking_logic.calculateRankingForStudent(student, tasks) ranking_logic.calculateScore(student, tasks, program) if students: # schedule task to do the rest of the students params = { 'cursor': q.cursor(), } taskqueue.add(queue_name='gci-update', url=request.path, params=params) return responses.terminateTask()
def getListData(self): """Returns the list data as requested by the current request. If the lists as requested is not supported by this component None is returned. """ if lists.getListIndex(self.request) != 6: return None q = GCIProfile.all() q.filter("mentor_for IN", self.data.org_admin_for) starter = lists.keyStarter response_builder = lists.RawQueryContentResponseBuilder(self.request, self._list_config, q, starter) return response_builder.build()
def process(task): org = task.org q = GCIProfile.all() q.filter('org_admin_for', org) # It is sufficient to fetch one org admin (and any one is fine). org_admin = q.get() if not task.created_by: task.created_by = org_admin if not task.modified_by: task.modified_by = org_admin yield operation.db.Put(task) yield operation.counters.Increment("task_authorship_updated")
def getListData(self): if lists.getListIndex(self.request) != 0: return None q = GCIProfile.all() q.filter('scope', self.data.program) q.filter('is_mentor', True) starter = lists.keyStarter prefetcher = lists.listPrefetcher( GCIProfile, ['org_admin_for', 'mentor_for']) response_builder = lists.RawQueryContentResponseBuilder( self.request, self._list_config, q, starter, prefetcher=prefetcher) return response_builder.build()
def recalculateGCIRanking(self, request, *args, **kwargs): """Recalculates student ranking for the entire program. Args in POST dict: cursor: Query cursor to figure out where we need to start processing """ key_name = "%s/%s" % (kwargs["sponsor"], kwargs["program"]) cursor = request.POST.get("cursor") program = GCIProgram.get_by_key_name(key_name) if not program: logging.warning("Enqueued recalculate ranking task for non-existing program: %s", key_name) return responses.terminateTask() # Retrieve the students for the program q = GCIProfile.all() q.filter("program", program) q.filter("is_student", True) if cursor: q.with_cursor(cursor) students = q.fetch(25) for student in students: # get all the tasks that the student has completed task_q = GCITask.all() task_q.filter("student", student) task_q.filter("status", "Closed") tasks = task_q.fetch(1000) # calculate score with all the tasks score_logic.calculateScore(student, tasks, program) # calculate org score with all the tasks db.run_in_transaction(org_score_logic.updateOrgScoresTxn(tasks)) if students: # schedule task to do the rest of the students params = {"cursor": q.cursor()} taskqueue.add(queue_name="gci-update", url=request.path, params=params) return responses.terminateTask()
def getListData(self): """Returns the list data as requested by the current request. If the lists as requested is not supported by this component None is returned. """ if lists.getListIndex(self.data.request) != 6: return None q = GCIProfile.all() q.filter('mentor_for IN', self.data.org_admin_for) starter = lists.keyStarter response_builder = lists.RawQueryContentResponseBuilder( self.data.request, self._list_config, q, starter) return response_builder.build()
def queryAllMentorsForOrg(org, keys_only=False, limit=1000): """Returns a list of keys of all the mentors for the organization Args: org: the organization entity for which we need to get all the mentors keys_only: True if only the entity keys must be fetched instead of the entities themselves. limit: the maximum number of entities that must be fetched returns: List of all the mentors for the organization """ # get all mentors keys first query = GCIProfile.all(keys_only=keys_only) query.filter('mentor_for', org) mentors = query.fetch(limit=limit) return mentors
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 _convertReferenceProperty(model_property, entity): """Converts the specified ReferenceProperty whose value is either a key of GSoCProfile or GCIProfile type. Args: model_property: Property instance. entity: Entity. Returns: The new value for the specified property which Profile key. """ reference_key = model_property.get_value_for_datastore(entity) if not reference_key: return None elif reference_key.kind() not in [GSoCProfile.kind(), GCIProfile.kind()]: raise ValueError('Invalid kind %s for property %s', (reference_key.kind(), model_property.name)) else: return _newKey(reference_key)
def _convertReferenceProperty(model_property, entity): """Converts the specified ReferenceProperty whose value is either a key of GSoCProfile or GCIProfile type. Args: model_property: Property instance. entity: Entity. Returns: The new value for the specified property which Profile key. """ reference_key = model_property.get_value_for_datastore(entity) if not reference_key: return None elif reference_key.kind() not in [GSoCProfile.kind(), GCIProfile.kind()]: raise ValueError( 'Invalid kind %s for property %s', (reference_key.kind(), model_property.name)) else: return _newKey(reference_key)
def canTakeOrgApp(self): """A user can take the GCI org app if he/she participated in GSoC or GCI as a non-student. """ from soc.modules.gsoc.models.profile import GSoCProfile self.isUser() q = GSoCProfile.all() q.filter('is_student', False) q.filter('status', 'active') q.filter('user', self.data.user) gsoc_profile = q.get() q = GCIProfile.all() q.filter('is_student', False) q.filter('status', 'active') q.filter('user', self.data.user) gci_profile = q.get() if not (gsoc_profile or gci_profile): raise AccessViolation(DEF_NO_PREV_ORG_MEMBER)
def clean_email(self): email_cleaner = cleaning.clean_email("email") try: email_address = email_cleaner(self) except djangoforms.ValidationError as e: if e.code != "invalid": raise msg = ugettext(u"Enter a valid email address.") raise djangoforms.ValidationError(msg, code="invalid") account = users.User(email_address) user_account = accounts.normalizeAccount(account) user = User.all().filter("account", user_account).get() if not user: raise djangoforms.ValidationError("There is no user with that email address") self.cleaned_data["user"] = user q = GCIProfile.all() q.filter("program", self.request_data.program) q.ancestor(user) self.cleaned_data["profile"] = q.get()
def _cleanTask(self, task, org): """Cleans the data given so that it can be safely stored as a task. Args: task: Dictionary as constructed by the csv.DictReader(). org: the GCIOrganization for which the task is created. Returns: A list of error messages if any have occurred. """ errors = [] # check title if not task['title']: errors.append('No valid title present.') # clean description try: parser = HTMLParser(tokenizer=sanitizer.HTMLSanitizer) parsed = parser.parseFragment(task['description'], encoding='utf-8') cleaned_string = ''.join( [tag.toxml() for tag in parsed.childNodes]) task['description'] = cleaned_string.strip().replace('\r\n', '\n') except (HTMLParseError, ParseError, TypeError) as e: logging.warning('Cleaning of description failed with: %s', e) errors.append( 'Failed to clean the description, do not use naughty HTML such as ' '<script>.') # clean time to complete try: hours_to_complete = int(task['time_to_complete']) # Must be at least 2 days (48hrs) if hours_to_complete < 2 * 24: errors.append( 'Time to complete must be at least 48 hrs, given was: %s' % hours_to_complete) else: task['time_to_complete'] = hours_to_complete except (ValueError, TypeError) as e: errors.append('No valid time to completion found, given was: %s.' % task['time_to_complete']) # clean mentors mentor_ids = set(task['mentors'].split(',')) mentors = [] mentor_entities = [] for mentor_id in mentor_ids: q = GCIProfile.all() q.filter('link_id', mentor_id.strip()) q.filter('mentor_for', org) q.filter('status', 'active') mentor = q.get() if mentor: mentors.append(mentor.key()) mentor_entities.append(mentor) else: errors.append('%s is not a mentor.' % mentor_id) task['mentors'] = mentors task['mentor_entities'] = mentor_entities program_entity = org.program # clean task types types = [] for task_type in set(task['task_type'].split(',')): task_type = task_type.strip() if task_type in program_entity.task_types: types.append(task_type) else: errors.append('%s is not a valid task type.' % task_type) task['types'] = types # clean task tags tags = [] for tag in set(task['arbit_tag'].split(',')): tags.append(tag.strip()) task['tags'] = tags return errors
def seed(request, *args, **kwargs): """Seeds the datastore with some default values. """ site_properties = { 'key_name': 'site', 'link_id': 'site', } site = Site(**site_properties) site.put() account = accounts.getCurrentAccount() if not account: account = users.User(email='*****@*****.**') user_properties = { 'key_name': 'test', 'link_id': 'test', 'account': account, 'name': 'Test', } current_user = User(**user_properties) current_user.put() group_properties = { 'key_name': 'google', 'link_id': 'google', 'name': 'Google Inc.', 'short_name': 'Google', 'founder': current_user, 'home_page': 'http://www.google.com', 'email': '*****@*****.**', 'description': 'This is the profile for Google.', 'contact_street': 'Some Street', 'contact_city': 'Some City', 'contact_country': 'United States', 'contact_postalcode': '12345', 'phone': '1-555-BANANA', 'status': 'active', } google = Sponsor(**group_properties) google.put() role_properties = { 'key_name': 'google/test', 'link_id': 'test', 'public_name': 'test', 'scope': google, 'scope_path': 'google', 'user': current_user, 'given_name': 'Test', 'surname': 'Example', 'name_on_documents': 'Test Example', 'email': '*****@*****.**', 'res_street': 'Some Street', 'res_city': 'Some City', 'res_state': 'Some State', 'res_country': 'United States', 'res_postalcode': '12345', 'phone': '1-555-BANANA', 'birth_date': db.DateProperty.now(), 'agreed_to_tos': True, 'is_org_admin': True, 'is_mentor': True, } current_user.host_for = [google.key()] current_user.put() google_host = Host(**role_properties) google_host.put() from datetime import datetime from datetime import timedelta now = datetime.now() before = now - timedelta(365) after = now + timedelta(365) timeline_properties = { 'key_name': 'google/gsoc2009', 'link_id': 'gsoc2009', 'scope_path': 'google', 'scope': google, 'program_start': before, 'program_end': after, 'accepted_organization_announced_deadline': before, 'student_signup_start': before, 'student_signup_end': after, } gsoc2009_timeline = GSoCTimeline(**timeline_properties) gsoc2009_timeline.put() program_properties = { 'key_name': 'google/gsoc2009', 'link_id': 'gsoc2009', 'scope_path': 'google', 'scope': google, 'name': 'Google Summer of Code 2009', 'short_name': 'GSoC 2009', 'group_label': 'GSOC', 'description': 'This is the program for GSoC 2009.', 'apps_tasks_limit': 42, 'slots': 42, 'timeline': gsoc2009_timeline, 'status': 'visible', } gsoc2009 = GSoCProgram(**program_properties) gsoc2009.put() timeline_properties.update({ 'key_name': 'google/gsoc2010', 'link_id': 'gsoc2010', }) gsoc2010_timeline = GSoCTimeline(**timeline_properties) gsoc2010_timeline.put() program_properties.update({ 'key_name': 'google/gsoc2010', 'link_id': 'gsoc2010', 'name': 'Google Summer of Code 2010', 'description': 'This is the program for GSoC 2010.', 'short_name': 'GSoC 2010', 'timeline': gsoc2010_timeline, }) gsoc2010 = GSoCProgram(**program_properties) gsoc2010.put() timeline_properties = { 'key_name': 'google/gci2009', 'link_id': 'gci2009', 'scope_path': 'google', 'scope': google, 'program_start': before, 'program_end': after, 'accepted_organization_announced_deadline': before, 'student_signup_start': before, 'student_signup_end': after, 'tasks_publicly_visible': before, 'task_claim_deadline': after, 'stop_all_work_deadline': after, } gci2009_timeline = GCITimeline(**timeline_properties) gci2009_timeline.put() program_properties.update({ 'key_name': 'google/gci2009', 'link_id': 'gci2009', 'name': 'Google Code In Contest 2009', 'short_name': 'GCI 2009', 'group_label': 'GCI', 'description': 'This is the program for GCI 2009.', 'timeline': gci2009_timeline, }) gci2009 = GCIProgram(**program_properties) gci2009.put() site.active_program = gci2009 site.put() group_properties.update({ 'key_name': 'google/gci2009/melange', 'link_id': 'melange', 'name': 'Melange Development Team', 'short_name': 'Melange', 'scope_path': 'google/gci2009', 'scope': gci2009, 'home_page': 'http://code.google.com/p/soc', 'description': 'Melange, share the love!', 'license_name': 'Apache License', 'ideas': 'http://code.google.com/p/soc/issues', }) melange = GCIOrganization(**group_properties) melange.put() group_properties.update({ 'scope_path': 'google/gsoc2009', 'scope': gsoc2009, }) role_properties.update({ 'key_name': 'google/gsoc2009/test', 'link_id': 'test', 'scope_path': 'google/gsoc2009', 'scope': gsoc2009, 'program': gsoc2009, 'parent': current_user, }) profile = GSoCProfile(**role_properties) role_properties.pop('parent') orgs = [] for i in range(15): group_properties.update({ 'key_name': 'google/gsoc2009/org_%d' % i, 'link_id': 'org_%d' % i, 'name': 'Organization %d' % i, 'short_name': 'Org %d' % i, 'description': 'Organization %d!' % i, }) entity = GSoCOrganization(**group_properties) orgs.append(entity) entity.put() # Admin (and thus mentor) for the first org if i == 0: profile.org_admin_for.append(entity.key()) profile.mentor_for.append(entity.key()) profile.is_mentor = True profile.is_org_admin = True profile.put() # Mentor for the second org if i == 1: profile.mentor_for.append(entity.key()) profile.is_mentor = True profile.put() role_properties.update({ 'key_name': 'google/gci2009/test', 'link_id': 'test', 'scope_path': 'google/gci2009', 'scope': gci2009, 'program': gci2009, 'org_admin_for': [melange.key()], 'mentor_for': [melange.key()], 'parent': current_user, }) melange_admin = GCIProfile(**role_properties) # TODO: add GCI orgs melange_admin.put() task_properties = { 'status': 'Open', 'modified_by': melange_admin.key(), 'subscribers': [melange_admin.key()], 'title': 'Awesomeness', 'created_by': melange_admin.key(), 'created_on': now, 'program': gci2009, 'time_to_complete': 1337, 'modified_on': now, 'org': melange.key(), 'description': '<p>AWESOME</p>', 'difficulty_level': DifficultyLevel.MEDIUM, 'types': ['Code'] } gci_task = GCITask(**task_properties) gci_task.put() user_properties = { 'key_name': 'student', 'link_id': 'student', 'account': users.User(email='*****@*****.**'), 'name': 'Student', } student_user = User(**user_properties) student_user.put() student_id = 'student' student_properties = { 'key_name': gsoc2009.key().name() + "/" + student_id, 'link_id': student_id, 'scope_path': gsoc2009.key().name(), 'parent': student_user, 'scope': gsoc2009, 'program': gsoc2009, 'user': student_user, 'is_student': True, 'public_name': 'Student', 'given_name': 'Student', 'surname': 'Student', 'birth_date': db.DateProperty.now(), 'email': '*****@*****.**', 'im_handle': 'student_im_handle', 'major': 'test major', 'name_on_documents': 'Student', 'res_country': 'United States', 'res_city': 'city', 'res_street': 'test street', 'res_postalcode': '12345', 'publish_location': True, 'blog': 'http://www.blog.com/', 'home_page': 'http://www.homepage.com/', 'photo_url': 'http://www.photosite.com/thumbnail.png', 'ship_state': None, 'tshirt_size': 'XS', 'tshirt_style': 'male', 'degree': 'Undergraduate', 'phone': '1650253000', 'can_we_contact_you': True, 'program_knowledge': 'I heard about this program through a friend.' } melange_student = GSoCProfile(**student_properties) student_info_properties = { 'key_name': melange_student.key().name(), 'parent': melange_student, 'expected_graduation': 2009, 'program': gsoc2009, 'school_country': 'United States', 'school_name': 'Test School', 'school_home_page': 'http://www.example.com', 'program': gsoc2009, } student_info = GSoCStudentInfo(**student_info_properties) student_info.put() melange_student.student_info = student_info melange_student.put() user_properties = { 'key_name': 'student2', 'link_id': 'student2', 'account': users.User(email='*****@*****.**'), 'name': 'Student 2', } student_user2 = User(**user_properties) student_user2.put() student_id = 'student2' student_properties.update({ 'key_name': gsoc2009.key().name() + "/" + student_id, 'link_id': student_id, 'user': student_user2, 'parent': student_user2, }) melange_student2 = GSoCProfile(**student_properties) melange_student2.put() project_id = 'test_project' project_properties = { 'key_name': gsoc2009.key().name() + "/org_1/" + project_id, 'link_id': project_id, 'scope_path': gsoc2009.key().name() + "/org_1", 'scope': orgs[1].key(), 'title': 'test project', 'abstract': 'test abstract', 'status': 'accepted', 'student': melange_student, 'mentor': profile, 'program': gsoc2009 } melange_project = StudentProject(**project_properties) melange_project.put() student_info_properties.update({'number_of_projects': 1, 'project_for_orgs': [orgs[1].key()]}) student_info = GSoCStudentInfo(**student_info_properties) student_info.put() melange_student.student_info = student_info melange_student.put() project_id = 'test_project2' project_properties.update({ 'key_name': gsoc2009.key().name() + "/org_1/" + project_id, 'link_id': project_id, 'student': melange_student2, 'title': 'test project2' }) student_info_properties.update({ 'key_name': gsoc2009.key().name() + "/" + student_id, 'link_id': student_id, 'parent': melange_student2, }) student_info2 = GSoCStudentInfo(**student_info_properties) student_info2.put() melange_student2.student_info = student_info2 melange_student2.put() melange_project2 = StudentProject(**project_properties) melange_project2.put() student_id = 'student' student_properties.update({ 'key_name': gci2009.key().name() + '/' + student_id, 'parent': student_user, 'scope': gci2009, 'scope_path': gci2009.key().name(), }) gci_student = GCIProfile(**student_properties) gci_student.put() student_info_properties.update({ 'key_name': gci_student.key().name(), 'parent': gci_student, 'program': gci2009, }) student_info = GCIStudentInfo(**student_info_properties) student_info.put() gci_student.student_info = student_info gci_student.put() score_properties = { 'parent': gci_student, 'program': gci2009, 'points': 5, 'tasks': [gci_task.key()] } score = GCIScore(**score_properties) score.put() document_properties = { 'key_name': 'site/site/home', 'link_id': 'home', 'scope_path': 'site', 'scope': site, 'prefix': 'site', 'author': current_user, 'title': 'Home Page', 'short_name': 'Home', 'content': 'This is the Home Page', 'modified_by': current_user, } home_document = Document(**document_properties) home_document.put() document_properties = { 'key_name': 'user/test/notes', 'link_id': 'notes', 'scope_path': 'test', 'scope': current_user, 'prefix': 'user', 'author': current_user, 'title': 'My Notes', 'short_name': 'Notes', 'content': 'These are my notes', 'modified_by': current_user, } notes_document = Document(**document_properties) notes_document.put() site.home = home_document site.put() # pylint: disable=E1101 memcache.flush_all() return http.HttpResponse('Done')
def populate(self, redirect, request, args, kwargs): """Populates the fields in the RequestData object. Args: request: Django HTTPRequest object. args & kwargs: The args and kwargs django sends along. """ super(RequestData, self).populate(redirect, request, args, kwargs) if kwargs.get('sponsor') and kwargs.get('program'): program_key_name = "%s/%s" % (kwargs['sponsor'], kwargs['program']) program_key = db.Key.from_path('GCIProgram', program_key_name) else: program_key = Site.active_program.get_value_for_datastore(self.site) program_key_name = program_key.name() timeline_key = db.Key.from_path('GCITimeline', program_key_name) org_app_key_name = 'gci_program/%s/orgapp' % program_key_name org_app_key = db.Key.from_path('OrgAppSurvey', org_app_key_name) keys = [program_key, timeline_key, org_app_key] self.program, self.program_timeline, self.org_app = db.get(keys) if not self.program: raise NotFound("There is no program for url '%s'" % program_key_name) self.timeline = TimelineHelper(self.program_timeline, self.org_app) if kwargs.get('organization'): fields = [self.program.key().id_or_name(), kwargs.get('organization')] org_key_name = '/'.join(fields) self.organization = GCIOrganization.get_by_key_name(org_key_name) if not self.organization: raise NotFound("There is no organization for url '%s'" % org_key_name) if self.user: key_name = '%s/%s' % (self.program.key().name(), self.user.link_id) self.profile = GCIProfile.get_by_key_name( key_name, parent=self.user) host_key = GCIProgram.scope.get_value_for_datastore(self.program) self.is_host = host_key in self.user.host_for if self.profile and self.profile.status != 'invalid': org_keys = set(self.profile.mentor_for + self.profile.org_admin_for) prop = GCIProfile.student_info student_info_key = prop.get_value_for_datastore(self.profile) if student_info_key: self.student_info = db.get(student_info_key) self.is_student = True else: orgs = db.get(org_keys) org_map = self.org_map = dict((i.key(), i) for i in orgs) self.mentor_for = org_map.values() self.org_admin_for = [org_map[i] for i in self.profile.org_admin_for] self.is_org_admin = self.is_host or bool(self.org_admin_for) self.is_mentor = self.is_org_admin or bool(self.mentor_for)
def _cleanTask(self, task, org): """Cleans the data given so that it can be safely stored as a task. Args: task: Dictionary as constructed by the csv.DictReader(). org: the GCIOrganization for which the task is created. Returns: A list of error messages if any have occurred. """ errors = [] # check title if not task['title']: errors.append('No valid title present.') # clean description try: parser = HTMLParser(tokenizer=sanitizer.HTMLSanitizer) parsed = parser.parseFragment(task['description'], encoding='utf-8') cleaned_string = ''.join([tag.toxml() for tag in parsed.childNodes]) task['description'] = cleaned_string.strip().replace('\r\n', '\n') except (HTMLParseError, ParseError, TypeError) as e: logging.warning('Cleaning of description failed with: %s', e) errors.append( 'Failed to clean the description, do not use naughty HTML such as ' '<script>.') # clean time to complete try: hours_to_complete = int(task['time_to_complete']) # Must be at least 2 days (48hrs) if hours_to_complete < 2*24: errors.append('Time to complete must be at least 48 hrs, given was: %s' % hours_to_complete) else: task['time_to_complete'] = hours_to_complete except (ValueError, TypeError) as e: errors.append('No valid time to completion found, given was: %s.' % task['time_to_complete']) # clean mentors mentor_ids = set(task['mentors'].split(',')) mentors = [] mentor_entities = [] for mentor_id in mentor_ids: q = GCIProfile.all() q.filter('link_id', mentor_id.strip()) q.filter('mentor_for', org) q.filter('status', 'active') mentor = q.get() if mentor: mentors.append(mentor.key()) mentor_entities.append(mentor) else: errors.append('%s is not a mentor.' % mentor_id) task['mentors'] = mentors task['mentor_entities'] = mentor_entities program_entity = org.program # clean task types types = [] for task_type in set(task['task_type'].split(',')): task_type = task_type.strip() if task_type in program_entity.task_types: types.append(task_type) else: errors.append('%s is not a valid task type.' % task_type) task['types'] = types # clean task tags tags = [] for tag in set(task['arbit_tag'].split(',')): tags.append(tag.strip()) task['tags'] = tags return errors
def _getProfile(self, user_to_invite): key_name = '/'.join([ self.request_data.program.key().name(), user_to_invite.link_id]) return GCIProfile.get_by_key_name(key_name, parent=user_to_invite)
def _getInvitedProfile(self): key_name = '/'.join([ self.data.program.key().name(), self.data.invited_user.link_id]) return GCIProfile.get_by_key_name(key_name, parent=self.data.invited_user)
'<script>.') # clean time to complete try: task['time_to_complete'] = int(task['time_to_complete']) except (ValueError, TypeError), e: errors.append('No valid time to completion found, given was: %s.' % task['time_to_complete']) # clean mentors mentor_ids = set(task['mentors'].split(',')) mentors = [] mentor_entities = [] for mentor_id in mentor_ids: q = GCIProfile.all() q.filter('link_id', mentor_id.strip()) q.filter('mentor_for', org) q.filter('status', 'active') mentor = q.get() if mentor: mentors.append(mentor.key()) mentor_entities.append(mentor) else: errors.append('%s is not a mentor.' % mentor_id) task['mentors'] = mentors task['mentor_entities'] = mentor_entities program_entity = org.scope