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.
 
 
 
 
 
 

147 lines
4.8 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.auth.admin import UserAdmin
  6. from django.contrib.auth.models import User
  7. from django.utils.translation import ugettext as _
  8. from lambdainst.models import VPNUser, GiftCode, GiftCodeUser
  9. from . import core
  10. def make_user_link(user):
  11. change_url = resolve_url('admin:auth_user_change', user.id)
  12. return '<a href="%s">%s</a>' % (change_url, user.username)
  13. class GiftCodeAdminForm(forms.ModelForm):
  14. def clean(self):
  15. input_code = self.cleaned_data.get('code', '')
  16. code_charset = string.ascii_letters + string.digits
  17. if any(c not in code_charset for c in input_code):
  18. raise forms.ValidationError(_("Code must be [a-zA-Z0-9]"))
  19. if not 1 <= len(input_code) <= 32:
  20. raise forms.ValidationError(_("Code must be between 1 and 32 characters"))
  21. return self.cleaned_data
  22. class VPNUserInline(admin.StackedInline):
  23. model = VPNUser
  24. can_delete = False
  25. fk_name = 'user'
  26. fields = ('notes', 'expiration', 'last_expiry_notice', 'notify_expiration',
  27. 'trial_periods_given', 'referrer_a', 'campaign', 'last_vpn_auth')
  28. readonly_fields = ('referrer_a', 'last_vpn_auth', 'campaign')
  29. def referrer_a(self, object):
  30. if not object.referrer:
  31. return "-"
  32. s = make_user_link(object.referrer) + " "
  33. if object.referrer_used:
  34. s += _("(rewarded)")
  35. else:
  36. s += _("(not rewarded)")
  37. return s
  38. referrer_a.allow_tags = True
  39. referrer_a.short_description = _("Referrer")
  40. def is_paid(self, object):
  41. return object.is_paid
  42. is_paid.boolean = True
  43. is_paid.short_description = _("Is paid?")
  44. class GiftCodeUserAdmin(admin.TabularInline):
  45. model = GiftCodeUser
  46. fields = ('user_link', 'code_link', 'date')
  47. readonly_fields = ('user_link', 'code_link', 'date')
  48. list_display = ('user', )
  49. original = False
  50. def user_link(self, object):
  51. return make_user_link(object.user)
  52. user_link.allow_tags = True
  53. user_link.short_description = 'User'
  54. def code_link(self, object):
  55. change_url = resolve_url('admin:lambdainst_giftcode_change', object.code.id)
  56. return '<a href="%s">%s</a>' % (change_url, object.code.code)
  57. code_link.allow_tags = True
  58. code_link.short_description = 'Code'
  59. def has_add_permission(self, request):
  60. return False
  61. def has_delete_permission(self, request, obj=None):
  62. return False
  63. class UserAdmin(UserAdmin):
  64. inlines = (VPNUserInline, GiftCodeUserAdmin)
  65. list_display = ('username', 'email', 'is_staff', 'date_joined', 'is_paid')
  66. ordering = ('-date_joined', )
  67. fieldsets = (
  68. (None, {'fields': ('username', 'password', 'email', 'links')}),
  69. (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
  70. (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
  71. 'groups', 'user_permissions')}),
  72. )
  73. readonly_fields = ('last_login', 'date_joined', 'links')
  74. def is_paid(self, object):
  75. return object.vpnuser.is_paid
  76. is_paid.boolean = True
  77. is_paid.short_description = _("Is paid?")
  78. def links(self, object):
  79. fmt = '<a href="%s?user__id__exact=%d">%s</a>'
  80. payments_url = resolve_url('admin:payments_payment_changelist')
  81. tickets_url = resolve_url('admin:tickets_ticket_changelist')
  82. s = fmt % (payments_url, object.id, "Payments")
  83. s += ' - ' + fmt % (tickets_url, object.id, "Tickets")
  84. return s
  85. links.allow_tags = True
  86. def save_model(self, request, obj, form, change):
  87. super().save_model(request, obj, form, change)
  88. # Notify core
  89. if change and core.VPN_AUTH_STORAGE == 'core':
  90. core.update_user_expiration(obj)
  91. def delete_model(self, request, obj):
  92. if core.VPN_AUTH_STORAGE == 'core':
  93. core.delete_user(obj.username)
  94. super().delete_model(request, obj)
  95. class GiftCodeAdmin(admin.ModelAdmin):
  96. fields = ('code', 'time', 'created', 'created_by', 'single_use', 'free_only',
  97. 'available', 'comment')
  98. readonly_fields = ('created', 'created_by')
  99. list_display = ('code', 'time', 'comment_head', 'available')
  100. search_fields = ('code', 'comment', 'users__username')
  101. inlines = (GiftCodeUserAdmin,)
  102. list_filter = ('available', 'time')
  103. form = GiftCodeAdminForm
  104. def comment_head(self, object):
  105. return object.comment_head
  106. comment_head.short_description = _("Comment")
  107. def save_model(self, request, obj, form, change):
  108. if not change:
  109. obj.created_by = request.user
  110. obj.save()
  111. admin.site.unregister(User)
  112. admin.site.register(User, UserAdmin)
  113. admin.site.register(GiftCode, GiftCodeAdmin)