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):
|
|
|
|
mnts = request.user.maintainer_set.all()
|
2017-04-15 16:57:19 +02:00
|
|
|
requestedFromMe = Request.objects.filter(applicant__in=mnts)
|
2017-03-14 20:18:57 +01:00
|
|
|
requestedToMe = Request.objects.filter(provider__in=mnts)
|
2017-03-20 00:16:33 +01:00
|
|
|
requests = (requestedFromMe | requestedToMe).order_by("-lastAction")
|
2017-03-14 20:18:57 +01:00
|
|
|
|
2017-03-27 02:05:57 +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):
|
|
|
|
template_name = "rrequests/request_create.html"
|
|
|
|
form_class = RequestForm
|
|
|
|
|
|
|
|
def get_form_kwargs(self, *args, **kwargs):
|
|
|
|
kwargs = super(RrequestCreate, self).get_form_kwargs(*args, **kwargs)
|
|
|
|
kwargs['user'] = self.request.user
|
|
|
|
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
requestMsg = RequestMessage(
|
|
|
|
request=request,
|
|
|
|
|
|
|
|
creator=formData['applicant'],
|
|
|
|
message=formData['message'],
|
|
|
|
)
|
|
|
|
requestMsg.save()
|
|
|
|
|
|
|
|
return HttpResponseRedirect(request.get_absolute_url())
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def rrequestDetail(request, pk):
|
2017-03-27 02:53:19 +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)
|
2017-03-14 20:18:57 +01:00
|
|
|
|
|
|
|
mnts = request.user.maintainer_set.all()
|
|
|
|
formClass = None
|
|
|
|
provider = None
|
2017-04-15 16:57:19 +02:00
|
|
|
initialFormData = {}
|
2017-03-14 20:18:57 +01:00
|
|
|
if reqObj.provider in mnts:
|
|
|
|
provider = True
|
|
|
|
formClass = ProviderResponseForm
|
2017-04-15 16:57:19 +02:00
|
|
|
initialFormData["createdResources"] = reqObj.grantedResources
|
2017-03-14 20:18:57 +01:00
|
|
|
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()
|
|
|
|
|
2017-03-20 00:16:33 +01:00
|
|
|
if "createdResources" in form.cleaned_data and \
|
|
|
|
form.cleaned_data['createdResources'].strip() != "":
|
|
|
|
reqObj.grantedResources = form.cleaned_data["createdResources"].strip()
|
|
|
|
|
2017-03-14 20:18:57 +01:00
|
|
|
reqObj.lastAction = timezone.now()
|
|
|
|
reqObj.save()
|
|
|
|
|
|
|
|
|
|
|
|
return HttpResponseRedirect(reverse("rrequests:show", args=(pk,)))
|
|
|
|
else:
|
2017-04-15 16:57:19 +02:00
|
|
|
form = formClass(request=reqObj, user=request.user, initial=initialFormData)
|
2017-03-14 20:18:57 +01:00
|
|
|
|
|
|
|
return render(request, "rrequests/request_detail.html", {"request": reqObj, "form": form})
|
|
|
|
|