def PUT_send_list(id): """ Sends new agreement to client via SMS @param {id} _id of list to send as agreement to client payload: Request is made with entire list object - have _cleaner as cleaner._id Returns 200 response """ try: list_data = JSONencoder.load(request.data) # verify phonenumber in list_data -- need it to send link to receipt to client if not 'phonenumber' in list_data: raise APIexception(code=1) phonenumber = list_data['phonenumber'] # need to fetch cleaner for just cleaner's name in SMS message cleaner_id = list_data[ '_cleaner'] # something went wrong with request if _cleaner not in payload c = cleaner.find_one(id=cleaner_id) # send SMS to client that has link to viewable agreement twilio_tools.send_agreement(phonenumber, c['name'], id) return respond200() except Exception as e: return respond500(e)
def POST_receipt(list_id): """ Creates receipt + sends receipt to client via SMS A receipt is a snapshot of the list at time of POST When a receipt is posted, the list/receipt models do the work list.create_receipt retrieves a fully populated list and inserts the receipt @param {list_id} _id of list of which to take snapshot and save as receipt payload: Request is made with entire list object - have _cleaner as cleaner._id Returns _id of newly inserted receipt """ try: list_data = JSONencoder.load(request.data) # verify phonenumber in list_data -- need it to send link to receipt to client if not 'phonenumber' in list_data: raise APIexception(code=1) phonenumber = list_data['phonenumber'] # need to fetch cleaner for just cleaner's name in SMS message cleaner_id = list_data[ '_cleaner'] # something went wrong with request if _cleaner not in payload c = cleaner.find_one(id=cleaner_id) # create the receipt that will live forever receipt_id = List.create_receipt(list_id) # send SMS to client that has link to viewable receipt twilio_tools.send_receipt(phonenumber, c['name'], receipt_id) return dumpJSON({'_id': receipt_id}) except Exception as e: return respond500(e)
def POST_list(cleaner_id): try: list_data = JSONencoder.load(request.data) list_id = cleaner.add_list(cleaner_id, list_data=list_data) return dumpJSON({'_id': list_id}) except Exception as e: return respond500(e)
def POST_room(list_id): try: data = JSONencoder.load(request.data) room_id = List.add_room(list_id, room_data=data) return dumpJSON({'_id': room_id}) except Exception as e: return respond500(e)
def PUT_room(id): try: data = JSONencoder.load(request.data) room.update(id, data=data) return dumpJSON({'_id': id}) except Exception as e: return respond500(e)
def PUT_cleaner(id): try: data = JSONencoder.load(request.data) cleaner.update(id, data) return respond200() except Exception as e: return respond500(e)
def POST_task(room_id): try: data = JSONencoder.load(request.data) id = room.add_task(room_id, data) return dumpJSON({'_id': id}) except Exception as e: return respond500(e)
def send_reset_code(): """ Send reset_code via SMS to the user Each reset_code expires after RESET_CODE_EXPIRATION If not yet set, or if expired, reset reset_code and reset_code_expires """ try: data = json.loads(request.data) if not 'phonenumber' in data: raise APIexception(code=1) phonenumber = data['phonenumber'] c = cleaner.find_one(phonenumber=phonenumber) if not c: raise APIexception(code=2) if ('reset_code' in c and 'reset_code_expires' in c and (datetime.now() < c['reset_code_expires'])): reset_code = c["reset_code"] else: (reset_code, reset_code_expires) = cleaner.generate_reset_code() cleaner.update(c["_id"], { "reset_code": reset_code, "reset_code_expires": reset_code_expires }) twilio_tools.send_SMS( phonenumber, str("Your password reset code is: " + reset_code)) return respond200() except Exception as e: return respond500(e)
def GET_list_by_id(id): try: populate_cleaner = request.args[ 'populate_cleaner'] if 'populate_cleaner' in request.args else False result = List.find_one( id=id, populate_cleaner=populate_cleaner) # returns list return dumpJSON(result) except Exception as e: return respond500(e)
def GET_task_search(): """ Returns List [] of all tasks """ try: result = task.find() return dumpJSON(result) except Exception as e: return respond500(e)
def GET_feedback_search(): """ Returns all feedback documents """ try: feedbacks = feedback.find() return dumpJSON(feedbacks) except Exception as e: return respond500(e)
def POST_cleaner(): """ Insert and login new cleaner """ try: data = JSONencoder.load(request.data) id = cleaner.insert_new(data) c = cleaner.find_public(id=id) auth.login(c) return dumpJSON(c) except Exception as e: return respond500(e)
def GET_data_all(): """ Return all the cleaners with all their lists with all their rooms with all their tasks -- HUGE JOIN """ try: all_cleaners = cleaner.find() for c in all_cleaners: c['lists'] = List.find(_cleaner=c['_id']) for l in c['lists']: l['rooms'] = room.find(_list=l['_id'], populate_tasks=True) return dumpJSON(all_cleaners) except Exception as e: return respond500(e)
def GET_cleaner_search(): """ If phonenumber in request args, search by phonenumber, else search for all returns List TODO: return public result only? """ try: if 'phonenumber' in request.args: result = cleaner.find(phonenumber=request.args['phonenumber']) else: result = cleaner.find() return dumpJSON(result) except Exception as e: return respond500(e)
def POST_reset_password(): try: data = json.loads(request.data) c = cleaner.find_one(phonenumber=data['phonenumber']) if not (c and 'reset_code' in c): raise APIexception(code=0) if not ((data['reset_code'] == c["reset_code"]) and (datetime.now() < c['reset_code_expires'])): raise APIexception(code=3) # if they made it this far all is good cleaner.update_password(c["_id"], data["password"], c["salt"]) login(cleaner.public(c)) return respond200() except Exception as e: return respond500(e)
def GET_room_search(): """ Returns List [] Parameters: _id -> limit search to _id (helpful for testing) _list -> search by list populate_tasks -> populate tasks list searches all if no parameters """ try: _id = request.args['_id'] if '_id' in request.args else None _list = request.args['_list'] if '_list' in request.args else None populate_tasks = request.args[ 'populate_tasks'] if 'populate_tasks' in request.args else None result = room.find(id=_id, _list=_list, populate_tasks=populate_tasks) return dumpJSON(result) except Exception as e: return respond500(e)
def POST_feedback(list_id): """ @param {list_id} _id of list that is owner of feedback payload: feedback data Sends notification via SMS to cleaner that feedback has been sent with link to list Returns _id of newly inserted feedback """ try: feedback_data = JSONencoder.load(request.data) feedback_id = List.add_feedback(list_id, feedback_data) # get list and cleaner so that can send SMS l = List.find_one(id=list_id) c = cleaner.find_one(id=l['_cleaner']) twilio_tools.send_feedback_notification(c['phonenumber'], c['name'], list_id) return dumpJSON({'_id': feedback_id}) except Exception as e: return respond500(e)
def POST_login(): try: data = json.loads(request.data) if not ("phonenumber" in data and "password" in data): # client-side shouldn't have allowed post raise APIexception( code=0, message="phonenumber and password required to sign in") c = cleaner.find_one(phonenumber=data["phonenumber"]) if not c: raise APIexception(code=2) if not cleaner.password_valid(data["password"], c["salt"], c["hashed_pwd"]): raise APIexception(code=4) profile = cleaner.public(c) login(profile) return dumpJSON(profile) except Exception as e: return respond500(e)
def GET_list_search(): """ Returns List [] of found lists Parameters: _id -> search by id _cleaner -> search by cleaner populate_rooms -> populate rooms list which will also populate tasks list searches all if no parameters populates feedback """ try: _id = request.args['_id'] if '_id' in request.args else None _cleaner = request.args[ '_cleaner'] if '_cleaner' in request.args else None populate_rooms = request.args[ 'populate_rooms'] if 'populate_rooms' in request.args else None result = List.find(id=_id, _cleaner=_cleaner, populate_rooms=populate_rooms) return dumpJSON(result) except Exception as e: return respond500(e)
def GET_validate_new_phonenumber(): """ Expects phonenumber and name in request arguments Validates that phonenumber is new and sends welcome message via SMS """ try: # get name from request if 'name' not in request.args: raise APIexception(code=6) name = request.args['name'] # get phonenumber from request if 'phonenumber' not in request.args: raise APIexception(code=1) phonenumber = request.args['phonenumber'] # insure phonenumber is new if cleaner.find(phonenumber=phonenumber): raise APIexception(code=5) twilio_tools.send_welcome(phonenumber, name) return respond200() except Exception as e: return respond500(e)
def DELETE_cleaner(id): try: cleaner.delete(id) return respond200() except Exception as e: return respond500(e)
def DELETE_task(id): try: task.delete(id) return respond200() except Exception as e: return respond500(e)
def GET_cleaner_by_id(id): try: return dumpJSON(cleaner.find_one(id=id)) except Exception as e: return respond500(e)
def GET_room_by_id(id): try: return dumpJSON(room.find_one(id=id)) except Exception as e: return respond500(e)
def DELETE_list(id): try: List.delete(id) return respond200() except Exception as e: return respond500(e)
def DELETE_feedback(id): try: feedback.delete(id) return respond200() except Exception as e: return respond500(e)