admin.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import autocomplete_light
  4. from django.contrib import admin
  5. from django.contrib import messages
  6. from django.http import HttpResponseRedirect
  7. from .models import MaillingList, MaillingListSubscription, SyncCommandError
  8. import coin.members.admin
  9. class AddMaillingListSubscriptionInline(admin.StackedInline):
  10. model = MaillingListSubscription
  11. extra = 0
  12. fields = ('member', 'maillinglist')
  13. verbose_name_plural = "Ajouter un abonnement à une liste mail"
  14. verbose_name = "abonnement"
  15. form = autocomplete_light.modelform_factory(MaillingListSubscription, fields='__all__')
  16. def get_queryset(self, request):
  17. qs = super(AddMaillingListSubscriptionInline, self).get_queryset(request)
  18. return qs.none()
  19. def has_delete_permission(self, request, obj=None):
  20. return False
  21. class AddSubscriberInline(AddMaillingListSubscriptionInline):
  22. verbose_name_plural = "Ajouter des abonné·e·s"
  23. verbose_name = "abonné·e"
  24. class MaillingListSubscriptionInline(admin.TabularInline):
  25. model = MaillingListSubscription
  26. readonly_fields = ('member', 'maillinglist',)
  27. def has_add_permission(self, request, obj=None):
  28. return False
  29. class SubscribersInline(MaillingListSubscriptionInline):
  30. fields = ('member', 'email', 'maillinglist',)
  31. readonly_fields = ('member', 'email', 'maillinglist',)
  32. verbose_name_plural = "Abonné·e·s"
  33. verbose_name = "abonné·e"
  34. def email(self, instance):
  35. return instance.member.email
  36. class MaillingListAdmin(admin.ModelAdmin):
  37. list_display = ('email', 'verbose_name')
  38. actions = ['sync_to_server']
  39. def sync_to_server(self, request, queryset):
  40. for _list in queryset.all():
  41. try:
  42. _list.sync_to_list_server()
  43. except Exception as e:
  44. messages.error(
  45. request,
  46. 'Impossible de synchroniser la liste {} : "{}"'.format(
  47. _list, e))
  48. else:
  49. messages.success(
  50. request,
  51. 'Liste {} synchronisée vers le serveur'.format(
  52. _list.email))
  53. sync_to_server.short_description = (
  54. 'Synchroniser les listes sélectionnées vers le serveur')
  55. inlines = [AddSubscriberInline, SubscribersInline]
  56. def change_view(self, request, object_id, *args, **kwargs):
  57. try:
  58. return super(MaillingListAdmin, self).change_view(
  59. request, object_id, *args, **kwargs)
  60. except SyncCommandError as e:
  61. try:
  62. ml = MaillingList.objects.get(pk=object_id)
  63. ml_name = "La liste mail « {} »".format(ml.short_name)
  64. except MaillingList.DoesNotExist:
  65. ml_name = "La nouvelle liste mail"
  66. messages.error(
  67. request,
  68. "{} n'a pas pu être synchronisée".format(ml_name) +
  69. " vers le serveur de listes : « {} ».".format(e))
  70. return HttpResponseRedirect(request.path)
  71. admin.site.register(MaillingList, MaillingListAdmin)
  72. class MemberAdmin(coin.members.admin.MemberAdmin):
  73. inlines = coin.members.admin.MemberAdmin.inlines + [
  74. MaillingListSubscriptionInline,
  75. AddMaillingListSubscriptionInline,
  76. ]
  77. def change_view(self, request, *args, **kwargs):
  78. try:
  79. return super(MemberAdmin, self).change_view(
  80. request, *args, **kwargs)
  81. except SyncCommandError as e:
  82. messages.error(
  83. request,
  84. "Les listes mails n'ont pas pu être synchronisées" +
  85. " vers le serveur de listes : « {} ».".format(e))
  86. return HttpResponseRedirect(request.path)
  87. admin.site.unregister(coin.members.admin.Member)
  88. admin.site.register(coin.members.admin.Member, MemberAdmin)