diff --git a/topologie/admin.py b/topologie/admin.py
index cc32c4ff..f388c688 100644
--- a/topologie/admin.py
+++ b/topologie/admin.py
@@ -7,7 +7,7 @@ class SwitchAdmin(admin.ModelAdmin):
list_display = ('building','number','details')
class PortAdmin(admin.ModelAdmin):
- list_display = ('switch', 'port','room','details')
+ list_display = ('switch', 'port','room','machine_interface','details')
class RoomAdmin(admin.ModelAdmin):
list_display = ('name',)
diff --git a/topologie/forms.py b/topologie/forms.py
new file mode 100644
index 00000000..8e7355ac
--- /dev/null
+++ b/topologie/forms.py
@@ -0,0 +1,11 @@
+from .models import Port
+from django.forms import ModelForm, Form
+
+class PortForm(ModelForm):
+ class Meta:
+ model = Port
+ fields = '__all__'
+
+class EditPortForm(ModelForm):
+ class Meta(PortForm.Meta):
+ fields = ['room', 'machine_interface', 'related', 'details']
diff --git a/topologie/migrations/0012_port_machine_interface.py b/topologie/migrations/0012_port_machine_interface.py
new file mode 100644
index 00000000..558680a8
--- /dev/null
+++ b/topologie/migrations/0012_port_machine_interface.py
@@ -0,0 +1,21 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('machines', '0014_auto_20160706_1220'),
+ ('topologie', '0011_auto_20160704_2153'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='port',
+ name='machine_interface',
+ field=models.OneToOneField(on_delete=django.db.models.deletion.PROTECT, null=True, blank=True, to='machines.Interface'),
+ ),
+ ]
diff --git a/topologie/migrations/0013_port_related.py b/topologie/migrations/0013_port_related.py
new file mode 100644
index 00000000..afce7dce
--- /dev/null
+++ b/topologie/migrations/0013_port_related.py
@@ -0,0 +1,19 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('topologie', '0012_port_machine_interface'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='port',
+ name='related',
+ field=models.OneToOneField(null=True, to='topologie.Port', blank=True, related_name='related_port'),
+ ),
+ ]
diff --git a/topologie/migrations/0014_auto_20160706_1238.py b/topologie/migrations/0014_auto_20160706_1238.py
new file mode 100644
index 00000000..3be29e79
--- /dev/null
+++ b/topologie/migrations/0014_auto_20160706_1238.py
@@ -0,0 +1,26 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('topologie', '0013_port_related'),
+ ]
+
+ operations = [
+ migrations.AlterUniqueTogether(
+ name='port',
+ unique_together=set([('switch', 'port')]),
+ ),
+ migrations.RemoveField(
+ model_name='port',
+ name='_content_type',
+ ),
+ migrations.RemoveField(
+ model_name='port',
+ name='_object_id',
+ ),
+ ]
diff --git a/topologie/migrations/0015_auto_20160706_1452.py b/topologie/migrations/0015_auto_20160706_1452.py
new file mode 100644
index 00000000..29f9ca99
--- /dev/null
+++ b/topologie/migrations/0015_auto_20160706_1452.py
@@ -0,0 +1,23 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('topologie', '0014_auto_20160706_1238'),
+ ]
+
+ operations = [
+ migrations.RemoveField(
+ model_name='port',
+ name='related',
+ ),
+ migrations.AddField(
+ model_name='port',
+ name='related',
+ field=models.ManyToManyField(related_name='_port_related_+', to='topologie.Port', blank=True),
+ ),
+ ]
diff --git a/topologie/migrations/0016_auto_20160706_1531.py b/topologie/migrations/0016_auto_20160706_1531.py
new file mode 100644
index 00000000..649eab2d
--- /dev/null
+++ b/topologie/migrations/0016_auto_20160706_1531.py
@@ -0,0 +1,23 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('topologie', '0015_auto_20160706_1452'),
+ ]
+
+ operations = [
+ migrations.RemoveField(
+ model_name='port',
+ name='related',
+ ),
+ migrations.AddField(
+ model_name='port',
+ name='related',
+ field=models.OneToOneField(blank=True, to='topologie.Port', related_name='related_port', null=True),
+ ),
+ ]
diff --git a/topologie/migrations/__pycache__/0009_auto_20160703_1200.cpython-34.pyc b/topologie/migrations/__pycache__/0009_auto_20160703_1200.cpython-34.pyc
index 91251c86..128040d5 100644
Binary files a/topologie/migrations/__pycache__/0009_auto_20160703_1200.cpython-34.pyc and b/topologie/migrations/__pycache__/0009_auto_20160703_1200.cpython-34.pyc differ
diff --git a/topologie/models.py b/topologie/models.py
index 99686ca5..80af6534 100644
--- a/topologie/models.py
+++ b/topologie/models.py
@@ -1,7 +1,18 @@
from django.db import models
+from django.forms import ModelForm, Form
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
+from django.core.exceptions import ValidationError
+def make_port_related(port):
+ related_port = port.related
+ related_port.related = port
+ related_port.save()
+
+def clean_port_related(port):
+ related_port = port.related_port
+ related_port.related = None
+ related_port.save()
class Switch(models.Model):
building = models.CharField(max_length=10)
@@ -17,25 +28,26 @@ class Switch(models.Model):
class Port(models.Model):
switch = models.ForeignKey(Switch, related_name="ports")
port = models.IntegerField()
- details = models.CharField(max_length=255, blank=True)
room = models.ForeignKey('Room', on_delete=models.PROTECT, blank=True, null=True)
-# machine_interface = models.OneToOneField('machines.Interface', on_delete=models.PROTECT, blank=True, null=True)
+ machine_interface = models.OneToOneField('machines.Interface', on_delete=models.PROTECT, blank=True, null=True)
+ related = models.OneToOneField('self', null=True, blank=True, related_name='related_port')
+ details = models.CharField(max_length=255, blank=True)
class Meta:
- unique_together = ('_content_type', '_object_id')
+ unique_together = ('switch', 'port')
- _content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, blank=True, null=True)
- _object_id = models.PositiveIntegerField(blank=True, null=True)
- goto = GenericForeignKey('_content_type', '_object_id')
-
- @property
- def comefrom(self):
- ctype = ContentType.objects.get_for_model(self.__class__)
- try:
- event = Port.objects.get(_content_type__pk=ctype.id, _object_id=self.id)
- except:
- return None
- return event
+ def clean(self):
+ if self.room and self.machine_interface or self.room and self.related or self.machine_interface and self.related:
+ raise ValidationError("Chambre, interface et related_port sont mutuellement exclusifs")
+ if self.related==self:
+ raise ValidationError("On ne peut relier un port à lui même")
+ if self.related and not self.related.related:
+ if self.related.machine_interface or self.related.room:
+ raise ValidationError("Le port relié est déjà occupé, veuillez le libérer avant de créer une relation")
+ else:
+ make_port_related(self)
+ elif hasattr(self, 'related_port'):
+ clean_port_related(self)
def __str__(self):
return str(self.switch) + " - " + str(self.port)
diff --git a/topologie/templates/topologie/aff_port.html b/topologie/templates/topologie/aff_port.html
new file mode 100644
index 00000000..d94b341f
--- /dev/null
+++ b/topologie/templates/topologie/aff_port.html
@@ -0,0 +1,23 @@
+
Switch {% if port_list.0 %}{{ port_list.0.switch }}{% endif %}
+
+
+
+ Port |
+ Room |
+ Interface machine |
+ Related |
+ Détails |
+ |
+
+
+ {% for port in port_list %}
+
+ {{ port.port }} |
+ {{ port.room }} |
+ {{ port.machine_interface }} |
+ {{ port.related }} |
+ {{ port.details }} |
+ Editer |
+
+ {% endfor %}
+
diff --git a/topologie/templates/topologie/aff_switch.html b/topologie/templates/topologie/aff_switch.html
new file mode 100644
index 00000000..880c264a
--- /dev/null
+++ b/topologie/templates/topologie/aff_switch.html
@@ -0,0 +1,18 @@
+
+
+
+ Bâtiment |
+ Numero |
+ Détails |
+ |
+
+
+ {% for switch in switch_list %}
+
+ {{switch.building}} |
+ {{switch.number}} |
+ {{switch.details}} |
+ Editer |
+
+ {% endfor %}
+
diff --git a/topologie/templates/topologie/index.html b/topologie/templates/topologie/index.html
new file mode 100644
index 00000000..c95f1491
--- /dev/null
+++ b/topologie/templates/topologie/index.html
@@ -0,0 +1,11 @@
+{% extends "topologie/sidebar.html" %}
+{% load bootstrap3 %}
+
+{% block title %}Switchs{% endblock %}
+
+{% block content %}
+ {% include "topologie/aff_switch.html" with switch_list=switch_list %}
+
+
+
+{% endblock %}
diff --git a/topologie/templates/topologie/index_p.html b/topologie/templates/topologie/index_p.html
new file mode 100644
index 00000000..5309bc46
--- /dev/null
+++ b/topologie/templates/topologie/index_p.html
@@ -0,0 +1,11 @@
+{% extends "topologie/sidebar.html" %}
+{% load bootstrap3 %}
+
+{% block title %}Ports du switch{% endblock %}
+
+{% block content %}
+ {% include "topologie/aff_port.html" with port_list=port_list %}
+
+
+
+{% endblock %}
diff --git a/topologie/templates/topologie/port.html b/topologie/templates/topologie/port.html
new file mode 100644
index 00000000..bd6713b7
--- /dev/null
+++ b/topologie/templates/topologie/port.html
@@ -0,0 +1,17 @@
+{% extends "topologie/sidebar.html" %}
+{% load bootstrap3 %}
+
+{% block title %}Création et modificationd 'utilisateur{% endblock %}
+
+{% block content %}
+{% bootstrap_form_errors topoform %}
+
+
+
+
+
+{% endblock %}
diff --git a/topologie/templates/topologie/sidebar.html b/topologie/templates/topologie/sidebar.html
new file mode 100644
index 00000000..b74e53d3
--- /dev/null
+++ b/topologie/templates/topologie/sidebar.html
@@ -0,0 +1,5 @@
+{% extends "base.html" %}
+
+{% block sidebar %}
+
Liste des switchs
+{% endblock %}
diff --git a/topologie/urls.py b/topologie/urls.py
new file mode 100644
index 00000000..bc960eb6
--- /dev/null
+++ b/topologie/urls.py
@@ -0,0 +1,10 @@
+from django.conf.urls import url
+
+from . import views
+
+urlpatterns = [
+ url(r'^$', views.index, name='index'),
+ url(r'^switch/(?P
[0-9]+)$', views.index_port, name='index-port'),
+ url(r'^edit_port/(?P[0-9]+)$', views.edit_port, name='edit-port'),
+]
+
diff --git a/topologie/views.py b/topologie/views.py
index 91ea44a2..af3befe2 100644
--- a/topologie/views.py
+++ b/topologie/views.py
@@ -1,3 +1,35 @@
-from django.shortcuts import render
+from django.shortcuts import render, redirect
+from django.contrib import messages
-# Create your views here.
+
+from topologie.models import Switch, Port
+from topologie.forms import EditPortForm
+from users.views import form
+
+
+def index(request):
+ switch_list = Switch.objects.order_by('building', 'number')
+ return render(request, 'topologie/index.html', {'switch_list': switch_list})
+
+def index_port(request, switch_id):
+ try:
+ switch = Switch.objects.get(pk=switch_id)
+ except Switch.DoesNotExist:
+ messages.error(request, u"Switch inexistant")
+ return redirect("/topologie/")
+ port_list = Port.objects.filter(switch = switch).order_by('port')
+ return render(request, 'topologie/index_p.html', {'port_list':port_list})
+
+def edit_port(request, port_id):
+ try:
+ port = Port.objects.get(pk=port_id)
+ except Port.DoesNotExist:
+ messages.error(request, u"Port inexistant")
+ return redirect("/topologie/")
+ port = EditPortForm(request.POST or None, instance=port)
+ if port.is_valid():
+ port.save()
+ messages.success(request, "Le port a bien été modifié")
+ return redirect("/topologie")
+ return form({'topoform':port}, 'topologie/port.html', request)
+
diff --git a/users/migrations/0016_auto_20160706_1220.py b/users/migrations/0016_auto_20160706_1220.py
new file mode 100644
index 00000000..e7983681
--- /dev/null
+++ b/users/migrations/0016_auto_20160706_1220.py
@@ -0,0 +1,30 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+import users.models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('users', '0015_whitelist'),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name='ban',
+ name='date_end',
+ field=models.DateTimeField(help_text='%d/%m/%y %H:%M:%S'),
+ ),
+ migrations.AlterField(
+ model_name='user',
+ name='pseudo',
+ field=models.CharField(unique=True, validators=[users.models.linux_user_validator], max_length=32, help_text='Doit contenir uniquement des lettres, chiffres, ou tirets'),
+ ),
+ migrations.AlterField(
+ model_name='whitelist',
+ name='date_end',
+ field=models.DateTimeField(help_text='%d/%m/%y %H:%M:%S'),
+ ),
+ ]
diff --git a/users/models.py b/users/models.py
index b37f6066..c12def82 100644
--- a/users/models.py
+++ b/users/models.py
@@ -1,6 +1,7 @@
from django.db import models
from django.forms import ModelForm, Form
from django import forms
+import re
from django.utils import timezone
@@ -15,6 +16,15 @@ def remove_user_room(room):
user.room = None
user.save()
+def linux_user_validator(login):
+ """ Validation du pseudo pour respecter les contraintes unix"""
+ UNIX_LOGIN_PATTERN = re.compile("^[a-z_][a-z0-9_-]*[$]?$")
+ if not UNIX_LOGIN_PATTERN.match(login):
+ raise forms.ValidationError(
+ ", ce pseudo ('%(label)s') contient des carractères interdits",
+ params={'label': login},
+ )
+
class User(models.Model):
STATE_ACTIVE = 0
STATE_DEACTIVATED = 1
@@ -27,9 +37,9 @@ class User(models.Model):
name = models.CharField(max_length=255)
surname = models.CharField(max_length=255)
- pseudo = models.CharField(max_length=255, unique=True)
+ pseudo = models.CharField(max_length=32, unique=True, help_text="Doit contenir uniquement des lettres, chiffres, ou tirets", validators=[linux_user_validator])
email = models.EmailField()
- school = models.ForeignKey('School', on_delete=models.PROTECT)
+ school = models.ForeignKey('School', on_delete=models.PROTECT, null=False, blank=False)
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)
pwd_ssha = models.CharField(max_length=255)
@@ -66,7 +76,7 @@ class Ban(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')
+ date_end = models.DateTimeField(help_text='%d/%m/%y %H:%M:%S')
def __str__(self):
return str(self.user) + ' ' + str(self.raison)
@@ -75,24 +85,11 @@ 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')
+ date_end = models.DateTimeField(help_text='%d/%m/%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)
- self.fields['name'].label = 'Nom'
- self.fields['surname'].label = 'Prénom'
- self.fields['school'].label = 'Établissement'
- self.fields['comment'].label = 'Commentaire'
- self.fields['room'].label = 'Chambre'
-
- class Meta:
- model = User
- fields = '__all__'
-
class InfoForm(ModelForm):
force = forms.BooleanField(label="Forcer le déménagement ?", initial=False, required=False)
@@ -103,6 +100,8 @@ class InfoForm(ModelForm):
self.fields['school'].label = 'Établissement'
self.fields['comment'].label = 'Commentaire'
self.fields['room'].label = 'Chambre'
+ self.fields['room'].empty_label = "Pas de chambre"
+ self.fields['school'].empty_label = "Séléctionner un établissement"
def clean_force(self):
if self.cleaned_data.get('force', False):
@@ -113,6 +112,10 @@ class InfoForm(ModelForm):
model = User
fields = ['name','surname','pseudo','email', 'school', 'comment', 'room']
+class UserForm(InfoForm):
+ class Meta(InfoForm.Meta):
+ fields = '__all__'
+
class PasswordForm(ModelForm):
class Meta:
model = User
@@ -128,10 +131,22 @@ class SchoolForm(ModelForm):
model = School
fields = ['name']
+ def __init__(self, *args, **kwargs):
+ super(SchoolForm, self).__init__(*args, **kwargs)
+ self.fields['name'].label = 'Établissement à ajouter'
+
+class DelSchoolForm(ModelForm):
+ schools = forms.ModelMultipleChoiceField(queryset=School.objects.all(), label="Etablissements actuels", widget=forms.CheckboxSelectMultiple)
+
+ class Meta:
+ exclude = ['name']
+ model = School
+
class RightForm(ModelForm):
def __init__(self, *args, **kwargs):
super(RightForm, self).__init__(*args, **kwargs)
self.fields['right'].label = 'Droit'
+ self.fields['right'].empty_label = "Choisir un nouveau droit"
class Meta:
model = Right
diff --git a/users/templates/users/aff_bans.html b/users/templates/users/aff_bans.html
index cc94a24b..a32876d8 100644
--- a/users/templates/users/aff_bans.html
+++ b/users/templates/users/aff_bans.html
@@ -14,7 +14,7 @@
{{ ban.raison }} |
{{ ban.date_start }} |
{{ ban.date_end }} |
- Editer |
+ Editer |
{% endfor %}
diff --git a/users/templates/users/aff_users.html b/users/templates/users/aff_users.html
index d9cfaab7..07dd5bef 100644
--- a/users/templates/users/aff_users.html
+++ b/users/templates/users/aff_users.html
@@ -4,7 +4,7 @@
Prénom |
Nom |
Pseudo |
- Inscrit le |
+ Fin de cotisation le |
Connexion |
Profil |
@@ -14,19 +14,15 @@
{{ donnee.0.name }} |
{{ donnee.0.surname }} |
{{ donnee.0.pseudo }} |
- {{ donnee.0.registered }} |
+ {{ donnee.2 }} |
{% if donnee.1 == True %}
Active
{% else %}
Désactivée
{% endif %}
|
-
- |
+
+ |
{% endfor %}
diff --git a/users/templates/users/aff_whitelists.html b/users/templates/users/aff_whitelists.html
index a23a78ce..cc919628 100644
--- a/users/templates/users/aff_whitelists.html
+++ b/users/templates/users/aff_whitelists.html
@@ -14,7 +14,7 @@
{{ whitelist.raison }} |
{{ whitelist.date_start }} |
{{ whitelist.date_end }} |
- Editer |
+ Editer |
{% endfor %}
diff --git a/users/templates/users/index.html b/users/templates/users/index.html
index 34a4f3d7..690399d2 100644
--- a/users/templates/users/index.html
+++ b/users/templates/users/index.html
@@ -4,6 +4,7 @@
{% block title %}Utilisateurs{% endblock %}
{% block content %}
+ Adhérents
{% include "users/aff_users.html" with users_list=users_list %}
diff --git a/users/templates/users/index_ban.html b/users/templates/users/index_ban.html
new file mode 100644
index 00000000..86428706
--- /dev/null
+++ b/users/templates/users/index_ban.html
@@ -0,0 +1,13 @@
+{% extends "users/sidebar.html" %}
+{% load bootstrap3 %}
+
+{% block title %}Utilisateurs{% endblock %}
+
+{% block content %}
+ Bannissements
+ {% include "users/aff_bans.html" with ban_list=ban_list %}
+
+
+
+{% endblock %}
+
diff --git a/users/templates/users/index_whitelist.html b/users/templates/users/index_whitelist.html
new file mode 100644
index 00000000..009fb06f
--- /dev/null
+++ b/users/templates/users/index_whitelist.html
@@ -0,0 +1,13 @@
+{% extends "users/sidebar.html" %}
+{% load bootstrap3 %}
+
+{% block title %}Utilisateurs{% endblock %}
+
+{% block content %}
+ Accès à titre gracieux
+ {% include "users/aff_whitelists.html" with white_list=white_list %}
+
+
+
+{% endblock %}
+
diff --git a/users/templates/users/profil.html b/users/templates/users/profil.html
index 91097c6b..ea5793fd 100644
--- a/users/templates/users/profil.html
+++ b/users/templates/users/profil.html
@@ -5,7 +5,7 @@
{% block content %}
Adhérent
- Editer
+ Editer
Changer le mot de passe
Changer le statut
Ajouter un droit
@@ -74,7 +74,7 @@
{% endif %}
Droits |
{% if list_droits %}
- {% for droit in list_droits %}{{ droit.right }} - {% endfor %} |
+ {% for droit in list_droits %}{{ droit.right }}{% if list_droits|length != forloop.counter %} - {% endif %} {% endfor %} |
{% else %}
Aucun |
{% endif %}
@@ -101,7 +101,7 @@
Aucun bannissement
{% endif %}
Accès à titre gracieux :
-
+
{% if white_list %}
{% include "users/aff_whitelists.html" with white_list=white_list %}
{% else %}
diff --git a/users/templates/users/sidebar.html b/users/templates/users/sidebar.html
index a976d4ac..d4d34a19 100644
--- a/users/templates/users/sidebar.html
+++ b/users/templates/users/sidebar.html
@@ -2,9 +2,10 @@
{% block sidebar %}
Créer un adhérent
- Editer un adhérent
Liste des adhérents
- Ajouter un bannissement
- Gérer les bannissements
+ Liste des bannissements
+ Liste des accès à titre gracieux
+ Ajouter un établissement
+ Supprimer un établissement
Retirer un droit rezo
{% endblock %}
diff --git a/users/urls.py b/users/urls.py
index e4a92a27..616f3a88 100644
--- a/users/urls.py
+++ b/users/urls.py
@@ -13,7 +13,11 @@ urlpatterns = [
url(r'^edit_whitelist/(?P[0-9]+)$', views.edit_whitelist, name='edit-whitelist'),
url(r'^add_right/(?P[0-9]+)$', views.add_right, name='add-right'),
url(r'^del_right/$', views.del_right, name='del-right'),
- url(r'^profil/$', views.profil, name='profil'),
+ url(r'^add_school/$', views.add_school, name='add-school'),
+ url(r'^del_school/$', views.del_school, name='del-school'),
+ url(r'^profil/(?P[0-9]+)$', views.profil, name='profil'),
+ url(r'^index_ban/$', views.index_ban, name='index-ban'),
+ url(r'^index_white/$', views.index_white, name='index-white'),
url(r'^$', views.index, name='index'),
]
diff --git a/users/views.py b/users/views.py
index d29e0621..c3e60a9b 100644
--- a/users/views.py
+++ b/users/views.py
@@ -6,11 +6,11 @@ from django.shortcuts import render_to_response, get_object_or_404
from django.core.context_processors import csrf
from django.template import Context, RequestContext, loader
from django.contrib import messages
-from django.db.models import Max
+from django.db.models import Max, ProtectedError
from django.db import IntegrityError
from django.utils import timezone
-from users.models import User, Right, Ban, DelRightForm, UserForm, InfoForm, PasswordForm, StateForm, RightForm, BanForm, ProfilForm, Whitelist, WhitelistForm
+from users.models import User, Right, Ban, DelRightForm, UserForm, InfoForm, PasswordForm, StateForm, RightForm, BanForm, ProfilForm, Whitelist, WhitelistForm, DelSchoolForm, SchoolForm
from cotisations.models import Facture
from machines.models import Machine, Interface
from users.forms import PassForm
@@ -62,10 +62,13 @@ def is_whitelisted(user):
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) or is_whitelisted(user)):
- return True
- else:
- return False
+ return user.state == User.STATE_ACTIVE and not is_ban(user) and ( is_adherent(user) or is_whitelisted(user))
+
+def is_active(interface):
+ """ Renvoie si une interface doit avoir accès ou non """
+ machine = interface.machine
+ user = machine.user
+ return machine.active and has_access(user)
def form(ctx, template, request):
c = ctx
@@ -214,31 +217,63 @@ def edit_whitelist(request, whitelistid):
return redirect("/users/")
return form({'userform': whitelist}, 'users/user.html', request)
+def add_school(request):
+ school = SchoolForm(request.POST or None)
+ if school.is_valid():
+ school.save()
+ messages.success(request, "L'établissement a été ajouté")
+ return redirect("/users/")
+ return form({'userform': school}, 'users/user.html', request)
+
+def del_school(request):
+ school = DelSchoolForm(request.POST or None)
+ if school.is_valid():
+ school_dels = school.cleaned_data['schools']
+ for school_del in school_dels:
+ try:
+ school_del.delete()
+ messages.success(request, "L'établissement a été supprimé")
+ except ProtectedError:
+ messages.error(request, "L'établissement %s est affecté à au moins un user, vous ne pouvez pas le supprimer" % school_del)
+ return redirect("/users/")
+ return form({'userform': school}, 'users/user.html', request)
+
def index(request):
users_list = User.objects.order_by('pk')
connexion = []
for user in users_list:
- connexion.append([user, has_access(user)])
+ end = end_adhesion(user)
+ access = has_access(user)
+ if(end!=None):
+ connexion.append([user, access, end])
+ else:
+ connexion.append([user, access, "Non adhérent"])
return render(request, 'users/index.html', {'users_list': connexion})
-def profil(request):
- if request.method == 'POST':
- profil = ProfilForm(request.POST or None)
- if profil.is_valid():
- profils = profil.cleaned_data['user']
- users = User.objects.get(pseudo = profils)
- 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)
- whitelists = Whitelist.objects.filter(user__pseudo = users)
- end_bans = None
- end_whitelists = None
- if(is_ban(users)):
- end_bans=end_ban(users)
- if(is_whitelisted(users)):
- end_whitelists=end_whitelist(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 redirect("/users/")
- return redirect("/users/")
+def index_ban(request):
+ ban_list = Ban.objects.order_by('date_start')
+ return render(request, 'users/index_ban.html', {'ban_list':ban_list})
+
+def index_white(request):
+ white_list = Whitelist.objects.order_by('date_start')
+ return render(request, 'users/index_whitelist.html', {'white_list':white_list})
+
+def profil(request, userid):
+ try:
+ users = User.objects.get(pk=userid)
+ except User.DoesNotExist:
+ messages.error(request, u"Utilisateur inexistant" )
+ return redirect("/users/")
+ 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)
+ whitelists = Whitelist.objects.filter(user__pseudo = users)
+ end_bans = None
+ end_whitelists = None
+ if(is_ban(users)):
+ end_bans=end_ban(users)
+ if(is_whitelisted(users)):
+ end_whitelists=end_whitelist(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})