102 lines
3.6 KiB
Python
102 lines
3.6 KiB
Python
from django import forms
|
|
from django.contrib import admin
|
|
from .models import (
|
|
Hotel,
|
|
UserHotel,
|
|
APIConfiguration,
|
|
APIRequestLog,
|
|
Reservation,
|
|
Guest,
|
|
FraudLog
|
|
)
|
|
from django.urls import path
|
|
from django.shortcuts import redirect
|
|
from django.utils.html import format_html
|
|
from pms_integration.api_client import APIClient
|
|
|
|
# Custom form for Hotel to filter APIConfiguration
|
|
class HotelForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Hotel
|
|
fields = "__all__"
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
# Exclude APIs already linked to another hotel
|
|
used_apis = Hotel.objects.exclude(api__isnull=True).values_list('api', flat=True)
|
|
self.fields['api'].queryset = APIConfiguration.objects.exclude(id__in=used_apis)
|
|
|
|
|
|
@admin.register(Hotel)
|
|
class HotelAdmin(admin.ModelAdmin):
|
|
list_display = ['name', 'pms', 'sync_button']
|
|
|
|
def sync_button(self, obj):
|
|
return format_html(
|
|
'<a class="button" href="{}">Синхронизировать</a>',
|
|
f"/admin/hotels/sync/{obj.id}/"
|
|
)
|
|
|
|
def get_urls(self):
|
|
urls = super().get_urls()
|
|
custom_urls = [
|
|
path('sync/<int:hotel_id>/', self.sync_hotel_data),
|
|
]
|
|
return custom_urls + urls
|
|
|
|
def sync_hotel_data(self, request, hotel_id):
|
|
try:
|
|
hotel = Hotel.objects.get(id=hotel_id)
|
|
client = APIClient(hotel.pms)
|
|
client.run(hotel)
|
|
self.message_user(request, f"Данные отеля {hotel.name} успешно синхронизированы.")
|
|
except Exception as e:
|
|
self.message_user(request, f"Ошибка: {str(e)}", level="error")
|
|
return redirect("..")
|
|
|
|
@admin.register(FraudLog)
|
|
class FroudAdmin(admin.ModelAdmin):
|
|
list_display = ('hotel', 'reservation_id', 'guest_name', 'check_in_date', 'detected_at', 'message')
|
|
search_fields = ('hotel__name', 'reservation_id', 'guest_name', 'check_in_date', 'message')
|
|
list_filter = ('hotel', 'check_in_date', 'detected_at')
|
|
ordering = ('-detected_at',)
|
|
|
|
@admin.register(UserHotel)
|
|
class UserHotelAdmin(admin.ModelAdmin):
|
|
list_display = ('user', 'hotel')
|
|
search_fields = ('user__username', 'hotel__name')
|
|
# list_filter = ('hotel',)
|
|
# ordering = ('-hotel',)
|
|
|
|
|
|
@admin.register(APIConfiguration)
|
|
class ApiConfigurationAdmin(admin.ModelAdmin):
|
|
list_display = ('name', 'url', 'token', 'username', 'password', 'last_updated')
|
|
search_fields = ('name', 'url', 'token', 'username')
|
|
list_filter = ('last_updated',)
|
|
ordering = ('-last_updated',)
|
|
|
|
|
|
@admin.register(APIRequestLog)
|
|
class ApiRequestLogAdmin(admin.ModelAdmin):
|
|
list_display = ('api', 'request_time', 'response_status', 'response_data')
|
|
search_fields = ('api__name', 'request_time', 'response_status')
|
|
list_filter = ('api', 'response_status', 'request_time')
|
|
ordering = ('-request_time',)
|
|
|
|
|
|
@admin.register(Reservation)
|
|
class ReservationAdmin(admin.ModelAdmin):
|
|
list_display = ('reservation_id', 'hotel', 'room_number', 'room_type', 'check_in', 'check_out', 'status', 'price', 'discount')
|
|
search_fields = ('hotel', 'room_number', 'room_type', 'check_in', 'check_out', 'status', 'price', 'discount')
|
|
list_filter = ('hotel', 'room_number', 'room_type', 'check_in', 'check_out', 'status', 'price', 'discount')
|
|
ordering = ('-check_in',)
|
|
|
|
|
|
@admin.register(Guest)
|
|
class GuestAdmin(admin.ModelAdmin):
|
|
list_display = ('reservation', 'name', 'birthdate', 'phone', 'email')
|
|
search_fields = ('reservation__reservation_id', 'name', 'phone', 'email')
|
|
list_filter = ('reservation',)
|
|
ordering = ('-reservation',)
|