2018-01-17 00:23:00 +01:00
|
|
|
# This file is part of dnmgmt, a number resource management system
|
|
|
|
# Licensed under GNU General Public License v3 or later
|
|
|
|
# Written by Sebastian Lohff (seba@someserver.de)
|
|
|
|
|
2017-03-14 20:18:57 +01:00
|
|
|
from django.shortcuts import render, get_object_or_404
|
|
|
|
from django.http import HttpResponseRedirect
|
|
|
|
from django.urls import reverse
|
|
|
|
from django.views.generic import FormView
|
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
|
|
from django.contrib.auth.decorators import login_required
|
2017-03-27 02:53:19 +02:00
|
|
|
from django.db.models import Q
|
2017-03-14 20:18:57 +01:00
|
|
|
|
|
|
|
from django.utils import timezone
|
|
|
|
|
|
|
|
from .models import Request, RequestMessage
|
|
|
|
from .forms import RequestForm, ResponseForm, ProviderResponseForm
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def listRequests(request):
|
2019-05-30 22:10:55 +02:00
|
|
|
mnts = request.user.maintainer_set.all()
|
|
|
|
requestedFromMe = Request.objects.filter(applicant__in=mnts)
|
|
|
|
requestedToMe = Request.objects.filter(provider__in=mnts)
|
|
|
|
requests = (requestedFromMe | requestedToMe).order_by("-lastAction")
|
2017-03-14 20:18:57 +01:00
|
|
|
|
2019-05-30 22:10:55 +02:00
|
|
|
return render(request, "rrequests/list.html", {"mnts": mnts, "requests": requests, "requestedFromMe": requestedFromMe, "requestedToMe": requestedToMe})
|
2017-03-14 20:18:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
class RrequestCreate(LoginRequiredMixin, FormView):
|
2019-05-30 22:10:55 +02:00
|
|
|
template_name = "rrequests/request_create.html"
|
|
|
|
form_class = RequestForm
|
2017-03-14 20:18:57 +01:00
|
|
|
|
2019-05-30 22:10:55 +02:00
|
|
|
def get_form_kwargs(self, *args, **kwargs):
|
|
|
|
kwargs = super(RrequestCreate, self).get_form_kwargs(*args, **kwargs)
|
|
|
|
kwargs['user'] = self.request.user
|
2017-03-14 20:18:57 +01:00
|
|
|
|
2019-05-30 22:10:55 +02:00
|
|
|
return kwargs
|
2017-03-14 20:18:57 +01:00
|
|
|
|
2019-05-30 22:10:55 +02:00
|
|
|
def form_valid(self, form):
|
|
|
|
formData = form.cleaned_data
|
|
|
|
print(formData)
|
|
|
|
request = Request(
|
|
|
|
subject=formData['subject'],
|
|
|
|
status=Request.STATE_OPEN,
|
|
|
|
applicant=formData['applicant'],
|
|
|
|
provider=formData['provider'],
|
|
|
|
)
|
|
|
|
request.save()
|
2017-03-14 20:18:57 +01:00
|
|
|
|
2019-05-30 22:10:55 +02:00
|
|
|
requestMsg = RequestMessage(
|
|
|
|
request=request,
|
2017-03-14 20:18:57 +01:00
|
|
|
|
2019-05-30 22:10:55 +02:00
|
|
|
creator=formData['applicant'],
|
|
|
|
message=formData['message'],
|
|
|
|
)
|
|
|
|
requestMsg.save()
|
2017-03-14 20:18:57 +01:00
|
|
|
|
2019-05-30 22:10:55 +02:00
|
|
|
return HttpResponseRedirect(request.get_absolute_url())
|
2017-03-14 20:18:57 +01:00
|
|
|
|
|
|
|
@login_required
|
|
|
|
def rrequestDetail(request, pk):
|
2019-05-30 22:10:55 +02:00
|
|
|
mnts = request.user.maintainer_set.all()
|
|
|
|
reqObj = get_object_or_404(Request.objects.filter(Q(provider__in=mnts) | Q(applicant__in=mnts)), pk=pk)
|
|
|
|
|
|
|
|
mnts = request.user.maintainer_set.all()
|
|
|
|
formClass = None
|
|
|
|
provider = None
|
|
|
|
initialFormData = {}
|
|
|
|
if reqObj.provider in mnts:
|
|
|
|
provider = True
|
|
|
|
formClass = ProviderResponseForm
|
|
|
|
initialFormData["createdResources"] = reqObj.grantedResources
|
|
|
|
else:
|
|
|
|
provider = False
|
|
|
|
formClass = ResponseForm
|
|
|
|
|
|
|
|
if request.method == "POST":
|
|
|
|
form = formClass(request=reqObj, user=request.user, data=request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
# create message object
|
|
|
|
msg = RequestMessage(
|
|
|
|
request=reqObj,
|
|
|
|
creator=reqObj.provider if provider else reqObj.applicant,
|
|
|
|
message=form.cleaned_data['message']
|
|
|
|
)
|
|
|
|
if form.cleaned_data['new_status'] != "KEEP":
|
|
|
|
msg.statusChanged = form.cleaned_data['new_status']
|
|
|
|
reqObj.status = msg.statusChanged
|
|
|
|
|
|
|
|
msg.save()
|
|
|
|
|
|
|
|
if "createdResources" in form.cleaned_data and \
|
|
|
|
form.cleaned_data['createdResources'].strip() != "":
|
|
|
|
reqObj.grantedResources = form.cleaned_data["createdResources"].strip()
|
|
|
|
|
|
|
|
reqObj.lastAction = timezone.now()
|
|
|
|
reqObj.save()
|
|
|
|
|
|
|
|
|
|
|
|
return HttpResponseRedirect(reverse("rrequests:show", args=(pk,)))
|
|
|
|
else:
|
|
|
|
form = formClass(request=reqObj, user=request.user, initial=initialFormData)
|
|
|
|
|
|
|
|
return render(request, "rrequests/request_detail.html", {"request": reqObj, "form": form})
|
2017-03-14 20:18:57 +01:00
|
|
|
|