views.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. import backend.models as bmodels
  7. from backend import const
  8. from django.views.generic import TemplateView, View
  9. from django.views.generic.edit import UpdateView
  10. from backend.mixins import VisitorMixin, VisitPersonMixin
  11. import markdown
  12. import json
  13. class Person(VisitPersonMixin, TemplateView):
  14. template_name = "person/person.html"
  15. def get_context_data(self, **kw):
  16. from django.db.models import Min, Max
  17. ctx = super(Person, self).get_context_data(**kw)
  18. processes = self.person.processes \
  19. .annotate(started=Min("log__logdate"), ended=Max("log__logdate")) \
  20. .order_by("is_active", "ended")
  21. import process.models as pmodels
  22. processes2 = pmodels.Process.objects.filter(person=self.person).order_by("-closed")
  23. adv_processes2 = []
  24. for req in pmodels.Requirement.objects.filter(type="advocate", statements__uploaded_by=self.person).distinct().select_related("process"):
  25. adv_processes2.append(req.process)
  26. if self.person.is_am:
  27. am = self.person.am
  28. am_processes = am.processed \
  29. .annotate(started=Min("log__logdate"), ended=Max("log__logdate")) \
  30. .order_by("is_active", "ended")
  31. am_processes2 = pmodels.Process.objects.filter(ams__am=am).distinct()
  32. else:
  33. am = None
  34. am_processes = []
  35. am_processes2 = []
  36. audit_log = []
  37. if "view_person_audit_log" in self.visit_perms:
  38. is_admin = self.visitor.is_admin
  39. for e in self.person.audit_log.order_by("-logdate"):
  40. if is_admin:
  41. changes = sorted((k, v[0], v[1]) for k, v in json.loads(e.changes).items())
  42. else:
  43. changes = sorted((k, v[0], v[1]) for k, v in json.loads(e.changes).items() if k not in ("fd_comment", "pending"))
  44. audit_log.append({
  45. "logdate": e.logdate,
  46. "author": e.author,
  47. "notes": e.notes,
  48. "changes": changes,
  49. })
  50. ctx.update(
  51. am=am,
  52. processes=processes,
  53. processes2=processes2,
  54. am_processes=am_processes,
  55. am_processes2=am_processes2,
  56. adv_processes=self.person.advocated \
  57. .annotate(started=Min("log__logdate"), ended=Max("log__logdate")) \
  58. .order_by("is_active", "ended"),
  59. adv_processes2=adv_processes2,
  60. audit_log=audit_log,
  61. )
  62. if self.person.bio:
  63. ctx["bio_html"] = markdown.markdown(self.person.bio, safe_mode="escape")
  64. else:
  65. ctx["bio_html"] = ""
  66. return ctx
  67. class EditLDAP(VisitPersonMixin, UpdateView):
  68. """
  69. Edit a person's information
  70. """
  71. require_visit_perms = "edit_ldap"
  72. model = bmodels.Person
  73. fields = ("cn", "mn", "sn", "email_ldap", "uid")
  74. template_name = "person/edit_ldap.html"
  75. def get_object(self):
  76. return self.person
  77. def form_valid(self, form):
  78. """
  79. If the form is valid, save the associated model.
  80. """
  81. self.object = form.save(commit=False)
  82. self.object.save(audit_author=self.visitor, audit_notes="edited LDAP information")
  83. return super(EditLDAP, self).form_valid(form)
  84. class EditBio(VisitPersonMixin, UpdateView):
  85. """
  86. Edit a person's information
  87. """
  88. require_visit_perms = "edit_bio"
  89. model = bmodels.Person
  90. fields = ("bio",)
  91. template_name = "person/edit_bio.html"
  92. def get_object(self):
  93. return self.person
  94. def form_valid(self, form):
  95. """
  96. If the form is valid, save the associated model.
  97. """
  98. self.object = form.save(commit=False)
  99. self.object.save(audit_author=self.visitor, audit_notes="edited bio information")
  100. return super(EditBio, self).form_valid(form)
  101. class EditEmail(VisitPersonMixin, UpdateView):
  102. """
  103. Edit a person's information
  104. """
  105. require_visit_perms = "edit_email"
  106. model = bmodels.Person
  107. fields = ("email",)
  108. template_name = "person/edit_email.html"
  109. def get_object(self):
  110. return self.person
  111. def form_valid(self, form):
  112. """
  113. If the form is valid, save the associated model.
  114. """
  115. self.object = form.save(commit=False)
  116. self.object.save(audit_author=self.visitor, audit_notes="edited email")
  117. return super(EditEmail, self).form_valid(form)