def logout(): # set this journey_id to null for this driver # and also set end time token = request.args['token'] # check all attendance stuff are already done by driver Driver.logout_session(token) return jsonify(status='ok')
def login(): # require username,password as json js = request.json if js is not None: if 'username' in js and 'password' in js and 'journey_type' in js: u_id = js['username'] _pass = js['password'] j_type = js['journey_type'] # validate j_type if j_type not in [0, 1]: return jsonify(status="error", message="Incorrect journey type") user = Driver.get_user(u_id) if not user: return make_response( jsonify(status='error', message='Invalid Credential'), 403) name = user[0] pass_hash = user[1] bus_no = user[2] if pass_hash is not False and SessionHelper.is_password_correct( pass_hash, _pass): # ok correct user # make sure, if similar ride is not already completed by this driver if Driver.is_ride_already_completed(u_id, j_type): return jsonify(status='error', message='ride already completed for today.') # get active ride active_ride = user[3] # generate a random token m_token = utils.rand(40) m_expire = utils.get_expiry_date_full() Driver.update_token(m_token, m_expire, u_id) if active_ride is None or active_ride is '': # no active session, start new session # and create new journey and set it bus_id = user[4] Journey.trans_create_journey(j_type, utils.get_date_full(), bus_id, u_id) else: # no need to create new journey pass return jsonify(status="ok", message="Correct Credentials", token=m_token, valid_till=m_expire, name=name, bus=bus_no) else: return make_response( jsonify(status="error", message="Invalid Credential"), 403) else: return jsonify(status="error", message="Incorrect Request") else: return jsonify(status="error", message="Only Json Body is allowed")
def pick_attendance(): js = request.json if 'kid_ids' in js and 'lat' in js and 'lon' in js: ids = js['kid_ids'] lat = js['lat'] lon = js['lon'] gps = Gps.tuple_to_str(lat, lon) time = utils.get_date_full() token = request.args['token'] j_id = Driver.get_active_ride(token) if j_id is None or j_id is '': return jsonify(status='error', message='Cant add,as not active ride') else: # filter valid ids id_from_db = Kid.get_kid_ids(j_id) ids_set = set(ids) ids_db_set = set(id_from_db) ids_valid = list(ids_set & ids_db_set) # intersection of kid ids for id in ids_valid: atten = Attendance(pick_present=1, kid_id=id, journey_id=j_id, pick_gps=gps, pick_time=time) atten.add() return jsonify(status='ok', message='Attendance taken') else: return jsonify(status='error', message='incorrect request')
def add_gps(): js = request.json if 'lat' in js and 'lon' in js: token = request.args['token'] lat = js['lat'] lon = js['lon'] # add location to this active journey print(lat, lon) j_id = Driver.get_active_ride(request.args['token']) if j_id is None or j_id is '': return jsonify(status='error', message='unauthorized user or inactive session') else: # add location to location table # check if 1 minute passed since last update if Location.is_delta_time_passed(j_id, utils.get_prev_time()): # now add location l = Location(Gps.tuple_to_str(lat, lon), j_id) l.add() print("adding location...") # update gps table Journey.update_gps(j_id, Gps.tuple_to_str(lat, lon)) return jsonify(status="ok", message="Gps data added") else: return jsonify(status='error', message='Incorrect parameters')
def decorated_function(*args, **kwargs): if 'token' in request.args and Driver.is_valid_token(request.args['token']): pass else: return make_response(jsonify(status='error', message='unauthorized user'), 403) return f(*args, **kwargs)
def driver(): # handle driver tasks form = AddDriverForm(request.form) # TODO : Disable add when no bus unallocated (for now using front-end logic) un_alloc = Bus.unallocated_bus() if form.validate_on_submit() and request.method == 'POST': # if 'bus' not in request.form: # TODO: later add option for update d = Driver(user_id=form.username.data, bus_id=request.form['bus'] , name=form.name.data, contact=form.contact.data, password=SessionHelper.get_password_hash(form.password.data)) d.add() # clear fileds form.username.data = form.name.data = form.contact.data = '' # return render_template('admin/driver.html',form=form) alloc_driver = Driver.get_all_allocated() return render_template('admin/driver.html', form=form, bus_data=un_alloc, driver_list=alloc_driver)
def get_kids_by_dropping(): if 'dropped' in request.args: # but make sure a ride session exists j_id = Driver.get_active_ride(request.args['token']) if j_id is None or j_id is '': return jsonify(status='error', message='No active ride.') dropped = request.args['dropped'] token = request.args['token'] if dropped is '1' or dropped is 1: kids = Attendance.get_kid_dropped(token) return jsonify(status='ok', kids=kids_json(kids)) elif dropped is '0' or dropped is 0: kids = Attendance.get_kid_not_dropped(token) return jsonify(status='ok', kids=kids_json(kids)) else: return jsonify(status='error', message='present arg can be 0 or 1') else: return jsonify(status='error', message='incorrect request')
def drop_attendance(): js = request.json if 'kid_ids' in js and 'lat' in js and 'lon' in js: ids = js['kid_ids'] lat = js['lat'] lon = js['lon'] gps = Gps.tuple_to_str(lat, lon) time = utils.get_date_full() token = request.args['token'] j_id = Driver.get_active_ride(token) if j_id is None or j_id is '': return jsonify(status='error', message='Cant add,since not active ride') else: # filter valid ids id_from_db = Kid.get_kid_drop_not_present(j_id) ids_set = set(ids) ids_db_set = set(id_from_db) ids_valid = list(ids_set & ids_db_set) # intersection of kid ids for id in ids_valid: Attendance.update_drop_attendance(id, gps, time, j_id) return jsonify(status='ok', message='Attendance taken') else: return jsonify(status='error', message='incorrect request')