No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

views.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # This file is part of dnmgmt, a number resource management system
  2. # Licensed under GNU General Public License v3 or later
  3. # Written by Sebastian Lohff (seba@someserver.de)
  4. from django.shortcuts import render, get_object_or_404
  5. from django.http import HttpResponseRedirect
  6. from django.urls import reverse
  7. from django.views.generic import FormView
  8. from django.contrib.auth.mixins import LoginRequiredMixin
  9. from django.contrib.auth.decorators import login_required
  10. from django.db.models import Q
  11. from django.utils import timezone
  12. from .models import Request, RequestMessage
  13. from .forms import RequestForm, ResponseForm, ProviderResponseForm
  14. @login_required
  15. def listRequests(request):
  16. mnts = request.user.maintainer_set.all()
  17. requestedFromMe = Request.objects.filter(applicant__in=mnts)
  18. requestedToMe = Request.objects.filter(provider__in=mnts)
  19. requests = (requestedFromMe | requestedToMe).order_by("-lastAction")
  20. return render(request, "rrequests/list.html", {"mnts": mnts, "requests": requests, "requestedFromMe": requestedFromMe, "requestedToMe": requestedToMe})
  21. class RrequestCreate(LoginRequiredMixin, FormView):
  22. template_name = "rrequests/request_create.html"
  23. form_class = RequestForm
  24. def get_form_kwargs(self, *args, **kwargs):
  25. kwargs = super(RrequestCreate, self).get_form_kwargs(*args, **kwargs)
  26. kwargs['user'] = self.request.user
  27. return kwargs
  28. def form_valid(self, form):
  29. formData = form.cleaned_data
  30. print(formData)
  31. request = Request(
  32. subject=formData['subject'],
  33. status=Request.STATE_OPEN,
  34. applicant=formData['applicant'],
  35. provider=formData['provider'],
  36. )
  37. request.save()
  38. requestMsg = RequestMessage(
  39. request=request,
  40. creator=formData['applicant'],
  41. message=formData['message'],
  42. )
  43. requestMsg.save()
  44. return HttpResponseRedirect(request.get_absolute_url())
  45. @login_required
  46. def rrequestDetail(request, pk):
  47. mnts = request.user.maintainer_set.all()
  48. reqObj = get_object_or_404(Request.objects.filter(Q(provider__in=mnts) | Q(applicant__in=mnts)), pk=pk)
  49. mnts = request.user.maintainer_set.all()
  50. formClass = None
  51. provider = None
  52. initialFormData = {}
  53. if reqObj.provider in mnts:
  54. provider = True
  55. formClass = ProviderResponseForm
  56. initialFormData["createdResources"] = reqObj.grantedResources
  57. else:
  58. provider = False
  59. formClass = ResponseForm
  60. if request.method == "POST":
  61. form = formClass(request=reqObj, user=request.user, data=request.POST)
  62. if form.is_valid():
  63. # create message object
  64. msg = RequestMessage(
  65. request=reqObj,
  66. creator=reqObj.provider if provider else reqObj.applicant,
  67. message=form.cleaned_data['message']
  68. )
  69. if form.cleaned_data['new_status'] != "KEEP":
  70. msg.statusChanged = form.cleaned_data['new_status']
  71. reqObj.status = msg.statusChanged
  72. msg.save()
  73. if "createdResources" in form.cleaned_data and \
  74. form.cleaned_data['createdResources'].strip() != "":
  75. reqObj.grantedResources = form.cleaned_data["createdResources"].strip()
  76. reqObj.lastAction = timezone.now()
  77. reqObj.save()
  78. return HttpResponseRedirect(reverse("rrequests:show", args=(pk,)))
  79. else:
  80. form = formClass(request=reqObj, user=request.user, initial=initialFormData)
  81. return render(request, "rrequests/request_detail.html", {"request": reqObj, "form": form})