You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.4 KiB

  1. from django.contrib import admin
  2. from django.urls import reverse
  3. from django.utils.safestring import mark_safe
  4. from hc.accounts.models import Profile
  5. from hc.payments.models import Subscription
  6. @admin.register(Subscription)
  7. class SubsAdmin(admin.ModelAdmin):
  8. readonly_fields = ("email",)
  9. search_fields = (
  10. "customer_id",
  11. "payment_method_token",
  12. "subscription_id",
  13. "user__email",
  14. )
  15. list_display = (
  16. "id",
  17. "email",
  18. "customer_id",
  19. "address_id",
  20. "payment_method_token",
  21. "subscription_id",
  22. "plan_id",
  23. "plan_name",
  24. "profile",
  25. )
  26. list_filter = ("plan_id",)
  27. raw_id_fields = ("user",)
  28. actions = ("cancel",)
  29. def email(self, obj):
  30. return obj.user.email if obj.user else None
  31. @mark_safe
  32. def profile(self, obj):
  33. if obj.user.profile:
  34. url = reverse("admin:accounts_profile_change", args=[obj.user.profile.id])
  35. return "<a href='%s'>View Profile</a>" % url
  36. return ""
  37. profile.allow_tags = True
  38. def cancel(self, request, qs):
  39. for sub in qs.all():
  40. sub.cancel()
  41. profile = Profile.objects.for_user(sub.user)
  42. profile.check_limit = 20
  43. profile.team_limit = 2
  44. profile.sms_limit = 0
  45. profile.save()
  46. self.message_user(request, "%d subscriptions cancelled" % qs.count())