def testValidateUserEdit(self): """Tests that existent user field can be cleaned. """ link_id_field = 'link_id' account_field = 'account' clean_field = cleaning.validate_user_edit(link_id_field, account_field) # Test that the values will be returned if the filled user account # is that of the user with the specified link_id self.form.cleaned_data = { link_id_field: self.user.link_id, account_field: self.user.account } cleaned_data_after = clean_field(self.form) self.assertEqual(cleaned_data_after[account_field], self.user.account) self.assertEqual(cleaned_data_after[link_id_field], self.user.link_id) # Test that forms.ValidationError will be raised if the user account # is an existing one but not that of the user with the specified link_id self.form.cleaned_data = { link_id_field: self.user.link_id, account_field: self.another_user.account } self.assertRaises(forms.ValidationError, clean_field, self.form) # Test that the values will be returned if the filled user account # is not an existing one but the filled link_id is an existing one account = users.User(email='*****@*****.**') self.form.cleaned_data = { link_id_field: self.user.link_id, account_field: account } cleaned_data_after = clean_field(self.form) self.assertEqual(cleaned_data_after[account_field], account) self.assertEqual(cleaned_data_after[link_id_field], self.user.link_id) # Test that AttributeError will be raised if the filled user link_id # does not exist but the filled user account exisits (Probably # forms.ValidationError should be raised instead or it may cause http 500) self.form.cleaned_data = { link_id_field: 'non_existent_link_id', account_field: self.another_user.account } self.assertRaises(AttributeError, clean_field, self.form) # Test that AttributeError will be raised if neither the filled user link_id # nor the filled user account exists (Probably forms.ValidationError # should be raised instead or it may cause http 500) self.form.cleaned_data = { link_id_field: 'non_existent_link_id', account_field: users.User(email='*****@*****.**') } self.assertRaises(AttributeError, clean_field, self.form)
def testValidateUserEdit(self): """Tests that existent user field can be cleaned. """ link_id_field = 'link_id' account_field = 'account' clean_field = cleaning.validate_user_edit(link_id_field, account_field) # Test that the values will be returned if the filled user account # is that of the user with the specified link_id self.form.cleaned_data = {link_id_field: self.user.link_id, account_field: self.user.account} cleaned_data_after = clean_field(self.form) self.assertEqual(cleaned_data_after[account_field], self.user.account) self.assertEqual(cleaned_data_after[link_id_field], self.user.link_id) # Test that forms.ValidationError will be raised if the user account # is an existing one but not that of the user with the specified link_id self.form.cleaned_data = {link_id_field: self.user.link_id, account_field: self.another_user.account} self.assertRaises(forms.ValidationError, clean_field, self.form) # Test that the values will be returned if the filled user account # is not an existing one but the filled link_id is an existing one account = users.User(email='*****@*****.**') self.form.cleaned_data = {link_id_field: self.user.link_id, account_field: account} cleaned_data_after = clean_field(self.form) self.assertEqual(cleaned_data_after[account_field], account) self.assertEqual(cleaned_data_after[link_id_field], self.user.link_id) # Test that AttributeError will be raised if the filled user link_id # does not exist but the filled user account exisits (Probably # forms.ValidationError should be raised instead or it may cause http 500) self.form.cleaned_data = {link_id_field: 'non_existent_link_id', account_field: self.another_user.account} self.assertRaises(AttributeError, clean_field, self.form) # Test that AttributeError will be raised if neither the filled user link_id # nor the filled user account exists (Probably forms.ValidationError # should be raised instead or it may cause http 500) self.form.cleaned_data = {link_id_field: 'non_existent_link_id', account_field: users.User(email='*****@*****.**')} self.assertRaises(AttributeError, clean_field, self.form)
def __init__(self, params=None): """Defines the fields and methods required for the base View class to provide the user with list, public, create, edit and delete views. Params: params: a dict with params for this View """ rights = access.Checker(params) rights['create'] = ['checkIsDeveloper'] rights['edit'] = ['checkIsDeveloper'] rights['delete'] = ['checkIsDeveloper'] rights['show'] = ['allow'] rights['list'] = ['checkIsDeveloper'] rights['list_developers'] = ['checkIsDeveloper'] new_params = {} new_params['logic'] = soc.logic.models.user.logic new_params['rights'] = rights new_params['name'] = "User" new_params['edit_template'] = 'soc/user/edit.html' new_params['pickable'] = True new_params['cache_pick'] = True new_params['sidebar_heading'] = 'Users' new_params['extra_dynaexclude'] = ['former_accounts', 'agreed_to_tos', 'agreed_to_tos_on', 'status'] new_params['create_extra_dynaproperties'] = { 'clean_link_id': cleaning.clean_user_not_exist('link_id'), 'clean_account': cleaning.clean_user_account_not_in_use('account')} # recreate the choices for the edit form status_choices = [] for choice in user_logic.getModel().status.choices: status_choices.append((choice, choice)) new_params['edit_extra_dynaproperties'] = { 'link_id': forms.CharField(widget=widgets.ReadOnlyInput(), required=True), 'clean_link_id': cleaning.clean_link_id('link_id'), 'agreed_to_tos_on': forms.DateTimeField( widget=widgets.ReadOnlyInput(attrs={'disabled':'true'}), required=False), 'status': forms.ChoiceField(choices=status_choices), 'clean_account': cleaning.clean_user_account('account'), 'clean': cleaning.validate_user_edit('link_id', 'account'), } patterns = [] patterns += [(r'^%(url_name)s/(?P<access_type>list_developers)$', 'soc.views.models.%(module_name)s.list_developers', "List Developers")] new_params['extra_django_patterns'] = patterns new_params['sidebar_developer'] = [('/%s/list_developers', 'List Developers', 'list_developers')] new_params['public_field_prefetch'] = ['account'] new_params['public_field_extra'] = lambda entity: { "email": entity.account.email(), } new_params['public_field_keys'] = ['email', 'name', 'link_id'] new_params['public_field_names'] = ['Email', 'Public name', 'Link ID'] params = dicts.merge(params, new_params) super(View, self).__init__(params=params)