bot refactor
This commit is contained in:
26
bot/utils/database.py
Normal file
26
bot/utils/database.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from users.models import User
|
||||
from hotels.models import Hotel, Reservation
|
||||
from asgiref.sync import sync_to_async
|
||||
|
||||
async def get_user_from_chat_id(chat_id):
|
||||
return await sync_to_async(User.objects.filter(chat_id=chat_id).first)()
|
||||
|
||||
async def get_hotel_by_id(hotel_id):
|
||||
return await sync_to_async(Hotel.objects.get)(id=hotel_id)
|
||||
|
||||
async def get_hotels_for_user(user):
|
||||
"""Получение отелей, связанных с пользователем."""
|
||||
# Проверяем, является ли пользователь сотрудником какого-либо отеля
|
||||
user_hotels = await sync_to_async(list)(
|
||||
Hotel.objects.filter(user_hotel__user=user).distinct()
|
||||
)
|
||||
print(user_hotels)
|
||||
return user_hotels
|
||||
|
||||
async def get_reservations(hotel_id, start_date=None, end_date=None):
|
||||
query = Reservation.objects.filter(hotel_id=hotel_id)
|
||||
if start_date:
|
||||
query = query.filter(check_in__gte=start_date)
|
||||
if end_date:
|
||||
query = query.filter(check_out__lte=end_date)
|
||||
return await sync_to_async(list)(query.prefetch_related('guests'))
|
||||
Reference in New Issue
Block a user