def setupUniqueUserIdAdder(job_entity): """Job that setup jobs that will add unique user ids to all Users. Args: job_entity: a Job entity with key_data set to [last_completed_user] """ key_data = job_entity.key_data user_fields = {'user_id': None} if len(key_data) == 1: # start where we left off user_fields['__key__ >'] = key_data[0] m_users = user_logic.getForFields(user_fields, limit=DEF_USER_STEP_SIZE) # set the default fields for the jobs we are going to create priority_group = priority_logic.getGroup(priority_logic.CONVERT) job_fields = { 'priority_group': priority_group, 'task_name': 'addUniqueUserIds'} job_query_fields = job_fields.copy() while m_users: # for each user create a adder job for user in m_users: job_query_fields['key_data'] = user.key() adder_job = job_logic.getForFields(job_query_fields, unique=True) if not adder_job: # this user doesn't have unique id yet job_fields['key_data'] = [user.key()] job_logic.updateOrCreateFromFields(job_fields) # update our own job last_user_key = m_users[-1].key() if len(key_data) == 1: key_data[0] = last_user_key else: key_data.append(last_user_key) updated_job_fields = {'key_data': key_data} job_logic.updateEntityProperties(job_entity, updated_job_fields) # rinse and repeat user_fields['__key__ >'] = last_user_key m_users = user_logic.getForFields(user_fields, limit=DEF_USER_STEP_SIZE) # we are finished return
def setupUniqueUserIdAdder(job_entity): """Job that setup jobs that will add unique user ids to all Users. Args: job_entity: a Job entity with key_data set to [last_completed_user] """ key_data = job_entity.key_data user_fields = {'user_id': None} if len(key_data) == 1: # start where we left off user_fields['__key__ >'] = key_data[0] m_users = user_logic.getForFields(user_fields, limit=DEF_USER_STEP_SIZE) # set the default fields for the jobs we are going to create priority_group = priority_logic.getGroup(priority_logic.CONVERT) job_fields = { 'priority_group': priority_group, 'task_name': 'addUniqueUserIds' } job_query_fields = job_fields.copy() while m_users: # for each user create a adder job for user in m_users: job_query_fields['key_data'] = user.key() adder_job = job_logic.getForFields(job_query_fields, unique=True) if not adder_job: # this user doesn't have unique id yet job_fields['key_data'] = [user.key()] job_logic.updateOrCreateFromFields(job_fields) # update our own job last_user_key = m_users[-1].key() if len(key_data) == 1: key_data[0] = last_user_key else: key_data.append(last_user_key) updated_job_fields = {'key_data': key_data} job_logic.updateEntityProperties(job_entity, updated_job_fields) # rinse and repeat user_fields['__key__ >'] = last_user_key m_users = user_logic.getForFields(user_fields, limit=DEF_USER_STEP_SIZE) # we are finished return
def testDeleteUserNonExisted(self): """Test that a developer user cannot delete a non existed user. """ entities = user_logic.getForFields() count_before = len(entities) url = '/user/delete/not_a_user' xsrf_token = self.getXsrfToken(url) postdata = {'xsrf_token': xsrf_token} response = self.client.post(url, postdata) self.assertEqual(response.status_code, httplib.OK) data = simplejson.loads(response.context.get('json', None)) self.assertTrue('error' in data) entities = user_logic.getForFields() count_after = len(entities) self.assertEqual(count_before, count_after)
def sendInviteNotification(entity): """Sends out an invite notification to the user the request is for. Args: entity : A request containing the information needed to create the message """ from soc.logic.models.user import logic as user_logic # get the user the request is for properties = {'link_id': entity.link_id } to_user = user_logic.getForFields(properties, unique=True) invitation_url = "http://%(host)s%(index)s" % { 'host' : os.environ['HTTP_HOST'], 'index': redirects.getInviteProcessRedirect(entity, None), } message_properties = { 'role_verbose' : entity.role_verbose, 'group': entity.scope.name, 'invitation_url': invitation_url, } subject = DEF_INVITATION_MSG_FMT % { 'role_verbose' : entity.role_verbose, 'group' : entity.scope.name } template = DEF_GROUP_INVITE_NOTIFICATION_TEMPLATE from_user = user_logic.getForCurrentAccount() sendNotification(to_user, from_user, message_properties, subject, template)
def checkIsUnusedAccount(self, django_args=None): """Raises an alternate HTTP response if Google Account has a User entity. Args: django_args: a dictionary with django's arguments, not used Raises: AccessViolationResponse: * if a User exists for the logged-in Google Account, or * if a User has this Gooogle Account in their formerAccounts list """ self.checkIsLoggedIn() fields = {'account': self.id} user_entity = user_logic.getForFields(fields, unique=True) if not user_entity and not user_logic.isFormerAccount(self.id): # this account has not been used yet return message_fmt = DEF_USER_ACCOUNT_INVALID_MSG_FMT % { 'email' : self.id.email() } raise out_of_band.LoginRequest(message_fmt=message_fmt)
def sendNewRequestNotification(request_entity): """Sends out a notification to the persons who can process this Request. Args: request_entity: an instance of Request model """ from soc.logic.helper import notifications from soc.logic.models.role import ROLE_LOGICS from soc.logic.models.user import logic as user_logic # get the users who should get the notification to_users = [] # retrieve the Role Logics which we should query on role_logic = ROLE_LOGICS[request_entity.role] role_logics_to_notify = role_logic.getRoleLogicsToNotifyUponNewRequest() # the scope of the roles is the same as the scope of the Request entity fields = {'scope': request_entity.scope, 'status': 'active'} for role_logic in role_logics_to_notify: roles = role_logic.getForFields(fields) for role_entity in roles: # TODO: this might lead to double notifications to_users.append(role_entity.user) # get the user the request is from properties = {'link_id': request_entity.link_id } user_entity = user_logic.getForFields(properties, unique=True) message_properties = { 'requester_name': user_entity.name, 'entity': request_entity, 'request_url': redirects.getProcessRequestRedirect(request_entity, None)} subject = DEF_NEW_REQUEST_MSG_FMT % { 'requester': user_entity.name, 'role_verbose' : request_entity.role_verbose, 'group' : request_entity.scope.name } template = DEF_NEW_REQUEST_NOTIFICATION_TEMPLATE for to_user in to_users: notifications.sendNotification(to_user, None, message_properties, subject, template)
def testDeleteAnotherUserThroughPostWithCorrectXsrfToken(self): """Test that a developer user can delete another user. Through HTTP POST with a correct XSRF token. """ entities = user_logic.getForFields() count_before = len(entities) link_id_another_user = self.another_entity.link_id url = '/user/delete/another_user' xsrf_token = self.getXsrfToken(url) postdata = {'xsrf_token': xsrf_token} response = self.client.post(url, postdata) self.assertEqual(response.status_code, httplib.OK) entities = user_logic.getForFields() count_after = len(entities) expected = count_before - 1 # Test that the user is actually deleted self.assertEqual(count_after, expected) for entity in entities: self.failIfEqual(entity.link_id, link_id_another_user) # assertRedirects does not work #self.assertRedirects(response, '/user/list', status_code=httplib.OK) data = simplejson.loads(response.context.get('json', None)) self.assertEqual(data.get("new_location", None), "/user/list")
def testSendGradingSurveyReminderForProjectThroughPostWithoutCorrectXsrfToken( self): """Tests that sending grading reminders is forbidden without correct token. Without a correct XSRF token, the attempt to send grading survey reminders is forbidden. """ entities = user_logic.getForFields() count_before = len(entities) url = '/tasks/surveys/projects/send_reminder/send' postdata = { 'project_key': self.projects[0].key().name(), 'survey_key': self.grading_survey.key().name(), 'survey_type': 'grading' } response = self.client.post(url, postdata) self.assertEqual(response.status_code, httplib.FORBIDDEN)
def testSendGradingSurveyReminderForProjectThroughPostWithCorrectXsrfToken( self): """Tests that grading survey reminders are sent out with correct XSRF token. With correct XSRF token, grading survey reminders for a project are sent to its mentor. """ entities = user_logic.getForFields() count_before = len(entities) url = '/tasks/surveys/projects/send_reminder/send' postdata = { 'project_key': self.projects[0].key().name(), 'survey_key': self.grading_survey.key().name(), 'survey_type': 'grading' } xsrf_token = self.getXsrfToken(url, data=postdata) postdata.update(xsrf_token=xsrf_token) response = self.client.post(url, postdata) self.assertEqual(response.status_code, httplib.OK) self.assertEmailSent(to=self.mentor.email, html='survey')