def create_rental(): '''This route is used to create a new rental in the database.\n It can only be reached via POST. To update a rentals's data, use the route '/updaterental'.\n The route expects that the data comes in with the following fields:\n item - JSON tag - description\n address - 'address' - the address of the rental\n userID - 'userID' - the userID of the person who created the rental This function will always return an empty JSON object and status code 201 ''' # Pull fields from the request address = request.json['address'] userID = request.json['userID'] # grab the user and update their rental fields user = dq.getUserById(userID) # Create a rental and update the user's current rental roommates = Roommates(roommate1=userID) dq.add(roommates) board = Board() dq.add(board) rental = Rental(address=address, roommates=roommates.id, board=board.id) dq.add(rental) dq.updateUserRentals(user, rental.id) data = {} data['currentRental'] = user.currentRental data['pastRental'] = user.pastRental return jsonify(data), 201
def get_rental_IDs(): userID = request.args.get('userID') user = dq.getUserById(userID) if user is not None: data = {} data['currentRental'] = user.currentRental data['pastRental'] = user.pastRental return jsonify(data), 200 else: return jsonify({'reason': "User not found"}), 404
def change_user_info(): userID = request.json['userID'] user = dq.getUserById(userID) if user is None: return jsonify({'reason': 'User does not exist'}), 404 if (request.json['email'] is not None and dq.getUserByEmail(request.json['email']) is None): dq.update(user, 'email', request.json['email']) if request.json['firstName'] is not None: dq.update(user, 'firstName', request.json['firstName']) if request.json['lastName'] is not None: dq.update(user, 'lastName', request.json['lastName']) if request.json['phoneNumber'] is not None: dq.update(user, 'phoneNumber', request.json['phoneNumber']) return jsonify({}), 201
def get_info(): ''' emergency contact info and the user info ''' data = {} userID = request.args.get('userID') user = dq.getUserById(userID) contacts = dq.getContactsWithAssocUser(userID) if user is not None: data['firstName'] = user.firstName data['lastName'] = user.lastName data['phoneNumber'] = user.phoneNumber data['email'] = user.email else: return jsonify({'reason': "User not found"}), 404 if len(contacts) != 0: counter = 0 for ind, cont in enumerate(contacts): counter += 1 contact_str = 'contact' + repr(ind) data[contact_str] = {} data[contact_str]['relation'] = cont.relationship data[contact_str]['name'] = cont.name data[contact_str]['phoneNumber'] = cont.phoneNumber if counter != 2: contact_str = 'contact' + repr(counter) data[contact_str] = {} data[contact_str]['relation'] = 'Relative ' + repr(counter) name = 'Default Name' data[contact_str]['name'] = name data[contact_str]['phoneNumber'] = "1234567890" return jsonify(data), 200 else: for num in range(2): contact_str = 'contact' + repr(num) data[contact_str] = {} data[contact_str]['relation'] = 'Relative ' + repr(num) name = 'Default Name' data[contact_str]['name'] = name data[contact_str]['phoneNumber'] = "1234567890" return jsonify(data), 200
def load_user(user_id): return dq.getUserById(int(user_id))