2015-02-12 20:57:05 +01:00
|
|
|
from random import choice
|
2014-10-11 14:34:49 +02:00
|
|
|
|
2015-02-12 20:57:05 +01:00
|
|
|
from django.contrib.auth.hashers import make_password
|
2015-09-16 00:55:27 +02:00
|
|
|
from django.contrib.auth.models import (
|
2015-02-12 20:57:05 +01:00
|
|
|
AbstractBaseUser,
|
|
|
|
BaseUserManager,
|
2015-02-12 22:42:54 +01:00
|
|
|
Group,
|
2015-06-16 10:37:23 +02:00
|
|
|
PermissionsMixin,
|
2015-02-12 22:42:54 +01:00
|
|
|
)
|
2014-10-11 14:34:49 +02:00
|
|
|
from django.db import models
|
|
|
|
from django.utils.translation import ugettext_lazy, ugettext_noop
|
|
|
|
|
2015-09-16 00:55:27 +02:00
|
|
|
from ..core.config import config
|
|
|
|
from ..utils.models import RESTModelMixin
|
|
|
|
from .exceptions import UsersError
|
2015-02-12 20:57:05 +01:00
|
|
|
|
2014-10-11 14:34:49 +02:00
|
|
|
|
|
|
|
class UserManager(BaseUserManager):
|
2015-01-22 18:29:12 +01:00
|
|
|
"""
|
2015-09-16 00:55:27 +02:00
|
|
|
Customized manager that creates new users only with a password and a
|
|
|
|
username.
|
2015-01-22 18:29:12 +01:00
|
|
|
"""
|
2014-10-11 14:34:49 +02:00
|
|
|
def create_user(self, username, password, **kwargs):
|
2015-09-16 00:55:27 +02:00
|
|
|
"""
|
|
|
|
Creates a new user only with a password and a username.
|
|
|
|
"""
|
2014-10-11 14:34:49 +02:00
|
|
|
user = self.model(username=username, **kwargs)
|
|
|
|
user.set_password(password)
|
|
|
|
user.save(using=self._db)
|
|
|
|
return user
|
|
|
|
|
2015-02-12 20:57:05 +01:00
|
|
|
def create_or_reset_admin_user(self):
|
|
|
|
"""
|
2015-09-16 00:55:27 +02:00
|
|
|
Creates an user with the username 'admin'. If such a user already
|
|
|
|
exists, resets it. The password is (re)set to 'admin'. The user
|
|
|
|
becomes member of the group 'Staff' (pk=4).
|
2015-02-12 20:57:05 +01:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
staff = Group.objects.get(pk=4)
|
|
|
|
except Group.DoesNotExist:
|
2015-09-16 00:55:27 +02:00
|
|
|
raise UsersError("Admin user can not be created or reset because "
|
|
|
|
"the group 'Staff' (pk=4) is not available.")
|
2015-02-12 20:57:05 +01:00
|
|
|
admin, created = self.get_or_create(
|
|
|
|
username='admin',
|
|
|
|
defaults={'last_name': 'Administrator'})
|
|
|
|
admin.default_password = 'admin'
|
|
|
|
admin.password = make_password(admin.default_password, '', 'md5')
|
|
|
|
admin.save()
|
|
|
|
admin.groups.add(staff)
|
|
|
|
return created
|
|
|
|
|
|
|
|
def generate_username(self, first_name, last_name):
|
|
|
|
"""
|
|
|
|
Generates a username from first name and last name.
|
|
|
|
"""
|
|
|
|
first_name = first_name.strip()
|
|
|
|
last_name = last_name.strip()
|
|
|
|
|
|
|
|
if first_name and last_name:
|
|
|
|
base_name = ' '.join((first_name, last_name))
|
|
|
|
else:
|
|
|
|
base_name = first_name or last_name
|
|
|
|
if not base_name:
|
|
|
|
raise ValueError("Either 'first_name' or 'last_name' must not be "
|
2015-09-16 00:55:27 +02:00
|
|
|
"empty.")
|
2015-02-12 20:57:05 +01:00
|
|
|
|
|
|
|
if not self.filter(username=base_name).exists():
|
|
|
|
generated_username = base_name
|
|
|
|
else:
|
|
|
|
counter = 0
|
|
|
|
while True:
|
|
|
|
counter += 1
|
|
|
|
test_name = '%s %d' % (base_name, counter)
|
|
|
|
if not self.filter(username=test_name).exists():
|
|
|
|
generated_username = test_name
|
|
|
|
break
|
|
|
|
|
|
|
|
return generated_username
|
|
|
|
|
|
|
|
def generate_password(self):
|
|
|
|
"""
|
2015-09-16 00:55:27 +02:00
|
|
|
Generates a random passwort. Do not use l, o, I, O, 1 or 0.
|
2015-02-12 20:57:05 +01:00
|
|
|
"""
|
|
|
|
chars = 'abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789'
|
|
|
|
size = 8
|
|
|
|
return ''.join([choice(chars) for i in range(size)])
|
|
|
|
|
2014-10-11 14:34:49 +02:00
|
|
|
|
2015-06-29 13:31:07 +02:00
|
|
|
class User(RESTModelMixin, PermissionsMixin, AbstractBaseUser):
|
2015-02-12 20:57:05 +01:00
|
|
|
"""
|
2015-09-16 00:55:27 +02:00
|
|
|
Model for users in OpenSlides. A client can login as an user with
|
|
|
|
credentials. An user can also just be used as representation for a person
|
|
|
|
in other OpenSlides apps like motion submitter or (assignment) election
|
2015-02-12 20:57:05 +01:00
|
|
|
candidates.
|
|
|
|
"""
|
2014-10-11 14:34:49 +02:00
|
|
|
USERNAME_FIELD = 'username'
|
|
|
|
|
|
|
|
username = models.CharField(
|
2015-09-16 00:55:27 +02:00
|
|
|
ugettext_lazy('Username'),
|
|
|
|
max_length=255,
|
|
|
|
unique=True,
|
|
|
|
blank=True)
|
2014-10-11 14:34:49 +02:00
|
|
|
|
|
|
|
first_name = models.CharField(
|
2015-09-16 00:55:27 +02:00
|
|
|
ugettext_lazy('First name'),
|
|
|
|
max_length=255,
|
|
|
|
blank=True)
|
2014-10-11 14:34:49 +02:00
|
|
|
|
|
|
|
last_name = models.CharField(
|
2015-09-16 00:55:27 +02:00
|
|
|
ugettext_lazy('Last name'),
|
|
|
|
max_length=255,
|
|
|
|
blank=True)
|
|
|
|
|
|
|
|
# TODO: Try to remove the default argument in the following fields.
|
2014-10-11 14:34:49 +02:00
|
|
|
|
|
|
|
structure_level = models.CharField(
|
2015-09-16 00:55:27 +02:00
|
|
|
ugettext_lazy('Structure level'),
|
|
|
|
max_length=255,
|
|
|
|
blank=True,
|
|
|
|
default='',
|
2014-10-11 14:34:49 +02:00
|
|
|
help_text=ugettext_lazy('Will be shown after the name.'))
|
|
|
|
|
|
|
|
title = models.CharField(
|
2015-09-16 00:55:27 +02:00
|
|
|
ugettext_lazy('Title'),
|
|
|
|
max_length=50,
|
|
|
|
blank=True,
|
|
|
|
default='',
|
2014-10-11 14:34:49 +02:00
|
|
|
help_text=ugettext_lazy('Will be shown before the name.'))
|
|
|
|
|
|
|
|
about_me = models.TextField(
|
2015-09-16 00:55:27 +02:00
|
|
|
ugettext_lazy('About me'),
|
|
|
|
blank=True,
|
|
|
|
default='',
|
|
|
|
help_text=ugettext_lazy('Profile text.'))
|
2014-10-11 14:34:49 +02:00
|
|
|
|
|
|
|
comment = models.TextField(
|
2015-09-16 00:55:27 +02:00
|
|
|
ugettext_lazy('Comment'),
|
|
|
|
blank=True,
|
|
|
|
default='',
|
2014-10-11 14:34:49 +02:00
|
|
|
help_text=ugettext_lazy('Only for notes.'))
|
|
|
|
|
|
|
|
default_password = models.CharField(
|
2015-09-16 00:55:27 +02:00
|
|
|
ugettext_lazy('Default password'),
|
|
|
|
max_length=100,
|
|
|
|
blank=True,
|
|
|
|
default='')
|
2014-10-11 14:34:49 +02:00
|
|
|
|
|
|
|
is_active = models.BooleanField(
|
2015-09-16 00:55:27 +02:00
|
|
|
ugettext_lazy('Active'),
|
|
|
|
default=True,
|
2014-10-11 14:34:49 +02:00
|
|
|
help_text=ugettext_lazy(
|
|
|
|
'Designates whether this user should be treated as '
|
2015-09-16 00:55:27 +02:00
|
|
|
'active. Unselect this instead of deleting the account.'))
|
2014-10-11 14:34:49 +02:00
|
|
|
|
|
|
|
is_present = models.BooleanField(
|
2015-09-16 00:55:27 +02:00
|
|
|
ugettext_lazy('Present'),
|
|
|
|
default=False,
|
|
|
|
help_text=ugettext_lazy(
|
|
|
|
'Designates whether this user is in the room or not.'))
|
2014-10-11 14:34:49 +02:00
|
|
|
|
|
|
|
objects = UserManager()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
permissions = (
|
2015-01-17 14:25:05 +01:00
|
|
|
('can_see_name', ugettext_noop('Can see names of users')),
|
|
|
|
('can_see_extra_data', ugettext_noop('Can see extra data of users')),
|
2014-10-11 14:34:49 +02:00
|
|
|
('can_manage', ugettext_noop('Can manage users')),
|
|
|
|
)
|
2015-09-16 00:55:27 +02:00
|
|
|
ordering = ('last_name', 'first_name', 'username', )
|
2014-10-11 14:34:49 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.get_full_name()
|
|
|
|
|
|
|
|
def get_full_name(self):
|
|
|
|
"""
|
|
|
|
Returns a long form of the name.
|
|
|
|
|
|
|
|
E. g.: * Dr. Max Mustermann (Villingen)
|
|
|
|
* Professor Dr. Enders, Christoph (Leipzig)
|
|
|
|
"""
|
2015-01-22 18:29:12 +01:00
|
|
|
structure = '(%s)' % self.structure_level if self.structure_level else ''
|
2014-10-11 14:34:49 +02:00
|
|
|
return ' '.join((self.title, self.get_short_name(), structure)).strip()
|
|
|
|
|
|
|
|
def get_short_name(self):
|
|
|
|
"""
|
|
|
|
Returns only the name of the user.
|
|
|
|
|
|
|
|
E. g.: * Max Mustermann
|
|
|
|
* Enders, Christoph
|
|
|
|
"""
|
2015-01-22 18:29:12 +01:00
|
|
|
# Strip white spaces from the name parts
|
|
|
|
first_name = self.first_name.strip()
|
|
|
|
last_name = self.last_name.strip()
|
|
|
|
|
|
|
|
# The user has a last_name and a first_name
|
|
|
|
if first_name and last_name:
|
|
|
|
if config['users_sort_users_by_first_name']:
|
|
|
|
name = ' '.join((first_name, last_name))
|
|
|
|
else:
|
|
|
|
name = ', '.join((last_name, first_name))
|
|
|
|
|
|
|
|
# The user has only a first_name or a last_name or no name
|
|
|
|
else:
|
|
|
|
name = first_name or last_name or self.username
|
2014-10-11 14:34:49 +02:00
|
|
|
|
2015-09-16 00:55:27 +02:00
|
|
|
# Return result
|
|
|
|
return name
|
2015-06-24 22:11:54 +02:00
|
|
|
|
|
|
|
def get_view_class(self):
|
|
|
|
"""
|
|
|
|
Returns the main view class (viewset class) that should be unlocked
|
|
|
|
if the user (means its name) appears on a slide.
|
|
|
|
"""
|
|
|
|
from .views import UserViewSet
|
|
|
|
return UserViewSet
|