389 lines
14 KiB
Python
389 lines
14 KiB
Python
from django.shortcuts import render, redirect
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
from django.contrib.auth.models import User
|
|
from django.views.generic import CreateView, ListView, UpdateView, DetailView, DeleteView, View
|
|
from .models import Standards
|
|
from django.contrib import messages
|
|
from django.http import HttpResponse, JsonResponse
|
|
from .forms import StandardAddStandard, StandardAddStandardEditor, StandardUpdateStandard, StandardUpdateStandardEditor
|
|
from django.contrib.auth.decorators import login_required
|
|
from tasks.models import Tasks
|
|
from areas.models import Areas
|
|
from datetime import datetime
|
|
from users.models import AgencyGroup
|
|
from cloud.models import DataFile, DataDir
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
# ALLE STANDARDS EINER AGENTUR
|
|
class StandardsManagement(LoginRequiredMixin, ListView):
|
|
model = Standards
|
|
# Adding active_link
|
|
# Loading only user same agency
|
|
# Change context and return for template-data
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
# # Get all Users of the Same Agency as logged user
|
|
areas = Areas.objects.filter(agency__pk=self.request.user.profile.agency.pk).order_by("areaorder")
|
|
standards_of_user = Standards.objects.filter(agency__pk=self.request.user.profile.agency.pk, created_standard_by=self.request.user.pk)
|
|
standards_of_agency = Standards.objects.filter(agency__pk=self.request.user.profile.agency.pk, public=True)
|
|
unpubstandards_of_user = Standards.objects.filter(agency__pk=self.request.user.profile.agency.pk, public=False)
|
|
tasks = Tasks.objects.filter(agency__pk=self.request.user.profile.agency.pk)
|
|
context.update({'active_link' : 'standards', 'tasks': tasks, 'unpubstandards_of_user' : unpubstandards_of_user, 'standards_of_agency' : standards_of_agency, 'areas' : areas, 'standards_of_user' : standards_of_user})
|
|
return context
|
|
|
|
|
|
@login_required
|
|
def checkUserDirRights(request, startdir, userid):
|
|
canview = True
|
|
user = User.objects.get(pk=userid)
|
|
usergroups=list(user.groups.all())
|
|
grouptomach = []
|
|
singleObj = DataDir.objects.get(pk=startdir.pk)
|
|
# AGENCYCHECK
|
|
if(singleObj.agency.pk == user.profile.agency.pk):
|
|
|
|
# Get dirs to check
|
|
while( singleObj.is_root != True and canview == True):
|
|
|
|
for g in singleObj.visibleby.all():
|
|
grouptomach.append(g.group)
|
|
|
|
if(len(grouptomach) == 0):
|
|
canview = True
|
|
else:
|
|
if(len(set(usergroups).intersection(grouptomach)) > 0):
|
|
canview = True
|
|
else:
|
|
canview = False
|
|
|
|
grouptomach = []
|
|
singleObj = DataDir.objects.get(pk=singleObj.parent.pk)
|
|
|
|
else:
|
|
canview = False
|
|
return canview
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
def StandardAdd(request):
|
|
if request.method == 'POST':
|
|
normalForm = StandardAddStandard(request.POST, instance=request.user)
|
|
editorForm = StandardAddStandardEditor(request.POST, instance=request.user)
|
|
if editorForm.is_valid() and normalForm.is_valid():
|
|
normalForm.agency = request.user.profile.agency
|
|
normalForm.created_standard_by = request.user
|
|
normalForm.created_standard_date = datetime.now()
|
|
normalForm.published_by = request.user
|
|
normalForm.last_modifed_by = request.user
|
|
normalForm.save()
|
|
editorForm.save()
|
|
new_standard = Standards()
|
|
new_standard.agency = request.user.profile.agency
|
|
new_standard.created_standard_by = request.user
|
|
new_standard.published_by = request.user
|
|
new_standard.last_modified_by = request.user
|
|
new_standard.created_standard_date = datetime.now()
|
|
new_standard.last_modified_on = datetime.now()
|
|
new_standard.task = normalForm.cleaned_data['task']
|
|
new_standard.area = normalForm.cleaned_data['area']
|
|
new_standard.name = normalForm.cleaned_data['name']
|
|
new_standard.content = editorForm.cleaned_data['content']
|
|
new_standard.public = normalForm.cleaned_data['public']
|
|
|
|
new_standard.representative = normalForm.cleaned_data['representative']
|
|
new_standard.executor = normalForm.cleaned_data['executor']
|
|
new_standard.authority = normalForm.cleaned_data['authority']
|
|
|
|
# GROUPS
|
|
new_standard.save()
|
|
|
|
|
|
'''
|
|
|
|
HIER WEITER MACHEN: Gruppen, Dateien und Files werden nicht gespeichert, Länge des übergebenen
|
|
Arrays ist komisch, da Strings ankommen und es umgebaut werden muss. Wenn das Array aber LEER
|
|
ist, gibt es '' zurück und das ist komisch...
|
|
|
|
|
|
'''
|
|
|
|
# ADD GROUPS
|
|
groups = normalForm.cleaned_data['checked_groups'].split(",")
|
|
|
|
|
|
for g in groups:
|
|
if(g.isdigit()):
|
|
new_standard.visibleby.add(AgencyGroup.objects.get(pk=g))
|
|
|
|
# ADD STANDARDS
|
|
standards = normalForm.cleaned_data['added_standards'].split(",")
|
|
for s in standards:
|
|
if(s.isdigit()):
|
|
new_standard.linked_standards.add(Standards.objects.get(pk=s))
|
|
|
|
# ADD FILES
|
|
files = normalForm.cleaned_data['added_files'].split(",")
|
|
|
|
for f in files:
|
|
if(f.isdigit()):
|
|
new_standard.addedfiles.add(DataFile.objects.get(pk=f))
|
|
|
|
tempstandardname = normalForm.cleaned_data['name']
|
|
if(new_standard.public and request.user.has_perm('users.standardmanager')):
|
|
messages.success(request, f'Standard {tempstandardname} hinzugefügt und veröffentlicht.')
|
|
else:
|
|
new_standard.public = False
|
|
new_standard.save()
|
|
messages.success(request, f'Standard {tempstandardname} hinzugefügt! Dieser muss noch veröffentlicht werden.')
|
|
return redirect('standards')
|
|
|
|
else:
|
|
normalForm = StandardAddStandard(instance=request.user)
|
|
editorForm = StandardAddStandardEditor(instance=request.user)
|
|
|
|
'''
|
|
Hier werden nur die Dateien dem aktuellen User zur Auswahl gestellt, auf die er auch Zugriff hat.
|
|
Das geht NICHT rekursiv! Es wird nur das oberste Verzeichnis geprüft! SPÄTER!
|
|
'''
|
|
|
|
possibleFilesByVisible = []
|
|
|
|
allfiles = DataFile.objects.filter(agency=request.user.profile.agency)
|
|
|
|
for f in allfiles:
|
|
actParent = DataDir.objects.get(pk=f.parent.pk)
|
|
if actParent.is_root:
|
|
possibleFilesByVisible.append(f)
|
|
else:
|
|
if(checkUserDirRights(request, actParent, request.user.pk)):
|
|
possibleFilesByVisible.append(f)
|
|
|
|
|
|
|
|
|
|
context = {
|
|
'normalForm' : normalForm,
|
|
'editorForm' : editorForm,
|
|
'active_link' : 'standards',
|
|
'agencygroups' : AgencyGroup.objects.filter(agency=request.user.profile.agency),
|
|
'files' : possibleFilesByVisible,
|
|
'parentid' : list(DataDir.objects.filter(agency=request.user.profile.agency, is_root=True))[0].pk,
|
|
'standards' : Standards.objects.filter(agency=request.user.profile.agency, public=True)
|
|
}
|
|
return render(request, 'standards/standards_add.html', context)
|
|
|
|
|
|
@login_required
|
|
def StandardUpdate(request, id):
|
|
standard = Standards.objects.get(pk=id, agency=request.user.profile.agency)
|
|
if request.method == 'POST':
|
|
#normalForm = StandardUpdateStandard(request.POST, instance=standard)
|
|
normalForm = StandardUpdateStandard(request.POST, instance=standard)
|
|
editorForm = StandardUpdateStandardEditor(request.POST, instance=standard)
|
|
|
|
if editorForm.is_valid() and normalForm.is_valid():
|
|
existing_standard = Standards.objects.get(pk=id)
|
|
existing_standard.last_modified_by = request.user
|
|
existing_standard.last_modified_on = datetime.now()
|
|
existing_standard.task = normalForm.cleaned_data['task']
|
|
existing_standard.area = normalForm.cleaned_data['area']
|
|
existing_standard.name = normalForm.cleaned_data['name']
|
|
existing_standard.content = editorForm.cleaned_data['content']
|
|
|
|
existing_standard.representative = normalForm.cleaned_data['representative']
|
|
existing_standard.executor = normalForm.cleaned_data['executor']
|
|
existing_standard.authority = normalForm.cleaned_data['authority']
|
|
|
|
'''
|
|
|
|
AKTUALISIERUNG
|
|
|
|
Wennn der User selbst den Standard erstellt hat und Rechte zur Standardverwaltung hat,
|
|
dann wird der Status nicht verändert (public bleibt true bzw. false). Hat der User
|
|
aber keine Rechte und ist der Standarf public, wird er auf public=false gesetzt!
|
|
|
|
'''
|
|
if request.user.has_perm('users.standardmanager'):
|
|
messages.success(request, f'Standard {existing_standard.name} aktualisiert!')
|
|
else:
|
|
if existing_standard.public:
|
|
existing_standard.public = False
|
|
messages.warning(request, f'Standard {existing_standard.name} aktualisiert und ist nicht mehr öffentlich, damit Änderungen geprüft werden können.')
|
|
else:
|
|
messages.success(request, f'Standard {existing_standard.name} aktualisiert!')
|
|
existing_standard.save()
|
|
return redirect('/standards')
|
|
|
|
else:
|
|
#normalForm = StandardUpdateStandard(instance=standard)
|
|
normalForm = StandardUpdateStandard(instance=standard)
|
|
editorForm = StandardUpdateStandardEditor(instance=standard)
|
|
|
|
'''
|
|
Hier werden nur die Dateien dem aktuellen User zur Auswahl gestellt, auf die er auch Zugriff hat.
|
|
Das geht NICHT rekursiv! Es wird nur das oberste Verzeichnis geprüft! SPÄTER!
|
|
'''
|
|
|
|
possibleFilesByVisible = []
|
|
|
|
allfiles = DataFile.objects.filter(agency=request.user.profile.agency)
|
|
|
|
for f in allfiles:
|
|
actParent = DataDir.objects.get(pk=f.parent.pk, agency=request.user.profile.agency)
|
|
if actParent.is_root:
|
|
possibleFilesByVisible.append(f)
|
|
else:
|
|
if(checkUserDirRights(request, actParent, request.user.pk) and f not in standard.addedfiles.all()):
|
|
possibleFilesByVisible.append(f)
|
|
|
|
|
|
|
|
|
|
possiblestandards = Standards.objects.filter(agency=request.user.profile.agency, public=True)
|
|
possiblestandards_final = []
|
|
for s in possiblestandards:
|
|
if s not in standard.linked_standards.all():
|
|
possiblestandards_final.append(s)
|
|
|
|
context = {
|
|
'normalForm' : normalForm,
|
|
'editorForm' : editorForm,
|
|
'standard' : standard,
|
|
'active_link' : 'standards',
|
|
'standard_id' : standard.pk,
|
|
'standard_status' : standard.public,
|
|
'possibleFilesByVisible' : possibleFilesByVisible,
|
|
'agencygroups' : AgencyGroup.objects.filter(agency=request.user.profile.agency),
|
|
'parentid' : list(DataDir.objects.filter(agency=request.user.profile.agency, is_root=True))[0].pk,
|
|
'possiblestandards' : possiblestandards_final
|
|
}
|
|
return render(request, 'standards/standards_update.html', context)
|
|
|
|
@login_required
|
|
def load_tasks(request):
|
|
areaid = request.GET.get('areaid')
|
|
tasks = Tasks.objects.filter(area__id=areaid, agency=request.user.profile.agency).order_by('name')
|
|
return render(request, 'standards/standards_tasklist.html', {'tasks': tasks})
|
|
|
|
|
|
class StandardDeleteView(LoginRequiredMixin, DeleteView):
|
|
model = Standards
|
|
success_url = '/standards'
|
|
template_name = 'standards/standard_confirm_delete.html'
|
|
|
|
def delete(self, request, *args, **kwargs):
|
|
standard = Standards.objects.get(pk=kwargs['pk'], agency=request.user.profile.agency)
|
|
response = super(StandardDeleteView, self).delete(request, *args, **kwargs)
|
|
names = standard.name
|
|
messages.success(request, f'Standard ' +names+ ' wurde gelöscht!')
|
|
return response
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(StandardDeleteView, self).get_context_data(**kwargs)
|
|
context['active_link'] = 'standards'
|
|
return context
|
|
|
|
@login_required
|
|
def StandardChangePublic(request, pk):
|
|
standard = Standards.objects.get(pk=pk)
|
|
if standard.public:
|
|
standard.public = False
|
|
messages.warning(request, f'Standard {standard.name} ist nicht mehr öffentlich!')
|
|
else:
|
|
standard.public = True
|
|
messages.success(request, f'Standard {standard.name} wurde veröffentlicht und ist innerhalb der Agentur sichtbar!')
|
|
standard.save()
|
|
return redirect('standards')
|
|
|
|
@login_required
|
|
def StandardSingle(request, pk):
|
|
|
|
# CHECK IF USER HAS RIGHTS TO SEE THIS DIR
|
|
groupsofstandard = Standards.objects.get(pk=pk, agency=request.user.profile.agency)
|
|
|
|
userisingroup = False
|
|
|
|
if len(groupsofstandard.visibleby.all()) == 0:
|
|
userisingroup = True
|
|
else:
|
|
for ag in groupsofstandard.visibleby.all():
|
|
if ag.group in request.user.groups.all():
|
|
userisingroup = True
|
|
|
|
if userisingroup:
|
|
standard = Standards.objects.get(pk=pk)
|
|
context = {
|
|
'active_link':'standards',
|
|
'standard' : standard
|
|
}
|
|
return render(request, 'standards/standards_single.html', context)
|
|
else:
|
|
context = {
|
|
'active_link':'standards'
|
|
}
|
|
return render(request, 'standards/standards_noentrie.html', context)
|
|
|
|
|
|
@login_required
|
|
def StandardArea(request, pk):
|
|
standards = Standards.objects.filter(agency__pk=request.user.profile.agency.pk).filter(area__pk=pk)
|
|
area = Areas.objects.get(pk=pk, agency=request.user.profile.agency)
|
|
context = {
|
|
'active_link':'standards',
|
|
'standards_of_agency_area' : standards,
|
|
'areaid' : pk,
|
|
'areaname' : area.name
|
|
}
|
|
return render(request, 'standards/standard_area.html', context)
|
|
|
|
@login_required
|
|
def StandardTask(request, pk):
|
|
standards = Standards.objects.filter(agency__pk=request.user.profile.agency.pk).filter(task__pk=pk)
|
|
task = Tasks.objects.get(pk=pk, agency=request.user.profile.agency)
|
|
area = Areas.objects.get(pk=task.area.pk, agency=request.user.profile.agency)
|
|
context = {
|
|
'active_link':'standards',
|
|
'standards_of_agency_task' : standards,
|
|
'taskid' : pk,
|
|
'taskname' : task.name,
|
|
'areaid' : area.pk,
|
|
'areaname' : area.name
|
|
}
|
|
return render(request, 'standards/standard_task.html', context)
|
|
|
|
|
|
@login_required
|
|
def updatesbyajax(request, pk):
|
|
if(request.method == "GET"):
|
|
success = True
|
|
workingstandard = Standards.objects.get(pk=pk, agency=request.user.profile.agency)
|
|
# Check for correct user and userrights
|
|
if(request.user.profile.agency == workingstandard.agency and request.user.has_perm("users.standardmanager")):
|
|
# CHANGE GROUP
|
|
# ADD
|
|
if(request.GET["action"] == "s_addgroup"):
|
|
workingstandard.visibleby.add(AgencyGroup.objects.get(pk=request.GET["groupid"], agency=request.user.profile.agency))
|
|
# REMOVE
|
|
elif(request.GET["action"] == "s_remgroup"):
|
|
workingstandard.visibleby.remove(AgencyGroup.objects.get(pk=request.GET["groupid"], agency=request.user.profile.agency))
|
|
# FILES
|
|
# REMOVE
|
|
elif(request.GET["action"] == "s_remfile"):
|
|
workingstandard.addedfiles.remove(DataFile.objects.get(pk=request.GET["fileid"], agency=request.user.profile.agency))
|
|
# ADD
|
|
elif(request.GET["action"] == "s_addfile"):
|
|
workingstandard.addedfiles.add(DataFile.objects.get(pk=request.GET["fileid"], agency=request.user.profile.agency))
|
|
# STANDARD
|
|
# REMOVE
|
|
elif(request.GET["action"] == "s_remstandard"):
|
|
workingstandard.linked_standards.remove(Standards.objects.get(pk=request.GET["standardid"], agency=request.user.profile.agency))
|
|
# ADD
|
|
elif(request.GET["action"] == "s_addstandard"):
|
|
workingstandard.linked_standards.add(Standards.objects.get(pk=request.GET["standardid"], agency=request.user.profile.agency))
|
|
else:
|
|
success = False
|
|
|
|
return JsonResponse({"success" : success}) |