|
|
|
from django.contrib.auth.models import User, Group
|
|
|
|
from django.http import HttpResponseRedirect
|
|
|
|
from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
|
|
|
|
from django.utils.safestring import mark_safe
|
|
|
|
from .models import Orders, Users
|
|
|
|
|
|
|
|
from django.contrib import admin, messages
|
|
|
|
from django.urls import reverse
|
|
|
|
|
|
|
|
|
|
|
|
class OrdersAdmin(admin.ModelAdmin):
|
|
|
|
|
|
|
|
list_display = (
|
|
|
|
'orderName', 'orderCreateDateTime', 'orderDesc', 'orderAddress',
|
|
|
|
'catID', 'userName', 'orderStatus', 'show_photo', 'show_photopay',
|
|
|
|
)
|
|
|
|
actions = ['send_message']
|
|
|
|
exclude = ['orderAccess', 'userID', 'orderPhoto', 'orderPhotoPay']
|
|
|
|
|
|
|
|
|
|
|
|
def userName(self, obj):
|
|
|
|
user = Users.objects.get(user_id=obj.userID)
|
|
|
|
return user.userName
|
|
|
|
|
|
|
|
def changeform_view(self, request, object_id=None, form_url='', extra_context=None):
|
|
|
|
extra_context = extra_context or {}
|
|
|
|
|
|
|
|
extra_context['show_delete'] = False
|
|
|
|
extra_context['show_save'] = False
|
|
|
|
extra_context['show_save_and_continue'] = False
|
|
|
|
|
|
|
|
return super().changeform_view(request, object_id, form_url, extra_context)
|
|
|
|
|
|
|
|
def has_add_permission(self, request):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def show_photo(self, obj):
|
|
|
|
html = obj.get_photo_html()
|
|
|
|
return mark_safe(html)
|
|
|
|
|
|
|
|
show_photo.short_description = 'Фото'
|
|
|
|
|
|
|
|
def show_photopay(self, obj):
|
|
|
|
html = obj.get_photopay_html()
|
|
|
|
return mark_safe(html)
|
|
|
|
|
|
|
|
show_photopay.short_description = 'Чек'
|
|
|
|
|
|
|
|
def send_message(orders, request, queryset):
|
|
|
|
selected_objects = request.POST.getlist(ACTION_CHECKBOX_NAME)
|
|
|
|
if len(selected_objects) != 1:
|
|
|
|
messages.error(request, "Выберите только один объект")
|
|
|
|
return
|
|
|
|
|
|
|
|
selected_user_id = int(selected_objects[0])
|
|
|
|
obj = queryset.get(orderID=selected_user_id)
|
|
|
|
user_id = obj.userID
|
|
|
|
url = reverse('send_telegram_message', kwargs={'chat_id': user_id})
|
|
|
|
return HttpResponseRedirect(url)
|
|
|
|
|
|
|
|
send_message.short_description = 'Отправка сообщения'
|
|
|
|
|
|
|
|
class CustomUserAdmin(admin.ModelAdmin):
|
|
|
|
# Переопределение метода, чтобы запретить добавление пользователей
|
|
|
|
def has_add_permission(self, request):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def has_delete_permission(self, request, obj=None):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def has_view_or_change_permission(self, request, obj=None):
|
|
|
|
return False
|
|
|
|
|
|
|
|
class CustomGroupAdmin(admin.ModelAdmin):
|
|
|
|
#Переопределение метода, чтобы запретить добавление групп
|
|
|
|
def has_add_permission(self, request):
|
|
|
|
return False
|
|
|
|
|
|
|
|
admin.site.unregister(User) # Снятие регистрации стандартного класса администратора пользователей
|
|
|
|
admin.site.unregister(Group) # Снятие регистрации стандартного класса администратора групп
|
|
|
|
|
|
|
|
admin.site.register(User, CustomUserAdmin) # Регистрация настроенного класса администратора пользователей
|
|
|
|
admin.site.register(Group, CustomGroupAdmin) # Регистрация настроенного класса администратора групп
|
|
|
|
|
|
|
|
admin.site.register(Orders, OrdersAdmin)
|