admin.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 = 1
  12. max_num = 1
  13. fields = ('member', 'maillinglist')
  14. verbose_name_plural = "Ajouter un abonnement à la liste mail"
  15. verbose_name = "Ajouter un abonnement"
  16. form = autocomplete_light.modelform_factory(MaillingListSubscription, fields='__all__')
  17. def get_queryset(self, request):
  18. qs = super(AddMaillingListSubscriptionInline, self).get_queryset(request)
  19. return qs.none()
  20. def has_delete_permission(self, request, obj=None):
  21. return False
  22. class MaillingListSubscriptionInline(admin.TabularInline):
  23. model = MaillingListSubscription
  24. readonly_fields = ('member', 'maillinglist',)
  25. def has_add_permission(self, request, obj=None):
  26. return False
  27. class MaillingListAdmin(admin.ModelAdmin):
  28. list_display = ('email', 'verbose_name')
  29. actions = ['sync_to_server']
  30. def sync_to_server(self, request, queryset):
  31. for _list in queryset.all():
  32. try:
  33. _list.sync_to_list_server()
  34. except Exception as e:
  35. messages.error(
  36. request,
  37. 'Impossible de synchroniser la liste {} : "{}"'.format(
  38. _list, e))
  39. else:
  40. messages.success(
  41. request,
  42. 'Liste {} synchronisée vers le serveur'.format(
  43. _list.email))
  44. sync_to_server.short_description = (
  45. 'Synchroniser les listes sélectionnées vers le serveur')
  46. inlines = [AddMaillingListSubscriptionInline, MaillingListSubscriptionInline,]
  47. def change_view(self, request, object_id, *args, **kwargs):
  48. try:
  49. return super(MaillingListAdmin, self).change_view(
  50. request, object_id, *args, **kwargs)
  51. except SyncCommandError as e:
  52. try:
  53. ml = MaillingList.objects.get(pk=object_id)
  54. ml_name = "La liste mail « {} »".format(ml.short_name)
  55. except MaillingList.DoesNotExist:
  56. ml_name = "La nouvelle liste mail"
  57. messages.error(
  58. request,
  59. "{} n'a pas pu être synchronisée".format(ml_name) +
  60. " vers le serveur de listes : « {} ».".format(e))
  61. return HttpResponseRedirect(request.path)
  62. admin.site.register(MaillingList, MaillingListAdmin)
  63. class MemberAdmin(coin.members.admin.MemberAdmin):
  64. inlines = coin.members.admin.MemberAdmin.inlines + [AddMaillingListSubscriptionInline, MaillingListSubscriptionInline]
  65. def change_view(self, request, *args, **kwargs):
  66. try:
  67. return super(MemberAdmin, self).change_view(
  68. request, *args, **kwargs)
  69. except SyncCommandError as e:
  70. messages.error(
  71. request,
  72. "Les listes mails n'ont pas pu être synchronisées" +
  73. " vers le serveur de listes : « {} ».".format(e))
  74. return HttpResponseRedirect(request.path)
  75. admin.site.unregister(coin.members.admin.Member)
  76. admin.site.register(coin.members.admin.Member, MemberAdmin)