admin.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. )
  52. list_filter = (
  53. 'last_login',
  54. 'is_superuser',
  55. 'is_staff',
  56. 'is_active',
  57. 'date_joined',
  58. )
  59. fieldsets = (
  60. ("Главное", {'fields': ('phone_number', 'password')}),
  61. ('Персональные данные', {'fields': ('username', 'email', 'first_name','last_name')}),
  62. ('Разрешения', {'fields': ('is_staff', 'is_active', 'is_superuser',)}),
  63. ('Прочие разрешения', {'fields': ('groups', 'user_permissions'), 'classes': ['collapse']}),
  64. ('Прочее', {'fields': ('last_login', 'date_joined',)}),
  65. )
  66. add_fieldsets = (
  67. ("Главное", {'fields': ('phone_number', 'password1', 'password2')}),
  68. ('Персональные данные', {'fields': ('username', 'email', 'first_name','last_name')}),
  69. ('Разрешения', {'fields': ('is_staff', 'is_active', 'is_superuser',)}),
  70. ('Прочие разрешения', {'fields': ('groups', 'user_permissions'), 'classes': ['collapse']}),
  71. ('Прочее', {'fields': ('last_login', 'date_joined',)}),
  72. )
  73. #raw_id_fields = ('groups', 'user_permissions')
  74. actions = [send_phone]
  75. #admin.site.register(SharixUser, UserAdmin)