digitaleagenturnc/standards/views.py

168 lines
6.2 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 datetime import datetime
# Create your views here.
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
'''
class StandardAdd(LoginRequiredMixin, CreateView):
model = Standards
success_url = '/standards'
form_class = StandardAddStandardForm
def get_form_kwargs(self):
kwargs = super(StandardAdd, self).get_form_kwargs()
kwargs['user'] = self.request.user
return kwargs
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({'active_link' : 'standards'})
return context
def form_valid(self, form):
# Send message to the site
messages.success(self.request, f'Standard angelegt!')
form.instance.agency = self.request.user.profile.agency
form.instance.created_standard_by = self.request.user
form.instance.published_by = self.request.user
form.instance.last_modifed_by = self.request.user
return super().form_valid(form)
'''
@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.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']
if request.user.has_perm('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)