Merge pull request #54 from ostcar/registered_group

Created a default group for registered users
This commit is contained in:
Oskar Hahn 2012-11-23 09:33:20 -08:00
commit b75c9f32db
5 changed files with 53 additions and 25 deletions

View File

@ -12,7 +12,6 @@
from django.conf import settings from django.conf import settings
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.models import Group, Permission
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.utils.importlib import import_module from django.utils.importlib import import_module
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
@ -24,6 +23,7 @@ from openslides.utils.views import FormView, TemplateView
from openslides.config.forms import GeneralConfigForm from openslides.config.forms import GeneralConfigForm
from openslides.config.models import config from openslides.config.models import config
from openslides.participant.api import get_or_create_anonymous_group
class GeneralConfig(FormView): class GeneralConfig(FormView):
@ -61,22 +61,7 @@ class GeneralConfig(FormView):
# system # system
if form.cleaned_data['system_enable_anonymous']: if form.cleaned_data['system_enable_anonymous']:
config['system_enable_anonymous'] = True config['system_enable_anonymous'] = True
# check for Anonymous group and (re)create it as needed anonymous = get_or_create_anonymous_group()
try:
anonymous = Group.objects.get(name='Anonymous')
except Group.DoesNotExist:
default_perms = ['can_see_agenda', 'can_see_projector',
'can_see_motion', 'can_see_assignment',
'can_see_dashboard']
anonymous = Group()
anonymous.name = 'Anonymous'
anonymous.save()
anonymous.permissions = Permission.objects.filter(
codename__in=default_perms)
anonymous.save()
messages.success(self.request,
_('Anonymous access enabled. Please modify the "Anonymous" ' \
'group to fit your required permissions.'))
else: else:
config['system_enable_anonymous'] = False config['system_enable_anonymous'] = False

View File

@ -17,12 +17,17 @@ from random import choice
import string import string
import csv import csv
from django.contrib.auth.models import User from django.contrib.auth.models import Permission
from django.db import transaction from django.db import transaction
from openslides.utils import csv_ext from openslides.utils import csv_ext
from openslides.participant.models import User from openslides.participant.models import User, Group
DEFAULT_PERMS = ['can_see_agenda', 'can_see_projector',
'can_see_motion', 'can_see_assignment',
'can_see_dashboard']
def gen_password(): def gen_password():
@ -93,3 +98,23 @@ def import_users(csv_file):
except UnicodeDecodeError: except UnicodeDecodeError:
error_messages.appen(_('Import file has wrong character encoding, only UTF-8 is supported!')) error_messages.appen(_('Import file has wrong character encoding, only UTF-8 is supported!'))
return (count_success, error_messages) return (count_success, error_messages)
def get_or_create_registered_group():
registered, created = Group.objects.get_or_create(
name__iexact='Registered', defaults={'name': 'Registered'})
if created:
registered.permissions = Permission.objects.filter(
codename__in=DEFAULT_PERMS)
registered.save()
return registered
def get_or_create_anonymous_group():
anonymous, created = Group.objects.get_or_create(
name__iexact='Anonymous', defaults={'name': 'Anonymous'})
if created:
anonymous.permissions = Permission.objects.filter(
codename__in=DEFAULT_PERMS)
anonymous.save()
return anonymous

View File

@ -18,6 +18,7 @@ from openslides.utils.forms import (
CssClassMixin, LocalizedModelMultipleChoiceField) CssClassMixin, LocalizedModelMultipleChoiceField)
from openslides.participant.models import User, Group from openslides.participant.models import User, Group
from openslides.participant.api import get_or_create_registered_group
class UserCreateForm(forms.ModelForm, CssClassMixin): class UserCreateForm(forms.ModelForm, CssClassMixin):
@ -25,6 +26,13 @@ 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 = get_or_create_registered_group()
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 +84,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,18 @@ 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):
from openslides.participant.api import get_or_create_registered_group
if not kwargs['created']:
return
registered = get_or_create_registered_group()
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>