def __init__(self, *args, **kwargs): super(GroupEditForm, self).__init__(*args, **kwargs) ordering_field = orm_user_lookup() if self.instance.id: self.fields['name'].widget.attrs['readonly'] = True initial_members = User.objects.filter( groups=self.instance).order_by(ordering_field) initial_perms = HuePermission.objects.filter( grouppermission__group=self.instance).order_by( 'app', 'description') else: initial_members = [] initial_perms = [] self.fields["members"] = _make_model_field( _("members"), initial_members, User.objects.order_by(ordering_field)) self.fields["permissions"] = _make_model_field( _("permissions"), initial_perms, HuePermission.objects.order_by('app', 'description')) if 'organization' in self.fields: self.fields['organization'] = forms.ChoiceField( choices=((default_organization().id, default_organization()), ), initial=default_organization())
def __init__(self, *args, **kwargs): super(OrganizationUserChangeForm, self).__init__(*args, **kwargs) if self.instance.id: self.fields['email'].widget.attrs['readonly'] = True self.fields['organization'] = forms.ChoiceField( choices=((default_organization().id, default_organization()), ), initial=default_organization())
def make_logged_in_client(username="******", password="******", is_superuser=True, recreate=False, groupname=None): """ Create a client with a user already logged in. Sometimes we recreate the user, because some tests like to mess with is_active and such. """ if ENABLE_ORGANIZATIONS.get() and username == 'test': username = username + '@gethue.com' try: lookup = {orm_user_lookup(): username} user = User.objects.get(**lookup) if recreate: user.delete() raise User.DoesNotExist except User.DoesNotExist: user_attrs = { 'password': password } if ENABLE_ORGANIZATIONS.get(): user_attrs['email'] = username user_attrs['organization'] = default_organization() else: user_attrs['username'] = username user_attrs['email'] = username + '@localhost' user = User.objects.create_user(**user_attrs) user.is_superuser = is_superuser user.save() else: if user.is_superuser != is_superuser: user.is_superuser = is_superuser user.save() if groupname is not None: group_attrs = {'name': groupname} if ENABLE_ORGANIZATIONS.get(): group_attrs['organization'] = default_organization() group, created = Group.objects.get_or_create(**group_attrs) if not user.groups.filter(name=group.name).exists(): user.groups.add(group) user.save() c = Client() ret = c.login(username=username, password=password) assert ret, "Login failed (user '%s')." % username return c