admin.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. verbose_name_plural = "Abonné·e·s"
  31. verbose_name = "abonné·e"
  32. class MaillingListAdmin(admin.ModelAdmin):
  33. list_display = ('email', 'verbose_name')
  34. actions = ['sync_to_server']
  35. def sync_to_server(self, request, queryset):
  36. for _list in queryset.all():
  37. try:
  38. _list.sync_to_list_server()
  39. except Exception as e:
  40. messages.error(
  41. request,
  42. 'Impossible de synchroniser la liste {} : "{}"'.format(
  43. _list, e))
  44. else:
  45. messages.success(
  46. request,
  47. 'Liste {} synchronisée vers le serveur'.format(
  48. _list.email))
  49. sync_to_server.short_description = (
  50. 'Synchroniser les listes sélectionnées vers le serveur')
  51. inlines = [AddSubscriberInline, SubscribersInline]
  52. def change_view(self, request, object_id, *args, **kwargs):
  53. try:
  54. return super(MaillingListAdmin, self).change_view(
  55. request, object_id, *args, **kwargs)
  56. except SyncCommandError as e:
  57. try:
  58. ml = MaillingList.objects.get(pk=object_id)
  59. ml_name = "La liste mail « {} »".format(ml.short_name)
  60. except MaillingList.DoesNotExist:
  61. ml_name = "La nouvelle liste mail"
  62. messages.error(
  63. request,
  64. "{} n'a pas pu être synchronisée".format(ml_name) +
  65. " vers le serveur de listes : « {} ».".format(e))
  66. return HttpResponseRedirect(request.path)
  67. admin.site.register(MaillingList, MaillingListAdmin)
  68. class MemberAdmin(coin.members.admin.MemberAdmin):
  69. inlines = coin.members.admin.MemberAdmin.inlines + [
  70. MaillingListSubscriptionInline,
  71. AddMaillingListSubscriptionInline,
  72. ]
  73. def change_view(self, request, *args, **kwargs):
  74. try:
  75. return super(MemberAdmin, self).change_view(
  76. request, *args, **kwargs)
  77. except SyncCommandError as e:
  78. messages.error(
  79. request,
  80. "Les listes mails n'ont pas pu être synchronisées" +
  81. " vers le serveur de listes : « {} ».".format(e))
  82. return HttpResponseRedirect(request.path)
  83. admin.site.unregister(coin.members.admin.Member)
  84. admin.site.register(coin.members.admin.Member, MemberAdmin)