def all_user(): """ GET: req: [null] res: [profile] """ profiles = Profile.query.all() pages = Page.query.all() return jsonify({ "success": True, "profiles": get_dict_array(profiles), "pages": get_dict_array(pages) })
def certification_root(): if request.method == "POST": # expected data [equipment_type_id, worker_id] data = request.get_json() # sanitization if not data: return jsonify({"success": False, "message": "Missing body."}), 400 if not 'equipment_type_id' in data or not 'worker_id' in data: return jsonify({ "success": False, "message": "Missing body fields." }), 400 if not isinstance(data['equipment_type_id'], int) or not isinstance( data['worker_id'], int): return jsonify({ "success": False, "message": "Invalid body fields." }), 400 if not EquipmentType.query.get(data['equipment_type_id']): return jsonify({ "success": False, "message": "Equipment Type key not found." }), 404 if not Worker.query.get(data['worker_id']): return jsonify({ "success": False, "message": "Worker key not found." }), 404 # add to db certification = Certification(data['equipment_type_id'], data['worker_id']) db.session.add(certification) db.session.commit() return jsonify(get_dict(certification)) else: # get facilities certifications = Certification.query.all() return jsonify(get_dict_array(certifications))
def equipment_root(): if request.method == "POST": # expected data [equipment_type_id, facility_id] data = request.get_json() # sanitization if not data: return jsonify({"success": False, "message": "Missing body."}), 400 if not 'equipment_type_id' in data or not 'facility_id' in data: return jsonify({ "success": False, "message": "Missing body fields." }), 400 if not isinstance(data['equipment_type_id'], int) or not isinstance( data['facility_id'], int): return jsonify({ "success": False, "message": "Invalid body fields." }), 400 if not EquipmentType.query.get(data['equipment_type_id']): return jsonify({ "success": False, "message": "Equipment Type key not found." }), 404 if not Facility.query.get(data['facility_id']): return jsonify({ "success": False, "message": "Facility key not found." }), 404 # add to db equipment = Equipment(data['equipment_type_id'], data['facility_id']) db.session.add(equipment) db.session.commit() return jsonify(get_dict(equipment)) else: # get equipments equipments = Equipment.query.all() return jsonify(get_dict_array(equipments))
def worker_root(): if request.method == "POST": # expected data [name, shift] data = request.get_json() # sanitization if not data: return jsonify({"success": False, "message": "Missing body."}), 400 if not 'name' in data or not 'shift' in data: return jsonify({ "success": False, "message": "Missing body fields." }), 400 if not isinstance(data['name'], str) or not isinstance( data['shift'], str): return jsonify({ "success": False, "message": "Invalid body fields." }), 400 if len(data['name'].strip()) < 1 or len(data['name']) > 100: return jsonify({ "success": False, "message": "Name length out of range." }), 400 if len(data['shift'].strip()) < 1 or len(data['shift']) > 15: return jsonify({ "success": False, "message": "Shift length out of range." }), 400 # add to db worker = Worker(data['name'], data['shift']) db.session.add(worker) db.session.commit() return jsonify(get_dict(worker)) else: # get request workers = Worker.query.all() return jsonify(get_dict_array(workers))
def facility_root(): if request.method == "POST": # expected data [lat, lon] data = request.get_json() # sanitization if not data: return jsonify({"success": False, "message": "Missing body."}), 400 if not 'lat' in data or not 'lon' in data: return jsonify({ "success": False, "message": "Missing body fields." }), 400 if not isinstance(data['lat'], (int, float)) or not isinstance( data['lon'], (int, float)): return jsonify({ "success": False, "message": "Invalid body fields." }), 400 if data['lat'] < -90.0 or data['lat'] > 90.0: return jsonify({ "success": False, "message": "Latitude out of range." }), 400 if data['lon'] < -180.0 or data['lon'] > 180.0: return jsonify({ "success": False, "message": "Longitude out of range." }), 400 # add to db facility = Facility(data['lat'], data['lon']) db.session.add(facility) db.session.commit() return jsonify(get_dict(facility)) else: # get facilities facilities = Facility.query.all() return jsonify(get_dict_array(facilities))
def tag_api(): # check for signin if session.get('user_id') is None: return jsonify({"success": False, "message": "Not logged in!"}) # process request if request.method == "POST": # add tag to db data = request.get_json() tag = Tag(data['name']) db.session.add(tag) db.session.commit() return jsonify({"success": True, "tag": get_dict(tag)}) else: # get tags tags = Tag.query.all() return jsonify({"success": True, "tags": get_dict_array(tags)})
def worker(worker_id): # get response worker = Worker.query.get(worker_id) if worker is None: return jsonify({ "success": False, "message": "Worker with key not found!" }), 404 # build response res = get_dict(worker) # add relations res['certifications'] = [] for cert in worker.certifications: res['certifications'].append({ "name": cert.equipment_type.name, "id": cert.id }) res['orders'] = get_dict_array(worker.orders) return jsonify(res)
def facility(fid): # get response facility = Facility.query.get(fid) if facility is None: return jsonify({ "success": False, "message": "Facility with key not found!" }), 404 res = get_dict(facility) # add relations res['equipments'] = [] for e in facility.equipments: res['equipments'].append({ "id": e.id, "name": e.equipment_type.name, "equipment_type_id": e.equipment_type_id }) res['orders'] = get_dict_array(facility.orders) return jsonify(res)
def comment_route(): """ GET: req: post_id! res: comment POST: req: body!, post_id!, page_id? res: success """ if request.method == 'POST': # add comment to post_id from page_id if specified # else add as current logged in user if session.get('user_id') is None: return jsonify({ "success": False, "message": "Profile not logged in!" }) body = request.get_json() if not body: return jsonify({"success": False, "message": "Body not found!"}) if 'body' not in body: return jsonify({ "success": False, "message": "Comment body required!" }) if 'post_id' not in body: return jsonify({"success": False, "message": "Post id required!"}) if 'page_id' in body: # add comment as a page page = Page.query.get(body['page_id']) if not page: return jsonify({ "success": False, "message": "Page doesn't exist!" }) comment = Comment(body['body'], body['post_id'], None, body['page_id']) db.session.add(comment) db.session.commit() return jsonify({"success": True}) else: # add comment as logged in user comment = Comment(body['body'], body['post_id'], session.get('user_id'), None) db.session.add(comment) db.session.commit() return jsonify({"success": True}) else: # send post_id's comments post_id = request.args.get('post_id') if not post_id: return jsonify({"success": False, "message": "Post id required!"}) # grab post from db post = Post.query.get(post_id) if not post: return jsonify({"success": False, "message": "Post not found!"}) return jsonify({ "success": True, "comments": get_dict_array(post.comments) })
def post_route(): """ GET: req: profile_id?, page_id? res: [posts] POST: req: type!, body!, page_id? res: success """ if request.method == "POST": # can only do this if logged in for profile # or page if profile is admin if session.get('user_id') is None: return jsonify({ "success": False, "message": "Profile not logged in!" }) # grab req body body = request.get_json() if not body: return jsonify({"success": False, "message": "No request body!"}) if 'type' not in body: return jsonify({ "success": False, "message": "Post type not specified!" }) if 'body' not in body: return jsonify({ "success": False, "message": "Post body not specified!" }) # if body has page_id make post from page else make profile post if 'page_id' in body: # check if page exists page = Page.query.get(body['page_id']) if not page: return jsonify({ "success": False, "message": "Page doesn't exist" }) # TODO: check if user is admin of page # add post to db post = Post(body['type'], body['body'], None, body['page_id']) db.session.add(post) else: # make current user post post = Post(body['type'], body['body'], session.get('user_id'), None) db.session.add(post) db.session.commit() return jsonify({"success": True}) else: # if user_id or page_id exists return posts for that # else return posts of current logged in user body = { "profile_id": request.args.get('profile_id'), "page_id": request.args.get('page_id') } if body['profile_id'] != None: # return posts for given profile_id # grab profile profile = Profile.query.get(body['profile_id']) if not profile: return jsonify({ "success": False, "message": "Profile not found" }) # build response res = [] for p in profile.posts: obj = get_dict(p) obj['name'] = profile.first_name + ' ' + profile.last_name obj['likes'] = get_dict_array(p.likes) res.append(obj) return jsonify({"success": True, "posts": res}) elif body['page_id'] != None: # return posts from page # grab page page = Page.query.get(body['page_id']) if not page: return jsonify({"success": False, "message": "Page not found"}) # build response res = [] for p in page.posts: obj = get_dict(p) obj['name'] = page.name obj['likes'] = get_dict_array(p.likes) res.append(obj) return jsonify({"success": True, "posts": res}) else: # return all posts # grab all posts posts = Post.query.all() # build response res = [] for post in posts: obj = get_dict(post) if post.profile != None: obj['name'] = post.profile.first_name + \ ' ' + post.profile.last_name elif post.page != None: obj['name'] = post.page.name obj['likes'] = get_dict_array(post.likes) res.append(obj) return jsonify({"success": True, "posts": res}) # should not happen return jsonify({"success": False, "message": "Invalid request!"})
def profile_route(): """ GET: req: profile_id? res: profile UPDATE: req: profile_id?, first_name?, last_name?, mobile_no?, email?, username?, password? res: success, profile DELETE: req: [null] res: success """ if request.method == "DELETE": # delete the current user if session.get('user_id') is None: return jsonify({ "success": False, "message": "Profile not logged in!" }) # grab user obj and delete profile = Profile.query.get(session.get('user_id')) db.session.delete(profile) db.session.commit() # clear the session session.clear() return jsonify({"success": True}) elif request.method == "UPDATE": # update user info pass else: # send back user info if logged in # else send limited info for given profile id body = {"profile_id": request.args.get('profile_id')} if body['profile_id'] != None: # profile of profile id profile = Profile.query.get(body['profile_id']) if not profile: return jsonify({ "success": False, "message": "Profile with id not found!" }) # add adminof and memberof pages to response res = get_dict(profile) res['adminof'] = get_dict_array(profile.adminof) res['memberof'] = get_dict_array(profile.memberof) # TODO: only return non-sensitive info return jsonify({"success": True, "profile": res}) else: # send current user if logged in if session.get('user_id') is None: return jsonify({ "success": False, "message": "Profile not logged in!" }) # grab profile from db profile = Profile.query.get(session.get('user_id')) # add adminof and memberof pages to response res = get_dict(profile) res['adminof'] = get_dict_array(profile.adminof) res['memberof'] = get_dict_array(profile.memberof) return jsonify({"success": True, "profile": res}) # should not happen return jsonify({"success": False, "message": "Invalid request!"})
def order_root(): if request.method == "POST": # expected data [priority, time_to_completion, facility_id, equipment_id] data = request.get_json() # sanitization if not data: return jsonify({"success": False, "message": "Missing body."}), 400 if not 'priority' in data or not 'time_to_completion' in data or not 'facility_id' in data or not 'equipment_id' in data: return jsonify({ "success": False, "message": "Missing body fields." }), 400 if not isinstance(data['priority'], int) or not isinstance( data['time_to_completion'], int) or not isinstance( data['facility_id'], int) or not isinstance( data['equipment_id'], int): return jsonify({ "success": False, "message": "Invalid body fields." }), 400 if data['priority'] < 1 or data['priority'] > 5: return jsonify({ "success": False, "message": "Priority out of range." }), 400 if data['time_to_completion'] < 1: return jsonify({ "success": False, "message": "Time to completion out of range." }), 400 if not Equipment.query.get(data['equipment_id']): return jsonify({ "success": False, "message": "Equipment key not found." }), 404 if not Facility.query.get(data['facility_id']): return jsonify({ "success": False, "message": "Facility key not found." }), 404 # add to db order = Order(data['priority'], data['time_to_completion'], data['facility_id'], data['equipment_id']) db.session.add(order) db.session.commit() # after each call algo.main() return jsonify(get_dict(order)) else: # get orders orders = Order.query.order_by(Order.created_at.desc()).all() return jsonify(get_dict_array(orders))
def equipment_type_root(): if request.method == "POST": # expected data [name, prob, hour_min, hour_max] data = request.get_json() # sanitization if not data: return jsonify({"success": False, "message": "Missing body."}), 400 if not 'name' in data or not 'prob' in data or not 'hour_min' in data or not 'hour_max' in data: return jsonify({ "success": False, "message": "Missing body fields." }), 400 if not isinstance(data['name'], str) or not isinstance( data['prob'], (int, float)) or not isinstance( data['hour_min'], int) or not isinstance( data['hour_max'], int): return jsonify({ "success": False, "message": "Invalid body fields." }), 400 if len(data['name'].strip()) < 1 or len(data['name']) > 25: return jsonify({ "success": False, "message": "Name length out of range." }), 400 if data['prob'] < 0 or data['prob'] > 1: return jsonify({ "success": False, "message": "Probability out of range." }), 400 if data['hour_min'] < 1: return jsonify({ "success": False, "message": "Minimum hour out of range." }), 400 if data['hour_max'] < 1 or data['hour_max'] < data['hour_min']: return jsonify({ "success": False, "message": "Maximum hour out of range." }), 400 # add to db e_type = EquipmentType(data['name'], data['prob'], data['hour_min'], data['hour_max']) db.session.add(e_type) db.session.commit() # main algorithm run algo.main() return jsonify(get_dict(e_type)) else: # get equipments equipment_types = EquipmentType.query.all() return jsonify(get_dict_array(equipment_types))