def request_to_borrow(lenderID, itemCopyID): emailText = [] emailText.append("You have received the following message from " + current_user().name + ", a Sharing Commons user.\n----\n\n") emailText.append(request.data) emailText.append("\n\n----\nReply to this message to send an email to " + current_user().name + " and set up the exchange. Once you've lent the item, visit beta.sharingcommons.com to confirm lending the item. " + current_user().name + " will receive an email when the item is due") emailBody = ''.join(emailText) # Request item try: borrower = current_user() lender = UserAccount.getuser(int(lenderID)) itemCopy = ItemCopy.get_by_id(int(itemCopyID)) rtb1 = RequestToBorrow() rtb1.useraccount = lender.key rtb1.connection = borrower.key rtb1.item = itemCopy.key rtb1.put() wtb1 = WaitingToBorrow() wtb1.useraccount = borrower.key wtb1.connection = lender.key wtb1.item = itemCopy.key wtb1.put() except: return jsonify({"result":"error"}) # Send email mail.send_mail(sender="Sharing Commons <*****@*****.**>", to=lender.name + " <" + lender.email + ">", reply_to=borrower.name + " <" + borrower.email + ">", subject='Sharing Commons: Request to Borrow "' + Item.query(Item.key == itemCopy.item).get().title + '"', body=emailBody) return jsonify({"result":"success"})
def change_due_date(self, itemCopyID, newDueDate): """Update the date that a item is due Arguments: itemCopyID: an ID representing a ItemCopy object, the item to be returned newDueDate: a string representing the new due date of the item Return Value: A message describing the success or failure or the operation """ from src.items.models import ItemCopy from src.activity.models import DueDateExtension itemcopy = ItemCopy.get_by_id(int(itemCopyID)) new_date = datetime.strptime(newDueDate, "%Y-%m-%d") + timedelta( days=1 ) # For some reason the datepickers are returning one day prior to the date selected, so this is a workaround to compensate if itemcopy == None: return "Invalid item ID" if itemcopy.owner == self.key: itemcopy.update_due_date(new_date) itemcopy.put() return "Due date successfully updated" elif itemcopy.borrower == self.key: notification = DueDateExtension(useraccount=itemcopy.owner, item=itemcopy.key, due_date=new_date) notification.put() return "Request sent to owner" else: return "You are not the owner of this item, nor are you borrowing it"
def return_item(self, itemCopyID): """Return the given item to it's owner Arguments: itemCopyID: an ID representing a ItemCopy object, the item to be returned Return Value: A message describing the success or failure or the operation """ from src.items.models import ItemCopy from src.activity.models import ConfirmReturn itemcopy = ItemCopy.get_by_id(int(itemCopyID)) # verify the itemCopyID was valid if itemcopy == None: return "Invalid item ID" if itemcopy.owner == self.key: itemcopy.return_item() itemcopy.put() return "Item successfully returned to your library" elif itemcopy.borrower == self.key: notification = ConfirmReturn(useraccount=itemcopy.owner, item=itemcopy.key) notification.put() return "Notice sent to owner, awaiting their confirmantion" else: return "You are not the owner of this item, nor are you borrowing it"
def borrow_item(self, itemID, lenderID, due_date=None): """Borrow an item from another user Arguments: itemID: an ID representing the itemCopy object that will be borrowed lenderID: an ID representing the user that will lend the item due_date: the date the item should be returned, if none is given the default for the lender is used Return value: A string describing the success or failure of the operation """ from src.items.models import ItemCopy itemCopy = ItemCopy.get_by_id(itemID) # check to see if the item copy is valid if itemCopy == None: return "Invalid item ID" if itemCopy.owner.id() != lenderID: return "That user does not own this item" if itemCopy.borrower != None: return "That item is not avaiable to be lent out" itemCopy.lend(self.key.id(), due_date) itemCopy.put() return "Item successfully borrowed"
def manual_checkout(itemCopyID): if request.method == 'POST': itemCopy = ItemCopy.get_by_id(int(itemCopyID)) # Manually checkout itemCopy.borrower = current_user().key itemCopy.lender = current_user().key itemCopy.manual_borrower_name = request.form["borrowerName"] if "borrowerEmail" in request.form: itemCopy.manual_borrower_email = request.form["borrowerEmail"] itemCopy.due_date = datetime.strptime(request.form["dueDate"], "%m/%d/%Y") itemCopy.put() return jsonify({"result":"success"}) else: return jsonify({"result":"error"})
def setup_item_borrow_actions(lenderID, itemCopyID): borrower = current_user() lender = UserAccount.getuser(int(lenderID)) itemCopy = ItemCopy.get_by_id(int(itemCopyID)) rtb1 = RequestToBorrow() rtb1.useraccount = lender.key rtb1.connection = borrower.key rtb1.item = itemCopy.key rtb1.put() wtb1 = WaitingToBorrow() wtb1.useraccount = borrower.key wtb1.connection = lender.key wtb1.item = itemCopy.key wtb1.put() return jsonify({"Message":"OK"})