158 lines
5.7 KiB
Python
158 lines
5.7 KiB
Python
from django import forms
|
|
from django.db import models
|
|
from django.contrib.auth.models import User
|
|
from django.contrib.auth.forms import UserCreationForm
|
|
from .models import Profile, Agency
|
|
from django.contrib.auth.models import Permission
|
|
from areas.models import Areas
|
|
from tasks.models import Tasks
|
|
from PIL import Image
|
|
|
|
|
|
# Standard-User-Formular - NUR Username und Password wird hier genutzt
|
|
class UsersAddNewUser(UserCreationForm):
|
|
email = models.EmailField()
|
|
|
|
class Meta:
|
|
model = User
|
|
fields = ['username', 'first_name', 'last_name', 'password1', 'password2', 'email']
|
|
|
|
# Change logged Users Data (Usernamen an Email) NUR HIER MÖGLICH!
|
|
class UsersChangeProfil(forms.ModelForm):
|
|
email = forms.EmailField()
|
|
|
|
class Meta:
|
|
model = User
|
|
fields = ['first_name', 'last_name', 'username', 'email']
|
|
|
|
# Formular zum hinzufügen neuer Agentur-Mitglieder
|
|
class UsersAddProfileForm(forms.ModelForm):
|
|
x = forms.FloatField(widget=forms.HiddenInput())
|
|
y = forms.FloatField(widget=forms.HiddenInput())
|
|
width = forms.FloatField(widget=forms.HiddenInput())
|
|
height = forms.FloatField(widget=forms.HiddenInput())
|
|
|
|
class Meta:
|
|
model = Profile
|
|
labels = {
|
|
"phoneland" : "Telefon",
|
|
"phonemobile" : "Mobil",
|
|
"func" : "Agenturfunktion",
|
|
"compfunc" : "Tätigkeit",
|
|
"image" : "Profilbild",
|
|
"visible" : "Im Organigramm sichtbar"
|
|
}
|
|
|
|
fields = ['phoneland','phonemobile', 'visible', 'func', 'compfunc', 'image', 'x', 'y', 'width', 'height']
|
|
|
|
def save(self):
|
|
photo = super(UsersAddProfileForm, self).save()
|
|
try:
|
|
x = self.cleaned_data.get('x')
|
|
y = self.cleaned_data.get('y')
|
|
w = self.cleaned_data.get('width')
|
|
h = self.cleaned_data.get('height')
|
|
image = Image.open(photo.image)
|
|
cropped_image = image.crop((x, y, w+x, h+y))
|
|
resized_image = cropped_image.resize((300, 300), Image.ANTIALIAS)
|
|
resized_image.save(photo.image.path)
|
|
return photo
|
|
except:
|
|
print("no photo")
|
|
|
|
# Formular zum hinzufügen neuer Agentur-Mitglieder
|
|
class AgencyUpdateForm(forms.ModelForm):
|
|
|
|
x = forms.FloatField(widget=forms.HiddenInput())
|
|
y = forms.FloatField(widget=forms.HiddenInput())
|
|
width = forms.FloatField(widget=forms.HiddenInput())
|
|
height = forms.FloatField(widget=forms.HiddenInput())
|
|
rotation = forms.FloatField(widget=forms.HiddenInput())
|
|
class Meta:
|
|
model = Agency
|
|
labels = {
|
|
"name" : "Agenturname",
|
|
"inhaber" : "Inhaber",
|
|
"street" : "Straße und Hausnummer",
|
|
"plz" : "PLZ",
|
|
"city" : "Stadt",
|
|
"agency_email" : "E-Mail",
|
|
"phone" : "Telefon",
|
|
"dynamicprofile" : "Dynamischer Steckbrief",
|
|
"agencypic" : "Agenturbild"
|
|
}
|
|
fields = ['name','inhaber','agency_email', 'phone', 'street', 'plz', 'city', 'dynamicprofile', 'agencypic', 'x', 'y', 'width', 'height', 'rotation']
|
|
|
|
def save(self):
|
|
photo = super(AgencyUpdateForm, self).save()
|
|
try:
|
|
x = self.cleaned_data.get('x')
|
|
y = self.cleaned_data.get('y')
|
|
w = self.cleaned_data.get('width')
|
|
h = self.cleaned_data.get('height')
|
|
rotation = self.cleaned_data.get('rotation')
|
|
image = Image.open(photo.agencypic)
|
|
rotatet_image = image.rotate(rotation, expand=True)
|
|
cropped_image = rotatet_image.crop((x, y, w+x, h+y))
|
|
#resized_image = cropped_image.resize((w, h), Image.ANTIALIAS)
|
|
cropped_image.save(photo.agencypic.path)
|
|
return photo
|
|
except:
|
|
print("no photo")
|
|
|
|
|
|
######################## USERSPERMFORM LÖSCHEN WEIL IN AGENCYGROUP ################################
|
|
# PERMISSION USER FORM
|
|
class UsersPermForm(forms.Form):
|
|
'''
|
|
Permission-System
|
|
|
|
Persmissions werden im Model gesetzt, hier automatisch als Form ausgegeben.
|
|
Hat der Nutzer eine der genannten Rechte, wird die Checkbox automatisch TRUE gesetzt.
|
|
Die erstellen Felder werden entsprechend den Feldern hinzugefügt und ausgegeben.
|
|
|
|
@param: user
|
|
- User ist der aufgerufene User!
|
|
'''
|
|
def __init__(self, user, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
temprof = Profile
|
|
for ele in temprof._meta.permissions:
|
|
if isinstance(user, User):
|
|
if user.has_perm('users.' + ele[0]):
|
|
self.fields[ele[0]] = forms.BooleanField(required=False, initial=True, help_text=(ele[1]))
|
|
else:
|
|
self.fields[ele[0]] = forms.BooleanField(required=False, initial=False, help_text=(ele[1]))
|
|
|
|
class UserAreaTaskForm(forms.Form):
|
|
def __init__(self, user, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
areas = Areas.objects.filter(agency__pk=user.profile.agency.pk)
|
|
tasks = Tasks.objects.filter(agency__pk=user.profile.agency.pk)
|
|
|
|
for area in areas:
|
|
if user in area.usersfield.all():
|
|
self.fields['area_'+str(area.pk)] = forms.BooleanField(required=False, initial=True, label="<h3>Bereich "+area.name+"</h3>")
|
|
else:
|
|
self.fields['area_'+str(area.pk)] = forms.BooleanField(required=False, initial=False, label="<h3>Bereich "+area.name+"</h3>")
|
|
|
|
for task in tasks:
|
|
if task.area == area:
|
|
if user in task.usersfield.all():
|
|
self.fields['task_'+str(task.pk)] = forms.BooleanField(required=False, initial=True, label="<h5>"+task.name+"</h5>")
|
|
else:
|
|
self.fields['task_'+str(task.pk)] = forms.BooleanField(required=False, initial=False, label="<h5>"+task.name+"</h5>")
|
|
|
|
|
|
class SupportForm(forms.Form):
|
|
def __init__(self, user, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
user_name = user.first_name + " " + user.last_name
|
|
self.fields['name'] = forms.CharField(required=True, label="Name", initial=user_name)
|
|
self.fields['mail'] = forms.EmailField(required=True, label="E-Mail", initial=user.email)
|
|
self.fields['problemconc'] = forms.CharField(required=True, label="Problemzusammenfassung")
|
|
self.fields['problem'] = forms.CharField(required=True, widget=forms.Textarea, label="Ausführliche Beschreibung")
|
|
self.fields['name'].widget.attrs['readonly'] = True
|
|
self.fields['mail'].widget.attrs['readonly'] = True |