def createRequest(email): meal_time, meal_type, longitude, latitude, location_string, userId = prepareRequest( email) newRequest = requestData(meal_time=meal_time, meal_type=meal_type, longitude=longitude, latitude=latitude, location_string=location_string, user_id=userId) session = get_db() try: session.add(newRequest) session.commit() return returnStatus("Request created!!") except ValueError as err: session.rollback() print err.message return returnStatus("Malformed Input!!") except exc.IntegrityError as err: print err.message session.rollback() return returnStatus("Missing required field") except Exception as err: session.rollback() print err.message return returnStatus("Something went wrong, we also dont know!!")
def modifyRequest(id, emailId): new_meal_time, new_meal_type, new_longitude, new_latitude, new_location_string, userId = prepareRequest( emailId) session = get_db() try: request = session.query(requestData).filter_by(id=id).one() if not (isUserAuthorized(request.user_id, userId)): return returnStatus("Not authorized to perform this operation!!") except Exception as err: print err.message return returnStatus("No such request exist!!") try: if new_meal_time is not None: request.meal_time = new_meal_time if new_meal_type is not None: request.meal_type = new_meal_type if new_location_string is not None: request.location_string = new_location_string request.latitude = new_latitude request.longitude = new_longitude session.commit() except Exception as err: session.rollback print err.message return returnStatus("Request modified!!")
def getRequest(): session = get_db() try: request = session.query(requestData).all() return jsonify(RequestDetails=[i.serialize for i in request]) except exc.OperationalError: return returnStatus("The database doesn't exist yet") except Exception as err: print err.message return returnStatus("Database exception occurraed")
def deleteUser(_id): session = get_db() try: user = session.query(userData).filter_by(id=_id).one() except Exception: session.rollback() return returnStatus("No such user exists!!") session.delete(user) session.commit() return returnStatus("User deleted!!")
def getAllUsers(): session = get_db() try: user = session.query(userData).all() return jsonify(UserDetails=[i.serialize for i in user]) except exc.OperationalError: return returnStatus("The database doesn't exist yet") except Exception as err: print err.message return returnStatus("Database exception occurraed")
def deleteRequest(id, emailId): userId = extractUserId(emailId) session = get_db() try: requests = session.query(requestData).filter_by(id=id).one() if not (isUserAuthorized(requests.user_id, userId)): return returnStatus("Not authorized to perform this operation!!") except Exception: session.rollback() return returnStatus("No such request exist!!") session.delete(requests) return returnStatus("Request deleted!!")
def getProposals(email): userProposedFrom = extractUserName(extractUserId(email)) session = get_db() try: proposal = session.query(proposalData)\ .filter((proposalData.user_proposed_from == userProposedFrom) | (proposalData.user_proposed_to == userProposedFrom)) if proposal.count() == 0: return returnStatus("No matching proposals found for you!!") except Exception as err: session.rollback() print err.message return returnStatus("Something went wrong, we also dont know!!") return jsonify(ProposalDetails=[i.serialize for i in proposal])
def userId(id, emailId): userId = extractUserId(emailId) session = get_db() try: user = session.query(userData).filter_by(id=id).one() if not (isUserAuthorized(user.id, userId)): return returnStatus("Not authorized to perform this operation!!") except Exception: session.rollback() return returnStatus("No such user exists!!") if request.method == 'GET': return getUser(id) elif request.method == 'PUT': return modifyUser(id) elif request.method == 'DELETE': return deleteUser(id)
def extractUser(id): session = get_db() try: request = session.query(requestData).filter_by(id=id).one() return request.user_id except Exception as err: print err.message() return returnStatus("No such user exist!!")
def getRequestId(id): session = get_db() try: request = session.query(requestData).filter_by(id=id).one() return jsonify(RequestDetails=[request.serialize]) except Exception: session.rollback() return returnStatus("No such request exist!!")
def getUser(id): session = get_db() try: user = session.query(userData).filter_by(id=id).one() return jsonify(UserDetails=[user.serialize]) except Exception as err: print err.message return returnStatus("No such user exists!!")
def makeANewUser(name, email, passwords): newUser = userData(name=name, email=email, password_hash=passwords) session = get_db() try: session.add(newUser) session.commit() return returnStatus("User registered!!") except exc.IntegrityError as err: session.rollback() print err.message return returnStatus("User Id already registered!!") except ValueError as err: session.rollback() print err.message return returnStatus("Malformed Input!!") except Exception as err: session.rollback() err.message return returnStatus("Something went wrong, we also dont know!!")
def acceptRequest(id): session = get_db() try: request = session.query(requestData).filter_by(id=id).one() request.filled = True session.commit() except Exception as err: session.rollback() print err.message() return returnStatus("We dont know dude!!")
def getProposal(id, email): session = get_db() userProposedFrom = extractUserName(extractUserId(email)) try: proposal = session.query(proposalData).filter_by(id=id) if proposal.count() == 0: msg = "Prosposal with id: " + str(id) + " not found" return returnStatus(msg) proposal = session.query(proposalData)\ .filter_by(id = id)\ .filter((proposalData.user_proposed_from == userProposedFrom) | (proposalData.user_proposed_to == userProposedFrom)) if proposal.count() == 0: return returnStatus( "Sorry you are not authorised to view the proposal!!") except Exception as err: session.rollback() print err.message return returnStatus("Something went wrong, we also dont know!!") return jsonify(ProposalDetails=[i.serialize for i in proposal])
def deleteProposal(id, email): userProposedFrom = extractUserName(extractUserId(email)) session = get_db() try: proposal = session.query(proposalData).filter_by(id=id) if proposal.count() == 0: msg = "Prosposal with id: " + str(id) + " not found" return returnStatus(msg) proposal = session.query(proposalData) \ .filter_by(id=id) \ .filter_by(user_proposed_from=userProposedFrom) if proposal.count() == 0: return returnStatus( "Sorry you are not authorised to delete the proposal!!") session.delete(proposal.one()) session.commit() except Exception as err: session.rollback() print err.message return returnStatus("Something went wrong, we also dont know!!") return returnStatus("Proposal deleted successfully!!")
def modifyProposal(id, email): userProposedTo = extractUserName(extractUserId(email)) session = get_db() try: proposal = session.query(proposalData).filter_by(id=id) if proposal.count() == 0: msg = "Prosposal with id: " + str(id) + " not found" return returnStatus(msg) proposal = session.query(proposalData) \ .filter_by(id=id) \ .filter_by(user_proposed_to=userProposedTo) if proposal.count() == 0: return returnStatus( "Sorry you are not authorised to modify the proposal!!") proposal.one().filled = True session.commit() except Exception as err: session.rollback() print err.message return returnStatus("Something went wrong, we also dont know!!") acceptRequest(proposal.one().request_id) return returnStatus("Proposal accepted successfully!!")
def modifyUser(id): session = get_db() try: user = session.query(userData).filter_by(id=id).one() except Exception as err: print err.message return returnStatus("No such user exists!!") rdata = request.data rawdata = json.loads(rdata) jsonData = rawdata["UserDetails"] for item in jsonData: password = item.get("newpassword") try: if password is not None: password_hash = generate_password_hash(password) user.password_hash = password_hash session.commit() except Exception as err: session.rollback print err.message user = session.query(userData).filter_by(id=id).one() return returnStatus("User modified!!")
def prepareRequest(emailId): rdata = request.data rawdata = json.loads(rdata) jsonData = rawdata["RequestDetails"] try: for item in jsonData: mealType = item.get("mealType") mealTime = item.get("mealTime") location = item.get("location") userId = extractUserId(emailId) latitude = None longitude = None if location is not None: latitude, longitude = getGeocodeLocation(location) return mealTime, mealType, longitude, latitude, location, userId except TypeError as err: return returnStatus("Mandatory fields missing or Incorrect datatype passed " \ "All fields - MealType, MealTime, Location and EmailId must be String")
def createProposal(email): rdata = request.data rawdata = json.loads(rdata) jsonData = rawdata["ProposalDetails"] session = get_db() try: for item in jsonData: request_id = item.get("requestId") except Exception as err: print err.message return returnStatus( "Mandatory field - requestId missing or Incorrect datatype passed") userProposedFrom = extractUserName(extractUserId(email)) try: proposal = session.query(proposalData).filter_by( request_id=request_id, user_proposed_from=userProposedFrom).one() print proposal.request_id return returnStatus("Proposal already exist!!") except Exception as err: userProposedTo = extractUserName(extractUser(request_id)) if (extractUserId(email) == int(extractUser(request_id))): return returnStatus("Cannot propose to yourself!!") newProposal = proposalData(request_id=request_id, user_proposed_from=userProposedFrom, user_proposed_to=userProposedTo) try: session.add(newProposal) session.commit() return returnStatus("Proposal created!!") except ValueError as err: session.rollback() print err.message return returnStatus("Malformed Input!!") except exc.IntegrityError as err: print err.message session.rollback() return returnStatus("Missing required field") except Exception as err: session.rollback() print err.message return returnStatus("Something went wrong, we also dont know!!")
def userFunction(): if request.method == 'GET': return getAllUsers() elif request.method == 'POST': print "Making a New User" rdata = request.data rawdata = json.loads(rdata) jsonData = rawdata["UserDetails"] try: for item in jsonData: name = item.get("name") print name email = item.get("email") print email passwords = item.get("password") print passwords password_hash = generate_password_hash(passwords) print "%s", password_hash return makeANewUser(name, email, password_hash) except TypeError as err: print err.message return returnStatus("Mandatory fields missing or Incorrect datatype passed " \ "All fields - Name, Email, Password must be String")