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 __init__(self, board=0, start=None, end=None, gps=None, bus_id=None): self.b_id = bus_id # board boolean:true = 'home to school', false:'school to home' self.board = board self.date = utils.get_date_full() self.start = start # start time self.end = end # end time of journey self.gps = gps # current gps information (lat,lon)
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 is_valid_token(token): date = utils.get_date_full() cur = conn.execute( 'select email from parent where token=? and DATETIME(expires) > DATETIME(?)', (token, date)) user = cur.fetchone() if user is None: return False else: return True
def logout_session(token): # set end journey time # set token,expiry and active_b_id to null cur = conn.cursor() cur.execute('begin') cur.execute( 'update journey set end=? where id=(select active_ride_j_id from driver where token=?)', (utils.get_date_full(), token)) cur.execute( "update driver set active_ride_j_id=?,token=?,expires=? WHERE token=?", ('', '', '', token)) cur.execute('commit')
def feedback(): js = request.json if 'title' in js and 'detail' in js: token = request.args['token'] title = js['title'] detail = js['detail'] date = utils.get_date_full() p = Parent.get_parent_id(token) p_id = p[0] name = p[1] email = p[2] f = Feedback(name=name, email=email, title=title, message=detail, date=date, p_id=p_id) f.add() return jsonify(status='ok', message='feedback sent') 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')
def __init__(self, gps=None, journey_id=None): self.gps = gps self.time = utils.get_date_full() self.j_id = journey_id
def update_gps(journey_id, gps): cur = conn.execute('update journey set gps=?,last_update=? where id=?', (gps, utils.get_date_full(), journey_id)) conn.commit()