CCrypto VPN public website https://vpn.ccrypto.org/
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.
 
 
 
 
 
 

124 lines
3.5 KiB

  1. import string
  2. from django.shortcuts import resolve_url
  3. from django import forms
  4. from django.contrib import admin
  5. from django.contrib import messages
  6. from django.contrib.auth.admin import UserAdmin
  7. from django.contrib.auth.models import User
  8. from django.utils.translation import ugettext as _
  9. from django.utils.html import format_html
  10. import django_lcore
  11. import lcoreapi
  12. from lambdainst.models import VPNUser
  13. from payments.admin import UserCouponInline, UserLedgerInline
  14. def make_user_link(user):
  15. change_url = resolve_url("admin:auth_user_change", user.id)
  16. return format_html('<a href="{}">{}</a>', change_url, user.username)
  17. class VPNUserInline(admin.StackedInline):
  18. model = VPNUser
  19. can_delete = False
  20. fk_name = "user"
  21. fields = (
  22. "notes",
  23. "expiration",
  24. "last_expiry_notice",
  25. "notify_expiration",
  26. "last_vpn_auth_fail_notice",
  27. "notify_vpn_auth_fail",
  28. "trial_periods_given",
  29. "referrer_a",
  30. "campaign",
  31. "last_vpn_auth",
  32. "last_core_sync",
  33. )
  34. readonly_fields = ("referrer_a", "last_vpn_auth", "last_core_sync", "campaign")
  35. def referrer_a(self, object):
  36. if not object.referrer:
  37. return "-"
  38. s = make_user_link(object.referrer) + " "
  39. if object.referrer_used:
  40. s += _("(rewarded)")
  41. else:
  42. s += _("(not rewarded)")
  43. return s
  44. referrer_a.allow_tags = True
  45. referrer_a.short_description = _("Referrer")
  46. def is_paid(self, object):
  47. return object.is_paid
  48. is_paid.boolean = True
  49. is_paid.short_description = _("Is paid?")
  50. class UserAdmin(UserAdmin):
  51. inlines = (VPNUserInline, UserLedgerInline, UserCouponInline)
  52. list_display = ("username", "email", "is_staff", "date_joined", "is_paid")
  53. ordering = ("-date_joined",)
  54. fieldsets = (
  55. (None, {"fields": ("username", "password", "email", "links")}),
  56. (_("Important dates"), {"fields": ("last_login", "date_joined")}),
  57. (
  58. _("Permissions"),
  59. {
  60. "fields": (
  61. "is_active",
  62. "is_staff",
  63. "is_superuser",
  64. "groups",
  65. "user_permissions",
  66. )
  67. },
  68. ),
  69. )
  70. readonly_fields = ("last_login", "date_joined", "links")
  71. actions = (django_lcore.core_sync_action,)
  72. def is_paid(self, object):
  73. return object.vpnuser.is_paid
  74. is_paid.boolean = True
  75. is_paid.short_description = _("Is paid?")
  76. def links(self, object):
  77. payments_url = resolve_url("admin:payments_payment_changelist")
  78. tickets_url = resolve_url("admin:tickets_ticket_changelist")
  79. fmt = '<a href="{}?user__id__exact={}">{}</a>'
  80. return format_html(
  81. fmt + " - " + fmt,
  82. payments_url,
  83. object.id,
  84. "Payments",
  85. tickets_url,
  86. object.id,
  87. "Tickets",
  88. )
  89. def save_model(self, request, obj, form, change):
  90. super().save_model(request, obj, form, change)
  91. # Notify core
  92. if change:
  93. django_lcore.sync_user(obj.vpnuser)
  94. def delete_model(self, request, obj):
  95. try:
  96. django_lcore.api.get_user(obj.username).delete()
  97. except lcoreapi.APIError as e:
  98. messages.error(request, "failed to delete vpn user: %r" % e)
  99. super().delete_model(request, obj)
  100. admin.site.unregister(User)
  101. admin.site.register(User, UserAdmin)