mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-11-30 08:32:26 +00:00
Introduce custom user model, pep8 code cleaning
This commit is contained in:
parent
418685c083
commit
08d17a9642
5 changed files with 364 additions and 57 deletions
|
@ -28,6 +28,8 @@ PASSWORD_HASHERS = (
|
||||||
'django.contrib.auth.hashers.PBKDF2PasswordHasher',
|
'django.contrib.auth.hashers.PBKDF2PasswordHasher',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
AUTH_USER_MODEL = 'users.User'
|
||||||
|
|
||||||
|
|
||||||
# Application definition
|
# Application definition
|
||||||
|
|
||||||
|
|
|
@ -1,28 +1,79 @@
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
from django.contrib.auth.models import Group
|
||||||
|
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
|
||||||
|
|
||||||
from .models import User, School, Right, ListRight, Ban, Whitelist
|
from .models import User, School, Right, ListRight, Ban, Whitelist
|
||||||
|
from .forms import UserChangeForm, UserCreationForm
|
||||||
|
|
||||||
|
|
||||||
class UserAdmin(admin.ModelAdmin):
|
class UserAdmin(admin.ModelAdmin):
|
||||||
list_display = ('name','surname','pseudo','room','email', 'school', 'state')
|
list_display = (
|
||||||
|
'name',
|
||||||
|
'surname',
|
||||||
|
'pseudo',
|
||||||
|
'room',
|
||||||
|
'email',
|
||||||
|
'school',
|
||||||
|
'state'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class SchoolAdmin(admin.ModelAdmin):
|
class SchoolAdmin(admin.ModelAdmin):
|
||||||
list_display = ('name',)
|
list_display = ('name',)
|
||||||
|
|
||||||
|
|
||||||
class ListRightAdmin(admin.ModelAdmin):
|
class ListRightAdmin(admin.ModelAdmin):
|
||||||
list_display = ('listright',)
|
list_display = ('listright',)
|
||||||
|
|
||||||
|
|
||||||
class RightAdmin(admin.ModelAdmin):
|
class RightAdmin(admin.ModelAdmin):
|
||||||
list_display = ('user', 'right')
|
list_display = ('user', 'right')
|
||||||
|
|
||||||
|
|
||||||
class BanAdmin(admin.ModelAdmin):
|
class BanAdmin(admin.ModelAdmin):
|
||||||
list_display = ('user', 'raison', 'date_start', 'date_end')
|
list_display = ('user', 'raison', 'date_start', 'date_end')
|
||||||
|
|
||||||
|
|
||||||
class WhitelistAdmin(admin.ModelAdmin):
|
class WhitelistAdmin(admin.ModelAdmin):
|
||||||
list_display = ('user', 'raison', 'date_start', 'date_end')
|
list_display = ('user', 'raison', 'date_start', 'date_end')
|
||||||
|
|
||||||
|
|
||||||
|
class UserAdmin(BaseUserAdmin):
|
||||||
|
# The forms to add and change user instances
|
||||||
|
form = UserChangeForm
|
||||||
|
add_form = UserCreationForm
|
||||||
|
|
||||||
|
# The fields to be used in displaying the User model.
|
||||||
|
# These override the definitions on the base UserAdmin
|
||||||
|
# that reference specific fields on auth.User.
|
||||||
|
list_display = ('pseudo', 'name', 'surname', 'email', 'school', 'is_admin')
|
||||||
|
list_filter = ()
|
||||||
|
fieldsets = (
|
||||||
|
(None, {'fields': ('pseudo', 'password')}),
|
||||||
|
('Personal info', {'fields': ('name', 'surname', 'email', 'school')}),
|
||||||
|
('Permissions', {'fields': ('is_admin', )}),
|
||||||
|
)
|
||||||
|
# add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
|
||||||
|
# overrides get_fieldsets to use this attribute when creating a user.
|
||||||
|
add_fieldsets = (
|
||||||
|
(None, {
|
||||||
|
'classes': ('wide',),
|
||||||
|
'fields': ('pseudo', 'name', 'surname', 'email', 'school', 'is_admin', 'password1', 'password2')}
|
||||||
|
),
|
||||||
|
)
|
||||||
|
search_fields = ('pseudo',)
|
||||||
|
ordering = ('pseudo',)
|
||||||
|
filter_horizontal = ()
|
||||||
|
|
||||||
admin.site.register(User, UserAdmin)
|
admin.site.register(User, UserAdmin)
|
||||||
admin.site.register(School, SchoolAdmin)
|
admin.site.register(School, SchoolAdmin)
|
||||||
admin.site.register(Right, RightAdmin)
|
admin.site.register(Right, RightAdmin)
|
||||||
admin.site.register(ListRight, ListRightAdmin)
|
admin.site.register(ListRight, ListRightAdmin)
|
||||||
admin.site.register(Ban, BanAdmin)
|
admin.site.register(Ban, BanAdmin)
|
||||||
admin.site.register(Whitelist, WhitelistAdmin)
|
admin.site.register(Whitelist, WhitelistAdmin)
|
||||||
|
# Now register the new UserAdmin...
|
||||||
|
admin.site.unregister(User)
|
||||||
|
admin.site.register(User, UserAdmin)
|
||||||
|
# ... and, since we're not using Django's built-in permissions,
|
||||||
|
# unregister the Group model from admin.
|
||||||
|
admin.site.unregister(Group)
|
||||||
|
|
|
@ -2,8 +2,71 @@
|
||||||
|
|
||||||
|
|
||||||
from django import forms
|
from django import forms
|
||||||
|
from django.contrib.auth.forms import ReadOnlyPasswordHashField
|
||||||
|
|
||||||
|
from .models import User, get_admin_right
|
||||||
|
|
||||||
|
|
||||||
class PassForm(forms.Form):
|
class PassForm(forms.Form):
|
||||||
passwd1 = forms.CharField(label=u'Nouveau mot de passe', max_length=255, widget=forms.PasswordInput)
|
passwd1 = forms.CharField(label=u'Nouveau mot de passe', max_length=255, widget=forms.PasswordInput)
|
||||||
passwd2 = forms.CharField(label=u'Saisir à nouveau le mot de passe', max_length=255, widget=forms.PasswordInput)
|
passwd2 = forms.CharField(label=u'Saisir à nouveau le mot de passe', max_length=255, widget=forms.PasswordInput)
|
||||||
|
|
||||||
|
|
||||||
|
class UserCreationForm(forms.ModelForm):
|
||||||
|
"""A form for creating new users. Includes all the required
|
||||||
|
fields, plus a repeated password."""
|
||||||
|
password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
|
||||||
|
password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
|
||||||
|
is_admin = forms.BooleanField(label='is admin')
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = User
|
||||||
|
fields = ('pseudo', 'name', 'surname', 'email')
|
||||||
|
|
||||||
|
def clean_password2(self):
|
||||||
|
# Check that the two password entries match
|
||||||
|
password1 = self.cleaned_data.get("password1")
|
||||||
|
password2 = self.cleaned_data.get("password2")
|
||||||
|
if password1 and password2 and password1 != password2:
|
||||||
|
raise forms.ValidationError("Passwords don't match")
|
||||||
|
return password2
|
||||||
|
|
||||||
|
def save(self, commit=True):
|
||||||
|
# Save the provided password in hashed format
|
||||||
|
user = super(UserCreationForm, self).save(commit=False)
|
||||||
|
user.set_password(self.cleaned_data["password1"])
|
||||||
|
user.save()
|
||||||
|
user.is_admin = self.cleaned_data.get("is_admin")
|
||||||
|
return user
|
||||||
|
|
||||||
|
|
||||||
|
class UserChangeForm(forms.ModelForm):
|
||||||
|
"""A form for updating users. Includes all the fields on
|
||||||
|
the user, but replaces the password field with admin's
|
||||||
|
password hash display field.
|
||||||
|
"""
|
||||||
|
password = ReadOnlyPasswordHashField()
|
||||||
|
is_admin = forms.BooleanField(label='is admin', required=False)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = User
|
||||||
|
fields = ('pseudo', 'password', 'name', 'surname', 'email')
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super(UserChangeForm, self).__init__(*args, **kwargs)
|
||||||
|
print("User is admin : %s" % kwargs['instance'].is_admin)
|
||||||
|
self.initial['is_admin'] = kwargs['instance'].is_admin
|
||||||
|
|
||||||
|
def clean_password(self):
|
||||||
|
# Regardless of what the user provides, return the initial value.
|
||||||
|
# This is done here, rather than on the field, because the
|
||||||
|
# field does not have access to the initial value
|
||||||
|
return self.initial["password"]
|
||||||
|
|
||||||
|
def save(self, commit=True):
|
||||||
|
# Save the provided password in hashed format
|
||||||
|
user = super(UserChangeForm, self).save(commit=False)
|
||||||
|
user.is_admin = self.cleaned_data.get("is_admin")
|
||||||
|
if commit:
|
||||||
|
user.save()
|
||||||
|
return user
|
||||||
|
|
146
users/models.py
146
users/models.py
|
@ -4,9 +4,11 @@ from django import forms
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
|
||||||
|
|
||||||
from topologie.models import Room
|
from topologie.models import Room
|
||||||
|
|
||||||
|
|
||||||
def remove_user_room(room):
|
def remove_user_room(room):
|
||||||
""" Déménage de force l'ancien locataire de la chambre """
|
""" Déménage de force l'ancien locataire de la chambre """
|
||||||
try:
|
try:
|
||||||
|
@ -16,16 +18,67 @@ def remove_user_room(room):
|
||||||
user.room = None
|
user.room = None
|
||||||
user.save()
|
user.save()
|
||||||
|
|
||||||
def linux_user_validator(login):
|
|
||||||
|
def linux_user_check(login):
|
||||||
""" Validation du pseudo pour respecter les contraintes unix"""
|
""" Validation du pseudo pour respecter les contraintes unix"""
|
||||||
UNIX_LOGIN_PATTERN = re.compile("^[a-z_][a-z0-9_-]*[$]?$")
|
UNIX_LOGIN_PATTERN = re.compile("^[a-z_][a-z0-9_-]*[$]?$")
|
||||||
if not UNIX_LOGIN_PATTERN.match(login):
|
return UNIX_LOGIN_PATTERN.match(login)
|
||||||
|
|
||||||
|
|
||||||
|
def linux_user_validator(login):
|
||||||
|
if not linux_user_check(login):
|
||||||
raise forms.ValidationError(
|
raise forms.ValidationError(
|
||||||
", ce pseudo ('%(label)s') contient des carractères interdits",
|
", ce pseudo ('%(label)s') contient des carractères interdits",
|
||||||
params={'label': login},
|
params={'label': login},
|
||||||
)
|
)
|
||||||
|
|
||||||
class User(models.Model):
|
|
||||||
|
def get_admin_right():
|
||||||
|
try:
|
||||||
|
admin_right = ListRight.objects.get(listright="admin")
|
||||||
|
except ListRight.DoesNotExist:
|
||||||
|
admin_right = ListRight(listright="admin")
|
||||||
|
admin_right.save()
|
||||||
|
return admin_right
|
||||||
|
|
||||||
|
|
||||||
|
class UserManager(BaseUserManager):
|
||||||
|
def _create_user(self, pseudo, name, surname, email, password=None, su=False):
|
||||||
|
if not pseudo:
|
||||||
|
raise ValueError('Users must have an username')
|
||||||
|
|
||||||
|
if not linux_user_check(pseudo):
|
||||||
|
raise ValueError('Username shall only contain [a-z0-9_-]')
|
||||||
|
|
||||||
|
user = self.model(
|
||||||
|
pseudo=pseudo,
|
||||||
|
name=name,
|
||||||
|
surname=surname,
|
||||||
|
email=self.normalize_email(email),
|
||||||
|
)
|
||||||
|
|
||||||
|
user.set_password(password)
|
||||||
|
user.save(using=self._db)
|
||||||
|
if su:
|
||||||
|
user.make_admin()
|
||||||
|
return user
|
||||||
|
|
||||||
|
def create_user(self, pseudo, name, surname, email, password=None):
|
||||||
|
"""
|
||||||
|
Creates and saves a User with the given pseudo, name, surname, email,
|
||||||
|
and password.
|
||||||
|
"""
|
||||||
|
return self._create_user(pseudo, name, surname, email, password, False)
|
||||||
|
|
||||||
|
def create_superuser(self, pseudo, name, surname, email, password):
|
||||||
|
"""
|
||||||
|
Creates and saves a superuser with the given pseudo, name, surname,
|
||||||
|
email, and password.
|
||||||
|
"""
|
||||||
|
return self._create_user(pseudo, name, surname, email, password, True)
|
||||||
|
|
||||||
|
|
||||||
|
class User(AbstractBaseUser):
|
||||||
STATE_ACTIVE = 0
|
STATE_ACTIVE = 0
|
||||||
STATE_DEACTIVATED = 1
|
STATE_DEACTIVATED = 1
|
||||||
STATE_ARCHIVED = 2
|
STATE_ARCHIVED = 2
|
||||||
|
@ -42,14 +95,68 @@ class User(models.Model):
|
||||||
school = models.ForeignKey('School', on_delete=models.PROTECT, null=False, blank=False)
|
school = models.ForeignKey('School', on_delete=models.PROTECT, null=False, blank=False)
|
||||||
comment = models.CharField(help_text="Commentaire, promo", max_length=255, blank=True)
|
comment = models.CharField(help_text="Commentaire, promo", max_length=255, blank=True)
|
||||||
room = models.OneToOneField('topologie.Room', on_delete=models.PROTECT, blank=True, null=True)
|
room = models.OneToOneField('topologie.Room', on_delete=models.PROTECT, blank=True, null=True)
|
||||||
pwd_ssha = models.CharField(max_length=255)
|
|
||||||
pwd_ntlm = models.CharField(max_length=255)
|
pwd_ntlm = models.CharField(max_length=255)
|
||||||
state = models.IntegerField(choices=STATES, default=STATE_ACTIVE)
|
state = models.IntegerField(choices=STATES, default=STATE_ACTIVE)
|
||||||
registered = models.DateTimeField(auto_now_add=True)
|
registered = models.DateTimeField(auto_now_add=True)
|
||||||
|
|
||||||
|
USERNAME_FIELD = 'pseudo'
|
||||||
|
REQUIRED_FIELDS = ['name', 'surname', 'email']
|
||||||
|
|
||||||
|
objects = UserManager()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_active(self):
|
||||||
|
return self.state == self.STATE_ACTIVE
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_staff(self):
|
||||||
|
return self.is_admin
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_admin(self):
|
||||||
|
try:
|
||||||
|
Right.objects.get(user=self, right__listright='admin')
|
||||||
|
except Right.DoesNotExist:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
@is_admin.setter
|
||||||
|
def is_admin(self, value):
|
||||||
|
if value and not self.is_admin:
|
||||||
|
self.make_admin()
|
||||||
|
elif not value and self.is_admin:
|
||||||
|
self.un_admin()
|
||||||
|
|
||||||
|
def get_full_name(self):
|
||||||
|
return '%s %s' % (self.name, self.surname)
|
||||||
|
|
||||||
|
def get_short_name(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
def has_perm(self, perm, obj=None):
|
||||||
|
# Simplest version
|
||||||
|
return True
|
||||||
|
|
||||||
|
def has_module_perms(self, app_label):
|
||||||
|
# Simplest version again
|
||||||
|
return True
|
||||||
|
|
||||||
|
def make_admin(self):
|
||||||
|
""" Make User admin """
|
||||||
|
user_admin_right = Right(user=self, right=get_admin_right())
|
||||||
|
user_admin_right.save()
|
||||||
|
|
||||||
|
def un_admin(self):
|
||||||
|
try:
|
||||||
|
user_right = Right.objects.get(user=self,right=get_admin_right())
|
||||||
|
except Right.DoesNotExist:
|
||||||
|
return
|
||||||
|
user_right.delete()
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.pseudo
|
return self.pseudo
|
||||||
|
|
||||||
|
|
||||||
class Right(models.Model):
|
class Right(models.Model):
|
||||||
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
||||||
right = models.ForeignKey('ListRight', on_delete=models.PROTECT)
|
right = models.ForeignKey('ListRight', on_delete=models.PROTECT)
|
||||||
|
@ -60,18 +167,21 @@ class Right(models.Model):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(self.user) + " - " + str(self.right)
|
return str(self.user) + " - " + str(self.right)
|
||||||
|
|
||||||
|
|
||||||
class School(models.Model):
|
class School(models.Model):
|
||||||
name = models.CharField(max_length=255)
|
name = models.CharField(max_length=255)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
class ListRight(models.Model):
|
class ListRight(models.Model):
|
||||||
listright = models.CharField(max_length=255)
|
listright = models.CharField(max_length=255, unique=True)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.listright
|
return self.listright
|
||||||
|
|
||||||
|
|
||||||
class Ban(models.Model):
|
class Ban(models.Model):
|
||||||
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
||||||
raison = models.CharField(max_length=255)
|
raison = models.CharField(max_length=255)
|
||||||
|
@ -81,6 +191,7 @@ class Ban(models.Model):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(self.user) + ' ' + str(self.raison)
|
return str(self.user) + ' ' + str(self.raison)
|
||||||
|
|
||||||
|
|
||||||
class Whitelist(models.Model):
|
class Whitelist(models.Model):
|
||||||
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
||||||
raison = models.CharField(max_length=255)
|
raison = models.CharField(max_length=255)
|
||||||
|
@ -90,6 +201,7 @@ class Whitelist(models.Model):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(self.user) + ' ' + str(self.raison)
|
return str(self.user) + ' ' + str(self.raison)
|
||||||
|
|
||||||
|
|
||||||
class InfoForm(ModelForm):
|
class InfoForm(ModelForm):
|
||||||
force = forms.BooleanField(label="Forcer le déménagement ?", initial=False, required=False)
|
force = forms.BooleanField(label="Forcer le déménagement ?", initial=False, required=False)
|
||||||
|
|
||||||
|
@ -110,22 +222,34 @@ class InfoForm(ModelForm):
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = User
|
model = User
|
||||||
fields = ['name','surname','pseudo','email', 'school', 'comment', 'room']
|
fields = [
|
||||||
|
'name',
|
||||||
|
'surname',
|
||||||
|
'pseudo',
|
||||||
|
'email',
|
||||||
|
'school',
|
||||||
|
'comment',
|
||||||
|
'room',
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
class UserForm(InfoForm):
|
class UserForm(InfoForm):
|
||||||
class Meta(InfoForm.Meta):
|
class Meta(InfoForm.Meta):
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
|
|
||||||
|
|
||||||
class PasswordForm(ModelForm):
|
class PasswordForm(ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = User
|
model = User
|
||||||
fields = ['pwd_ssha','pwd_ntlm']
|
fields = ['password', 'pwd_ntlm']
|
||||||
|
|
||||||
|
|
||||||
class StateForm(ModelForm):
|
class StateForm(ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = User
|
model = User
|
||||||
fields = ['state']
|
fields = ['state']
|
||||||
|
|
||||||
|
|
||||||
class SchoolForm(ModelForm):
|
class SchoolForm(ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = School
|
model = School
|
||||||
|
@ -135,6 +259,7 @@ class SchoolForm(ModelForm):
|
||||||
super(SchoolForm, self).__init__(*args, **kwargs)
|
super(SchoolForm, self).__init__(*args, **kwargs)
|
||||||
self.fields['name'].label = 'Établissement à ajouter'
|
self.fields['name'].label = 'Établissement à ajouter'
|
||||||
|
|
||||||
|
|
||||||
class DelSchoolForm(ModelForm):
|
class DelSchoolForm(ModelForm):
|
||||||
schools = forms.ModelMultipleChoiceField(queryset=School.objects.all(), label="Etablissements actuels", widget=forms.CheckboxSelectMultiple)
|
schools = forms.ModelMultipleChoiceField(queryset=School.objects.all(), label="Etablissements actuels", widget=forms.CheckboxSelectMultiple)
|
||||||
|
|
||||||
|
@ -142,6 +267,7 @@ class DelSchoolForm(ModelForm):
|
||||||
exclude = ['name']
|
exclude = ['name']
|
||||||
model = School
|
model = School
|
||||||
|
|
||||||
|
|
||||||
class RightForm(ModelForm):
|
class RightForm(ModelForm):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(RightForm, self).__init__(*args, **kwargs)
|
super(RightForm, self).__init__(*args, **kwargs)
|
||||||
|
@ -152,6 +278,7 @@ class RightForm(ModelForm):
|
||||||
model = Right
|
model = Right
|
||||||
fields = ['right']
|
fields = ['right']
|
||||||
|
|
||||||
|
|
||||||
class DelRightForm(ModelForm):
|
class DelRightForm(ModelForm):
|
||||||
rights = forms.ModelMultipleChoiceField(queryset=Right.objects.all(), label="Droits actuels", widget=forms.CheckboxSelectMultiple)
|
rights = forms.ModelMultipleChoiceField(queryset=Right.objects.all(), label="Droits actuels", widget=forms.CheckboxSelectMultiple)
|
||||||
|
|
||||||
|
@ -159,6 +286,7 @@ class DelRightForm(ModelForm):
|
||||||
model = Right
|
model = Right
|
||||||
exclude = ['user', 'right']
|
exclude = ['user', 'right']
|
||||||
|
|
||||||
|
|
||||||
class BanForm(ModelForm):
|
class BanForm(ModelForm):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(BanForm, self).__init__(*args, **kwargs)
|
super(BanForm, self).__init__(*args, **kwargs)
|
||||||
|
@ -174,6 +302,7 @@ class BanForm(ModelForm):
|
||||||
raise forms.ValidationError("Triple buse, la date de fin ne peut pas être avant maintenant... Re2o ne voyage pas dans le temps")
|
raise forms.ValidationError("Triple buse, la date de fin ne peut pas être avant maintenant... Re2o ne voyage pas dans le temps")
|
||||||
return date_end
|
return date_end
|
||||||
|
|
||||||
|
|
||||||
class WhitelistForm(ModelForm):
|
class WhitelistForm(ModelForm):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(WhitelistForm, self).__init__(*args, **kwargs)
|
super(WhitelistForm, self).__init__(*args, **kwargs)
|
||||||
|
@ -189,5 +318,6 @@ class WhitelistForm(ModelForm):
|
||||||
raise forms.ValidationError("Triple buse, la date de fin ne peut pas être avant maintenant... Re2o ne voyage pas dans le temps")
|
raise forms.ValidationError("Triple buse, la date de fin ne peut pas être avant maintenant... Re2o ne voyage pas dans le temps")
|
||||||
return date_end
|
return date_end
|
||||||
|
|
||||||
|
|
||||||
class ProfilForm(Form):
|
class ProfilForm(Form):
|
||||||
user =forms.CharField(label ='Ok', max_length=100)
|
user = forms.CharField(label='Ok', max_length=100)
|
||||||
|
|
147
users/views.py
147
users/views.py
|
@ -1,45 +1,52 @@
|
||||||
# App de gestion des users pour re2o
|
# App de gestion des users pour re2o
|
||||||
# Goulven Kermarec, Gabriel Détraz
|
# Goulven Kermarec, Gabriel Détraz
|
||||||
# Gplv2
|
# Gplv2
|
||||||
from django.shortcuts import render, redirect
|
from django.shortcuts import render_to_response, render, redirect
|
||||||
from django.shortcuts import render_to_response, get_object_or_404
|
|
||||||
from django.core.context_processors import csrf
|
from django.core.context_processors import csrf
|
||||||
from django.template import Context, RequestContext, loader
|
from django.template import RequestContext
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
from django.db.models import Max, ProtectedError
|
from django.db.models import Max, ProtectedError
|
||||||
from django.db import IntegrityError
|
from django.db import IntegrityError
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
from users.models import User, Right, Ban, DelRightForm, UserForm, InfoForm, PasswordForm, StateForm, RightForm, BanForm, ProfilForm, Whitelist, WhitelistForm, DelSchoolForm, SchoolForm
|
from users.models import User, Right, Ban, Whitelist
|
||||||
|
from users.models import DelRightForm, BanForm, WhitelistForm, DelSchoolForm
|
||||||
|
from users.models import InfoForm, StateForm, RightForm, SchoolForm
|
||||||
from cotisations.models import Facture
|
from cotisations.models import Facture
|
||||||
from machines.models import Machine, Interface
|
from machines.models import Machine, Interface
|
||||||
from users.forms import PassForm
|
from users.forms import PassForm
|
||||||
from search.models import SearchForm
|
|
||||||
from cotisations.views import is_adherent, end_adhesion
|
from cotisations.views import is_adherent, end_adhesion
|
||||||
from machines.views import unassign_ips, assign_ips
|
from machines.views import unassign_ips, assign_ips
|
||||||
|
|
||||||
from re2o.login import makeSecret, hashNT
|
from re2o.login import hashNT
|
||||||
|
|
||||||
|
|
||||||
def archive(user):
|
def archive(user):
|
||||||
""" Archive un utilisateur """
|
""" Archive un utilisateur """
|
||||||
unassign_ips(user)
|
unassign_ips(user)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
def unarchive(user):
|
def unarchive(user):
|
||||||
""" Triger actions au desarchivage d'un user """
|
""" Triger actions au desarchivage d'un user """
|
||||||
assign_ips(user)
|
assign_ips(user)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
def end_ban(user):
|
def end_ban(user):
|
||||||
""" Renvoie la date de fin de ban d'un user, False sinon """
|
""" Renvoie la date de fin de ban d'un user, False sinon """
|
||||||
date_max = Ban.objects.all().filter(user=user).aggregate(Max('date_end'))['date_end__max']
|
date_max = Ban.objects.all().filter(
|
||||||
|
user=user).aggregate(Max('date_end'))['date_end__max']
|
||||||
return date_max
|
return date_max
|
||||||
|
|
||||||
|
|
||||||
def end_whitelist(user):
|
def end_whitelist(user):
|
||||||
""" Renvoie la date de fin de ban d'un user, False sinon """
|
""" Renvoie la date de fin de ban d'un user, False sinon """
|
||||||
date_max = Whitelist.objects.all().filter(user=user).aggregate(Max('date_end'))['date_end__max']
|
date_max = Whitelist.objects.all().filter(
|
||||||
|
user=user).aggregate(Max('date_end'))['date_end__max']
|
||||||
return date_max
|
return date_max
|
||||||
|
|
||||||
|
|
||||||
def is_ban(user):
|
def is_ban(user):
|
||||||
""" Renvoie si un user est banni ou non """
|
""" Renvoie si un user est banni ou non """
|
||||||
end = end_ban(user)
|
end = end_ban(user)
|
||||||
|
@ -50,6 +57,7 @@ def is_ban(user):
|
||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def is_whitelisted(user):
|
def is_whitelisted(user):
|
||||||
""" Renvoie si un user est whitelisté ou non """
|
""" Renvoie si un user est whitelisté ou non """
|
||||||
end = end_whitelist(user)
|
end = end_whitelist(user)
|
||||||
|
@ -60,9 +68,12 @@ def is_whitelisted(user):
|
||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def has_access(user):
|
def has_access(user):
|
||||||
""" Renvoie si un utilisateur a accès à internet"""
|
""" Renvoie si un utilisateur a accès à internet """
|
||||||
return user.state == User.STATE_ACTIVE and not is_ban(user) and ( is_adherent(user) or is_whitelisted(user))
|
return user.state == User.STATE_ACTIVE \
|
||||||
|
and not is_ban(user) and (is_adherent(user) or is_whitelisted(user))
|
||||||
|
|
||||||
|
|
||||||
def is_active(interface):
|
def is_active(interface):
|
||||||
""" Renvoie si une interface doit avoir accès ou non """
|
""" Renvoie si une interface doit avoir accès ou non """
|
||||||
|
@ -70,10 +81,16 @@ def is_active(interface):
|
||||||
user = machine.user
|
user = machine.user
|
||||||
return machine.active and has_access(user)
|
return machine.active and has_access(user)
|
||||||
|
|
||||||
|
|
||||||
def form(ctx, template, request):
|
def form(ctx, template, request):
|
||||||
c = ctx
|
c = ctx
|
||||||
c.update(csrf(request))
|
c.update(csrf(request))
|
||||||
return render_to_response(template, c, context_instance=RequestContext(request))
|
return render_to_response(
|
||||||
|
template,
|
||||||
|
c,
|
||||||
|
context_instance=RequestContext(request)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def new_user(request):
|
def new_user(request):
|
||||||
user = InfoForm(request.POST or None)
|
user = InfoForm(request.POST or None)
|
||||||
|
@ -83,11 +100,12 @@ def new_user(request):
|
||||||
return redirect("/users/")
|
return redirect("/users/")
|
||||||
return form({'userform': user}, 'users/user.html', request)
|
return form({'userform': user}, 'users/user.html', request)
|
||||||
|
|
||||||
|
|
||||||
def edit_info(request, userid):
|
def edit_info(request, userid):
|
||||||
try:
|
try:
|
||||||
user = User.objects.get(pk=userid)
|
user = User.objects.get(pk=userid)
|
||||||
except User.DoesNotExist:
|
except User.DoesNotExist:
|
||||||
messages.error(request, u"Utilisateur inexistant" )
|
messages.error(request, "Utilisateur inexistant")
|
||||||
return redirect("/users/")
|
return redirect("/users/")
|
||||||
user = InfoForm(request.POST or None, instance=user)
|
user = InfoForm(request.POST or None, instance=user)
|
||||||
if user.is_valid():
|
if user.is_valid():
|
||||||
|
@ -96,11 +114,12 @@ def edit_info(request, userid):
|
||||||
return redirect("/users/")
|
return redirect("/users/")
|
||||||
return form({'userform': user}, 'users/user.html', request)
|
return form({'userform': user}, 'users/user.html', request)
|
||||||
|
|
||||||
|
|
||||||
def state(request, userid):
|
def state(request, userid):
|
||||||
try:
|
try:
|
||||||
user = User.objects.get(pk=userid)
|
user = User.objects.get(pk=userid)
|
||||||
except User.DoesNotExist:
|
except User.DoesNotExist:
|
||||||
messages.error(request, u"Utilisateur inexistant" )
|
messages.error(request, "Utilisateur inexistant")
|
||||||
return redirect("/users/")
|
return redirect("/users/")
|
||||||
state = StateForm(request.POST or None, instance=user)
|
state = StateForm(request.POST or None, instance=user)
|
||||||
if state.is_valid():
|
if state.is_valid():
|
||||||
|
@ -114,29 +133,31 @@ def state(request, userid):
|
||||||
return redirect("/users/")
|
return redirect("/users/")
|
||||||
return form({'userform': state}, 'users/user.html', request)
|
return form({'userform': state}, 'users/user.html', request)
|
||||||
|
|
||||||
|
|
||||||
def password(request, userid):
|
def password(request, userid):
|
||||||
try:
|
try:
|
||||||
user = User.objects.get(pk=userid)
|
user = User.objects.get(pk=userid)
|
||||||
except User.DoesNotExist:
|
except User.DoesNotExist:
|
||||||
messages.error(request, u"Utilisateur inexistant" )
|
messages.error(request, "Utilisateur inexistant")
|
||||||
return redirect("/users/")
|
return redirect("/users/")
|
||||||
user_form = PassForm(request.POST or None)
|
u_form = PassForm(request.POST or None)
|
||||||
if user_form.is_valid():
|
if u_form.is_valid():
|
||||||
if user_form.cleaned_data['passwd1'] != user_form.cleaned_data['passwd2']:
|
if u_form.cleaned_data['passwd1'] != u_form.cleaned_data['passwd2']:
|
||||||
messages.error(request, u"Les 2 mots de passe différent" )
|
messages.error(request, "Les 2 mots de passe différent")
|
||||||
return form({'userform': user_form}, 'users/user.html', request)
|
return form({'userform': u_form}, 'users/user.html', request)
|
||||||
user.pwd_ssha = makeSecret(user_form.cleaned_data['passwd1'])
|
user.set_password(u_form.cleaned_data['passwd1'])
|
||||||
user.pwd_ntlm = hashNT(user_form.cleaned_data['passwd1'])
|
user.pwd_ntlm = hashNT(u_form.cleaned_data['passwd1'])
|
||||||
user.save()
|
user.save()
|
||||||
messages.success(request, "Le mot de passe a changé")
|
messages.success(request, "Le mot de passe a changé")
|
||||||
return redirect("/users/")
|
return redirect("/users/")
|
||||||
return form({'userform': user_form}, 'users/user.html', request)
|
return form({'userform': u_form}, 'users/user.html', request)
|
||||||
|
|
||||||
|
|
||||||
def add_right(request, userid):
|
def add_right(request, userid):
|
||||||
try:
|
try:
|
||||||
user = User.objects.get(pk=userid)
|
user = User.objects.get(pk=userid)
|
||||||
except User.DoesNotExist:
|
except User.DoesNotExist:
|
||||||
messages.error(request, u"Utilisateur inexistant" )
|
messages.error(request, "Utilisateur inexistant")
|
||||||
return redirect("/users/")
|
return redirect("/users/")
|
||||||
right = RightForm(request.POST or None)
|
right = RightForm(request.POST or None)
|
||||||
if right.is_valid():
|
if right.is_valid():
|
||||||
|
@ -150,6 +171,7 @@ def add_right(request, userid):
|
||||||
return redirect("/users/")
|
return redirect("/users/")
|
||||||
return form({'userform': right}, 'users/user.html', request)
|
return form({'userform': right}, 'users/user.html', request)
|
||||||
|
|
||||||
|
|
||||||
def del_right(request):
|
def del_right(request):
|
||||||
right = DelRightForm(request.POST or None)
|
right = DelRightForm(request.POST or None)
|
||||||
if right.is_valid():
|
if right.is_valid():
|
||||||
|
@ -159,11 +181,12 @@ def del_right(request):
|
||||||
return redirect("/users/")
|
return redirect("/users/")
|
||||||
return form({'userform': right}, 'users/user.html', request)
|
return form({'userform': right}, 'users/user.html', request)
|
||||||
|
|
||||||
|
|
||||||
def add_ban(request, userid):
|
def add_ban(request, userid):
|
||||||
try:
|
try:
|
||||||
user = User.objects.get(pk=userid)
|
user = User.objects.get(pk=userid)
|
||||||
except User.DoesNotExist:
|
except User.DoesNotExist:
|
||||||
messages.error(request, u"Utilisateur inexistant" )
|
messages.error(request, "Utilisateur inexistant")
|
||||||
return redirect("/users/")
|
return redirect("/users/")
|
||||||
ban_instance = Ban(user=user)
|
ban_instance = Ban(user=user)
|
||||||
ban = BanForm(request.POST or None, instance=ban_instance)
|
ban = BanForm(request.POST or None, instance=ban_instance)
|
||||||
|
@ -172,14 +195,18 @@ def add_ban(request, userid):
|
||||||
messages.success(request, "Bannissement ajouté")
|
messages.success(request, "Bannissement ajouté")
|
||||||
return redirect("/users/")
|
return redirect("/users/")
|
||||||
if is_ban(user):
|
if is_ban(user):
|
||||||
messages.error(request, u"Attention, cet utilisateur a deja un bannissement actif" )
|
messages.error(
|
||||||
|
request,
|
||||||
|
"Attention, cet utilisateur a deja un bannissement actif"
|
||||||
|
)
|
||||||
return form({'userform': ban}, 'users/user.html', request)
|
return form({'userform': ban}, 'users/user.html', request)
|
||||||
|
|
||||||
|
|
||||||
def edit_ban(request, banid):
|
def edit_ban(request, banid):
|
||||||
try:
|
try:
|
||||||
ban_instance = Ban.objects.get(pk=banid)
|
ban_instance = Ban.objects.get(pk=banid)
|
||||||
except Ban.DoesNotExist:
|
except Ban.DoesNotExist:
|
||||||
messages.error(request, u"Entrée inexistante" )
|
messages.error(request, "Entrée inexistante")
|
||||||
return redirect("/users/")
|
return redirect("/users/")
|
||||||
ban = BanForm(request.POST or None, instance=ban_instance)
|
ban = BanForm(request.POST or None, instance=ban_instance)
|
||||||
if ban.is_valid():
|
if ban.is_valid():
|
||||||
|
@ -188,11 +215,12 @@ def edit_ban(request, banid):
|
||||||
return redirect("/users/")
|
return redirect("/users/")
|
||||||
return form({'userform': ban}, 'users/user.html', request)
|
return form({'userform': ban}, 'users/user.html', request)
|
||||||
|
|
||||||
|
|
||||||
def add_whitelist(request, userid):
|
def add_whitelist(request, userid):
|
||||||
try:
|
try:
|
||||||
user = User.objects.get(pk=userid)
|
user = User.objects.get(pk=userid)
|
||||||
except User.DoesNotExist:
|
except User.DoesNotExist:
|
||||||
messages.error(request, u"Utilisateur inexistant" )
|
messages.error(request, "Utilisateur inexistant")
|
||||||
return redirect("/users/")
|
return redirect("/users/")
|
||||||
whitelist_instance = Whitelist(user=user)
|
whitelist_instance = Whitelist(user=user)
|
||||||
whitelist = WhitelistForm(request.POST or None, instance=whitelist_instance)
|
whitelist = WhitelistForm(request.POST or None, instance=whitelist_instance)
|
||||||
|
@ -201,14 +229,18 @@ def add_whitelist(request, userid):
|
||||||
messages.success(request, "Accès à titre gracieux accordé")
|
messages.success(request, "Accès à titre gracieux accordé")
|
||||||
return redirect("/users/")
|
return redirect("/users/")
|
||||||
if is_whitelisted(user):
|
if is_whitelisted(user):
|
||||||
messages.error(request, u"Attention, cet utilisateur a deja un accès gracieux actif" )
|
messages.error(
|
||||||
|
request,
|
||||||
|
"Attention, cet utilisateur a deja un accès gracieux actif"
|
||||||
|
)
|
||||||
return form({'userform': whitelist}, 'users/user.html', request)
|
return form({'userform': whitelist}, 'users/user.html', request)
|
||||||
|
|
||||||
|
|
||||||
def edit_whitelist(request, whitelistid):
|
def edit_whitelist(request, whitelistid):
|
||||||
try:
|
try:
|
||||||
whitelist_instance = Whitelist.objects.get(pk=whitelistid)
|
whitelist_instance = Whitelist.objects.get(pk=whitelistid)
|
||||||
except Whitelist.DoesNotExist:
|
except Whitelist.DoesNotExist:
|
||||||
messages.error(request, u"Entrée inexistante" )
|
messages.error(request, "Entrée inexistante")
|
||||||
return redirect("/users/")
|
return redirect("/users/")
|
||||||
whitelist = WhitelistForm(request.POST or None, instance=whitelist_instance)
|
whitelist = WhitelistForm(request.POST or None, instance=whitelist_instance)
|
||||||
if whitelist.is_valid():
|
if whitelist.is_valid():
|
||||||
|
@ -217,6 +249,7 @@ def edit_whitelist(request, whitelistid):
|
||||||
return redirect("/users/")
|
return redirect("/users/")
|
||||||
return form({'userform': whitelist}, 'users/user.html', request)
|
return form({'userform': whitelist}, 'users/user.html', request)
|
||||||
|
|
||||||
|
|
||||||
def add_school(request):
|
def add_school(request):
|
||||||
school = SchoolForm(request.POST or None)
|
school = SchoolForm(request.POST or None)
|
||||||
if school.is_valid():
|
if school.is_valid():
|
||||||
|
@ -225,6 +258,7 @@ def add_school(request):
|
||||||
return redirect("/users/")
|
return redirect("/users/")
|
||||||
return form({'userform': school}, 'users/user.html', request)
|
return form({'userform': school}, 'users/user.html', request)
|
||||||
|
|
||||||
|
|
||||||
def del_school(request):
|
def del_school(request):
|
||||||
school = DelSchoolForm(request.POST or None)
|
school = DelSchoolForm(request.POST or None)
|
||||||
if school.is_valid():
|
if school.is_valid():
|
||||||
|
@ -234,46 +268,73 @@ def del_school(request):
|
||||||
school_del.delete()
|
school_del.delete()
|
||||||
messages.success(request, "L'établissement a été supprimé")
|
messages.success(request, "L'établissement a été supprimé")
|
||||||
except ProtectedError:
|
except ProtectedError:
|
||||||
messages.error(request, "L'établissement %s est affecté à au moins un user, vous ne pouvez pas le supprimer" % school_del)
|
messages.error(
|
||||||
|
request,
|
||||||
|
"L'établissement %s est affecté à au moins un user, \
|
||||||
|
vous ne pouvez pas le supprimer" % school_del
|
||||||
|
)
|
||||||
return redirect("/users/")
|
return redirect("/users/")
|
||||||
return form({'userform': school}, 'users/user.html', request)
|
return form({'userform': school}, 'users/user.html', request)
|
||||||
|
|
||||||
|
|
||||||
def index(request):
|
def index(request):
|
||||||
users_list = User.objects.order_by('pk')
|
users_list = User.objects.order_by('pk')
|
||||||
connexion = []
|
connexion = []
|
||||||
for user in users_list:
|
for user in users_list:
|
||||||
end = end_adhesion(user)
|
end = end_adhesion(user)
|
||||||
access = has_access(user)
|
access = has_access(user)
|
||||||
if(end!=None):
|
if(end is not None):
|
||||||
connexion.append([user, access, end])
|
connexion.append([user, access, end])
|
||||||
else:
|
else:
|
||||||
connexion.append([user, access, "Non adhérent"])
|
connexion.append([user, access, "Non adhérent"])
|
||||||
return render(request, 'users/index.html', {'users_list': connexion})
|
return render(request, 'users/index.html', {'users_list': connexion})
|
||||||
|
|
||||||
|
|
||||||
def index_ban(request):
|
def index_ban(request):
|
||||||
ban_list = Ban.objects.order_by('date_start')
|
ban_list = Ban.objects.order_by('date_start')
|
||||||
return render(request, 'users/index_ban.html', {'ban_list':ban_list})
|
return render(request, 'users/index_ban.html', {'ban_list': ban_list})
|
||||||
|
|
||||||
|
|
||||||
def index_white(request):
|
def index_white(request):
|
||||||
white_list = Whitelist.objects.order_by('date_start')
|
white_list = Whitelist.objects.order_by('date_start')
|
||||||
return render(request, 'users/index_whitelist.html', {'white_list':white_list})
|
return render(
|
||||||
|
request,
|
||||||
|
'users/index_whitelist.html',
|
||||||
|
{'white_list': white_list}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def profil(request, userid):
|
def profil(request, userid):
|
||||||
try:
|
try:
|
||||||
users = User.objects.get(pk=userid)
|
users = User.objects.get(pk=userid)
|
||||||
except User.DoesNotExist:
|
except User.DoesNotExist:
|
||||||
messages.error(request, u"Utilisateur inexistant" )
|
messages.error(request, "Utilisateur inexistant")
|
||||||
return redirect("/users/")
|
return redirect("/users/")
|
||||||
machines = Interface.objects.filter(machine=Machine.objects.filter(user__pseudo = users))
|
machines = Interface.objects.filter(
|
||||||
factures = Facture.objects.filter(user__pseudo = users)
|
machine=Machine.objects.filter(user__pseudo=users)
|
||||||
bans = Ban.objects.filter(user__pseudo = users)
|
)
|
||||||
whitelists = Whitelist.objects.filter(user__pseudo = users)
|
factures = Facture.objects.filter(user__pseudo=users)
|
||||||
|
bans = Ban.objects.filter(user__pseudo=users)
|
||||||
|
whitelists = Whitelist.objects.filter(user__pseudo=users)
|
||||||
end_bans = None
|
end_bans = None
|
||||||
end_whitelists = None
|
end_whitelists = None
|
||||||
if(is_ban(users)):
|
if(is_ban(users)):
|
||||||
end_bans=end_ban(users)
|
end_bans = end_ban(users)
|
||||||
if(is_whitelisted(users)):
|
if(is_whitelisted(users)):
|
||||||
end_whitelists=end_whitelist(users)
|
end_whitelists = end_whitelist(users)
|
||||||
list_droits = Right.objects.filter(user=users)
|
list_droits = Right.objects.filter(user=users)
|
||||||
return render(request, 'users/profil.html', {'user': users, 'machine_list' :machines, 'facture_list':factures, 'ban_list':bans, 'white_list':whitelists,'end_ban':end_bans,'end_whitelist':end_whitelists, 'end_adhesion':end_adhesion(users), 'actif':has_access(users), 'list_droits': list_droits})
|
return render(
|
||||||
|
request,
|
||||||
|
'users/profil.html',
|
||||||
|
{
|
||||||
|
'user': users,
|
||||||
|
'machine_list': machines,
|
||||||
|
'facture_list': factures,
|
||||||
|
'ban_list': bans,
|
||||||
|
'white_list': whitelists,
|
||||||
|
'end_ban': end_bans,
|
||||||
|
'end_whitelist': end_whitelists,
|
||||||
|
'end_adhesion': end_adhesion(users),
|
||||||
|
'actif': has_access(users),
|
||||||
|
'list_droits': list_droits}
|
||||||
|
)
|
||||||
|
|
Loading…
Reference in a new issue