created new user-api
This commit is contained in:
parent
146a121606
commit
4ffb701397
@ -15,6 +15,7 @@ from django.contrib.auth.models import User
|
|||||||
from django.utils.translation import ugettext_lazy as _, ugettext_noop
|
from django.utils.translation import ugettext_lazy as _, ugettext_noop
|
||||||
|
|
||||||
from openslides.utils.forms import CssClassMixin
|
from openslides.utils.forms import CssClassMixin
|
||||||
|
from openslides.utils.user import UserFormField
|
||||||
from openslides.application.models import Application
|
from openslides.application.models import Application
|
||||||
|
|
||||||
|
|
||||||
@ -50,11 +51,7 @@ class ApplicationFormTrivialChanges(ApplicationForm):
|
|||||||
|
|
||||||
|
|
||||||
class ApplicationManagerForm(forms.ModelForm, CssClassMixin):
|
class ApplicationManagerForm(forms.ModelForm, CssClassMixin):
|
||||||
submitter = UserModelChoiceField(
|
submitter = UserFormField()
|
||||||
queryset=User.objects.all().exclude(profile=None).
|
|
||||||
order_by("first_name"),
|
|
||||||
label=_("Submitter"),
|
|
||||||
)
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Application
|
model = Application
|
||||||
|
@ -21,6 +21,7 @@ from django.utils.translation import pgettext
|
|||||||
from django.utils.translation import ugettext_lazy as _, ugettext_noop, ugettext
|
from django.utils.translation import ugettext_lazy as _, ugettext_noop, ugettext
|
||||||
|
|
||||||
from openslides.utils.utils import _propper_unicode
|
from openslides.utils.utils import _propper_unicode
|
||||||
|
from openslides.utils.user import UserField
|
||||||
|
|
||||||
from openslides.config.models import config
|
from openslides.config.models import config
|
||||||
from openslides.config.signals import default_config_value
|
from openslides.config.signals import default_config_value
|
||||||
@ -60,7 +61,7 @@ class Application(models.Model, SlideMixin):
|
|||||||
# genpoll
|
# genpoll
|
||||||
)
|
)
|
||||||
|
|
||||||
submitter = models.ForeignKey(User, verbose_name=_("Submitter"))
|
submitter = UserField(verbose_name=_("Submitter"))
|
||||||
supporter = models.ManyToManyField(User, related_name='supporter', \
|
supporter = models.ManyToManyField(User, related_name='supporter', \
|
||||||
null=True, blank=True, verbose_name=_("Supporters"))
|
null=True, blank=True, verbose_name=_("Supporters"))
|
||||||
number = models.PositiveSmallIntegerField(blank=True, null=True,
|
number = models.PositiveSmallIntegerField(blank=True, null=True,
|
||||||
|
@ -16,8 +16,8 @@
|
|||||||
<div id="sidebar">
|
<div id="sidebar">
|
||||||
<div class="box">
|
<div class="box">
|
||||||
<h4>{% trans "Submitter" %}:</h4>
|
<h4>{% trans "Submitter" %}:</h4>
|
||||||
{{ application.submitter.profile }}
|
{{ application.submitter }}
|
||||||
{% if user == application.submitter %}
|
{% if user == application.submitter.user %}
|
||||||
<img src="{% static 'images/icons/user-information.png' %}" title="{% trans 'You!' %}">
|
<img src="{% static 'images/icons/user-information.png' %}" title="{% trans 'You!' %}">
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
@ -113,7 +113,7 @@
|
|||||||
{{ application.creation_time }}
|
{{ application.creation_time }}
|
||||||
|
|
||||||
<p></p>
|
<p></p>
|
||||||
{% if "wit" in actions and user == application.submitter %}
|
{% if "wit" in actions and user == application.submitter.user %}
|
||||||
<p></p>
|
<p></p>
|
||||||
<a href='{% url application_set_status application.id 'wit' %}'>
|
<a href='{% url application_set_status application.id 'wit' %}'>
|
||||||
<span class="button"><span class="icon revert">{% trans 'Withdraw' %}</span></span>
|
<span class="button"><span class="icon revert">{% trans 'Withdraw' %}</span></span>
|
||||||
|
@ -180,7 +180,7 @@ def edit(request, application_id=None):
|
|||||||
return redirect(reverse('application_overview'))
|
return redirect(reverse('application_overview'))
|
||||||
if application_id is not None:
|
if application_id is not None:
|
||||||
application = Application.objects.get(id=application_id)
|
application = Application.objects.get(id=application_id)
|
||||||
if not request.user == application.submitter and not is_manager:
|
if not request.user == application.submitter.user and not is_manager:
|
||||||
messages.error(request, _("You can not edit this application. You are not the submitter."))
|
messages.error(request, _("You can not edit this application. You are not the submitter."))
|
||||||
return redirect(reverse('application_view', args=[application.id]))
|
return redirect(reverse('application_view', args=[application.id]))
|
||||||
actions = application.get_allowed_actions(user=request.user)
|
actions = application.get_allowed_actions(user=request.user)
|
||||||
@ -217,7 +217,7 @@ def edit(request, application_id=None):
|
|||||||
original_supporters.append(s)
|
original_supporters.append(s)
|
||||||
application = managerform.save(commit=False)
|
application = managerform.save(commit=False)
|
||||||
elif application_id is None:
|
elif application_id is None:
|
||||||
application = Application(submitter=request.user)
|
application = Application(submitter=request.user.profile)
|
||||||
application.title = dataform.cleaned_data['title']
|
application.title = dataform.cleaned_data['title']
|
||||||
application.text = dataform.cleaned_data['text']
|
application.text = dataform.cleaned_data['text']
|
||||||
application.reason = dataform.cleaned_data['reason']
|
application.reason = dataform.cleaned_data['reason']
|
||||||
@ -278,12 +278,14 @@ def edit(request, application_id=None):
|
|||||||
dataform = formclass(initial=initial, prefix="data")
|
dataform = formclass(initial=initial, prefix="data")
|
||||||
if is_manager:
|
if is_manager:
|
||||||
if application_id is None:
|
if application_id is None:
|
||||||
initial = {'submitter': str(request.user.id)}
|
try:
|
||||||
else:
|
initial = {'submitter': request.user.profile.uid}
|
||||||
|
except Profile.DoesNotExist:
|
||||||
initial = {}
|
initial = {}
|
||||||
managerform = managerformclass(initial=initial, \
|
else:
|
||||||
instance=application, \
|
initial = {'submitter': application.submitter.uid}
|
||||||
prefix="manager")
|
managerform = managerformclass(initial=initial,
|
||||||
|
instance=application, prefix="manager")
|
||||||
else:
|
else:
|
||||||
managerform = None
|
managerform = None
|
||||||
return {
|
return {
|
||||||
|
@ -10,19 +10,22 @@
|
|||||||
:license: GNU GPL, see LICENSE for more details.
|
:license: GNU GPL, see LICENSE for more details.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User, Group
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
from django.utils.translation import ugettext_lazy as _, ugettext_noop
|
from django.utils.translation import ugettext_lazy as _, ugettext_noop
|
||||||
|
|
||||||
|
from openslides.utils.user import UserMixin
|
||||||
|
from openslides.utils.user.signals import receiv_users
|
||||||
|
|
||||||
from openslides.config.signals import default_config_value
|
from openslides.config.signals import default_config_value
|
||||||
|
|
||||||
from openslides.participant.api import gen_password
|
from openslides.participant.api import gen_password
|
||||||
|
|
||||||
|
|
||||||
|
class Profile(models.Model, UserMixin):
|
||||||
class Profile(models.Model):
|
user_prefix = 'participant'
|
||||||
GENDER_CHOICES = (
|
GENDER_CHOICES = (
|
||||||
('male', _('Male')),
|
('male', _('Male')),
|
||||||
('female', _('Female')),
|
('female', _('Female')),
|
||||||
@ -86,6 +89,42 @@ class Profile(models.Model):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class ParticipantUsers(object):
|
||||||
|
user_prefix = Profile.user_prefix
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
for profile in Profile.objects.all():
|
||||||
|
yield profile
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
return Profile.objects.get(pk=key)
|
||||||
|
|
||||||
|
|
||||||
|
class DjangoGroup(models.Model, UserMixin):
|
||||||
|
user_prefix = 'djangogroup'
|
||||||
|
|
||||||
|
group = models.OneToOneField(Group)
|
||||||
|
|
||||||
|
def __unicode__(self):
|
||||||
|
return unicode(self.group)
|
||||||
|
|
||||||
|
|
||||||
|
class DjangoGroupUsers(object):
|
||||||
|
user_prefix = DjangoGroup.user_prefix
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
for group in DjangoGroup.objects.all():
|
||||||
|
yield group
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
return DjangoGroup.objects.get(pk=key)
|
||||||
|
|
||||||
|
|
||||||
|
@receiver(receiv_users, dispatch_uid="participant_profile")
|
||||||
|
def receiv_users(sender, **kwargs):
|
||||||
|
return [ParticipantUsers(), DjangoGroupUsers()]
|
||||||
|
|
||||||
|
|
||||||
@receiver(default_config_value, dispatch_uid="participant_default_config")
|
@receiver(default_config_value, dispatch_uid="participant_default_config")
|
||||||
def default_config(sender, key, **kwargs):
|
def default_config(sender, key, **kwargs):
|
||||||
"""
|
"""
|
||||||
|
141
openslides/utils/user/__init__.py
Normal file
141
openslides/utils/user/__init__.py
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
openslides.utils.user.api
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Useful functions for the user object.
|
||||||
|
|
||||||
|
:copyright: 2011, 2012 by OpenSlides team, see AUTHORS.
|
||||||
|
:license: GNU GPL, see LICENSE for more details.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from django.db import models
|
||||||
|
from django import forms
|
||||||
|
|
||||||
|
from openslides.utils.user.signals import receiv_users
|
||||||
|
|
||||||
|
|
||||||
|
class UserChoices():
|
||||||
|
def __init__(self, field):
|
||||||
|
self.field = field
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
if self.field.empty_label is not None:
|
||||||
|
yield (u"", self.field.empty_label)
|
||||||
|
for user in Users():
|
||||||
|
yield (user.uid, user)
|
||||||
|
|
||||||
|
|
||||||
|
class UserFormField(forms.fields.ChoiceField):
|
||||||
|
def __init__(self, required=True, initial=None, empty_label=u"---------", *args, **kwargs):
|
||||||
|
|
||||||
|
if required and (initial is not None):
|
||||||
|
self.empty_label = None
|
||||||
|
else:
|
||||||
|
self.empty_label = empty_label
|
||||||
|
#TODO use a own self.choices property, see ModelChoiceField
|
||||||
|
super(UserFormField, self).__init__(choices=UserChoices(field=self), required=required, initial=initial, *args, **kwargs)
|
||||||
|
|
||||||
|
def to_python(self, value):
|
||||||
|
print value
|
||||||
|
return get_user(value)
|
||||||
|
|
||||||
|
def valid_value(self, value):
|
||||||
|
return super(UserFormField, self).valid_value(value.uid)
|
||||||
|
|
||||||
|
|
||||||
|
class UserField(models.fields.Field):
|
||||||
|
__metaclass__ = models.SubfieldBase
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super(UserField, self).__init__(max_length=255, *args, **kwargs)
|
||||||
|
# TODO: Validate the uid
|
||||||
|
|
||||||
|
def get_internal_type(self):
|
||||||
|
return "CharField"
|
||||||
|
|
||||||
|
def to_python(self, value):
|
||||||
|
"""
|
||||||
|
Convert string value to a User Object.
|
||||||
|
"""
|
||||||
|
if hasattr(value, 'uid'):
|
||||||
|
user = value
|
||||||
|
else:
|
||||||
|
user = get_user(value)
|
||||||
|
|
||||||
|
user.prepare_database_save = lambda unused: UserField().get_prep_value(user)
|
||||||
|
return user
|
||||||
|
|
||||||
|
def get_prep_value(self, value):
|
||||||
|
return value.uid
|
||||||
|
|
||||||
|
def value_to_string(self, obj):
|
||||||
|
value = self._get_val_from_obj(obj)
|
||||||
|
return self.get_prep_value(value)
|
||||||
|
|
||||||
|
def formfield(self, **kwargs):
|
||||||
|
defaults = {'form_class': UserFormField}
|
||||||
|
defaults.update(kwargs)
|
||||||
|
return super(UserField, self).formfield(**defaults)
|
||||||
|
|
||||||
|
|
||||||
|
def split_uid(uid):
|
||||||
|
data = uid.split(':', 1)
|
||||||
|
if len(data) == 2 and data[0] and data[1]:
|
||||||
|
return data
|
||||||
|
raise TypeError("Invalid uid: '%s'" % uid)
|
||||||
|
|
||||||
|
|
||||||
|
def get_user(uid):
|
||||||
|
try:
|
||||||
|
user_type, id = split_uid(uid)
|
||||||
|
except TypeError:
|
||||||
|
return EmtyUser()
|
||||||
|
|
||||||
|
for receiver, users_list in receiv_users.send(sender='get_user'):
|
||||||
|
for users in users_list:
|
||||||
|
if users.user_prefix == user_type:
|
||||||
|
return users[id]
|
||||||
|
# TODO: Use own Exception
|
||||||
|
raise Exception('User with uid %s does not exist' % uid)
|
||||||
|
|
||||||
|
|
||||||
|
class Users(object):
|
||||||
|
"""
|
||||||
|
A Storage for a multiplicity of different User-Objects.
|
||||||
|
"""
|
||||||
|
def __iter__(self):
|
||||||
|
for receiver, users_list in receiv_users.send(sender='users'):
|
||||||
|
for users in users_list:
|
||||||
|
# Does iter(users) work?
|
||||||
|
print users
|
||||||
|
for user in users:
|
||||||
|
print user
|
||||||
|
yield user
|
||||||
|
|
||||||
|
|
||||||
|
def generate_uid(prefix, id):
|
||||||
|
if ':' in prefix:
|
||||||
|
# TODO: Use the right Error.
|
||||||
|
raise Exception("':' is not allowed in a the 'user_prefix'")
|
||||||
|
return "%s:%d" % (prefix, id)
|
||||||
|
|
||||||
|
|
||||||
|
# TODO: I dont need this object any more
|
||||||
|
class UserMixin(object):
|
||||||
|
@property
|
||||||
|
def uid(self):
|
||||||
|
try:
|
||||||
|
return generate_uid(self.user_prefix, self.id)
|
||||||
|
except AttributeError:
|
||||||
|
raise AttributeError("%s has to have a attribute 'user_prefix'" % self)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return 'User: %s' % self.uid
|
||||||
|
|
||||||
|
|
||||||
|
class EmtyUser(UserMixin):
|
||||||
|
@property
|
||||||
|
def uid(self):
|
||||||
|
return 'emtyuser'
|
15
openslides/utils/user/signals.py
Normal file
15
openslides/utils/user/signals.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
openslides.utils.user.signals
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
Defines Signals for the user.
|
||||||
|
|
||||||
|
:copyright: 2011, 2012 by OpenSlides team, see AUTHORS.
|
||||||
|
:license: GNU GPL, see LICENSE for more details.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from django.dispatch import Signal
|
||||||
|
|
||||||
|
receiv_users = Signal(providing_args=[])
|
Loading…
Reference in New Issue
Block a user