mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-12-28 01:43:46 +00:00
Ajoute une table whitelist
This commit is contained in:
parent
781d550263
commit
a0a2d5a6b5
7 changed files with 153 additions and 15 deletions
|
@ -1,6 +1,6 @@
|
|||
from django.contrib import admin
|
||||
|
||||
from .models import User, School, Right, ListRight, Ban
|
||||
from .models import User, School, Right, ListRight, Ban, Whitelist
|
||||
|
||||
class UserAdmin(admin.ModelAdmin):
|
||||
list_display = ('name','surname','pseudo','room','email', 'school', 'state')
|
||||
|
@ -17,8 +17,12 @@ class RightAdmin(admin.ModelAdmin):
|
|||
class BanAdmin(admin.ModelAdmin):
|
||||
list_display = ('user', 'raison', 'date_start', 'date_end')
|
||||
|
||||
class WhitelistAdmin(admin.ModelAdmin):
|
||||
list_display = ('user', 'raison', 'date_start', 'date_end')
|
||||
|
||||
admin.site.register(User, UserAdmin)
|
||||
admin.site.register(School, SchoolAdmin)
|
||||
admin.site.register(Right, RightAdmin)
|
||||
admin.site.register(ListRight, ListRightAdmin)
|
||||
admin.site.register(Ban, BanAdmin)
|
||||
admin.site.register(Whitelist, WhitelistAdmin)
|
||||
|
|
25
users/migrations/0015_whitelist.py
Normal file
25
users/migrations/0015_whitelist.py
Normal file
|
@ -0,0 +1,25 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('users', '0014_auto_20160704_1548'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Whitelist',
|
||||
fields=[
|
||||
('id', models.AutoField(serialize=False, verbose_name='ID', auto_created=True, primary_key=True)),
|
||||
('raison', models.CharField(max_length=255)),
|
||||
('date_start', models.DateTimeField(auto_now_add=True)),
|
||||
('date_end', models.DateTimeField(help_text='%m/%d/%y %H:%M:%S')),
|
||||
('user', models.ForeignKey(to='users.User', on_delete=django.db.models.deletion.PROTECT)),
|
||||
],
|
||||
),
|
||||
]
|
|
@ -71,6 +71,15 @@ class Ban(models.Model):
|
|||
def __str__(self):
|
||||
return str(self.user) + ' ' + str(self.raison)
|
||||
|
||||
class Whitelist(models.Model):
|
||||
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
||||
raison = models.CharField(max_length=255)
|
||||
date_start = models.DateTimeField(auto_now_add=True)
|
||||
date_end = models.DateTimeField(help_text='%m/%d/%y %H:%M:%S')
|
||||
|
||||
def __str__(self):
|
||||
return str(self.user) + ' ' + str(self.raison)
|
||||
|
||||
class UserForm(ModelForm):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(InfoForm, self).__init__(*args, **kwargs)
|
||||
|
@ -151,5 +160,20 @@ 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")
|
||||
return date_end
|
||||
|
||||
class WhitelistForm(ModelForm):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(WhitelistForm, self).__init__(*args, **kwargs)
|
||||
self.fields['date_end'].label = 'Date de fin'
|
||||
|
||||
class Meta:
|
||||
model = Whitelist
|
||||
exclude = ['user']
|
||||
|
||||
def clean_date_end(self):
|
||||
date_end = self.cleaned_data['date_end']
|
||||
if date_end < timezone.now():
|
||||
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
|
||||
|
||||
class ProfilForm(Form):
|
||||
user =forms.CharField(label ='Ok', max_length=100)
|
||||
|
|
21
users/templates/users/aff_whitelists.html
Normal file
21
users/templates/users/aff_whitelists.html
Normal file
|
@ -0,0 +1,21 @@
|
|||
<table class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Utilisateur</th>
|
||||
<th>Raison</th>
|
||||
<th>Date de début</th>
|
||||
<th>Date de fin</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
{% for whitelist in white_list %}
|
||||
<tr>
|
||||
<td>{{ whitelist.user }}</td>
|
||||
<td>{{ whitelist.raison }}</td>
|
||||
<td>{{ whitelist.date_start }}</td>
|
||||
<td>{{ whitelist.date_end }}</td>
|
||||
<td><a class="btn btn-primary btn-sm" role="button" href="{% url 'users:edit-whitelist' whitelist.id %}"><i class="glyphicon glyphicon-grain"></i> Editer</a></td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
|
|
@ -42,20 +42,19 @@
|
|||
{% else %}
|
||||
<td><font color="red">Non adhérent</font></td>
|
||||
{% endif %}
|
||||
<th>Accès gracieux</th>
|
||||
{% if end_whitelist != None %}
|
||||
<td><font color="green">{{ end_whitelist }}</font></td>
|
||||
{% else %}
|
||||
<td><font color="orange">Aucun</font></td>
|
||||
{% endif %}
|
||||
<tr>
|
||||
<th>Bannissement</th>
|
||||
{% if end_ban != None %}
|
||||
<td><font color="red">{{ end_ban }}</font></td>
|
||||
{% else %}
|
||||
<td><font color="green">Non banni</font></td>
|
||||
{% endif %}
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Connexion</th>
|
||||
{% if actif == True %}
|
||||
<td><font color="green">Active</font></td>
|
||||
{% else %}
|
||||
<td><font color="red">Désactivée</font></td>
|
||||
{% endif %}
|
||||
<th>Statut</th>
|
||||
{% if user.state == 0 %}
|
||||
<td><font color="green">Actif</font></td>
|
||||
|
@ -64,6 +63,14 @@
|
|||
{% else %}
|
||||
<td><font color="orange">Archivé</font></td>
|
||||
{% endif %}
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Connexion</th>
|
||||
{% if actif == True %}
|
||||
<td><font color="green">Active</font></td>
|
||||
{% else %}
|
||||
<td><font color="red">Désactivée</font></td>
|
||||
{% endif %}
|
||||
</table>
|
||||
<h2>Machines :</h2>
|
||||
<h4><a class="btn btn-primary btn-sm" role="button" href="{% url 'machines:new-machine' user.id %}"><i class="glyphicon glyphicon-phone"></i> Ajouter une machine</a></h4>
|
||||
|
@ -86,6 +93,13 @@
|
|||
{% else %}
|
||||
<p>Aucun bannissement</p>
|
||||
{% endif %}
|
||||
<h2>Accès à titre gracieux :</h2>
|
||||
<h4><a class="btn btn-primary btn-sm" role="button" href="{% url 'users:add-whitelist' user.id %}"><i class="glyphicon glyphicon-pushpin"></i> Accorder un accès à titre gracieux</a></h4>
|
||||
{% if white_list %}
|
||||
{% include "users/aff_whitelists.html" with white_list=white_list %}
|
||||
{% else %}
|
||||
<p>Aucun accès gracieux</p>
|
||||
{% endif %}
|
||||
<br />
|
||||
<br />
|
||||
<br />
|
||||
|
|
|
@ -9,6 +9,8 @@ urlpatterns = [
|
|||
url(r'^password/(?P<userid>[0-9]+)$', views.password, name='password'),
|
||||
url(r'^add_ban/(?P<userid>[0-9]+)$', views.add_ban, name='add-ban'),
|
||||
url(r'^edit_ban/(?P<banid>[0-9]+)$', views.edit_ban, name='edit-ban'),
|
||||
url(r'^add_whitelist/(?P<userid>[0-9]+)$', views.add_whitelist, name='add-whitelist'),
|
||||
url(r'^edit_whitelist/(?P<whitelistid>[0-9]+)$', views.edit_whitelist, name='edit-whitelist'),
|
||||
url(r'^add_right/$', views.add_right, name='add-right'),
|
||||
url(r'^del_right/$', views.del_right, name='del-right'),
|
||||
url(r'^profil/$', views.profil, name='profil'),
|
||||
|
|
|
@ -9,7 +9,7 @@ from django.contrib import messages
|
|||
from django.db.models import Max
|
||||
from django.utils import timezone
|
||||
|
||||
from users.models import User, Right, Ban, DelRightForm, UserForm, InfoForm, PasswordForm, StateForm, RightForm, BanForm, ProfilForm
|
||||
from users.models import User, Right, Ban, DelRightForm, UserForm, InfoForm, PasswordForm, StateForm, RightForm, BanForm, ProfilForm, Whitelist, WhitelistForm
|
||||
from cotisations.models import Facture
|
||||
from machines.models import Machine, Interface
|
||||
from users.forms import PassForm
|
||||
|
@ -34,6 +34,11 @@ def end_ban(user):
|
|||
date_max = Ban.objects.all().filter(user=user).aggregate(Max('date_end'))['date_end__max']
|
||||
return date_max
|
||||
|
||||
def end_whitelist(user):
|
||||
""" 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']
|
||||
return date_max
|
||||
|
||||
def is_ban(user):
|
||||
""" Renvoie si un user est banni ou non """
|
||||
end = end_ban(user)
|
||||
|
@ -44,9 +49,19 @@ def is_ban(user):
|
|||
else:
|
||||
return True
|
||||
|
||||
def is_whitelisted(user):
|
||||
""" Renvoie si un user est whitelisté ou non """
|
||||
end = end_whitelist(user)
|
||||
if not end:
|
||||
return False
|
||||
elif end < timezone.now():
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
def has_access(user):
|
||||
""" Renvoie si un utilisateur a accès à internet"""
|
||||
if user.state == User.STATE_ACTIVE and not is_ban(user) and is_adherent(user):
|
||||
if user.state == User.STATE_ACTIVE and not is_ban(user) and ( is_adherent(user) or is_whitelisted(user)):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
@ -149,7 +164,7 @@ def add_ban(request, userid):
|
|||
def edit_ban(request, banid):
|
||||
try:
|
||||
ban_instance = Ban.objects.get(pk=banid)
|
||||
except User.DoesNotExist:
|
||||
except Ban.DoesNotExist:
|
||||
messages.error(request, u"Entrée inexistante" )
|
||||
return redirect("/users/")
|
||||
ban = BanForm(request.POST or None, instance=ban_instance)
|
||||
|
@ -159,6 +174,35 @@ def edit_ban(request, banid):
|
|||
return redirect("/users/")
|
||||
return form({'userform': ban}, 'users/user.html', request)
|
||||
|
||||
def add_whitelist(request, userid):
|
||||
try:
|
||||
user = User.objects.get(pk=userid)
|
||||
except User.DoesNotExist:
|
||||
messages.error(request, u"Utilisateur inexistant" )
|
||||
return redirect("/users/")
|
||||
whitelist_instance = Whitelist(user=user)
|
||||
whitelist = WhitelistForm(request.POST or None, instance=whitelist_instance)
|
||||
if whitelist.is_valid():
|
||||
whitelist.save()
|
||||
messages.success(request, "Accès à titre gracieux accordé")
|
||||
return redirect("/users/")
|
||||
if is_whitelisted(user):
|
||||
messages.error(request, u"Attention, cet utilisateur a deja un accès gracieux actif" )
|
||||
return form({'userform': whitelist}, 'users/user.html', request)
|
||||
|
||||
def edit_whitelist(request, whitelistid):
|
||||
try:
|
||||
whitelist_instance = Whitelist.objects.get(pk=whitelistid)
|
||||
except Whitelist.DoesNotExist:
|
||||
messages.error(request, u"Entrée inexistante" )
|
||||
return redirect("/users/")
|
||||
whitelist = WhitelistForm(request.POST or None, instance=whitelist_instance)
|
||||
if whitelist.is_valid():
|
||||
whitelist.save()
|
||||
messages.success(request, "Whitelist modifiée")
|
||||
return redirect("/users/")
|
||||
return form({'userform': whitelist}, 'users/user.html', request)
|
||||
|
||||
def index(request):
|
||||
users_list = User.objects.order_by('pk')
|
||||
connexion = []
|
||||
|
@ -175,10 +219,14 @@ def profil(request):
|
|||
machines = Interface.objects.filter(machine=Machine.objects.filter(user__pseudo = users))
|
||||
factures = Facture.objects.filter(user__pseudo = users)
|
||||
bans = Ban.objects.filter(user__pseudo = users)
|
||||
end = None
|
||||
whitelists = Whitelist.objects.filter(user__pseudo = users)
|
||||
end_bans = None
|
||||
end_whitelists = None
|
||||
if(is_ban(users)):
|
||||
end=end_ban(users)
|
||||
return render(request, 'users/profil.html', {'user': users, 'machine_list' :machines, 'facture_list':factures, 'ban_list':bans, 'end_ban':end, 'end_adhesion':end_adhesion(users), 'actif':has_access(users)})
|
||||
end_bans=end_ban(users)
|
||||
if(is_whitelisted(users)):
|
||||
end_whitelists=end_whitelist(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)})
|
||||
return redirect("/users/")
|
||||
return redirect("/users/")
|
||||
|
||||
|
|
Loading…
Reference in a new issue