Shelter PMS fully functional

This commit is contained in:
2024-12-09 16:36:11 +09:00
parent 60eaef5527
commit e76a80fb2f
47 changed files with 665 additions and 909 deletions

View File

@@ -1,5 +1,5 @@
from django.contrib import admin
from django import forms
from django.contrib import admin
from .models import (
Hotel,
UserHotel,
@@ -8,6 +8,10 @@ from .models import (
Reservation,
Guest
)
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):
@@ -22,22 +26,40 @@ class HotelForm(forms.ModelForm):
self.fields['api'].queryset = APIConfiguration.objects.exclude(id__in=used_apis)
@admin.register(Hotel)
class HotelAdmin(admin.ModelAdmin):
form = HotelForm
list_display = ('name', 'api', 'created_at', 'pms')
search_fields = ('name',)
list_filter = ('pms', 'created_at')
ordering = ('-created_at',)
list_display = ['name', 'pms', 'sync_button']
admin.site.register(Hotel, HotelAdmin)
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(UserHotel)
class UserHotelAdmin(admin.ModelAdmin):
list_display = ('user', 'hotel')
search_fields = ('user__username', 'hotel__name')
list_filter = ('hotel',)
ordering = ('-hotel',)
# list_filter = ('hotel',)
# ordering = ('-hotel',)
@admin.register(APIConfiguration)