admin.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # -*- coding: utf-8 -*-
  2. from django.contrib import admin
  3. from django.http import HttpResponseRedirect
  4. from django.shortcuts import render
  5. from SharixAdmin.models import *
  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(SharixUser)
  45. class SharixUserAdmin(adm.UserAdmin):
  46. list_display = (
  47. 'username',
  48. 'phone_number',
  49. )
  50. list_filter = (
  51. 'last_login',
  52. 'is_superuser',
  53. 'is_staff',
  54. 'is_active',
  55. 'date_joined',
  56. )
  57. fieldsets = (
  58. ("Главное", {'fields': ('phone_number', 'password')}),
  59. ('Персональные данные', {'fields': ('username', 'email', 'first_name','last_name')}),
  60. ('Разрешения', {'fields': ('is_staff', 'is_active', 'is_superuser',)}),
  61. ('Прочие разрешения', {'fields': ('groups', 'user_permissions'), 'classes': ['collapse']}),
  62. ('Прочее', {'fields': ('last_login', 'date_joined',)}),
  63. )
  64. add_fieldsets = (
  65. ("Главное", {'fields': ('phone_number', 'password1', 'password2')}),
  66. ('Персональные данные', {'fields': ('username', 'email', 'first_name','last_name')}),
  67. ('Разрешения', {'fields': ('is_staff', 'is_active', 'is_superuser',)}),
  68. ('Прочие разрешения', {'fields': ('groups', 'user_permissions'), 'classes': ['collapse']}),
  69. ('Прочее', {'fields': ('last_login', 'date_joined',)}),
  70. )
  71. #raw_id_fields = ('groups', 'user_permissions')
  72. actions = [send_phone]
  73. #admin.site.register(SharixUser, UserAdmin)