def update_bus_schedule(bus_id): bus = Bus.query.get(bus_id) update_bus_schedule_form = UpdateBusScheduleForm() if request.method == "POST": if update_bus_schedule_form.validate_on_submit(): journey_id = update_bus_schedule_form.journey_id.data departure_time = update_bus_schedule_form.departure_time.data booking_deadline = departure_time - timedelta( minutes=update_bus_schedule_form.booking_deadline.data) free_bus_time = departure_time + timedelta( minutes=update_bus_schedule_form.free_bus_time.data) broadcast = update_bus_schedule_form.broadcast.data UTC = update_bus_schedule_form.UTC_offset.data bus.journey_id = journey_id bus.departure_time = departure_time bus.booking_deadline = booking_deadline bus.free_bus_time = free_bus_time bus.broadcast = broadcast bus.branch = get_current_branch() db.session.commit() flash("Bus scheduled.", "success") # set free bus task # print(dir(free_bus)) # send_task("celery.free_bus", (bus,), eta=free_bus_time) # free_bus.apply_async((bus), eta=free_bus_time) return redirect(url_for('bus.get_buses')) return render_template('bus/update-bus-schedule.html', bus=bus, update_bus_schedule_form=update_bus_schedule_form)
def get_journeys(): branch = get_current_branch() journeys = Journey.query.filter_by(branch_id=branch.id).all() create_journey_form = CreateJourneyForm() return render_template("journey/journeys.html", journeys=journeys, create_journey_form=create_journey_form)
def update_manager_profile(profile_id): update_profile_form = UpdateProfileForm() branch = get_current_branch() manager_profile = Profile.query.get(profile_id) if update_profile_form.validate_on_submit(): profile_id = update_profile_form.data.get("id") profile = Profile.query.get(profile_id) if manager_profile == profile: data = update_profile_form.data first_name = data.get("first_name") last_name = data.get("last_name") email = data.get("email") telephone_code = data.get("telephone_code") telephone = data.get("telephone") profile.first_name = first_name profile.last_name = last_name new_email = email new_telephone = join_telephone(telephone_code, telephone) update_profile_email_and_telephone(profile, new_email, new_telephone) db.session.commit() flash("Manager updated", "success") else: flash("An error occured!", "danger") else: flash(f"{update_profile_form.errors}", "danger") return redirect(request.referrer)
def get_buses(): branch = get_current_branch() company = branch.company free_buses = Bus.query.filter_by(company_id=company.id, branch_id=None) scheduled_buses = Bus.query.filter_by(branch_id=branch.id) return render_template("bus/buses.html", free_buses=free_buses, scheduled_buses=scheduled_buses)
def get_booking(booking_id): company = get_current_branch().company booking = Booking.query.get(booking_id) update_booking_schedule_form = UpdateBookingScheduleForm() update_booking_layout_form = UpdateBookingLayoutForm() return render_template( "booking/booking.html", booking=booking, update_booking_schedule_form=update_booking_schedule_form, update_booking_layout_form=update_booking_layout_form)
def get_journey(journey_id): branch = get_current_branch() journey = Journey.query.get(journey_id) statuses = branch.company.statuses create_pickup_form = CreatePickupForm() create_pricing_form = CreatePricingForm() update_journey_form = UpdateJourneyForm(obj=journey) return render_template("journey/journey.html", journey=journey, create_pickup_form=create_pickup_form, create_pricing_form=create_pricing_form, statuses=statuses, update_journey_form=update_journey_form)
def update_journey(journey_id): update_journey_form = UpdateJourneyForm() branch = get_current_branch() journey = Journey.query.get(journey_id) if update_journey_form.validate_on_submit(): journey_id = update_journey_form.id.data journey.to = update_journey_form.to.data journey.from_ = update_journey_form.from_.data journey.distance = update_journey_form.distance.data journey.duration = update_journey_form.duration.data db.session.commit() flash("Journey updated", "success") else: flash(f"{update_journey_form.errors}", "danger") return redirect(request.referrer)
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.datetime_format = app.config.get("DATETIME_FORMAT") self.min_date = now() self.max_date = self.min_date + timedelta(days=5) self.booking_deadline.choices = booking_deadline_choices self.free_bus_time.choices = free_bus_time_choices branch = get_current_branch() self.journey_id.choices = [ (journey.id, journey) for journey in list(filter( lambda journey: len(journey.pricings) and len(journey.pickups), Journey.query.filter_by(branch_id=branch.id) ) ) ]
def delete_cashier_profile(profile_id): delete_profile_form = DeleteProfileForm() branch = get_current_branch() cashier_profile = Profile.query.get(profile_id) if delete_profile_form.validate_on_submit(): profile_id = delete_profile_form.data.get("id") profile = Profile.query.get(profile_id) if cashier_profile == profile: db.session.delete(profile) db.session.delete(profile.user) db.session.commit() flash("Cashier deleted", "success") else: flash("An error occured!", "danger") else: flash(f"{delete_profile_form.errors}", "danger") return redirect(url_for('profile.get_cashier_profiles'))
def wrapper(*args, **kwargs): branch = get_current_branch() if branch: journeys = branch.journeys if len(journeys): for journey in journeys: if len(journey.pickups) and len(journey.pricings): return func(*args, **kwargs) else: flash( f"The journey {journey} has not been setup properly.", "warning") return func(*args, **kwargs) else: flash("Setup atleast one journey.", "warning") return func(*args, **kwargs) else: return func(*args, **kwargs)
def get_company_bookings(): company = get_current_branch().company buses_query = Bus.query.filter_by(company_id=company.id) bus_id_choices = [(bus.id, bus) for bus in buses_query.all()] filter_bookings_form = FilterBookingsForm(formdata=request.args, bus_id_choices=bus_id_choices) bookings_query = Booking.query bus_id = None created_on_gte = None created_on_lte = None if request.args: if filter_bookings_form.validate(): data = filter_bookings_form.data created_on_gte = filter_bookings_form.created_on_gte.data created_on_lte = filter_bookings_form.created_on_lte.data bus_id = filter_bookings_form.bus_id.data if bus_id: bookings_query = bookings_query.filter_by(bus_id=bus_id) if created_on_gte: bookings_query = bookings_query.filter( Booking.created_at >= created_on_gte) if created_on_lte: bookings_query = bookings_query.filter( Booking.created_at <= created_on_lte) bookings = bookings_query.all() filter_bookings_form.created_on_gte.data = request.args.get( "created_on_gte") filter_bookings_form.created_on_lte.data = request.args.get( "created_on_lte") return render_template("booking/booking-history.html", bookings=bookings, filter_bookings_form=filter_bookings_form)
def create_journey(): branch = get_current_branch() create_journey_form = CreateJourneyForm() if create_journey_form.validate_on_submit(): # create journey from_ = create_journey_form.from_.data to = create_journey_form.to.data distance = create_journey_form.distance.data duration = create_journey_form.duration.data journey = Journey(from_=from_, to=to, distance=distance, duration=duration, branch=branch) db.session.add(journey) db.session.commit() flash("Journey created.", "success") return redirect(url_for('journey.get_journey', journey_id=journey.id)) flash(str(create_journey_form.errors), "danger") return redirect(url_for('company.get_journeys'))
def get_journey_pricings(journey_id, status_id): journey = Journey.query.get(journey_id) status = Status.query.get(status_id) branch = get_current_branch() statuses = branch.company.statuses pricings = Pricing.query.filter_by(journey_id=journey.id, status_id=status.id) create_pricing_form = CreatePricingForm() pricings_patch_template = render_template( 'pricing/pricings-patch.html', pricings=pricings, journey=journey, active_status=status, statuses=statuses, create_pricing_form=create_pricing_form) data = { "form_templates": { "#pricingsPatch": pricings_patch_template } } return data
def create_payment(booking): grid = booking.booked_grid bus = grid.bus company = bus.company branch = get_current_branch() payment = Payment(reference=generate_reference(), amount=booking.fare, method="CASH", passenger_name=booking.passenger_name, passenger_telephone=booking.passenger_telephone, branch_name=branch.name, company_name=company.name, grid_number=grid.number, bus_number=bus.number, company=company, bus=bus, journey=bus.journey, pricing=booking.pricing, grid_id=grid.id) booking.payment = payment db.session.add(payment)
def create_cashier_profile(): create_profile_form = CreateProfileForm() branch = get_current_branch() if create_profile_form.validate_on_submit(): first_name = create_profile_form.data.get("first_name") last_name = create_profile_form.data.get("last_name") email = create_profile_form.data.get("email") telephone_code = create_profile_form.data.get("telephone_code") telephone = create_profile_form.data.get("telephone") user = User(username=email) profile = Profile(first_name=first_name, last_name=last_name, telephone=join_telephone(telephone_code, telephone), email=email, is_cashier=True) profile.branch = branch profile.user = user db.session.add(user) db.session.add(profile) create_user_token(user) db.session.commit() send_auth_mail(user.username, user.token.token) flash("Cashier created", "success") else: flash(f"{create_profile_form.errors}", "danger") return redirect(request.referrer)
def get_cashier_profiles(): branch = get_current_branch() cashiers = Profile.query.filter_by(branch_id=branch.id, is_cashier=True).all() create_profile_form = CreateProfileForm() return render_template('profile/cashier-profiles.html', cashiers=cashiers, create_profile_form=create_profile_form)
def get_company_bookings(bus_id): company = get_current_branch().company return render_template("company/company-dashboard.html", bookings=[])