Created a default group for registered users

This commit is contained in:
Oskar Hahn 2012-11-22 18:39:54 +01:00
parent e168445497
commit 1049141483
3 changed files with 28 additions and 6 deletions

View File

@ -25,6 +25,16 @@ class UserCreateForm(forms.ModelForm, CssClassMixin):
queryset=Group.objects.exclude(name__iexact='anonymous'), queryset=Group.objects.exclude(name__iexact='anonymous'),
label=_("Groups"), required=False) label=_("Groups"), required=False)
def __init__(self, *args, **kwargs):
if kwargs.get('instance', None) is None:
initial = kwargs.setdefault('initial', {})
registered, created = Group.objects.get_or_create(name__iexact='Registered')
if created:
registered.name = 'Registered'
registered.save()
initial['groups'] = [registered.pk]
super(UserCreateForm, self).__init__(*args, **kwargs)
class Meta: class Meta:
model = User model = User
fields = ('first_name', 'last_name', 'is_active', 'groups', 'structure_level', fields = ('first_name', 'last_name', 'is_active', 'groups', 'structure_level',
@ -76,13 +86,13 @@ class GroupForm(forms.ModelForm, CssClassMixin):
# Do not allow to change the name "anonymous" or give another group # Do not allow to change the name "anonymous" or give another group
# this name # this name
data = self.cleaned_data['name'] data = self.cleaned_data['name']
if self.instance.name.lower() == 'anonymous': if self.instance.name.lower() in ['anonymous', 'registered']:
# Editing the anonymous-user # Editing the anonymous-user
if self.instance.name.lower() != data.lower(): if self.instance.name.lower() != data.lower():
raise forms.ValidationError( raise forms.ValidationError(
_('You can not edit the name for the anonymous user')) _('You can not edit the name for this group.'))
else: else:
if data.lower() == 'anonymous': if data.lower() in ['anonymous', 'registered']:
raise forms.ValidationError( raise forms.ValidationError(
_('Group name "%s" is reserved for internal use.') % data) _('Group name "%s" is reserved for internal use.') % data)
return data return data

View File

@ -234,7 +234,7 @@ def default_config(sender, key, **kwargs):
@receiver(signals.post_save, sender=DjangoUser) @receiver(signals.post_save, sender=DjangoUser)
def user_post_save(sender, instance, signal, *args, **kwargs): def djangouser_post_save(sender, instance, signal, *args, **kwargs):
try: try:
instance.user instance.user
except User.DoesNotExist: except User.DoesNotExist:
@ -242,8 +242,20 @@ def user_post_save(sender, instance, signal, *args, **kwargs):
@receiver(signals.post_save, sender=DjangoGroup) @receiver(signals.post_save, sender=DjangoGroup)
def group_post_save(sender, instance, signal, *args, **kwargs): def djangogroup_post_save(sender, instance, signal, *args, **kwargs):
try: try:
instance.group instance.group
except Group.DoesNotExist: except Group.DoesNotExist:
Group(django_group=instance).save_base(raw=True) Group(django_group=instance).save_base(raw=True)
@receiver(signals.post_save, sender=User)
def user_post_save(sender, instance, *args, **kwargs):
if not kwargs['created']:
return
registered, created = Group.objects.get_or_create(name__iexact='Registered')
if created:
registered.name = 'Registered'
registered.save()
instance.groups.add(registered)
instance.save()

View File

@ -17,7 +17,7 @@
<tr class="{% cycle '' 'odd' %}"> <tr class="{% cycle '' 'odd' %}">
<td><a href="{% model_url group 'view' %}">{{ group.name }}</a></td> <td><a href="{% model_url group 'view' %}">{{ group.name }}</a></td>
<td><a href="{% url user_group_edit group.id %}"><img src="{% static 'images/icons/edit.png' %}" title="{% trans 'Edit group' %}"></a> <td><a href="{% url user_group_edit group.id %}"><img src="{% static 'images/icons/edit.png' %}" title="{% trans 'Edit group' %}"></a>
{% if group.name|lower != 'anonymous' %} {% if group.name|lower != 'anonymous' and group.name|lower != 'registered' %}
<a href="{% url user_group_delete group.id %}"><img src="{% static 'images/icons/delete.png' %}" title="{% trans 'Delete group' %}"></a> <a href="{% url user_group_delete group.id %}"><img src="{% static 'images/icons/delete.png' %}" title="{% trans 'Delete group' %}"></a>
{% endif %} {% endif %}
</td> </td>