Ejemplo n.º 1
0
def editschedule():
    global con

    # set debugging level
    db.enabled = False

    # Only administrators have access to this page
    if not session['administrator']:
        return render_template(
            'error.html', error="Only Administrators can access schedules")

    # create the objects we need
    sched = Schedule()
    bt = Boat()
    rt = Route()

    # process the data sent back from the form
    if request.method == 'POST':

        # Read the boats and schedule to display in option lists
        boats = readBoatTable()
        routes = readRouteTable()

        # If they processed the edit button then read the scchedule record and pass the details
        # to the new schedule scren
        if 'Edit' in request.form:
            CruiseDate = request.form['Edit'].split('.')[0]
            CruiseNo = int(request.form['Edit'].split('.')[1])

            (dbStatus, rows) = sched.readSched(con, CruiseDate, CruiseNo)
            if dbStatus == False:
                #                return render_template("newschedule.html",  sched = rows,  CruiseDate = CruiseDate, CruiseNo = CruiseNo, boats = boats, routes = routes, action = 'UPDATE', returnmessage = sched.error)
                return render_template("newschedule.html",
                                       sched=rows,
                                       CruiseDate=CruiseDate,
                                       CruiseNo=CruiseNo,
                                       boats=boats,
                                       routes=routes,
                                       action='UPDATE',
                                       returnmessage=sched.error)
            else:
                return render_template("newschedule.html",
                                       sched=rows[0],
                                       CruiseDate=CruiseDate,
                                       CruiseNo=CruiseNo,
                                       boats=boats,
                                       routes=routes,
                                       action='UPDATE')
        # If they have pressed add then create a blank 'rows' record and pass to the newschedule form
        if 'Add' in request.form:
            rows = sched.blankScheduleRow()
            CruiseDate = rows[0]["CruiseDate"]
            CruiseNo = int(rows[0]["CruiseNo"])
            return render_template("newschedule.html",
                                   sched=rows,
                                   CruiseDate=CruiseDate,
                                   CruiseNo=CruiseNo,
                                   boats=boats,
                                   routes=routes,
                                   action='ADD')
Ejemplo n.º 2
0
def confirmschedule():
    global con

    # set debugging level
    db.enabled = True
    db.print(" ---confirmschedule ---")

    # If we aren't logged in here then do not let the user get any further
    if not session['administrator']:
        return render_template(
            'error.html', error="Only Administrators can access schedules")

    usersched = {}

    # Assign a 'usersched' dictionary which we can
    # render back to the form with the values entered by the user
    def setuserdata():
        usersched['CruiseDate'] = request.form['CruiseDate']
        usersched['CruiseNo'] = request.form['CruiseNo']
        usersched['departure'] = request.form['departure']
        usersched['BoatID'] = request.form['BoatID']
        usersched['RouteID'] = request.form['RouteID']
        usersched['return'] = request.form['return']
        try:
            usersched['available'] = int(request.form['available'])
        except:
            usersched['available'] = 0

    # set the schedule properties from the page. we don't need
    # to do any validation here as the schedule object will do
    # all of that and return any any errors.
    def setschedvalues():
        sched.CruiseDate = usersched['CruiseDate']
        sched.CruiseNo = usersched['CruiseNo']
        sched.departure = usersched['departure']
        sched.BoatID = usersched['BoatID']
        sched.RouteID = usersched['RouteID']
        sched.returntime = usersched['return']
        sched.available = usersched['available']

    # pocess the data sent from this page
    if request.method == 'POST':

        # create a scheule object
        sched = Schedule()

        # keep the user data to diaply back if necessary
        setuserdata()

        # Read the boats and schedule to display in option lists
        boats = readBoatTable()
        routes = readRouteTable()

        # set the scheule properties
        setschedvalues()

        # If they selected the 'read' button then read this schedule record and pass back to user.
        # If not found then set the form to blank, except for the cruise date and number attempting to be found
        if 'read' in request.form:
            (dbStatus, rows) = sched.readSched(con, request.form['CruiseDate'],
                                               request.form['CruiseNo'])
            if dbStatus == False:
                rows = sched.blankScheduleRow()
            return render_template("newschedule.html",
                                   sched=rows[0],
                                   CruiseDate=request.form['CruiseDate'],
                                   CruiseNo=request.form['CruiseNo'],
                                   boats=boats,
                                   routes=routes,
                                   returnmessage=sched.error)

        # If updating, call the updateSchedule method and then return any error
        if 'update' in request.form:
            db.print(" * Processing update *")
            dbStatus = sched.updateSchedule(con, request.form['CruiseDate'],
                                            request.form['CruiseNo'])
            return render_template("newschedule.html",
                                   sched=usersched,
                                   CruiseDate=request.form['CruiseDate'],
                                   CruiseNo=request.form['CruiseNo'],
                                   boats=boats,
                                   routes=routes,
                                   returnmessage=sched.error)

        # If deleting blank the screen if successful otherwise return the error message
        if 'delete' in request.form:
            db.print(" * Processing delete *")
            # let's check to see if there are any bookings
            book = Booking()
            (dbStatus,
             bookings) = book.readBookingbySched(con,
                                                 request.form['CruiseDate'],
                                                 request.form['CruiseNo'])
            if dbStatus == False:
                db.print("Booking error = " + book.error)
                # If there are no bookings then proceed with the delete
                if book.error[0:30] == 'No Bookings found for schedule':
                    dbStatus = sched.deleteSchedule(con,
                                                    request.form['CruiseDate'],
                                                    request.form['CruiseNo'])
                    if dbStatus == False:
                        return render_template(
                            "newschedule.html",
                            sched=usersched,
                            CruiseDate=request.form['CruiseDate'],
                            CruiseNo=request.form['CruiseNo'],
                            boats=boats,
                            routes=routes,
                            returnmessage=sched.error)
                    else:
                        rows = sched.blankScheduleRow()
                        return render_template("newschedule.html",
                                               sched=rows[0],
                                               CruiseDate=None,
                                               CruiseNo=None,
                                               boats=boats,
                                               routes=routes,
                                               returnmessage=sched.error)
                else:
                    return render_template(
                        "newschedule.html",
                        sched=usersched,
                        CruiseDate=request.form['CruiseDate'],
                        CruiseNo=request.form['CruiseNo'],
                        boats=boats,
                        routes=routes,
                        returnmessage=book.error)
            else:
                return render_template(
                    "newschedule.html",
                    sched=usersched,
                    CruiseDate=request.form['CruiseDate'],
                    CruiseNo=request.form['CruiseNo'],
                    boats=boats,
                    routes=routes,
                    action='CONFIRM',
                    returnmessage=
                    'Schedule contains bookings, press confirm to delete')

        # If we are confirming a delete then simple delete the schedule and let the cascade rule in the database delete all the bookings.
        if 'confirmdelete' in request.form:
            db.print(" * Processing confirm delete *")
            dbStatus = sched.deleteSchedule(con, request.form['CruiseDate'],
                                            request.form['CruiseNo'])
            if dbStatus == False:
                return render_template("newschedule.html",
                                       sched=usersched,
                                       CruiseDate=request.form['CruiseDate'],
                                       CruiseNo=request.form['CruiseNo'],
                                       boats=boats,
                                       routes=routes,
                                       returnmessage=sched.error)
            else:
                rows = sched.blankScheduleRow()
                return render_template("newschedule.html",
                                       sched=rows,
                                       CruiseDate=None,
                                       CruiseNo=None,
                                       boats=boats,
                                       routes=routes,
                                       returnmessage=sched.error)
        # If they have canceled a delete, just rerender the page

        if 'canceldelete' in request.form:
            db.print(" * Processing cancel delete *")
            return render_template("newschedule.html",
                                   sched=usersched,
                                   CruiseDate=request.form['CruiseDate'],
                                   CruiseNo=request.form['CruiseNo'],
                                   boats=boats,
                                   routes=routes,
                                   returnmessage=sched.error)

        # If adding then call the insertSchedule method and let it hande any errors
        if 'add' in request.form:
            db.print(" * Processing add *")
            db.print("CruiseDate = " + request.form['CruiseDate'])
            db.print("CruiseNo = " + str(request.form['CruiseNo']))

            # Make sure schedule has not been added while they have been adding data to the screen.
            (dbStatus, schdle) = sched.readSched(con,
                                                 request.form['CruiseDate'],
                                                 request.form['CruiseNo'])
            if dbStatus == True:
                return render_template(
                    "newschedule.html",
                    sched=usersched,
                    CruiseDate=request.form['CruiseDate'],
                    CruiseNo=request.form['CruiseNo'],
                    boats=boats,
                    routes=routes,
                    returnmessage='Schedule already exists!')

            # set the schedule properties from the page.
            setschedvalues()

            todaysDate = datetime.now().strftime("%Y-%m-%d")
            if request.form['CruiseDate'] < todaysDate:
                return render_template(
                    "newschedule.html",
                    sched=usersched,
                    CruiseDate=request.form['CruiseDate'],
                    CruiseNo=request.form['CruiseNo'],
                    boats=boats,
                    routes=routes,
                    returnmessage='Cannot add schedule in the past')

            # Create a boat object
            boat = Boat()

            # and check it exists OK
            (dbStatus, thisboat) = boat.readBoatByID(con, sched.BoatID)
            if dbStatus == False:
                return render_template('error.html', error=boat.error)

            # Don't allow a boat to exceed capacity
            if sched.available > boat.capacity:
                return render_template(
                    "newschedule.html",
                    sched=usersched,
                    CruiseDate=request.form['CruiseDate'],
                    CruiseNo=request.form['CruiseNo'],
                    boats=boats,
                    routes=routes,
                    returnmessage='Available seats exceeds boat capacity of ' +
                    str(boat.capacity))

            # insert a schedule.  any errors will just get rendered back to this page
            dbStatus = sched.insertSchedule(con)
            return render_template("newschedule.html",
                                   sched=usersched,
                                   CruiseDate=request.form['CruiseDate'],
                                   CruiseNo=request.form['CruiseNo'],
                                   boats=boats,
                                   routes=routes,
                                   returnmessage=sched.error)