admin.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # -*- coding: utf-8 -*-
  2. from django.conf.urls import url
  3. from django.contrib import admin, messages
  4. from django.http import HttpResponseNotAllowed
  5. from django.core.urlresolvers import reverse
  6. from django.shortcuts import redirect, render
  7. from django.template.loader import get_template
  8. from django.template import engines as template_engines
  9. from django.utils.html import format_html
  10. from .emails import send_contributor_email
  11. from .forms import ReminderForm
  12. from .tokens import ContribTokenManager
  13. from .models import Contrib
  14. django_templates = template_engines['django']
  15. # Kinda hackish to do that here
  16. admin.site.site_header = "Administration − Wifi with me"
  17. admin.site.site_title = "Wifi with me"
  18. def send_reminder_action(modeladmin, request, queryset):
  19. selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
  20. return redirect(reverse(
  21. "admin:send_contrib_reminder",
  22. args=[",".join(selected)],
  23. ))
  24. send_reminder_action.short_description = "Renvoyer le lien de gestion par mail"
  25. @admin.register(Contrib)
  26. class ContribAdmin(admin.ModelAdmin):
  27. search_fields = ["name", "email", "phone"]
  28. list_display = ("name", "date", "phone", "email", "expired_string")
  29. readonly_fields = ['date', 'expiration_date']
  30. fieldsets = [
  31. [None, {
  32. 'fields': [
  33. ('name', 'contrib_type'),
  34. 'comment', 'email', 'phone',
  35. ('date', 'expiration_date'),
  36. ],
  37. }],
  38. ['Localisation', {
  39. 'fields': [
  40. ('latitude', 'longitude'),
  41. ('floor', 'floor_total'),
  42. 'orientations', 'roof']
  43. }],
  44. ['Partage de connexion', {
  45. 'fields': ['access_type'],
  46. 'classes': ['collapse'],
  47. }],
  48. ['Vie privée', {
  49. 'fields': [
  50. 'privacy_name', 'privacy_email', 'privacy_coordinates',
  51. 'privacy_place_details', 'privacy_comment'
  52. ],
  53. 'classes': ['collapse'],
  54. }]
  55. ]
  56. actions = [send_reminder_action]
  57. def expired_string(self, obj):
  58. if obj.is_expired():
  59. return format_html('<strong style="color: red; cursor: help;" title="Cette entrée excède la durée de rétention et aurait dû être supprimée automatiquement.">expiré</strong>')
  60. else:
  61. return 'non expiré'
  62. expired_string.short_description = 'Expiration'
  63. def send_reminder(self, request, pks):
  64. contribs = Contrib.objects.filter(pk__in=pks.split(','))
  65. contribs_with_email = contribs.exclude(email='')
  66. contribs_without_email = contribs.filter(email='')
  67. if request.method not in ('GET', 'POST'):
  68. return HttpResponseNotAllowed()
  69. if request.method == 'POST':
  70. form = ReminderForm(request.POST)
  71. if form.is_valid():
  72. for contrib in contribs:
  73. send_contributor_email(
  74. contrib,
  75. django_templates.from_string(form.cleaned_data['mail_subject']),
  76. django_templates.from_string(form.cleaned_data['mail_body']),
  77. ContribTokenManager().mk_token(contrib),
  78. request,
  79. )
  80. messages.add_message(
  81. request, messages.INFO,
  82. 'Mail de rappel bien envoyé à {} destinataires.'.format(
  83. contribs_with_email.count()
  84. )
  85. )
  86. return redirect('admin:contribmap_contrib_changelist')
  87. elif request.method == 'GET':
  88. form = ReminderForm(initial={
  89. 'mail_subject': get_template(
  90. 'contribmap/mails/link_reminder.subject'
  91. ).template.source,
  92. 'mail_body': get_template(
  93. 'contribmap/mails/link_reminder.txt'
  94. ).template.source,
  95. })
  96. if contribs_with_email.exists():
  97. if contribs_without_email.exists():
  98. messages.add_message(
  99. request, messages.WARNING,
  100. "Certaines contributions ne mentionnent"
  101. + " pas d'email : {}.".format(
  102. ', '.join(['« {} »'.format(i) for i in contribs_without_email]))
  103. + " Astuce : il doit y avoir un n° de téléphone …"
  104. )
  105. # case of form display or re-display (errors)
  106. return render(
  107. request, 'admin/contribmap/contrib/send_reminder.html', {
  108. 'contribs': contribs_with_email,
  109. 'form': form,
  110. }
  111. )
  112. else:
  113. messages.add_message(
  114. request, messages.ERROR,
  115. "Aucune des contributions sélectionnées ne mentionne une adresse"
  116. " mail ; impossible d'envoyer un email de rappel."
  117. )
  118. return redirect('admin:contribmap_contrib_changelist')
  119. def get_urls(self):
  120. return super().get_urls() + [
  121. url(
  122. r'^remind/(?P<pks>(\d+,?)+)',
  123. self.admin_site.admin_view(self.send_reminder),
  124. name='send_contrib_reminder',
  125. ),
  126. ]