|
@@ -8,148 +8,164 @@ from services.models import ServiceType
|
|
|
from .models import Payment
|
|
|
|
|
|
|
|
|
-class PaymentMixin:
|
|
|
- def get_fields(self, request, obj=None):
|
|
|
- fields = ('amount', 'period', 'payment_method', 'start', 'end')
|
|
|
- if request.user.has_perm('banking.validate_payment'):
|
|
|
- fields += ('validated',)
|
|
|
- return fields
|
|
|
+### Inlines
|
|
|
|
|
|
- def get_queryset(self, request):
|
|
|
- qs = super().get_queryset(request)
|
|
|
- if request.user.has_perm('banking.validate_payment'):
|
|
|
- return qs
|
|
|
- # Show only adherent contribution and payment related to a service for which the user is in managment group.
|
|
|
- user_ctype = ContentType.objects.get(app_label='adhesions', model='adherent')
|
|
|
- return qs.filter(Q(reason_type=user_ctype) | Q(service__service_type__group__in=request.user.groups.all()))
|
|
|
-
|
|
|
-
|
|
|
-class PaymentInline(PaymentMixin, GenericTabularInline):
|
|
|
+class PaymentInline(GenericTabularInline):
|
|
|
model = Payment
|
|
|
ct_field = 'reason_type'
|
|
|
ct_fk_field = 'reason_id'
|
|
|
- extra = 1
|
|
|
-
|
|
|
-
|
|
|
-class ValidatedPaymentInline(PaymentInline):
|
|
|
extra = 0
|
|
|
- verbose_name_plural = 'Paiements validés'
|
|
|
-
|
|
|
- def get_readonly_fields(self, request, obj=None):
|
|
|
- if request.user.has_perm('banking.validate_payment'):
|
|
|
- return ()
|
|
|
- else:
|
|
|
- return self.get_fields(request, obj)
|
|
|
-
|
|
|
- def has_add_permission(self, request):
|
|
|
- return False
|
|
|
-
|
|
|
- def has_delete_permission(self, request, obj=None):
|
|
|
- return request.user.has_perm('banking.validate_payment')
|
|
|
-
|
|
|
- def get_queryset(self, request):
|
|
|
- return super().get_queryset(request).filter(validated=True)
|
|
|
-
|
|
|
-
|
|
|
-class PendingOrNewPaymentInline(PaymentInline):
|
|
|
- verbose_name_plural = 'Paiements en attente de validation et nouveaux paiements'
|
|
|
-
|
|
|
- def get_queryset(self, request):
|
|
|
- return super().get_queryset(request).filter(validated=False)
|
|
|
-
|
|
|
-
|
|
|
-class PaymentTypeFilter(admin.SimpleListFilter):
|
|
|
- title = 'type de paiement'
|
|
|
- parameter_name = 'type'
|
|
|
-
|
|
|
- def lookups(self, request, model_admin):
|
|
|
- choices = [
|
|
|
- ('membership', 'Cotisation'),
|
|
|
- ('service', 'Service'),
|
|
|
- ]
|
|
|
- service_types = ServiceType.objects.all()
|
|
|
- if not (request.user.is_superuser or request.user.has_perm('banking.validate_payment')):
|
|
|
- service_types = service_types.filter(group__in=request.user.groups.all())
|
|
|
- for stype in service_types:
|
|
|
- choices.append((stype.pk, 'Service (%s)' % stype.name))
|
|
|
- return choices
|
|
|
-
|
|
|
- def queryset(self, request, queryset):
|
|
|
- if self.value() == 'membership':
|
|
|
- return queryset.filter(reason_type__app_label='adhesions',
|
|
|
- reason_type__model='adherent')
|
|
|
- if self.value() == 'service':
|
|
|
- return queryset.filter(reason_type__app_label='services',
|
|
|
- reason_type__model='service')
|
|
|
- try:
|
|
|
- service_type = ServiceType.objects.get(pk=int(self.value()))
|
|
|
- except (ValueError, TypeError, ServiceType.DoesNotExist,):
|
|
|
- return queryset
|
|
|
- else:
|
|
|
- return queryset.filter(service__service_type=service_type)
|
|
|
-
|
|
|
-
|
|
|
-def validate_payment(payment, request, queryset):
|
|
|
- queryset.update(validated=True)
|
|
|
-
|
|
|
-
|
|
|
-class PaymentAdmin(PaymentMixin, admin.ModelAdmin):
|
|
|
- list_display_links = None
|
|
|
- list_filter = (PaymentTypeFilter, 'payment_method', 'validated',)
|
|
|
-
|
|
|
- def get_list_display(self, request):
|
|
|
- list_display = ()
|
|
|
- if request.user.has_perm('auth.change_user'):
|
|
|
- list_display += ('adherent_link',)
|
|
|
- else:
|
|
|
- list_display += ('get_adherent',)
|
|
|
- list_display += ('payment_type_verbose', 'amount',
|
|
|
- 'period', 'payment_method', 'validated',)
|
|
|
- if request.user.has_perm('banking.validate_payment'):
|
|
|
- list_display += ('change',)
|
|
|
- else:
|
|
|
- list_display += ('change_pending',)
|
|
|
- return list_display
|
|
|
-
|
|
|
- def adherent_link(self, obj):
|
|
|
- adherent = obj.get_adherent()
|
|
|
- url = reverse('admin:adhesions_adherent_change', args=[adherent.pk])
|
|
|
- return '<a href="%s">%s</a>' % (url, adherent)
|
|
|
- adherent_link.short_description = 'Adhérent'
|
|
|
- adherent_link.allow_tags = True
|
|
|
-
|
|
|
- def change(self, obj):
|
|
|
- url = reverse('admin:banking_payment_change', args=[obj.pk])
|
|
|
- return '<a href="%s" class="changelink">Modifier</a>' % url
|
|
|
- change.short_description = ''
|
|
|
- change.allow_tags = True
|
|
|
-
|
|
|
- def change_pending(self, obj):
|
|
|
- if obj.validated:
|
|
|
- return '-'
|
|
|
- else:
|
|
|
- return self.change(obj)
|
|
|
- change_pending.short_description = ''
|
|
|
- change_pending.allow_tags = True
|
|
|
-
|
|
|
- def get_actions(self, request):
|
|
|
- actions = super().get_actions(request)
|
|
|
- if request.user.has_perm('banking.validate_payment'):
|
|
|
- actions['validate'] = (validate_payment, 'validate', 'Valider les paiements sélectionnés')
|
|
|
- return actions
|
|
|
-
|
|
|
- def has_add_permission(self, request):
|
|
|
- return False
|
|
|
-
|
|
|
- def has_change_permission(self, request, obj=None):
|
|
|
- if obj and not request.user.has_perm('banking.validate_payment'):
|
|
|
- return not obj.validated
|
|
|
- return True
|
|
|
-
|
|
|
- def has_delete_permission(self, request, obj=None):
|
|
|
- if obj and not request.user.has_perm('banking.validate_payment'):
|
|
|
- return not obj.validated
|
|
|
- return False
|
|
|
-
|
|
|
-
|
|
|
-admin.site.register(Payment, PaymentAdmin)
|
|
|
+ #max_num = 0
|
|
|
+ fields = ('amount', 'period', 'payment_method', 'start', 'end',)
|
|
|
+ #readonly_fields = ('amount', 'period', 'payment_method', 'start',)
|
|
|
+ verbose_name_plural = 'Contributions'
|
|
|
+
|
|
|
+ #def get_queryset(self, request):
|
|
|
+ # # Paiement récurrent en cours (sans date de fin)
|
|
|
+ # return super().get_queryset(request).filter(period__gt=0, end__isnull=True)
|
|
|
+
|
|
|
+ #def has_delete_permission(self, request, obj=None):
|
|
|
+ # return False
|
|
|
+
|
|
|
+
|
|
|
+#class ValidatedPaymentInline(PaymentInline):
|
|
|
+# extra = 0
|
|
|
+# verbose_name_plural = 'Paiements validés'
|
|
|
+#
|
|
|
+# def get_readonly_fields(self, request, obj=None):
|
|
|
+# if request.user.has_perm('banking.validate_payment'):
|
|
|
+# return ()
|
|
|
+# else:
|
|
|
+# return self.get_fields(request, obj)
|
|
|
+#
|
|
|
+# def has_add_permission(self, request):
|
|
|
+# return False
|
|
|
+#
|
|
|
+# def has_delete_permission(self, request, obj=None):
|
|
|
+# return request.user.has_perm('banking.validate_payment')
|
|
|
+#
|
|
|
+# def get_queryset(self, request):
|
|
|
+# return super().get_queryset(request).filter(validated=True)
|
|
|
+#
|
|
|
+#
|
|
|
+#class PendingOrNewPaymentInline(PaymentInline):
|
|
|
+# verbose_name_plural = 'Paiements en attente de validation et nouveaux paiements'
|
|
|
+#
|
|
|
+# def get_queryset(self, request):
|
|
|
+# return super().get_queryset(request).filter(validated=False)
|
|
|
+
|
|
|
+
|
|
|
+### Filters
|
|
|
+
|
|
|
+#class PaymentTypeFilter(admin.SimpleListFilter):
|
|
|
+# title = 'type de paiement'
|
|
|
+# parameter_name = 'type'
|
|
|
+#
|
|
|
+# def lookups(self, request, model_admin):
|
|
|
+# choices = [
|
|
|
+# ('membership', 'Cotisation'),
|
|
|
+# ('service', 'Service'),
|
|
|
+# ]
|
|
|
+# service_types = ServiceType.objects.all()
|
|
|
+# if not (request.user.is_superuser or request.user.has_perm('banking.validate_payment')):
|
|
|
+# service_types = service_types.filter(group__in=request.user.groups.all())
|
|
|
+# for stype in service_types:
|
|
|
+# choices.append((stype.pk, 'Service (%s)' % stype.name))
|
|
|
+# return choices
|
|
|
+#
|
|
|
+# def queryset(self, request, queryset):
|
|
|
+# if self.value() == 'membership':
|
|
|
+# return queryset.filter(reason_type__app_label='adhesions',
|
|
|
+# reason_type__model='adherent')
|
|
|
+# if self.value() == 'service':
|
|
|
+# return queryset.filter(reason_type__app_label='services',
|
|
|
+# reason_type__model='service')
|
|
|
+# try:
|
|
|
+# service_type = ServiceType.objects.get(pk=int(self.value()))
|
|
|
+# except (ValueError, TypeError, ServiceType.DoesNotExist,):
|
|
|
+# return queryset
|
|
|
+# else:
|
|
|
+# return queryset.filter(service__service_type=service_type)
|
|
|
+#
|
|
|
+#
|
|
|
+#### Actions
|
|
|
+#
|
|
|
+#def validate_payment(payment, request, queryset):
|
|
|
+# queryset.update(validated=True)
|
|
|
+#
|
|
|
+#
|
|
|
+#### ModelAdmin
|
|
|
+#
|
|
|
+#class PaymentAdmin(admin.ModelAdmin):
|
|
|
+# #list_display_links = None
|
|
|
+# list_filter = (PaymentTypeFilter, 'payment_method', 'validated',)
|
|
|
+#
|
|
|
+# def get_list_display(self, request):
|
|
|
+# list_display = ()
|
|
|
+# #if request.user.has_perm('auth.change_user'):
|
|
|
+# # list_display += ('adherent_link',)
|
|
|
+# #else:
|
|
|
+# #list_display += ('get_adherent',)
|
|
|
+# list_display += ('payment_type_verbose', 'get_adherent', 'amount',
|
|
|
+# 'period', 'payment_method', 'start_display', 'end_display', 'validated_display',)
|
|
|
+# #if request.user.has_perm('banking.validate_payment'):
|
|
|
+# # list_display += ('change',)
|
|
|
+# #else:
|
|
|
+# # list_display += ('change_pending',)
|
|
|
+# return list_display
|
|
|
+#
|
|
|
+# def start_display(self, obj):
|
|
|
+# return obj.start
|
|
|
+# start_display.short_description = 'Début'
|
|
|
+#
|
|
|
+# def end_display(self, obj):
|
|
|
+# return obj.end
|
|
|
+# end_display.short_description = 'Fin'
|
|
|
+#
|
|
|
+# def validated_display(self, obj):
|
|
|
+# return obj.validated
|
|
|
+# validated_display.short_description = 'Validé'
|
|
|
+# validated_display.boolean = True
|
|
|
+#
|
|
|
+# #def adherent_link(self, obj):
|
|
|
+# # adherent = obj.get_adherent()
|
|
|
+# # url = reverse('admin:adhesions_adherent_change', args=[adherent.pk])
|
|
|
+# # return '<a href="%s">%s</a>' % (url, adherent)
|
|
|
+# #adherent_link.short_description = 'Adhérent'
|
|
|
+# #adherent_link.allow_tags = True
|
|
|
+#
|
|
|
+# def change(self, obj):
|
|
|
+# url = reverse('admin:banking_payment_change', args=[obj.pk])
|
|
|
+# return '<a href="%s" class="changelink">Modifier</a>' % url
|
|
|
+# change.short_description = ''
|
|
|
+# change.allow_tags = True
|
|
|
+#
|
|
|
+# def change_pending(self, obj):
|
|
|
+# if obj.validated:
|
|
|
+# return '-'
|
|
|
+# else:
|
|
|
+# return self.change(obj)
|
|
|
+# change_pending.short_description = ''
|
|
|
+# change_pending.allow_tags = True
|
|
|
+#
|
|
|
+# def get_actions(self, request):
|
|
|
+# actions = super().get_actions(request)
|
|
|
+# if request.user.has_perm('banking.validate_payment'):
|
|
|
+# actions['validate'] = (validate_payment, 'validate', 'Valider les paiements sélectionnés')
|
|
|
+# return actions
|
|
|
+#
|
|
|
+# def has_add_permission(self, request):
|
|
|
+# return False
|
|
|
+#
|
|
|
+# def has_change_permission(self, request, obj=None):
|
|
|
+# if obj and not request.user.has_perm('banking.validate_payment'):
|
|
|
+# return not obj.validated
|
|
|
+# return True
|
|
|
+#
|
|
|
+# def has_delete_permission(self, request, obj=None):
|
|
|
+# if obj and not request.user.has_perm('banking.validate_payment'):
|
|
|
+# return not obj.validated
|
|
|
+# return False
|
|
|
+#
|
|
|
+#
|
|
|
+#admin.site.register(Payment, PaymentAdmin)
|