| from datetime import datetime, timedelta |
| |
| from flask import flash, redirect, render_template, request, url_for, current_app |
| from flask_login import current_user, login_user, logout_user, login_required |
| from werkzeug.urls import url_parse |
| |
| from app import db |
| from app.booking import bp |
| from app.booking.decorators import check_max_booking |
| from app.booking.forms import BookingForm |
| from app.email import send_email |
| from app.jenkins_request import handle_booking_job, delete_booking_job |
| from app.models import Booking, BookingStatus |
| from app.user.decorators import check_confirmed, check_sshkey |
| |
| @bp.route('/newbooking', methods=['GET', 'POST']) |
| @login_required |
| @check_confirmed |
| @check_sshkey |
| @check_max_booking |
| def newbooking(): |
| form = BookingForm(current_user.email) |
| if current_user.confirmed is False: |
| flash('Your mail address is not confirmed yet. Please confirm it to access full functionality.', 'danger') |
| if current_user.ssh_public_key is None or not current_user.ssh_public_key: |
| flash("You either haven't registered your SSH Public Key or it is invalid. Please register your SSH Public Key in order to be able to make a new booking!", 'danger') |
| if form.validate_on_submit(): |
| scenario = form.scenario.data |
| booking_length = int(form.booking_length.data) |
| current_datetime = datetime.utcnow() |
| created_on = current_datetime.isoformat() |
| updated_on = current_datetime.isoformat() |
| expires_on = (current_datetime + timedelta(days=booking_length)).isoformat() |
| booking = Booking(user_id=current_user.id, |
| stack=form.stack.data, |
| status_id=BookingStatus.query.filter_by(status_text='new').first().id, |
| scenario=scenario, |
| booking_length=booking_length, |
| created_on=created_on, |
| updated_on=updated_on, |
| expires_on=expires_on) |
| db.session.add(booking) |
| db.session.flush() |
| handle_booking_job(booking.id, scenario) |
| db.session.commit() |
| bookings = Booking.query.filter_by(user_id=current_user.id).order_by(Booking.created_on.desc()) |
| flash('Your booking is now registered and will be acted upon.', 'success') |
| return redirect(url_for('booking.bookings')) |
| elif request.method == 'GET': |
| form.email.data = current_user.email |
| return render_template('booking/newbooking.html', title='[nolabs] | New Booking', form=form) |
| |
| @bp.route('/cancelbooking/<booking_id>', methods=['GET', 'POST']) |
| @login_required |
| @check_confirmed |
| @check_sshkey |
| def cancel_booking(booking_id): |
| current_booking = Booking.query.filter_by(id=booking_id).first() |
| if BookingStatus.query.filter_by(id=current_booking.status_id).first().status_text == 'new': |
| current_booking.status_id = BookingStatus.query.filter_by(status_text='cancelled').first().id |
| flash('Your booking is now cancelled!', 'info') |
| else: |
| delete_booking_job(booking_id, current_booking.scenario) |
| current_booking.status_id = BookingStatus.query.filter_by(status_text='expired').first().id |
| flash('Your booking is now being deleted!', 'info') |
| db.session.commit() |
| bookings = Booking.query.filter_by(user_id=current_user.id).order_by(Booking.created_on.desc()) |
| return redirect(url_for('booking.bookings')) |
| |
| @bp.route('/bookings') |
| @login_required |
| @check_confirmed |
| @check_sshkey |
| def bookings(): |
| if current_user.confirmed is False: |
| flash('Your mail address is not confirmed yet. Please confirm it to access full functionality.', 'danger') |
| if current_user.ssh_public_key is None or not current_user.ssh_public_key: |
| flash("You either haven't registered your SSH Public Key or it is invalid. Please register your SSH Public Key in order to be able to make a new booking!", 'danger') |
| page = request.args.get('page', 1, type=int) |
| booking_status_id_text = dict() |
| for k, v in BookingStatus.query.with_entities(BookingStatus.id, BookingStatus.status_text).all(): |
| booking_status_id_text[k] = v |
| bookings = Booking.query.filter_by(user_id=current_user.id).order_by(Booking.created_on.desc()). \ |
| paginate(page, current_app.config['BOOKINGS_PER_PAGE'], False) |
| next_url = url_for('booking.bookings', page=bookings.next_num) \ |
| if bookings.has_next else None |
| prev_url = url_for('booking.bookings', page=bookings.prev_num) \ |
| if bookings.has_prev else None |
| return render_template('booking/bookings.html', title='[nolabs] | My Bookings', user=current_user, |
| bookings=bookings.items, booking_status_id_text=booking_status_id_text, next_url=next_url, prev_url=prev_url) |