mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-12-23 23:43:47 +00:00
Restreint les champs possibles pour les users non cableurs
This commit is contained in:
parent
61126e0173
commit
8833f5fe3b
4 changed files with 44 additions and 23 deletions
|
@ -17,6 +17,10 @@ class NewMachineForm(EditMachineForm):
|
|||
class Meta(EditMachineForm.Meta):
|
||||
fields = ['type','name']
|
||||
|
||||
class BaseEditMachineForm(EditMachineForm):
|
||||
class Meta(EditMachineForm.Meta):
|
||||
fields = ['type','name','active']
|
||||
|
||||
class EditInterfaceForm(ModelForm):
|
||||
class Meta:
|
||||
model = Interface
|
||||
|
@ -39,6 +43,14 @@ class NewInterfaceForm(EditInterfaceForm):
|
|||
class Meta(EditInterfaceForm.Meta):
|
||||
fields = ['mac_address','dns','details']
|
||||
|
||||
class BaseEditInterfaceForm(EditInterfaceForm):
|
||||
class Meta(EditInterfaceForm.Meta):
|
||||
fields = ['ipv4','mac_address','dns','details']
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(BaseEditInterfaceForm, self).__init__(*args, **kwargs)
|
||||
self.fields['ipv4'].empty_label = "Assignation automatique de l'ipv4"
|
||||
|
||||
class MachineTypeForm(ModelForm):
|
||||
class Meta:
|
||||
model = MachineType
|
||||
|
|
|
@ -9,7 +9,7 @@ from django.contrib import messages
|
|||
from django.contrib.auth.decorators import login_required, permission_required
|
||||
from django.db.models import ProtectedError
|
||||
|
||||
from .forms import NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm, NewInterfaceForm, MachineTypeForm, DelMachineTypeForm, ExtensionForm, DelExtensionForm
|
||||
from .forms import NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm, NewInterfaceForm, MachineTypeForm, DelMachineTypeForm, ExtensionForm, DelExtensionForm, BaseEditInterfaceForm, BaseEditMachineForm
|
||||
from .models import Machine, Interface, IpList, MachineType, Extension
|
||||
from users.models import User
|
||||
|
||||
|
@ -82,16 +82,20 @@ def edit_machine(request, interfaceid):
|
|||
except Interface.DoesNotExist:
|
||||
messages.error(request, u"Interface inexistante" )
|
||||
return redirect("/machines")
|
||||
if not request.user.has_perms(('cableur',)) and str(interface.machine.user.id)!=str(request.user.id):
|
||||
messages.error(request, "Vous ne pouvez pas éditer une machine d'un autre user que vous sans droit")
|
||||
return redirect("/users/profil/" + str(request.user.id))
|
||||
machine_form = EditMachineForm(request.POST or None, instance=interface.machine)
|
||||
interface_form = EditInterfaceForm(request.POST or None, instance=interface)
|
||||
if not request.user.has_perms(('cableur',)):
|
||||
if str(interface.machine.user.id)!=str(request.user.id):
|
||||
messages.error(request, "Vous ne pouvez pas éditer une machine d'un autre user que vous sans droit")
|
||||
return redirect("/users/profil/" + str(request.user.id))
|
||||
machine_form = BaseEditMachineForm(request.POST or None, instance=interface.machine)
|
||||
interface_form = BaseEditInterfaceForm(request.POST or None, instance=interface)
|
||||
else:
|
||||
machine_form = EditMachineForm(request.POST or None, instance=interface.machine)
|
||||
interface_form = EditInterfaceForm(request.POST or None, instance=interface)
|
||||
if machine_form.is_valid() and interface_form.is_valid():
|
||||
machine_form.save()
|
||||
interface_form.save()
|
||||
messages.success(request, "La machine a été modifiée")
|
||||
return redirect("/machines/")
|
||||
return redirect("/users/profil/" + str(interface.machine.user.id))
|
||||
return form({'machineform': machine_form, 'interfaceform': interface_form}, 'machines/machine.html', request)
|
||||
|
||||
@login_required
|
||||
|
@ -101,11 +105,14 @@ def new_interface(request, machineid):
|
|||
except Machine.DoesNotExist:
|
||||
messages.error(request, u"Machine inexistante" )
|
||||
return redirect("/machines")
|
||||
if not request.user.has_perms(('cableur',)) and str(machine.user.id)!=str(request.user.id):
|
||||
messages.error(request, "Vous ne pouvez pas ajouter une interface à une machine d'un autre user que vous sans droit")
|
||||
return redirect("/users/profil/" + str(request.user.id))
|
||||
if not request.user.has_perms(('cableur',)):
|
||||
if str(machine.user.id)!=str(request.user.id):
|
||||
messages.error(request, "Vous ne pouvez pas ajouter une interface à une machine d'un autre user que vous sans droit")
|
||||
return redirect("/users/profil/" + str(request.user.id))
|
||||
machine_form = BaseEditMachineForm(request.POST or None, instance=machine)
|
||||
else:
|
||||
machine_form = EditMachineForm(request.POST or None, instance=machine)
|
||||
interface_form = AddInterfaceForm(request.POST or None)
|
||||
machine_form = EditMachineForm(request.POST or None, instance=machine)
|
||||
if interface_form.is_valid() and machine_form.is_valid():
|
||||
machine_form.save()
|
||||
new_interface = interface_form.save(commit=False)
|
||||
|
|
|
@ -207,12 +207,9 @@ class Whitelist(models.Model):
|
|||
def __str__(self):
|
||||
return str(self.user) + ' ' + str(self.raison)
|
||||
|
||||
|
||||
class InfoForm(ModelForm):
|
||||
force = forms.BooleanField(label="Forcer le déménagement ?", initial=False, required=False)
|
||||
|
||||
class BaseInfoForm(ModelForm):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(InfoForm, self).__init__(*args, **kwargs)
|
||||
super(BaseInfoForm, self).__init__(*args, **kwargs)
|
||||
self.fields['name'].label = 'Nom'
|
||||
self.fields['surname'].label = 'Prénom'
|
||||
self.fields['school'].label = 'Établissement'
|
||||
|
@ -221,11 +218,6 @@ class InfoForm(ModelForm):
|
|||
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):
|
||||
remove_user_room(self.cleaned_data.get('room'))
|
||||
return
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
fields = [
|
||||
|
@ -238,6 +230,13 @@ class InfoForm(ModelForm):
|
|||
'room',
|
||||
]
|
||||
|
||||
class InfoForm(BaseInfoForm):
|
||||
force = forms.BooleanField(label="Forcer le déménagement ?", initial=False, required=False)
|
||||
|
||||
def clean_force(self):
|
||||
if self.cleaned_data.get('force', False):
|
||||
remove_user_room(self.cleaned_data.get('room'))
|
||||
return
|
||||
|
||||
class UserForm(InfoForm):
|
||||
class Meta(InfoForm.Meta):
|
||||
|
|
|
@ -12,7 +12,7 @@ from django.utils import timezone
|
|||
|
||||
from users.models import User, Right, Ban, Whitelist, School
|
||||
from users.models import DelRightForm, BanForm, WhitelistForm, DelSchoolForm
|
||||
from users.models import InfoForm, StateForm, RightForm, SchoolForm
|
||||
from users.models import InfoForm, BaseInfoForm, StateForm, RightForm, SchoolForm
|
||||
from cotisations.models import Facture
|
||||
from machines.models import Machine, Interface
|
||||
from users.forms import PassForm
|
||||
|
@ -112,7 +112,10 @@ def edit_info(request, userid):
|
|||
except User.DoesNotExist:
|
||||
messages.error(request, "Utilisateur inexistant")
|
||||
return redirect("/users/")
|
||||
user = InfoForm(request.POST or None, instance=user)
|
||||
if not request.user.has_perms(('cableur',)):
|
||||
user = BaseInfoForm(request.POST or None, instance=user)
|
||||
else:
|
||||
user = InfoForm(request.POST or None, instance=user)
|
||||
if user.is_valid():
|
||||
user.save()
|
||||
messages.success(request, "L'user a bien été modifié")
|
||||
|
|
Loading…
Reference in a new issue