| from functools import wraps |
| |
| from flask import flash, redirect, url_for |
| from flask_login import current_user |
| from sqlalchemy import or_, and_ |
| |
| from app import db |
| from app.models import Booking, BookingStatus, UserRole |
| |
| def check_max_booking(func): |
| @wraps(func) |
| def decorated_function(*args, **kwargs): |
| current_user_role_id = current_user.role_id |
| regular_user_role_id = UserRole.query.filter_by(role_name='regular').first().id |
| new_booking_status_id = BookingStatus.query.filter_by(status_text='new').first().id |
| deploying_booking_status_id = BookingStatus.query.filter_by(status_text='deploying').first().id |
| active_booking_status_id = BookingStatus.query.filter_by(status_text='active').first().id |
| no_of_active_bookings = Booking.query.filter(and_(Booking.user_id==current_user.id, or_(Booking.status_id==new_booking_status_id, Booking.status_id==deploying_booking_status_id, Booking.status_id==active_booking_status_id))).count() |
| #no_of_active_bookings = Booking.query.filter(or_(status=='new', status=='deploying', status=='active')).count() |
| |
| if current_user_role_id == regular_user_role_id and no_of_active_bookings > 0: |
| flash('You can only have 1 booking at a time!', 'warning') |
| return redirect(url_for('booking.bookings')) |
| return func(*args, **kwargs) |
| |
| return decorated_function |