Esempio n. 1
0
def display_dashboard(user_id, dash_type):
    if g.entity_type != 'residents' or g.user_id != user_id:
        return redirect('/')
    resident = residents.find_by_id(user_id, g.conn)

    if dash_type == 'guests':
        if request.method == 'POST':
            for guest_id, _ in request.form.iteritems():
                guests.delete_by_id(guest_id, g.conn)
        guests_of_resident = resident.get_guests(g.conn)
        return render_template(
                'resident_dashboard_guests.html',
                resident=resident,
                guests=guests_of_resident,
                entity_type="Resident")

    elif dash_type == 'add_guests':
        if request.method == 'POST':
            fname = request.form.get('fname')
            lname = request.form.get('lname')
            new_guest = guests(
                    building_id=resident.building_id,
                    unit_id=resident.unit_id,
                    fname=fname,
                    lname=lname)
            new_guest.put(g.conn)
            flash('Guest successfully added: {} {}'.format(fname, lname))
        return render_template(
                'resident_dashboard_add_guests.html',
                resident=resident)

    elif dash_type == 'cars':
        cars = resident.get_cars(g.conn)
        if request.method == 'POST':
            for car in cars:
                car_identifier = '{} {}'.format(car.plate_num, car.state)
                if car_identifier in request.form and not car.is_requested:
                    car.request(g.conn)
                elif car_identifier not in request.form and car.is_requested:
                    car.request(g.conn, False)
        map(lambda c: c.get_drivers(g.conn), cars)
        return render_template(
                'resident_dashboard_cars.html',
                resident=resident,
                cars=cars,
                entity_type="Resident")

    elif dash_type == 'services':
        services = service_providers.list_for_building(resident.building_id, g.conn)

        return render_template(
            'resident_dashboard_services.html',
            resident=resident,
            services=services)

    return redirect('/')
Esempio n. 2
0
def display_dashboard(user_id, dash_type):
    if g.entity_type != 'residents' or g.user_id != user_id:
        return redirect('/')
    resident = residents.find_by_id(user_id, g.conn)

    if dash_type == 'guests':
        if request.method == 'POST':
            for guest_id, _ in request.form.iteritems():
                guests.delete_by_id(guest_id, g.conn)
        guests_of_resident = resident.get_guests(g.conn)
        return render_template('resident_dashboard_guests.html',
                               resident=resident,
                               guests=guests_of_resident,
                               entity_type="Resident")

    elif dash_type == 'add_guests':
        if request.method == 'POST':
            fname = request.form.get('fname')
            lname = request.form.get('lname')
            new_guest = guests(building_id=resident.building_id,
                               unit_id=resident.unit_id,
                               fname=fname,
                               lname=lname)
            new_guest.put(g.conn)
            flash('Guest successfully added: {} {}'.format(fname, lname))
        return render_template('resident_dashboard_add_guests.html',
                               resident=resident)

    elif dash_type == 'cars':
        cars = resident.get_cars(g.conn)
        if request.method == 'POST':
            for car in cars:
                car_identifier = '{} {}'.format(car.plate_num, car.state)
                if car_identifier in request.form and not car.is_requested:
                    car.request(g.conn)
                elif car_identifier not in request.form and car.is_requested:
                    car.request(g.conn, False)
        map(lambda c: c.get_drivers(g.conn), cars)
        return render_template('resident_dashboard_cars.html',
                               resident=resident,
                               cars=cars,
                               entity_type="Resident")

    elif dash_type == 'services':
        services = service_providers.list_for_building(resident.building_id,
                                                       g.conn)

        return render_template('resident_dashboard_services.html',
                               resident=resident,
                               services=services)

    return redirect('/')
Esempio n. 3
0
def before_request():
    """
    This function is run at the beginning of every web request (every time you
    enter an address in the web browser).  We use it to setup a database
    connection that can be used throughout the request.

    The variable g is globally accessible.
    """
    try:
        g.conn = engine.connect()

        user_id = request.cookies.get('user_id')
        entity_type = request.cookies.get('entity_type')

        if user_id is None or entity_type is None:
            g.user_id = None
            g.entity_type = None
        elif entity_type == 'residents':
            if residents.find_by_id(user_id, g.conn) is None:
                return redirect('/')
        elif entity_type == 'businesses':
            if service_providers.find_by_id(user_id, g.conn) is None:
                return redirect('/')
        elif entity_type == 'admins':
            admin = admins.find_by_id(user_id, g.conn)
            if admin is None:
                return redirect('/')
            g.building_id = admin.building_id
        else:
            g.user_id = None
            g.entity_type = None
            return redirect('/')

        g.user_id = user_id
        g.entity_type = entity_type

    except:
        print "uh oh, problem connecting to database"
        import traceback
        traceback.print_exc()
        g.conn = None
Esempio n. 4
0
def before_request():
    """
    This function is run at the beginning of every web request (every time you
    enter an address in the web browser).  We use it to setup a database
    connection that can be used throughout the request.

    The variable g is globally accessible.
    """
    try:
        g.conn = engine.connect()

        user_id = request.cookies.get('user_id')
        entity_type = request.cookies.get('entity_type')

        if user_id is None or entity_type is None:
            g.user_id = None
            g.entity_type = None
        elif entity_type == 'residents':
            if residents.find_by_id(user_id, g.conn) is None:
                return redirect('/')
        elif entity_type == 'businesses':
            if service_providers.find_by_id(user_id, g.conn) is None:
                return redirect('/')
        elif entity_type == 'admins':
            admin = admins.find_by_id(user_id, g.conn)
            if admin is None:
                return redirect('/')
            g.building_id = admin.building_id
        else:
            g.user_id = None
            g.entity_type = None
            return redirect('/')

        g.user_id = user_id
        g.entity_type = entity_type

    except:
        print "uh oh, problem connecting to database"
        import traceback
        traceback.print_exc()
        g.conn = None
Esempio n. 5
0
def admin_dashboard(admin_id, dash_type):
    if g.entity_type != 'admins' or g.user_id != admin_id:
        return redirect('/')

    admin = admins.find_by_id(g.user_id, g.conn)
    admin.get_building(g.conn)
    if dash_type == 'manage':
        eid = None

        if request.method == 'POST':
            eid = request.form.get('eid')
            fname = request.form.get('fname')
            lname = request.form.get('lname')
            resident = None
            try:
                resident = residents.find_by_id(eid, g.conn)
            except:
                flash('There was an error finding the resident')
            if resident and fname:
                new_guest = guests(
                        building_id=resident.building_id,
                        unit_id=resident.unit_id,
                        fname=fname,
                        lname=lname)
                new_guest.put(g.conn)
                flash('{} {} added as guest of {} {}'.format(
                    fname, lname, resident.fname, resident.lname))
        if not eid:
            eid = request.args.get('eid', None)

        if eid is None:
            return redirect('/admin_dashboard/{}'.format(admin.entrant_id))

        entrant = residents.find_by_id(eid, g.conn)
        is_resident = True
        entrant_guests = []
        if not entrant:
            is_resident = False
            entrant = entrants.find_by_id(eid, g.conn)
        else:
            entrant_guests = entrant.get_guests(g.conn)

        if not entrant:
            return redirect('/admin_dashboard/{}'.format(admin.entrant_id))

        cars = entrant.get_driven_cars(g.conn)

        return render_template(
                'admin_dashboard_manage.html',
                entrant=entrant,
                guests=entrant_guests,
                admin=admin,
                cars=cars if cars else [],
                is_resident=is_resident)


    if dash_type == 'search':
        search_results = []

        fname = request.form.get('fname')
        lname = request.form.get('lname')
        if fname or lname:
            search_results = entrants.search_by_name(
                    fname, lname, admin.building_id, g.conn)
        else:
            fname = request.form.get('prev_fname', '')
            lname = request.form.get('prev_lname', '')
            search_results = entrants.search_by_name(
                    fname, lname, admin.building_id, g.conn)

        plate_num = request.form.get('plate_num', '')
        state = request.form.get('state', '')
        entrant_id = request.form.get('eid', '')

        if plate_num and state and entrant_id:
            entrant = entrants.find_by_id(entrant_id, g.conn)
            success = entrants.add_as_driver(state,
                                             plate_num,
                                             admin.building_id,
                                             entrant_id,
                                             g.conn)
            if success:
                flash('Successfully added {} {} as driver of {}'.format(
                    entrant.fname, entrant.lname, plate_num))
            else:
                flash('Car with state {} and plate number {} does not exist.'.format(
                    state, plate_num))

        return render_template(
                'admin_dashboard_search.html',
                admin=admin,
                results=search_results,
                fname_search=fname,
                lname_search=lname)

    if dash_type == 'requested_cars':
        if request.method == 'POST': # Unpark the car in the get params
            state_of_car_to_unpark = request.args.get('ups')
            pnum_of_car_to_unpark  = request.args.get('upl')

            unpark_car(admin.building_id, state_of_car_to_unpark, pnum_of_car_to_unpark)

        cars = vehicles.find_requested_cars(g.conn, admin.building_id)
        map(lambda c: c.get_drivers(g.conn), cars)
        return render_template('requested_cars.html', cars=cars, admin=admin)

    if dash_type == 'park':
        if request.method == 'POST':
            state = request.form.get('state')
            pnum = request.form.get('pnum')
            res = vehicles.find_by_license_plate(g.conn, state, pnum, admin.building_id)
            if res is not None:
                return redirect(
                        '/admin_dashboard/{}/edit_car?s={}&l={}'.format(
                            admin.entrant_id,
                            state,
                            pnum))
            else:
                return redirect(
                        '/admin_dashboard/{}/add_car?s={}&l={}'.format(
                            admin.entrant_id,
                            state,
                            pnum))

        return render_template('admin_dashboard_park.html', admin=admin)

    if dash_type == 'add_car':
        state = request.args.get('s')
        pnum = request.args.get('l')
        if request.method == 'POST':
            kwargs = { attr:val if val else None
                       for attr, val in request.form.iteritems()}
            kwargs['is_requested'] = False
            kwargs['building_id'] = admin.building_id
            kwargs['state'] = state
            kwargs['plate_num'] = pnum
            new_car = vehicles(**kwargs)
            try:
                new_car.put(g.conn)
            except Exception as e:
                flash('Spot, default spot, or key is already taken')

            return redirect(
                        '/admin_dashboard/{}/edit_car?s={}&l={}'.format(
                            admin.entrant_id,
                            state,
                            pnum))
        return render_template(
                'add_car.html', admin=admin, state=state, pnum=pnum)

    if dash_type == 'edit_car':
        state = request.args.get('s')
        pnum = request.args.get('l')
        if request.method == 'POST':
            try:
                update_car(state, pnum, request, admin.building_id)
            except:
                flash('Error: Default parking spot is taken.')

            try:
                park_car(state, pnum, request, admin.building_id)
            except:
                flash('Error: Spot is taken, key slot is taken, or spot does not exist.')

        car = vehicles.find_by_license_plate(g.conn, state, pnum, admin.building_id)
        if car is None:
            return redirect('/')

        car.get_drivers(g.conn)

        return render_template('edit_car.html', car=car, admin=admin)
Esempio n. 6
0
def admin_dashboard(admin_id, dash_type):
    if g.entity_type != 'admins' or g.user_id != admin_id:
        return redirect('/')

    admin = admins.find_by_id(g.user_id, g.conn)
    admin.get_building(g.conn)
    if dash_type == 'manage':
        eid = None

        if request.method == 'POST':
            eid = request.form.get('eid')
            fname = request.form.get('fname')
            lname = request.form.get('lname')
            resident = None
            try:
                resident = residents.find_by_id(eid, g.conn)
            except:
                flash('There was an error finding the resident')
            if resident and fname:
                new_guest = guests(building_id=resident.building_id,
                                   unit_id=resident.unit_id,
                                   fname=fname,
                                   lname=lname)
                new_guest.put(g.conn)
                flash('{} {} added as guest of {} {}'.format(
                    fname, lname, resident.fname, resident.lname))
        if not eid:
            eid = request.args.get('eid', None)

        if eid is None:
            return redirect('/admin_dashboard/{}'.format(admin.entrant_id))

        entrant = residents.find_by_id(eid, g.conn)
        is_resident = True
        entrant_guests = []
        if not entrant:
            is_resident = False
            entrant = entrants.find_by_id(eid, g.conn)
        else:
            entrant_guests = entrant.get_guests(g.conn)

        if not entrant:
            return redirect('/admin_dashboard/{}'.format(admin.entrant_id))

        cars = entrant.get_driven_cars(g.conn)

        return render_template('admin_dashboard_manage.html',
                               entrant=entrant,
                               guests=entrant_guests,
                               admin=admin,
                               cars=cars if cars else [],
                               is_resident=is_resident)

    if dash_type == 'search':
        search_results = []

        fname = request.form.get('fname')
        lname = request.form.get('lname')
        if fname or lname:
            search_results = entrants.search_by_name(fname, lname,
                                                     admin.building_id, g.conn)
        else:
            fname = request.form.get('prev_fname', '')
            lname = request.form.get('prev_lname', '')
            search_results = entrants.search_by_name(fname, lname,
                                                     admin.building_id, g.conn)

        plate_num = request.form.get('plate_num', '')
        state = request.form.get('state', '')
        entrant_id = request.form.get('eid', '')

        if plate_num and state and entrant_id:
            entrant = entrants.find_by_id(entrant_id, g.conn)
            success = entrants.add_as_driver(state, plate_num,
                                             admin.building_id, entrant_id,
                                             g.conn)
            if success:
                flash('Successfully added {} {} as driver of {}'.format(
                    entrant.fname, entrant.lname, plate_num))
            else:
                flash('Car with state {} and plate number {} does not exist.'.
                      format(state, plate_num))

        return render_template('admin_dashboard_search.html',
                               admin=admin,
                               results=search_results,
                               fname_search=fname,
                               lname_search=lname)

    if dash_type == 'requested_cars':
        if request.method == 'POST':  # Unpark the car in the get params
            state_of_car_to_unpark = request.args.get('ups')
            pnum_of_car_to_unpark = request.args.get('upl')

            unpark_car(admin.building_id, state_of_car_to_unpark,
                       pnum_of_car_to_unpark)

        cars = vehicles.find_requested_cars(g.conn, admin.building_id)
        map(lambda c: c.get_drivers(g.conn), cars)
        return render_template('requested_cars.html', cars=cars, admin=admin)

    if dash_type == 'park':
        if request.method == 'POST':
            state = request.form.get('state')
            pnum = request.form.get('pnum')
            res = vehicles.find_by_license_plate(g.conn, state, pnum,
                                                 admin.building_id)
            if res is not None:
                return redirect(
                    '/admin_dashboard/{}/edit_car?s={}&l={}'.format(
                        admin.entrant_id, state, pnum))
            else:
                return redirect('/admin_dashboard/{}/add_car?s={}&l={}'.format(
                    admin.entrant_id, state, pnum))

        return render_template('admin_dashboard_park.html', admin=admin)

    if dash_type == 'add_car':
        state = request.args.get('s')
        pnum = request.args.get('l')
        if request.method == 'POST':
            kwargs = {
                attr: val if val else None
                for attr, val in request.form.iteritems()
            }
            kwargs['is_requested'] = False
            kwargs['building_id'] = admin.building_id
            kwargs['state'] = state
            kwargs['plate_num'] = pnum
            new_car = vehicles(**kwargs)
            try:
                new_car.put(g.conn)
            except Exception as e:
                flash('Spot, default spot, or key is already taken')

            return redirect('/admin_dashboard/{}/edit_car?s={}&l={}'.format(
                admin.entrant_id, state, pnum))
        return render_template('add_car.html',
                               admin=admin,
                               state=state,
                               pnum=pnum)

    if dash_type == 'edit_car':
        state = request.args.get('s')
        pnum = request.args.get('l')
        if request.method == 'POST':
            try:
                update_car(state, pnum, request, admin.building_id)
            except:
                flash('Error: Default parking spot is taken.')

            try:
                park_car(state, pnum, request, admin.building_id)
            except:
                flash(
                    'Error: Spot is taken, key slot is taken, or spot does not exist.'
                )

        car = vehicles.find_by_license_plate(g.conn, state, pnum,
                                             admin.building_id)
        if car is None:
            return redirect('/')

        car.get_drivers(g.conn)

        return render_template('edit_car.html', car=car, admin=admin)