def prune(): """ Prune the cached extractions database to keep below 10,000 records, the free limit on Heroku. """ num = session.query(KvStore.id).count() if num > 9990: limit = num - 9900 log.info("Pruning {} records".format(limit)) for resolution in session.query(KvStore).order_by(KvStore.id.asc()).limit(limit): session.delete(resolution) session.commit() return num
def prune(): """ Prune the cached extractions database to keep below 10,000 records, the free limit on Heroku. """ num = session.query(KvStore.id).count() if(num > 9990): limit = num - 9900 log.info("Pruning {} records".format(limit)) for resolution in session.query( KvStore).order_by( KvStore.id.asc()).limit(limit): session.delete(resolution) session.commit() return num
def get_user(c1_json): a_user = session.query(Customer).filter( Customer.email == c1_json["email"]).first() return { "name": a_user.name, "address": a_user.address, "email": a_user.email }
def get(self): trucks_list = [] trucks = session.query(Truck).limit(20).all() for truck in trucks: trucks_list.append(truck.to_truck_list_item()) return {"objects": trucks_list}
def patch(): c1_json = request.get_json() one_customer = session.query(Customer).filter( Customer.email == c1_json["email"]).first() if one_customer == None: return (jsonify({"message": "Mail not found."}), 404) else: session.add(one_customer) session.commit() return (jsonify({}), 200)
def post(): c1_json = request.get_json() if session.query(Customer).filter( Customer.email == c1_json["email"]).first() == None: c1 = Customer(name=c1_json["name"], address=c1_json["address"], email=c1_json["email"]) session.add(c1) session.commit() return (jsonify({"message": "Successfully loaded."}), 200) else: return (jsonify({"message": "This email already exists."}), 404)
def delete(): c1_json = request.get_json() try: if Customer.check_user_existance_and_apply(c1_json, session): session.delete( session.query(Customer).filter( Customer.email == c1_json["email"]).first()) session.commit() return (jsonify({"Message": "User deleted."}), 200) else: return (jsonify({"Message": "User could not be found."}), 404) except: return (jsonify({"Message": "The database could not be accesed."}), 500)
def get(self): start_time = timeit.default_timer() parks_list = [] # add all the parks basic info into the list parks = session.query(Park).limit(20).all() for park in parks: parks_list.append(park.to_park_list_item()) end_time = timeit.default_timer() print('GET park list time: ' + str(end_time - start_time)) return {"objects": parks_list}
def get(): c1_json = request.get_json() try: if Customer.check_user_existance_and_apply(c1_json, session): a_user = session.query(Customer).filter( Customer.email == c1_json["email"]).first() return (jsonify({ "name": a_user.name, "address": a_user.address, "email": a_user.email }), 200) else: return (jsonify({"Message": "User could not be found."}), 404) except: return (jsonify({"Message": "The database could not be accesed."}), 500)
def get(self): statuscode = 200 statusmsg = "OK" data = None parser = reqparse.RequestParser() parser.add_argument('DeviceToken', location='headers') try: args = parser.parse_args() try: if (args['DeviceToken'] == None): return { "StatusCode": 401, "StatusMessage": "Unauthorized: DeviceToken is missing or invalid.", "Data": data } except: return { "StatusCode": 401, "StatusMessage": "Unauthorized: DeviceToken is missing or invalid.", "Data": data } # data = [dict(marketid=i.MarketID, name_en=i.MarketNameEn, name_ar=i.MarketNameAr) for i in session.query(Market.MarketID,Market.MarketNameEn,Market.MarketNameAr).all()] data = [ dict(marketid=i.MarketID, name_en=i.MarketNameEn, name_ar=i.MarketNameAr, country=i.Country.CountryNameEn) for i in session.query(Market).all() ] except Exception as e: statuscode = 500 statusmsg = str(e) return { "StatusCode": statuscode, "StatusMessage": statusmsg, "Data": data }
def post(self): json_user_data = request.data user_data = json.loads(json_user_data.decode('utf-8')) try: username = user_data['username'] password = user_data['password'] except: return {"status": 1, "message": "invalid format"} try: customer_query = session.query(Customer).filter(Customer.name == username) if customer_query.count() < 1: return {"status": 2, "message": "user " + username + " doesn't exist"} customer = customer_query.first() if password != customer.password: return {"status": 3, "message": "invalid password"} except Exception as e: print(e) return {"status": 4, "message": "error reading data from database"} return {"status": 0, "message": "login success"}
def patch_user(c1_json): session.query(Customer).filter( Customer.email == c1_json["email"]).first() return {"message": "User succesfully updated."}