def testNormalizeAccount(self): """Tests if normalizeAccount normalizes accounts properly. """ #normalize currently logged in user account = self.account normalized_acc = accounts.normalizeAccount(account) expected_email = self.regular_email self.assertEqual(normalized_acc.email(), expected_email) #normalize a non normal account account = users.User(email=self.non_normal_email) normalized_acc = accounts.normalizeAccount(account) expected_email = self.non_normal_email.lower() self.assertEqual(normalized_acc.email(), expected_email) #when account is None, e.g. if no user is logged in account = None self.assertRaises(AttributeError, accounts.normalizeAccount, account) #test invalid emails account = users.User(email=self.invalid_emails[0]) normalized_acc = accounts.normalizeAccount(account) expected_email = self.invalid_emails[0] self.assertEqual(normalized_acc.email(), expected_email) account = users.User(email=self.invalid_emails[1]) normalized_acc = accounts.normalizeAccount(account) expected_email = self.invalid_emails[1] self.assertEqual(normalized_acc.email(), expected_email) account = users.User(email=self.invalid_emails[2]) normalized_acc = accounts.normalizeAccount(account) expected_email = self.invalid_emails[2] self.assertEqual(normalized_acc.email(), expected_email)
def testNormalizeAccount(self): """Tests if normalizeAccount normalizes accounts properly. """ #normalize currently logged in user account = self.account normalized_acc = accounts.normalizeAccount(account) expected_email = self.regular_email self.assertEqual(normalized_acc.email(), expected_email) #a non normal account won't be normalized any more account = users.User(email=self.non_normal_email) normalized_acc = accounts.normalizeAccount(account) expected_email = self.non_normal_email self.assertEqual(normalized_acc.email(), expected_email)
def _updateField(self, entity, entity_properties, name): """Special case logic for account. When the account is changed, the former_accounts field should be appended with the old account. Also, if either is_developer or agrees_to_tos change, the user's rights have changed, so we need to flush the sidebar. Make sure once the user agreed ToS, the ToS fields can't be changed. """ value = entity_properties[name] # iff the agreed_to_tos is True and we want to set it to False if (name == "agreed_to_tos") and (not value) and entity.agreed_to_tos: return False # iff the agreed_to_tos_on has a value and we want to change it if (name == "agreed_to_tos_on") and entity.agreed_to_tos_on and (value != entity.agreed_to_tos_on): return False if (name == "is_developer") and (entity.is_developer != value): sidebar.flush(entity.account) if (name == "agreed_to_tos") and (entity.agreed_to_tos != value): sidebar.flush(entity.account) if name == "account": # normalize all accounts before doing anything with the value value = accounts.normalizeAccount(value) entity_properties[name] = value if entity.account != value: entity.former_accounts.append(entity.account) return True
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 _createField(self, entity_properties, name): """Normalize the account before storing it. """ value = entity_properties[name] if (name == 'account'): # normalize all accounts before doing anything with the value value = accounts.normalizeAccount(value) entity_properties[name] = value
class InviteForm(forms.ModelForm): """Django form for the invite page. """ link_id = djangoforms.CharField(label='Link ID/Email') class Meta: model = Request css_prefix = 'gsoc_intivation' fields = ['message'] def __init__(self, request_data, *args, **kwargs): super(InviteForm, self).__init__(*args, **kwargs) # store request object to cache results of queries self.request_data = request_data # reorder the fields so that link_id is the first one field = self.fields.pop('link_id') self.fields.insert(0, 'link_id', field) field.help_text = "The link_id or email address of the invitee" def clean_link_id(self): """Accepts link_id of users which may be invited. """ assert isSet(self.request_data.organization) invited_user = None link_id_cleaner = cleaning.clean_link_id('link_id') try: link_id = link_id_cleaner(self) except djangoforms.ValidationError, e: if e.code != 'invalid': raise email_cleaner = cleaning.clean_email('link_id') try: email_address = email_cleaner(self) except djangoforms.ValidationError, e: if e.code != 'invalid': raise msg = ugettext(u'Enter a valid link_id or email address.') raise djangoforms.ValidationError(msg, code='invalid') account = users.User(email_address) user_account = accounts.normalizeAccount(account) invited_user = User.all().filter('account', user_account).get() if not invited_user: raise djangoforms.ValidationError( "There is no user with that email address")
def testGetCurrentAccount(self): """Tests that current account is returned. """ expected_account = accounts.normalizeAccount(self.account) self.assertEqual(accounts.getCurrentAccount(), expected_account) expected_account = self.account self.assertEqual(accounts.getCurrentAccount(False), expected_account) default_account_setting = os.environ.get('USER_EMAIL', None) try: os.environ['USER_EMAIL'] = self.non_normal_email expected_account = users.get_current_user() self.assertEqual(accounts.getCurrentAccount(False), expected_account) self.assertEqual(accounts.getCurrentAccount(True), accounts.normalizeAccount(expected_account)) finally: if default_account_setting is None: del os.environ['USER_EMAIL'] else: os.environ['USER_EMAIL'] = default_account_setting
def convert_user_accounts(*args, **kwargs): """Converts all current user accounts to normalized form. """ data = user_logic.getAll(user_model.User.all()) for user in data: normalized = accounts.normalizeAccount(user.account) if user.account != normalized: user.account = normalized user.put() return http.HttpResponse('Done')
def getExtraFields(entity, *args): res = { 'student': entity.student.user.name, 'number': len(entity.tasks) } if is_host: fields = sparams['admin_field_keys'] extra = dicts.toDict(entity.student, fields) res.update(extra) res['group_name'] = entity.student.scope.name res['birth_date'] = entity.student.birth_date.isoformat() res['account_name'] = accounts.normalizeAccount(entity.student.user.account).email() res['forms_submitted'] = "Yes" if (entity.student.consent_form and entity.student.student_id_form) else "No" return res
def getForAccount(self, account): """Retrieves the user entity for the specified account. If there is no user logged in, or they have no valid associated User entity, None is returned. """ if not account: raise base.InvalidArgumentError account = accounts.normalizeAccount(account) fields = {"account": account, "status": "valid"} return self.getForFields(filter=fields, unique=True)
def forAccount(account): """Retrieves the user entity for the specified account. If there is no user logged in, or they have no valid associated User entity, None is returned. """ if not account: raise exception.BadRequest(message="Missing argument 'account'") account = accounts.normalizeAccount(account) q = user_model.User.all() q.filter('account', account) q.filter('status', 'valid') return q.get()
def forAccount(account): """Retrieves the user entity for the specified account. If there is no user logged in, or they have no valid associated User entity, None is returned. """ if not account: raise exception.BadRequest(message="Missing argument 'account'") account = accounts.normalizeAccount(account) q = user_model.User.all() q.filter('account', account) q.filter('status', 'valid') return q.get()
def forAccount(account): """Retrieves the user entity for the specified account. If there is no user logged in, or they have no valid associated User entity, None is returned. """ if not account: raise base.InvalidArgumentError("Missing argument 'account'") account = accounts.normalizeAccount(account) q = User.all() q.filter('account', account) q.filter('status', 'valid') return q.get()
def post(self): """Handler for HTTP POST request. """ from soc.modules.gsoc.views.forms import GSoCBoundField form = UserCreateForm(GSoCBoundField, self.data.POST) if not form.is_valid(): return self.get() cleaned_data = form.cleaned_data norm_account = accounts.normalizeAccount(self.data.gae_user) cleaned_data['account'] = norm_account cleaned_data['account_id'] = self.data.gae_user.user_id() form.create(key_name=cleaned_data['link_id']) self.redirect.to('edit_user', validated=True)
def post(self, data, check, mutator): """Handler for HTTP POST request.""" from soc.modules.gsoc.views.forms import GSoCBoundField form = UserCreateForm(GSoCBoundField, data=data.POST) if not form.is_valid(): # TODO(nathaniel): problematic self-call. return self.get(data, check, mutator) cleaned_data = form.cleaned_data norm_account = accounts.normalizeAccount(data.gae_user) cleaned_data['account'] = norm_account cleaned_data['account_id'] = data.gae_user.user_id() form.create(key_name=cleaned_data['link_id']) return data.redirect.to('edit_user', validated=True)
def getExtraFields(entity, *args): res = { 'student': entity.student.user.name, 'number': len(entity.tasks) } if is_host: fields = sparams['admin_field_keys'] extra = dicts.toDict(entity.student, fields) res.update(extra) res['group_name'] = entity.student.scope.name res['birth_date'] = entity.student.birth_date.isoformat() res['account_name'] = accounts.normalizeAccount( entity.student.user.account).email() res['forms_submitted'] = "Yes" if ( entity.student.consent_form and entity.student.student_id_form) else "No" return res
def validateUser(self, dirty): if self.data.user: return EmptyForm() else: user_form = self._getCreateUserForm() if not user_form.is_valid(): return user_form key_name = user_form.cleaned_data['link_id'] account = self.data.gae_user norm_account = accounts.normalizeAccount(account) user_form.cleaned_data['account'] = norm_account user_form.cleaned_data['user_id'] = account.user_id() # set the new user entity in self.data.user self.data.user = user_form.create(commit=False, key_name=key_name) dirty.append(self.data.user) return user_form
def getForAccount(self, account): """Retrieves the user entity for the specified account. If there is no user logged in, or they have no valid associated User entity, None is returned. """ if not account: raise base.InvalidArgumentError account = accounts.normalizeAccount(account) fields = { 'account': account, 'status':'valid', } return self.getForFields(filter=fields, unique=True)
def validateUser(self, data, dirty): if data.user: return EmptyForm(), data.user else: user_form = self._getCreateUserForm(data) if not user_form.is_valid(): return user_form, None key_name = user_form.cleaned_data['link_id'] account = data.gae_user norm_account = accounts.normalizeAccount(account) user_form.cleaned_data['account'] = norm_account user_form.cleaned_data['user_id'] = account.user_id() user = user_form.create(commit=False, key_name=key_name) dirty.append(user) return user_form, user
def validateUser(self, data, dirty): if data.user: return EmptyForm(), data.user else: user_form = self._getCreateUserForm(data) if not user_form.is_valid(): return user_form, None key_name = user_form.cleaned_data['link_id'] account = data.gae_user norm_account = accounts.normalizeAccount(account) user_form.cleaned_data['account'] = norm_account user_form.cleaned_data['user_id'] = account.user_id() user = user_form.create(commit=False, key_name=key_name) dirty.append(user) return user_form, user
def _updateField(self, entity, entity_properties, name): """Special case logic for account. When the account is changed, the former_accounts field should be appended with the old account. Also, if either is_developer or agrees_to_tos change, the user's rights have changed, so we need to flush the sidebar. Make sure once the user agreed ToS, the ToS fields can't be changed. """ value = entity_properties[name] # iff the agreed_to_tos is True and we want to set it to False if (name == 'agreed_to_tos') and (not value) and entity.agreed_to_tos: return False # iff the agreed_to_tos_on has a value and we want to change it if (name == 'agreed_to_tos_on') and entity.agreed_to_tos_on and ( value != entity.agreed_to_tos_on): return False if (name == 'is_developer') and (entity.is_developer != value): sidebar.flush(entity.account) if (name == 'agreed_to_tos') and (entity.agreed_to_tos != value): sidebar.flush(entity.account) if (name == 'account'): # normalize all accounts before doing anything with the value value = accounts.normalizeAccount(value) entity_properties[name] = value if entity.account != value: entity.former_accounts.append(entity.account) return True
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 __init__(self, params={}): """Defines the fields and methods required for the base View class to provide the user with list, public, create, edit and delete views. Args: params: This dictionary should be filled with the parameters """ new_params = {} patterns = params.get('extra_django_patterns') if not patterns: patterns = [] if params.get('allow_requests_and_invites'): # add patterns concerning requests and invites patterns += [(r'^%(url_name)s/(?P<access_type>invite)/%(scope)s$', '%(module_package)s.%(module_name)s.invite', 'Create invite for %(name)s'), (r'^%(url_name)s/(?P<access_type>accept_invite)/(?P<id>[0-9]*)$', '%(module_package)s.%(module_name)s.accept_invite', 'Accept invite for %(name)s'), (r'^%(url_name)s/(?P<access_type>process_request)/(?P<id>[0-9]*)$', '%(module_package)s.%(module_name)s.process_request', 'Process request'), (r'^%(url_name)s/(?P<access_type>request)/%(scope)s$', '%(module_package)s.%(module_name)s.role_request', 'Create a Request to become %(name)s')] elif params.get('allow_invites'): # add patterns concerning only invites patterns += [(r'^%(url_name)s/(?P<access_type>invite)/%(scope)s$', '%(module_package)s.%(module_name)s.invite', 'Create invite for %(name)s'), (r'^%(url_name)s/(?P<access_type>accept_invite)/(?P<id>[0-9]*)$', '%(module_package)s.%(module_name)s.accept_invite', 'Accept invite for %(name)s'), (r'^%(url_name)s/(?P<access_type>process_request)/(?P<id>[0-9]*)$', '%(module_package)s.%(module_name)s.process_request', 'Process request for %(name)s')] # add manage pattern patterns += [(r'^%(url_name)s/(?P<access_type>manage)/%(scope)s/%(lnp)s$', '%(module_package)s.%(module_name)s.manage', 'Manage a %(name)s'),] new_params['logic'] = soc.logic.models.role.logic new_params['extra_django_patterns'] = patterns new_params['scope_redirect'] = redirects.getInviteRedirect new_params['manage_redirect'] = redirects.getListRolesRedirect new_params['name'] = "Role" new_params['module_name'] = 'role' new_params['create_template'] = 'soc/role/edit.html' new_params['edit_template'] = 'soc/role/edit.html' new_params['create_extra_dynaproperties'] = { 'latitude':forms.fields.FloatField(widget=forms.HiddenInput, required=False), 'longitude': forms.fields.FloatField(widget=forms.HiddenInput, required=False), 'email': forms.fields.EmailField(required=True), 'clean_link_id': cleaning.clean_existing_user('link_id'), 'clean_given_name': cleaning.clean_valid_shipping_chars('given_name'), 'clean_surname': cleaning.clean_valid_shipping_chars('surname'), 'clean_phone': cleaning.clean_phone_number('phone'), 'clean_res_street': cleaning.clean_valid_shipping_chars('res_street'), 'clean_res_street_extra': cleaning.clean_valid_shipping_chars('res_street_extra'), 'clean_res_city': cleaning.clean_valid_shipping_chars('res_city'), 'clean_res_state': cleaning.clean_valid_shipping_chars('res_state'), 'clean_res_postalcode': cleaning.clean_valid_shipping_chars('res_postalcode'), 'clean_ship_name': cleaning.clean_valid_shipping_chars('ship_name'), 'clean_ship_street': cleaning.clean_valid_shipping_chars('ship_street'), 'clean_ship_street_extra': cleaning.clean_valid_shipping_chars( 'ship_street_extra'), 'clean_ship_city': cleaning.clean_valid_shipping_chars('ship_city'), 'clean_ship_state': cleaning.clean_valid_shipping_chars('ship_state'), 'clean_ship_postalcode': cleaning.clean_valid_shipping_chars('ship_postalcode'), 'clean_home_page': cleaning.clean_url('home_page'), 'clean_blog': cleaning.clean_url('blog'), 'clean_photo_url': cleaning.clean_url('photo_url'), 'clean': cleaning.validate_role(), 'scope_path': forms.CharField(widget=forms.HiddenInput, required=True), } new_params['extra_dynaexclude'] = ['user', 'status', 'agreed_to_tos_on', 'mentor_for', 'org_admin_for', 'student_info'] new_params['show_in_roles_overview'] = True # define the fields for the admin list new_params['admin_field_keys'] = [ 'link_id', 'given_name', 'surname', 'document_name', 'email', 'res_street', 'res_street_extra', 'res_city', 'res_state', 'res_country', 'res_postalcode', 'phone', 'recipient_name', 'shipping_street', 'shipping_street_extra', 'shipping_city', 'shipping_state', 'shipping_country', 'shipping_postalcode', 'birth_date', 'tshirt_style', 'tshirt_size', 'gender', 'group_name', 'status', 'account_name', ] new_params['admin_field_names'] = [ 'Link ID', 'Name', 'Surname', 'Name on Documents', 'Email', 'Street Address 1', 'Street Address 2', 'City', 'State', 'Country', 'Postal Code', 'Phone Number', 'Recipient Name', 'Shipping Street Address 1', 'Shipping Street Address 2', 'Shipping City', 'Shipping State', 'Shipping Country', 'Shipping Postal Code', 'Birth Date', 'T-Shirt Style', 'T-Shirt Size', 'Gender', 'Group Name', 'Status', 'Account Name', ] new_params['admin_field_prefetch'] = ['scope', 'user'] new_params['admin_field_extra'] = lambda entity: { 'group_name': entity.scope.name, 'birth_date': entity.birth_date.isoformat(), 'account_name': accounts.normalizeAccount(entity.user.account).email() } new_params['admin_field_hidden'] = ['tshirt_style', 'tshirt_size'] params = dicts.merge(params, new_params, sub_merge=True) super(View, self).__init__(params=params) # add manage template template = 'soc/%(module_name)s/manage.html' % self._params self._params['manage_template'] = template # register this View addRole(self)
def __init__(self, params={}): """Defines the fields and methods required for the base View class to provide the user with list, public, create, edit and delete views. Args: params: This dictionary should be filled with the parameters """ new_params = {} patterns = params.get('extra_django_patterns') if not patterns: patterns = [] if params.get('allow_requests_and_invites'): # add patterns concerning requests and invites patterns += [ (r'^%(url_name)s/(?P<access_type>invite)/%(scope)s$', '%(module_package)s.%(module_name)s.invite', 'Create invite for %(name)s'), (r'^%(url_name)s/(?P<access_type>accept_invite)/(?P<id>[0-9]*)$', '%(module_package)s.%(module_name)s.accept_invite', 'Accept invite for %(name)s'), (r'^%(url_name)s/(?P<access_type>process_request)/(?P<id>[0-9]*)$', '%(module_package)s.%(module_name)s.process_request', 'Process request'), (r'^%(url_name)s/(?P<access_type>request)/%(scope)s$', '%(module_package)s.%(module_name)s.role_request', 'Create a Request to become %(name)s') ] elif params.get('allow_invites'): # add patterns concerning only invites patterns += [ (r'^%(url_name)s/(?P<access_type>invite)/%(scope)s$', '%(module_package)s.%(module_name)s.invite', 'Create invite for %(name)s'), (r'^%(url_name)s/(?P<access_type>accept_invite)/(?P<id>[0-9]*)$', '%(module_package)s.%(module_name)s.accept_invite', 'Accept invite for %(name)s'), (r'^%(url_name)s/(?P<access_type>process_request)/(?P<id>[0-9]*)$', '%(module_package)s.%(module_name)s.process_request', 'Process request for %(name)s') ] # add manage pattern patterns += [ (r'^%(url_name)s/(?P<access_type>manage)/%(scope)s/%(lnp)s$', '%(module_package)s.%(module_name)s.manage', 'Manage a %(name)s'), ] new_params['logic'] = soc.logic.models.role.logic new_params['extra_django_patterns'] = patterns new_params['scope_redirect'] = redirects.getInviteRedirect new_params['manage_redirect'] = redirects.getListRolesRedirect new_params['name'] = "Role" new_params['module_name'] = 'role' new_params['create_template'] = 'soc/role/edit.html' new_params['edit_template'] = 'soc/role/edit.html' new_params['create_extra_dynaproperties'] = { 'latitude': forms.fields.FloatField(widget=forms.HiddenInput, required=False), 'longitude': forms.fields.FloatField(widget=forms.HiddenInput, required=False), 'email': forms.fields.EmailField(required=True), 'clean_link_id': cleaning.clean_existing_user('link_id'), 'clean_given_name': cleaning.clean_valid_shipping_chars('given_name'), 'clean_surname': cleaning.clean_valid_shipping_chars('surname'), 'clean_phone': cleaning.clean_phone_number('phone'), 'clean_res_street': cleaning.clean_valid_shipping_chars('res_street'), 'clean_res_street_extra': cleaning.clean_valid_shipping_chars('res_street_extra'), 'clean_res_city': cleaning.clean_valid_shipping_chars('res_city'), 'clean_res_state': cleaning.clean_valid_shipping_chars('res_state'), 'clean_res_postalcode': cleaning.clean_valid_shipping_chars('res_postalcode'), 'clean_ship_name': cleaning.clean_valid_shipping_chars('ship_name'), 'clean_ship_street': cleaning.clean_valid_shipping_chars('ship_street'), 'clean_ship_street_extra': cleaning.clean_valid_shipping_chars('ship_street_extra'), 'clean_ship_city': cleaning.clean_valid_shipping_chars('ship_city'), 'clean_ship_state': cleaning.clean_valid_shipping_chars('ship_state'), 'clean_ship_postalcode': cleaning.clean_valid_shipping_chars('ship_postalcode'), 'clean_home_page': cleaning.clean_url('home_page'), 'clean_blog': cleaning.clean_url('blog'), 'clean_photo_url': cleaning.clean_url('photo_url'), 'clean': cleaning.validate_role(), 'scope_path': forms.CharField(widget=forms.HiddenInput, required=True), } new_params['extra_dynaexclude'] = [ 'user', 'status', 'agreed_to_tos_on', 'mentor_for', 'org_admin_for', 'student_info' ] new_params['show_in_roles_overview'] = True # define the fields for the admin list new_params['admin_field_keys'] = [ 'link_id', 'given_name', 'surname', 'document_name', 'email', 'res_street', 'res_street_extra', 'res_city', 'res_state', 'res_country', 'res_postalcode', 'phone', 'recipient_name', 'shipping_street', 'shipping_street_extra', 'shipping_city', 'shipping_state', 'shipping_country', 'shipping_postalcode', 'birth_date', 'tshirt_style', 'tshirt_size', 'gender', 'group_name', 'status', 'account_name', ] new_params['admin_field_names'] = [ 'Link ID', 'Name', 'Surname', 'Name on Documents', 'Email', 'Street Address 1', 'Street Address 2', 'City', 'State', 'Country', 'Postal Code', 'Phone Number', 'Recipient Name', 'Shipping Street Address 1', 'Shipping Street Address 2', 'Shipping City', 'Shipping State', 'Shipping Country', 'Shipping Postal Code', 'Birth Date', 'T-Shirt Style', 'T-Shirt Size', 'Gender', 'Group Name', 'Status', 'Account Name', ] new_params['admin_field_prefetch'] = ['scope', 'user'] new_params['admin_field_extra'] = lambda entity: { 'group_name': entity.scope.name, 'birth_date': entity.birth_date.isoformat(), 'account_name': accounts.normalizeAccount(entity.user.account). email() } new_params['admin_field_hidden'] = ['tshirt_style', 'tshirt_size'] params = dicts.merge(params, new_params, sub_merge=True) super(View, self).__init__(params=params) # add manage template template = 'soc/%(module_name)s/manage.html' % self._params self._params['manage_template'] = template # register this View addRole(self)