views.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # coding: utf-8
  2. from __future__ import print_function
  3. from __future__ import absolute_import
  4. from __future__ import division
  5. from __future__ import unicode_literals
  6. from django.utils.translation import ugettext_lazy as _
  7. from django import forms, http
  8. from django.views.generic import TemplateView, FormView, View
  9. from backend.mixins import VisitorMixin
  10. from backend import const
  11. import backend.models as bmodels
  12. import process.models as pmodels
  13. from collections import defaultdict, OrderedDict
  14. class PersonForm(forms.Form):
  15. person = forms.CharField(label=_("Person"), required=False)
  16. class Advocate(VisitorMixin, FormView):
  17. template_name = "wizard/advocate.html"
  18. form_class = PersonForm
  19. def get_context_data(self, **kw):
  20. from django.db.models import Q
  21. ctx = super(Advocate, self).get_context_data(**kw)
  22. people = None
  23. processes = None
  24. if hasattr(ctx["form"], "cleaned_data"):
  25. person = ctx["form"].cleaned_data["person"]
  26. if person:
  27. query = (Q(username__icontains=person) |
  28. Q(cn__icontains=person) | Q(mn__icontains=person) |
  29. Q(sn__icontains=person) | Q(email__icontains=person) |
  30. Q(uid__icontains=person) |
  31. Q(fprs__fpr__icontains=person))
  32. people = OrderedDict()
  33. for p in bmodels.Person.objects.filter(query).order_by("uid", "cn"):
  34. people[p] = p
  35. if people:
  36. processes = OrderedDict()
  37. for process in pmodels.Process.objects.filter(closed__isnull=True).order_by("person__uid", "person__cn", "applying_for"):
  38. person = people.pop(process.person, None)
  39. if person is None: continue
  40. try:
  41. processes[process] = process.requirements.get(type="advocate").get_absolute_url()
  42. except pmodels.Process.DoesNotExist:
  43. processes[process] = None
  44. ctx["people"] = list(people.keys()) if people else None
  45. ctx["processes"] = processes
  46. ctx["wikihelp"] = "https://wiki.debian.org/nm.debian.org/Wizard/Advocate"
  47. return ctx
  48. def form_valid(self, form):
  49. return self.render_to_response(self.get_context_data(form=form))
  50. class NewProcess(VisitorMixin, TemplateView):
  51. template_name = "wizard/newprocess.html"
  52. def get_context_data(self, **kw):
  53. ctx = super(NewProcess, self).get_context_data(**kw)
  54. target = self.kwargs["applying_for"]
  55. comments = []
  56. allowed = False
  57. if target == "dm":
  58. target_desc = "Become Debian Maintainer"
  59. elif target == "ga":
  60. target_desc = "Request guest account"
  61. elif target == "return":
  62. target_desc = "Return from Emeritus"
  63. else:
  64. target_desc = "Become {}".format(const.ALL_STATUS_DESCS[target])
  65. if self.visitor:
  66. whitelist = self.visitor.possible_new_statuses
  67. if target == "dm":
  68. if self.visitor.status in (const.STATUS_DM, const.STATUS_DM_GA):
  69. comments.append("You are already a Debian Maintainer: problem solved!")
  70. elif self.visitor.status not in (const.STATUS_DC, const.STATUS_DC_GA):
  71. comments.append("You are already a {}.".format(const.ALL_STATUS_DESCS[self.visitor.status]))
  72. else:
  73. allowed = const.STATUS_DM in whitelist or const.STATUS_DM_GA in whitelist
  74. if not allowed:
  75. comments.append("You cannot start a new process for Debian Maintainer. Did you already start one?")
  76. elif target == "ga":
  77. if self.visitor.status not in (const.STATUS_DC, const.STATUS_DM):
  78. comments.append("As a {}, you should already have access to porter machines.".format(const.ALL_STATUS_DESCS[self.visitor.status]))
  79. else:
  80. allowed = const.STATUS_DC_GA in whitelist or const.STATUS_DM_GA in whitelist
  81. if not allowed:
  82. comments.append("You cannot request a guest account. Did you already request it?")
  83. elif target == "return":
  84. if self.visitor.status != const.STATUS_EMERITUS_DD:
  85. comments.append("You seem to be {}, not an Emeritus DD.".format(const.ALL_STATUS_DESCS[self.visitor.status]))
  86. else:
  87. allowed = True
  88. else:
  89. if target == self.visitor.status:
  90. comments.append("You are already {}: problem solved!".format(
  91. const.ALL_STATUS_DESCS[target]))
  92. else:
  93. allowed = target in whitelist
  94. if not allowed:
  95. comments.append("You are currently {} and you cannot become {}.".format(
  96. const.ALL_STATUS_DESCS[self.visitor.status],
  97. const.ALL_STATUS_DESCS[target]))
  98. ctx["comments"] = comments
  99. ctx["allowed"] = allowed
  100. ctx["target_desc"] = target_desc
  101. return ctx