add license
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
from flask import Blueprint, render_template, redirect, url_for, flash, request, session
|
||||
from flask_login import login_user, logout_user, login_required, current_user
|
||||
from app import db
|
||||
from app.models.user import User
|
||||
from app.forms.auth import LoginForm
|
||||
from datetime import datetime
|
||||
|
||||
auth_bp = Blueprint('auth', __name__)
|
||||
|
||||
@auth_bp.route('/login', methods=['GET', 'POST'])
|
||||
def login():
|
||||
"""Handle user login"""
|
||||
if current_user.is_authenticated:
|
||||
return redirect(url_for('main.dashboard'))
|
||||
|
||||
form = LoginForm()
|
||||
if form.validate_on_submit():
|
||||
user = User.query.filter_by(email=form.email.data).first()
|
||||
|
||||
if user and user.check_password(form.password.data):
|
||||
if not user.is_active:
|
||||
flash('Your account has been deactivated. Please contact administrator.', 'error')
|
||||
return render_template('auth/login.html', form=form)
|
||||
|
||||
login_user(user, remember=form.remember_me.data)
|
||||
user.update_last_login()
|
||||
|
||||
next_page = request.args.get('next')
|
||||
if not next_page or not next_page.startswith('/'):
|
||||
next_page = url_for('main.dashboard')
|
||||
|
||||
flash(f'Welcome back, {user.name}!', 'success')
|
||||
return redirect(next_page)
|
||||
else:
|
||||
flash('Invalid email or password.', 'error')
|
||||
|
||||
return render_template('auth/login.html', form=form)
|
||||
|
||||
@auth_bp.route('/logout')
|
||||
@login_required
|
||||
def logout():
|
||||
"""Handle user logout"""
|
||||
logout_user()
|
||||
flash('You have been logged out successfully.', 'info')
|
||||
return redirect(url_for('auth.login'))
|
||||
|
||||
@auth_bp.route('/profile')
|
||||
@login_required
|
||||
def profile():
|
||||
"""User profile page"""
|
||||
return render_template('auth/profile.html')
|
||||
|
||||
@auth_bp.route('/change_password', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def change_password():
|
||||
"""Change user password"""
|
||||
if request.method == 'POST':
|
||||
current_password = request.form.get('current_password')
|
||||
new_password = request.form.get('new_password')
|
||||
confirm_password = request.form.get('confirm_password')
|
||||
|
||||
if not current_user.check_password(current_password):
|
||||
flash('Current password is incorrect.', 'error')
|
||||
return redirect(url_for('auth.change_password'))
|
||||
|
||||
if new_password != confirm_password:
|
||||
flash('New passwords do not match.', 'error')
|
||||
return redirect(url_for('auth.change_password'))
|
||||
|
||||
if len(new_password) < 6:
|
||||
flash('Password must be at least 6 characters long.', 'error')
|
||||
return redirect(url_for('auth.change_password'))
|
||||
|
||||
current_user.set_password(new_password)
|
||||
db.session.commit()
|
||||
|
||||
flash('Password changed successfully!', 'success')
|
||||
return redirect(url_for('auth.profile'))
|
||||
|
||||
return render_template('auth/change_password.html')
|
||||
@@ -0,0 +1,214 @@
|
||||
from flask import Blueprint, render_template, request, redirect, url_for, flash, jsonify
|
||||
from flask_login import login_required
|
||||
from app import db
|
||||
from app.models.classroom import Classroom, ClassroomResource
|
||||
from app.models.reservation import Reservation, ReservationStatus
|
||||
from app.forms.classroom import ClassroomForm, ClassroomResourceForm
|
||||
from datetime import datetime
|
||||
|
||||
classrooms_bp = Blueprint('classrooms', __name__, url_prefix='/classrooms')
|
||||
|
||||
@classrooms_bp.route('/')
|
||||
@login_required
|
||||
def list_classrooms():
|
||||
"""List all classrooms"""
|
||||
page = request.args.get('page', 1, type=int)
|
||||
search = request.args.get('search', '')
|
||||
campus = request.args.get('campus', '')
|
||||
|
||||
query = Classroom.query.filter_by(is_active=True)
|
||||
|
||||
if search:
|
||||
query = query.filter(
|
||||
Classroom.code.contains(search) |
|
||||
Classroom.name.contains(search) |
|
||||
Classroom.building.contains(search)
|
||||
)
|
||||
|
||||
if campus:
|
||||
query = query.filter(Classroom.campus_location == campus)
|
||||
|
||||
classrooms = query.paginate(
|
||||
page=page,
|
||||
per_page=20,
|
||||
error_out=False
|
||||
)
|
||||
|
||||
return render_template('classrooms/list.html',
|
||||
classrooms=classrooms,
|
||||
search=search,
|
||||
campus=campus)
|
||||
|
||||
@classrooms_bp.route('/add', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def add_classroom():
|
||||
"""Add new classroom"""
|
||||
form = ClassroomForm()
|
||||
|
||||
if form.validate_on_submit():
|
||||
classroom = Classroom(
|
||||
code=form.code.data,
|
||||
name=form.name.data,
|
||||
building=form.building.data,
|
||||
floor=form.floor.data,
|
||||
capacity=form.capacity.data,
|
||||
description=form.description.data,
|
||||
campus_location=form.campus_location.data
|
||||
)
|
||||
|
||||
try:
|
||||
db.session.add(classroom)
|
||||
db.session.commit()
|
||||
flash(f'Classroom {classroom.code} added successfully!', 'success')
|
||||
return redirect(url_for('classrooms.list_classrooms'))
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
flash('Error adding classroom. Classroom code might already exist.', 'error')
|
||||
|
||||
return render_template('classrooms/add.html', form=form)
|
||||
|
||||
@classrooms_bp.route('/edit/<int:Id>', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def edit_classroom(Id):
|
||||
"""Edit classroom"""
|
||||
classroom = Classroom.query.get_or_404(Id)
|
||||
|
||||
if not classroom.is_active:
|
||||
flash('This classroom has been deactivated.', 'warning')
|
||||
return redirect(url_for('classrooms.list_classrooms'))
|
||||
|
||||
form = ClassroomForm(obj=classroom)
|
||||
|
||||
if form.validate_on_submit():
|
||||
try:
|
||||
classroom.code = form.code.data
|
||||
classroom.name = form.name.data
|
||||
classroom.building = form.building.data
|
||||
classroom.floor = form.floor.data
|
||||
classroom.capacity = form.capacity.data
|
||||
classroom.description = form.description.data
|
||||
classroom.campus_location = form.campus_location.data
|
||||
classroom.updated_at = datetime.utcnow()
|
||||
|
||||
db.session.commit()
|
||||
flash(f'Classroom {classroom.code} updated successfully!', 'success')
|
||||
return redirect(url_for('classrooms.view_classroom', Id=classroom.id))
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
flash('Error updating classroom.', 'error')
|
||||
|
||||
return render_template('classrooms/edit.html', form=form, classroom=classroom)
|
||||
|
||||
@classrooms_bp.route('/view/<int:Id>')
|
||||
@login_required
|
||||
def view_classroom(Id):
|
||||
"""View classroom details"""
|
||||
classroom = Classroom.query.get_or_404(Id)
|
||||
|
||||
if not classroom.is_active:
|
||||
flash('This classroom has been deactivated.', 'warning')
|
||||
return redirect(url_for('classrooms.list_classrooms'))
|
||||
|
||||
# Get classroom resources
|
||||
resources = ClassroomResource.query.filter_by(classroom_id=Id).all()
|
||||
|
||||
# Get upcoming reservations
|
||||
upcoming_reservations = Reservation.query.filter(
|
||||
Reservation.classroom_id == Id,
|
||||
Reservation.start_time >= datetime.utcnow(),
|
||||
Reservation.status.in_([ReservationStatus.CONFIRMED, ReservationStatus.PENDING])
|
||||
).order_by(Reservation.start_time).limit(10).all()
|
||||
|
||||
return render_template('classrooms/view.html',
|
||||
classroom=classroom,
|
||||
resources=resources,
|
||||
upcoming_reservations=upcoming_reservations)
|
||||
|
||||
@classrooms_bp.route('/delete/<int:Id>')
|
||||
@login_required
|
||||
def delete_classroom(Id):
|
||||
"""Delete/deactivate classroom"""
|
||||
classroom = Classroom.query.get_or_404(Id)
|
||||
|
||||
# Check for active reservations
|
||||
active_reservations = Reservation.query.filter(
|
||||
Reservation.classroom_id == Id,
|
||||
Reservation.status.in_([ReservationStatus.CONFIRMED, ReservationStatus.PENDING])
|
||||
).count()
|
||||
|
||||
if active_reservations > 0:
|
||||
flash('Cannot delete classroom with active reservations.', 'error')
|
||||
return redirect(url_for('classrooms.view_classroom', Id=Id))
|
||||
|
||||
try:
|
||||
classroom.is_active = False
|
||||
classroom.updated_at = datetime.utcnow()
|
||||
db.session.commit()
|
||||
flash(f'Classroom {classroom.code} has been deactivated.', 'success')
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
flash('Error deactivating classroom.', 'error')
|
||||
|
||||
return redirect(url_for('classrooms.list_classrooms'))
|
||||
|
||||
@classrooms_bp.route('/<int:classroom_id>/add_resource', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def add_resource(classroom_id):
|
||||
"""Add resource to classroom"""
|
||||
classroom = Classroom.query.get_or_404(classroom_id)
|
||||
form = ClassroomResourceForm()
|
||||
|
||||
if form.validate_on_submit():
|
||||
resource = ClassroomResource(
|
||||
classroom_id=classroom_id,
|
||||
resource_type=form.resource_type.data,
|
||||
brand=form.brand.data,
|
||||
model=form.model.data,
|
||||
specifications=form.specifications.data,
|
||||
quantity=form.quantity.data
|
||||
)
|
||||
|
||||
try:
|
||||
db.session.add(resource)
|
||||
db.session.commit()
|
||||
flash(f'{resource.resource_type} added to {classroom.code} successfully!', 'success')
|
||||
return redirect(url_for('classrooms.view_classroom', Id=classroom_id))
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
flash('Error adding resource.', 'error')
|
||||
|
||||
return render_template('classrooms/add_resource.html', form=form, classroom=classroom)
|
||||
|
||||
@classrooms_bp.route('/api/available')
|
||||
@login_required
|
||||
def api_available_classrooms():
|
||||
"""API endpoint to get available classrooms"""
|
||||
start_time = request.args.get('start_time')
|
||||
end_time = request.args.get('end_time')
|
||||
min_capacity = request.args.get('min_capacity', 0, type=int)
|
||||
|
||||
if not start_time or not end_time:
|
||||
return jsonify({'error': 'start_time and end_time are required'}), 400
|
||||
|
||||
try:
|
||||
start_dt = datetime.fromisoformat(start_time)
|
||||
end_dt = datetime.fromisoformat(end_time)
|
||||
except ValueError:
|
||||
return jsonify({'error': 'Invalid datetime format'}), 400
|
||||
|
||||
# Find available classrooms (without conflicts)
|
||||
conflicting_classroom_ids = [
|
||||
r.classroom_id for r in Reservation.query.filter(
|
||||
Reservation.start_time < end_dt,
|
||||
Reservation.end_time > start_dt,
|
||||
Reservation.status == ReservationStatus.CONFIRMED
|
||||
).all()
|
||||
]
|
||||
|
||||
available_classrooms = Classroom.query.filter(
|
||||
Classroom.is_active == True,
|
||||
Classroom.capacity >= min_capacity,
|
||||
~Classroom.id.in_(conflicting_classroom_ids)
|
||||
).all()
|
||||
|
||||
return jsonify([classroom.to_dict() for classroom in available_classrooms])
|
||||
@@ -0,0 +1,92 @@
|
||||
from flask import Blueprint, render_template, jsonify
|
||||
from flask_login import login_required, current_user
|
||||
from app.models.reservation import Reservation
|
||||
from app.models.classroom import Classroom
|
||||
from app.models.subject import Subject
|
||||
from datetime import datetime, timedelta
|
||||
from sqlalchemy import func
|
||||
|
||||
main_bp = Blueprint('main', __name__)
|
||||
|
||||
@main_bp.route('/')
|
||||
@login_required
|
||||
def dashboard():
|
||||
"""Main dashboard page"""
|
||||
# Get statistics
|
||||
total_classrooms = Classroom.query.filter_by(is_active=True).count()
|
||||
total_subjects = Subject.query.filter_by(is_active=True).count()
|
||||
|
||||
# Get today's reservations
|
||||
today = datetime.utcnow().date()
|
||||
today_reservations = Reservation.query.filter(
|
||||
func.date(Reservation.start_time) == today
|
||||
).count()
|
||||
|
||||
# Get this week's reservations
|
||||
week_start = today - timedelta(days=today.weekday())
|
||||
week_end = week_start + timedelta(days=6)
|
||||
week_reservations = Reservation.query.filter(
|
||||
func.date(Reservation.start_time) >= week_start,
|
||||
func.date(Reservation.start_time) <= week_end
|
||||
).count()
|
||||
|
||||
# Get upcoming reservations (next 7 days)
|
||||
upcoming_reservations = Reservation.query.filter(
|
||||
Reservation.start_time >= datetime.utcnow(),
|
||||
Reservation.start_time <= datetime.utcnow() + timedelta(days=7),
|
||||
Reservation.status.in_(['CONFIRMED', 'PENDING'])
|
||||
).join(Classroom).join(Subject).limit(10).all()
|
||||
|
||||
stats = {
|
||||
'total_classrooms': total_classrooms,
|
||||
'total_subjects': total_subjects,
|
||||
'today_reservations': today_reservations,
|
||||
'week_reservations': week_reservations
|
||||
}
|
||||
|
||||
return render_template('dashboard.html',
|
||||
stats=stats,
|
||||
upcoming_reservations=upcoming_reservations)
|
||||
|
||||
@main_bp.route('/api/dashboard-stats')
|
||||
@login_required
|
||||
def dashboard_stats_api():
|
||||
"""API endpoint for dashboard statistics"""
|
||||
# Get today's data
|
||||
today = datetime.utcnow().date()
|
||||
today_reservations = Reservation.query.filter(
|
||||
func.date(Reservation.start_time) == today
|
||||
).count()
|
||||
|
||||
# Get this week's data
|
||||
week_start = today - timedelta(days=today.weekday())
|
||||
week_end = week_start + timedelta(days=6)
|
||||
week_reservations = Reservation.query.filter(
|
||||
func.date(Reservation.start_time) >= week_start,
|
||||
func.date(Reservation.start_time) <= week_end
|
||||
).count()
|
||||
|
||||
# Get monthly data for chart
|
||||
current_date = datetime.utcnow()
|
||||
monthly_data = []
|
||||
|
||||
for i in range(12):
|
||||
month_date = current_date - timedelta(days=30 * i)
|
||||
month_start = month_date.replace(day=1)
|
||||
month_end = (month_start + timedelta(days=32)).replace(day=1) - timedelta(days=1)
|
||||
|
||||
monthly_reservations = Reservation.query.filter(
|
||||
func.date(Reservation.start_time) >= month_start.date(),
|
||||
func.date(Reservation.start_time) <= month_end.date()
|
||||
).count()
|
||||
|
||||
monthly_data.append({
|
||||
'month': month_start.strftime('%Y-%m'),
|
||||
'reservations': monthly_reservations
|
||||
})
|
||||
|
||||
return jsonify({
|
||||
'today_reservations': today_reservations,
|
||||
'week_reservations': week_reservations,
|
||||
'monthly_data': list(reversed(monthly_data))
|
||||
})
|
||||
@@ -0,0 +1,277 @@
|
||||
from flask import Blueprint, render_template, request, redirect, url_for, flash, jsonify
|
||||
from flask_login import login_required, current_user
|
||||
from app import db
|
||||
from app.models.reservation import Reservation, ReservationStatus
|
||||
from app.models.classroom import Classroom
|
||||
from app.models.subject import Subject, Commission
|
||||
from app.forms.reservation import ReservationForm
|
||||
from datetime import datetime, timedelta
|
||||
from sqlalchemy import func, or_
|
||||
|
||||
schedule_bp = Blueprint('schedule', __name__, url_prefix='/schedule')
|
||||
|
||||
@schedule_bp.route('/')
|
||||
@login_required
|
||||
def calendar_view():
|
||||
"""Calendar view of reservations"""
|
||||
return render_template('schedule/calendar.html')
|
||||
|
||||
@schedule_bp.route('/list')
|
||||
@login_required
|
||||
def list_reservations():
|
||||
"""List all reservations"""
|
||||
page = request.args.get('page', 1, type=int)
|
||||
status_filter = request.args.get('status', '')
|
||||
classroom_filter = request.args.get('classroom', 0, type=int)
|
||||
date_filter = request.args.get('date', '')
|
||||
|
||||
query = Reservation.query
|
||||
|
||||
# Apply filters
|
||||
if status_filter:
|
||||
query = query.filter(Reservation.status == status_filter)
|
||||
|
||||
if classroom_filter:
|
||||
query = query.filter(Reservation.classroom_id == classroom_filter)
|
||||
|
||||
if date_filter:
|
||||
try:
|
||||
filter_date = datetime.fromisoformat(date_filter).date()
|
||||
query = query.filter(func.date(Reservation.start_time) == filter_date)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
reservations = query.order_by(Reservation.start_time.desc()).paginate(
|
||||
page=page,
|
||||
per_page=20,
|
||||
error_out=False
|
||||
)
|
||||
|
||||
# Get filter options
|
||||
classrooms = Classroom.query.filter_by(is_active=True).all()
|
||||
|
||||
return render_template('schedule/list.html',
|
||||
reservations=reservations,
|
||||
classrooms=classrooms,
|
||||
status_filter=status_filter,
|
||||
classroom_filter=classroom_filter,
|
||||
date_filter=date_filter)
|
||||
|
||||
@schedule_bp.route('/add', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def add_reservation():
|
||||
"""Add new reservation"""
|
||||
form = ReservationForm()
|
||||
|
||||
# Populate classroom choices
|
||||
classrooms = Classroom.query.filter_by(is_active=True).all()
|
||||
form.classroom_id.choices = [(c.id, c'{c.code} - {c.name} ({c.capacity} seats)') for c in classrooms]
|
||||
|
||||
# Populate commission choices
|
||||
commissions = Commission.query.filter_by(is_active=True).all()
|
||||
form.commission_id.choices = [(c.id, c'{c.get_full_code()} - {c.teacher_name}') for c in commissions]
|
||||
|
||||
if form.validate_on_submit():
|
||||
# Check for conflicts
|
||||
conflicts = Reservation.find_conflicts(
|
||||
form.classroom_id.data,
|
||||
form.start_time.data,
|
||||
form.end_time.data
|
||||
)
|
||||
|
||||
if conflicts:
|
||||
flash('Classroom is already booked for this time slot.', 'error')
|
||||
return render_template('schedule/add.html', form=form)
|
||||
|
||||
# Check if end time is after start time
|
||||
if form.end_time.data <= form.start_time.data:
|
||||
flash('End time must be after start time.', 'error')
|
||||
return render_template('schedule/add.html', form=form)
|
||||
|
||||
# Check capacity
|
||||
classroom = Classroom.query.get(form.classroom_id.data)
|
||||
if form.expected_attendees.data > classroom.capacity:
|
||||
flash('Number of attendees exceeds classroom capacity.', 'error')
|
||||
return render_template('schedule/add.html', form=form)
|
||||
|
||||
reservation = Reservation(
|
||||
classroom_id=form.classroom_id.data,
|
||||
commission_id=form.commission_id.data,
|
||||
user_id=current_user.id,
|
||||
start_time=form.start_time.data,
|
||||
end_time=form.end_time.data,
|
||||
purpose=form.purpose.data,
|
||||
expected_attendees=form.expected_attendees.data,
|
||||
notes=form.notes.data,
|
||||
status=ReservationStatus.PENDING
|
||||
)
|
||||
|
||||
try:
|
||||
db.session.add(reservation)
|
||||
db.session.commit()
|
||||
flash('Reservation created successfully!', 'success')
|
||||
return redirect(url_for('schedule.view_reservation', Id=reservation.id))
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
flash('Error creating reservation.', 'error')
|
||||
|
||||
return render_template('schedule/add.html', form=form)
|
||||
|
||||
@schedule_bp.route('/view/<int:Id>')
|
||||
@login_required
|
||||
def view_reservation(Id):
|
||||
"""View reservation details"""
|
||||
reservation = Reservation.query.get_or_404(Id)
|
||||
return render_template('schedule/view.html', reservation=reservation)
|
||||
|
||||
@schedule_bp.route('/edit/<int:Id>', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def edit_reservation(Id):
|
||||
"""Edit reservation"""
|
||||
reservation = Reservation.query.get_or_404(Id)
|
||||
|
||||
# Only allow editing pending reservations
|
||||
if reservation.status != ReservationStatus.PENDING:
|
||||
flash('Only pending reservations can be edited.', 'warning')
|
||||
return redirect(url_for('schedule.view_reservation', Id=Id))
|
||||
|
||||
form = ReservationForm(obj=reservation)
|
||||
|
||||
# Populate choices
|
||||
classrooms = Classroom.query.filter_by(is_active=True).all()
|
||||
form.classroom_id.choices = [(c.id, c'{c.code} - {c.name} ({c.capacity} seats)') for c in classrooms]
|
||||
|
||||
commissions = Commission.query.filter_by(is_active=True).all()
|
||||
form.commission_id.choices = [(c.id, c'{c.get_full_code()} - {c.teacher_name}') for c in commissions]
|
||||
|
||||
if form.validate_on_submit():
|
||||
# Check for conflicts (excluding this reservation)
|
||||
conflicts = Reservation.find_conflicts(
|
||||
form.classroom_id.data,
|
||||
form.start_time.data,
|
||||
form.end_time.data,
|
||||
exclude_reservation_id=Id
|
||||
)
|
||||
|
||||
if conflicts:
|
||||
flash('Classroom is already booked for this time slot.', 'error')
|
||||
return render_template('schedule/edit.html', form=form, reservation=reservation)
|
||||
|
||||
# Update reservation
|
||||
reservation.classroom_id = form.classroom_id.data
|
||||
reservation.commission_id = form.commission_id.data
|
||||
reservation.start_time = form.start_time.data
|
||||
reservation.end_time = form.end_time.data
|
||||
reservation.purpose = form.purpose.data
|
||||
reservation.expected_attendees = form.expected_attendees.data
|
||||
reservation.notes = form.notes.data
|
||||
reservation.updated_at = datetime.utcnow()
|
||||
|
||||
try:
|
||||
db.session.commit()
|
||||
flash('Reservation updated successfully!', 'success')
|
||||
return redirect(url_for('schedule.view_reservation', Id=Id))
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
flash('Error updating reservation.', 'error')
|
||||
|
||||
return render_template('schedule/edit.html', form=form, reservation=reservation)
|
||||
|
||||
@schedule_bp.route('/confirm/<int:Id>')
|
||||
@login_required
|
||||
def confirm_reservation(Id):
|
||||
"""Confirm a reservation"""
|
||||
reservation = Reservation.query.get_or_404(Id)
|
||||
|
||||
if reservation.status != ReservationStatus.PENDING:
|
||||
flash('Only pending reservations can be confirmed.', 'warning')
|
||||
return redirect(url_for('schedule.view_reservation', Id=Id))
|
||||
|
||||
try:
|
||||
reservation.status = ReservationStatus.CONFIRMED
|
||||
reservation.updated_at = datetime.utcnow()
|
||||
db.session.commit()
|
||||
flash('Reservation confirmed successfully!', 'success')
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
flash('Error confirming reservation.', 'error')
|
||||
|
||||
return redirect(url_for('schedule.view_reservation', Id=Id))
|
||||
|
||||
@schedule_bp.route('/cancel/<int:Id>')
|
||||
@login_required
|
||||
def cancel_reservation(Id):
|
||||
"""Cancel a reservation"""
|
||||
reservation = Reservation.query.get_or_404(Id)
|
||||
|
||||
if reservation.status not in [ReservationStatus.PENDING, ReservationStatus.CONFIRMED]:
|
||||
flash('Cannot cancel this reservation.', 'warning')
|
||||
return redirect(url_for('schedule.view_reservation', Id=Id))
|
||||
|
||||
try:
|
||||
reservation.status = ReservationStatus.CANCELLED
|
||||
reservation.updated_at = datetime.utcnow()
|
||||
db.session.commit()
|
||||
flash('Reservation cancelled successfully!', 'success')
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
flash('Error cancelling reservation.', 'error')
|
||||
|
||||
return redirect(url_for('schedule.view_reservation', Id=Id))
|
||||
|
||||
@schedule_bp.route('/calendar-data')
|
||||
@login_required
|
||||
def calendar_data():
|
||||
"""API endpoint for calendar data"""
|
||||
start = request.args.get('start')
|
||||
end = request.args.get('end')
|
||||
|
||||
if not start or not end:
|
||||
return jsonify([])
|
||||
|
||||
try:
|
||||
start_date = datetime.fromisoformat(start)
|
||||
end_date = datetime.fromisoformat(end)
|
||||
except ValueError:
|
||||
return jsonify([])
|
||||
|
||||
reservations = Reservation.query.filter(
|
||||
Reservation.start_time >= start_date,
|
||||
Reservation.start_time <= end_date,
|
||||
Reservation.status.in_([ReservationStatus.CONFIRMED, ReservationStatus.PENDING])
|
||||
).all()
|
||||
|
||||
events = []
|
||||
for reservation in reservations:
|
||||
color = {
|
||||
ReservationStatus.CONFIRMED: '#28a745',
|
||||
ReservationStatus.PENDING: '#ffc107'
|
||||
}.get(reservation.status, '#6c757d')
|
||||
|
||||
events.append({
|
||||
'id': reservation.id,
|
||||
'title': f'{reservation.classroom.code} - {reservation.commission.get_full_code()}',
|
||||
'start': reservation.start_time.isoformat(),
|
||||
'end': reservation.end_time.isoformat(),
|
||||
'url': url_for('schedule.view_reservation', Id=reservation.id),
|
||||
'backgroundColor': color,
|
||||
'borderColor': color
|
||||
})
|
||||
|
||||
return jsonify(events)
|
||||
|
||||
@schedule_bp.route('/today')
|
||||
@login_required
|
||||
def today_schedule():
|
||||
"""Show today's schedule"""
|
||||
today = datetime.utcnow().date()
|
||||
today_start = datetime.combine(today, datetime.min.time())
|
||||
today_end = datetime.combine(today, datetime.max.time())
|
||||
|
||||
reservations = Reservation.query.filter(
|
||||
Reservation.start_time >= today_start,
|
||||
Reservation.start_time <= today_end,
|
||||
Reservation.status.in_([ReservationStatus.CONFIRMED, ReservationStatus.PENDING])
|
||||
).order_by(Reservation.start_time).all()
|
||||
|
||||
return render_template('schedule/today.html', reservations=reservations)
|
||||
Reference in New Issue
Block a user