1
0

admin.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # -*- coding: utf-8 -*-
  2. from django.contrib import admin
  3. from django.http import HttpResponseRedirect
  4. from django.shortcuts import render
  5. from django.contrib.auth import get_user_model
  6. from django import forms
  7. from xmpp import cli
  8. from django.contrib.auth.admin import UserAdmin
  9. from django.contrib.auth.models import Permission
  10. import django.contrib.auth.admin as adm
  11. class MessageForm(forms.Form):
  12. _selected_action = forms.CharField(widget=forms.MultipleHiddenInput)
  13. message = forms.CharField(label='Message', max_length=1000)
  14. #@admin.action(description='Отправить сообщение на номер телефона')
  15. def send_phone(modeladmin, request, queryset):
  16. form = None
  17. #print("Hello world")
  18. if 'apply' in request.POST:
  19. #print("Hello world")
  20. modeladmin.message_user(request, "Сообщения успешно отправлены!")
  21. form = MessageForm(request.POST)
  22. if form.is_valid():
  23. for i in queryset:
  24. if str(i.phone_number).startswith("7"):
  25. message = '{"phone":"+'+i.phone_number+'","msg":"'+request.POST['message']+'"}'
  26. else:
  27. message = '{"phone":"'+i.phone_number+'","msg":"'+request.POST['message']+'"}'
  28. cli.send_message("sender", "password", "getter", message)
  29. print(i)
  30. print(request.POST['message'])
  31. return HttpResponseRedirect(request.get_full_path())
  32. else:
  33. form = MessageForm(initial={'_selected_action': request.POST.getlist("_selected_action")})
  34. return render(request, "SharixAdmin/senderform.html", {"items":queryset, "form":form, 'title':'Отправка сообщений на номер телефона'})
  35. send_phone.short_description = u"Отправить сообщение на номер телефона"
  36. @admin.register(Permission)
  37. class PermissionsAdmin(admin.ModelAdmin):
  38. list_display = (
  39. 'id',
  40. 'name',
  41. 'content_type',
  42. 'codename',
  43. )
  44. @admin.register(get_user_model())
  45. class ShariXUserAdmin(adm.UserAdmin):
  46. list_display = (
  47. 'username',
  48. 'phone_number',
  49. 'first_name',
  50. 'last_name',
  51. 'avatar'
  52. )
  53. list_filter = (
  54. 'last_login',
  55. 'is_superuser',
  56. 'is_staff',
  57. 'is_active',
  58. 'date_joined',
  59. )
  60. fieldsets = (
  61. ("Главное", {'fields': ('phone_number', 'password')}),
  62. ('Персональные данные', {'fields': ('username', 'email', 'first_name','last_name', 'avatar')}),
  63. ('Разрешения', {'fields': ('is_staff', 'is_active', 'is_superuser',)}),
  64. ('Прочие разрешения', {'fields': ('groups', 'user_permissions'), 'classes': ['collapse']}),
  65. ('Прочее', {'fields': ('last_login', 'date_joined',)}),
  66. )
  67. add_fieldsets = (
  68. ("Главное", {'fields': ('phone_number', 'password1', 'password2')}),
  69. ('Персональные данные', {'fields': ('username', 'email', 'first_name','last_name', 'avatar')}),
  70. ('Разрешения', {'fields': ('is_staff', 'is_active', 'is_superuser',)}),
  71. ('Прочие разрешения', {'fields': ('groups', 'user_permissions'), 'classes': ['collapse']}),
  72. ('Прочее', {'fields': ('last_login', 'date_joined',)}),
  73. )
  74. #raw_id_fields = ('groups', 'user_permissions')
  75. actions = [send_phone]
  76. #admin.site.register(SharixUser, UserAdmin)