admin.py 3.1 KB

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