def add_note(): description = request.json['description'] title = request.json['title'] rentalID = request.json['rentalID'] rental = dq.getRentalByRentalID(rentalID) board = rental.board category = request.json['category'] note = Note(description=description, title=title, board=board, category=category) dq.add(note) return jsonify({}), 201
def delete_roommate(): rentalID = request.json['rentalID'] email = request.json['email'] user = dq.getUserByEmail(email) # that is trying to get deleted if user.deactivated: return jsonify({'reason': "User is deactivated"}), 400 roommatesID = dq.getRentalByRentalID(rentalID).roommates roommates = dq.getRentalRoommates(roommatesID) mates = list(filter(lambda x: x.startswith('room'), dir(roommates))) for ent in mates: if user.id == getattr(roommates, ent): dq.updateUserRentals(user, None) dq.update(roommates, ent, None) return jsonify({}), 201 return jsonify({'reason': "Roommate isn't a roommate"}), 404
def get_lease_end_date(): rentalID = request.args.get('rentalID') rental = dq.getRentalByRentalID(rentalID) if rental is not None: lease = dq.getLeaseByLeaseID(rental.lease) if lease is not None: dt = lease.endDT daysTill = (dt - d.today()).days data = {} data['endDT'] = dt.strftime("%Y-%m-%d %H:%M:%S") data['daysTill'] = daysTill return jsonify(data), 200 else: return jsonify({'reason': "Lease not found"}), 404 else: return jsonify({'reason': "Rental not found"}), 404
def get_notes(): rentalID = request.args.get('rentalID') rental = dq.getRentalByRentalID(rentalID) notes = dq.getNotesByBoardID(rental.board) data = {} for note in notes: if note.category in data: data[note.category].append({'title': note.title, 'noteID': note.id, 'description': note.description}) else: data[note.category] = list() data[note.category].append({'title': note.title, 'noteID': note.id, 'description': note.description}) return jsonify({'notes': data}), 200
def get_roommates(): userID = request.args.get('userID') rentalID = request.args.get('rentalID') rental = dq.getRentalByRentalID(rentalID) if rental is not None: roommates = dq.getRoommatesByID(rental.roommates, userID) data = {} for num in range(len(roommates)): val = 'roommate' + repr(num) data[val] = {} if roommates[num] is not None: name = roommates[num].firstName + " " + roommates[num].lastName data[val]['id'] = roommates[num].id data[val]['name'] = name data[val]['phoneNumber'] = roommates[num].phoneNumber data[val]['email'] = roommates[num].email return jsonify(data), 200 else: return jsonify({'reason': "Rental not found"}), 404
def get_lease_info(): rentalID = request.args.get('rentalID') rental = dq.getRentalByRentalID(rentalID) if rental is not None: lease = dq.getLeaseByLeaseID(rental.lease) if lease is not None: data = {} data['address'] = rental.address data['landlordFirstName'] = lease.landlordFirstName data['landlordLastName'] = lease.landlordLastName data['landlordPhoneNumber'] = lease.landlordPhoneNumber data['landlordEmail'] = lease.landlordEmail data['rentCost'] = str(lease.rentCost) data['rentDueDate'] = lease.rentDueDate return jsonify(data), 200 else: return jsonify({'reason': "Lease not found"}), 404 else: return jsonify({'reason': "Rental not found"}), 404
def add_roommate(): '''This route is used to add a roommate to a given rental\n It can only be reached via POST request.\n The route expects that the data comes in with the following fields:\n item - JSON tag - description\n rentalID - 'rentalID' - ID of the rental to add the roommate to\n email - 'email' - email of the roommmate to add ''' rentalID = request.json['rentalID'] email = request.json['email'] user = dq.getUserByEmail(email) if user is None: return jsonify({'reason': "User doesn't exist"}), 404 if user.deactivated: return jsonify({'reason': "User is deactivated"}), 400 rental = dq.getRentalByRentalID(rentalID) if rental is None: return jsonify({'reason': "Rental does not exist"}), 404 roommates = dq.getRentalRoommates(rental.roommates) if roommates is not None: # Filter out the roommate attributes from the field mates = list(filter(lambda x: x.startswith('room'), dir(roommates))) for ent in mates: if getattr(roommates, ent) is None: dq.update(roommates, ent, user.id) dq.updateUserRentals(user, rentalID) return jsonify({}), 201 elif getattr(roommates, ent) == user.id: return jsonify({'reason': "Roommate already entered"}), 400 return jsonify({'reason': "Adding 6th Roommate is not allowed"}), 400 else: err = "Roommates table somehow doesn't exist???" return jsonify({'reason': err}), 404
def add_lease(): rentalID = request.json['rentalID'] rental = dq.getRentalByRentalID(rentalID) landlordFirstName = request.json['landlordFirstName'] landlordLastName = request.json['landlordLastName'] landlordPhoneNumber = request.json['landlordPhoneNumber'] landlordEmail = request.json['landlordEmail'] rentCost = request.json['rentCost'] if rentCost == "": rentCost = 0 startDT = request.json['startDT'] endDT = request.json['endDT'] rentDueDate = request.json['rentDueDate'] lease = Lease(landlordFirstName=landlordFirstName, landlordLastName=landlordLastName, landlordPhoneNumber=landlordPhoneNumber, landlordEmail=landlordEmail, rentCost=rentCost, startDT=startDT, endDT=endDT, rentDueDate=rentDueDate) dq.add(lease) dq.update(rental, 'lease', lease.id) return jsonify({}), 201