185 lines
6.6 KiB
Python
185 lines
6.6 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
|
|
|
|
# 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
|
|
standards_of_agency = Standards.objects.filter(agency__pk=self.request.user.profile.agency.pk)
|
|
context.update({'active_link' : 'standards', 'standards_of_agency' : standards_of_agency})
|
|
return context
|
|
|
|
|
|
@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.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.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.save()
|
|
tempstandardname = normalForm.cleaned_data['name']
|
|
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)
|
|
|
|
|
|
context = {
|
|
'normalForm' : normalForm,
|
|
'editorForm' : editorForm,
|
|
'active_link' : 'standards'
|
|
}
|
|
return render(request, 'standards/standards_add.html', context)
|
|
|
|
|
|
@login_required
|
|
def StandardUpdate(request, id):
|
|
standard = Standards.objects.get(pk=id)
|
|
if request.method == 'POST':
|
|
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']
|
|
'''
|
|
|
|
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.standard_management'):
|
|
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)
|
|
editorForm = StandardUpdateStandardEditor(instance=standard)
|
|
|
|
|
|
context = {
|
|
'normalForm' : normalForm,
|
|
'editorForm' : editorForm,
|
|
'active_link' : 'standards',
|
|
'standard_id' : standard.pk,
|
|
'standard_status' : standard.public
|
|
}
|
|
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).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 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):
|
|
standard = Standards.objects.get(pk=pk)
|
|
context = {
|
|
'active_link':'standards',
|
|
'standard' : standard
|
|
}
|
|
return render(request, 'standards/standards_single.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)
|
|
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)
|
|
area = Areas.objects.get(pk=task.area.pk)
|
|
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)
|
|
|
|
|