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 add_item(self, item_subtype, item): """add a personal copy of a item to a user's account Arguments: item - Item object being attached to the User Return Value: a ItemCopy instance that links the User to the Item; None if the Item could not be linked """ from src.items.models import ItemCopy itemcopy = ItemCopy(item=item.key, owner=self.key, item_subtype=item_subtype) if itemcopy.put(): self.item_count = self.item_count + 1 self.put() return itemcopy
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 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 confirm(self): itemcopy = ItemCopy.query(ItemCopy.key==self.item).get() item = Item.query(Item.key==itemcopy.item).get() itemcopy.update_due_date(self.due_date) itemcopy.put() self.cleanup() return "%s has been extended to %s" %(item.title,str(self.due_date))
def confirm(self): itemcopy = ItemCopy.query(ItemCopy.key==self.item).get() other = UserAccount.query(UserAccount.key==itemcopy.borrower).get() item = Item.query(Item.key==itemcopy.item).get() itemcopy.return_item() itemcopy.put() self.cleanup() return "%s has been returned to your library" %(item.title)
def get_lent_items(self): """Get all the items that the user is currently lending to anther user Return Value: A list of ItemCopy objects of all the the items the user is currently lending """ from src.items.models import ItemCopy return ItemCopy.query(ItemCopy.owner == self.key, ItemCopy.borrower != None).fetch()
def get_library(self): """retrieve the user's library Return value: list of ItemCopy objects owned by the user """ from src.items.models import ItemCopy return ItemCopy.query(ItemCopy.owner == self.key).fetch()
def reject(self): other = UserAccount.query(UserAccount.key==self.connection).get() itemcopy = ItemCopy.query(ItemCopy.key==self.item).get() item = Item.query(Item.key==itemcopy.item).get() self.cleanup() otherAction = WaitingToBorrow.query(WaitingToBorrow.item == itemcopy.key and WaitingToBorrow.useraccount == other.key).get() otherAction.cleanup() return "You have denied %s permission to borrow %s" %(other.name,item.title)
def reject(self): other = UserAccount.query(UserAccount.key==self.connection).get() itemcopy = ItemCopy.query(ItemCopy.key==self.item).get() item = Item.query(Item.key==itemcopy.item).get() self.cleanup() otherAction = RequestToBorrow.query(RequestToBorrow.item == itemcopy.key and RequestToBorrow.useraccount == other.key).get() otherAction.cleanup() return "Request cancelled"
def get_network_items(self): """Get all the items owned by users connected to this owned Return value: an array will all the ItemCopy objects belonging to connected users """ from src.items.models import ItemCopy return ItemCopy.query(ItemCopy.owner.IN(self.get_connections())).fetch()
def movie_info(RTKey): itemMovie = Item.get_by_key("movie",RTKey) movie = itemMovie.to_dict() # Check if user owns book and pass itemCopy object if current_user().is_authenticated(): itemCopy = ItemCopy.query(ItemCopy.item==itemMovie.key,ItemCopy.owner==current_user().key).fetch() if itemCopy: movieCopy = itemCopy[0] else: movieCopy = None else: movieCopy = None return render_response('itemdetail.html', item=movie, itemCopy=movieCopy)
def item_due_reminders(): count = 0 """find all the items due tomorrow and send reminder emails""" items = ItemCopy.query(ItemCopy.due_date==date.today() + timedelta(days=1)).fetch() for item in items: count += 1 owner = UserAccount.query(UserAccount.key==item.owner).get() mail.send_mail(sender=owner.email, to=UserAccount.query(UserAccount.key==item.borrower).get().email, subject="Item Due Soon", body="""The following item is due to be returned tomorrow: '%s'. Please return it to %s"""%(Item.query(Item.key==item.item).get().title,owner.name)) return "%s reminders were sent out" %count
def get_item(self, item_subtype, item): """retrieve the user's copy of a particular item Arguments: item - the Item being retrieved Return value: the user's ItemCopy object associated with the provided Item; None if the user does not own item """ from src.items.models import ItemCopy myitem = ItemCopy.query( ItemCopy.item == item.key, ItemCopy.owner == self.key, ItemCopy.item_subtype == item_subtype ).get() return myitem
def confirm(self): other = UserAccount.query(UserAccount.key==self.connection).get() itemcopy = ItemCopy.query(ItemCopy.key==self.item).get() item = Item.query(Item.key==itemcopy.item).get() due_date = None cur_user = UserAccount.query(UserAccount.key==self.useraccount).get() cur_user.lend_item(int(itemcopy.key.id()), int(other.key.id()), due_date) self.cleanup() otherAction = WaitingToBorrow.query(WaitingToBorrow.item == itemcopy.key and WaitingToBorrow.useraccount == other.key).get() otherAction.cleanup() return "You have agreed to lend %s to %s" %(item.title,other.name)
def get_borrowed_items(self): """Get all the items that the user is currently borrowing from anther user Return Value: A list of ItemCopy objects of all the the items the user is currently borrowing """ from src.items.models import ItemCopy itemList = ItemCopy.query(ItemCopy.borrower == self.key).fetch() # Remove manually lent items (items being lended to the user's self) # They appear here because the borrower and lender is set to the user's self borrowedItems = [] for item in itemList: if not item.manual_borrower_name: borrowedItems.append(item) return borrowedItems
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"})
def star_rating(item_subtype, item_key, star_rating): # Get Item # Infer item_type if item_subtype in ('book', 'ebook', 'audiobook'): item_type = 'book' elif item_subtype in ('dvd', 'bluray'): item_type = 'movie' else: item_type = '' item = Item.get_by_key(item_type,item_key) # Get ItemCopy itemCopy = ItemCopy.query(ItemCopy.item==item.key,ItemCopy.owner==current_user().key,ItemCopy.item_subtype==item_subtype).get() if itemCopy.update_star_rating(star_rating): return jsonify({"result":"success"}) else: return jsonify({"result":"error"})
def remove_item(self, item_subtype, item): """delete a user's copy of a item Arguments: item - Item object that is to be removed Return value: the ItemCopy instance that was just deleted; None if the ItemCopy was not found """ from src.items.models import ItemCopy itemcopy = ItemCopy.query( ItemCopy.item == item.key, ItemCopy.owner == self.key, ItemCopy.item_subtype == item_subtype ).get() if itemcopy: itemcopy.key.delete() self.item_count = self.item_count - 1 self.put() return itemcopy
def book_info(OLKey): # Pass book object to template itemBook = Item.get_by_key("book",OLKey) book = itemBook.to_dict() # Determine if the user owns this book # Find all connections who own this book and get the status for each # Find out any pending actions regarding this book # Find any current loans or borrow of this book # Check if user owns book and pass itemCopy object if current_user().is_authenticated(): itemCopy = ItemCopy.query(ItemCopy.item==itemBook.key,ItemCopy.owner==current_user().key).fetch() if itemCopy: bookCopy = itemCopy[0] else: bookCopy = None else: bookCopy = None return render_response('itemdetail.html',item=book,itemCopy=bookCopy)
def confirm(self): other = UserAccount.query(UserAccount.key==self.connection).get() itemcopy = ItemCopy.query(ItemCopy.key==self.item).get() item = Item.query(Item.key==itemcopy.item).get() print "You have accepted a connection request from %s" %(other.name)
def text(self): itemcopy = ItemCopy.query(ItemCopy.key==self.item).get() other = UserAccount.query(UserAccount.key==itemcopy.borrower).get() item = Item.query(Item.key==itemcopy.item).get() return "%s reported checking in '%s'" %(other.name,item.title)
def reject(self): itemcopy = ItemCopy.query(ItemCopy.key==self.item).get() other = UserAccount.query(UserAccount.key==itemcopy.borrower).get() item = Item.query(Item.key==itemcopy.item).get() self.cleanup() return "Recorded that %s didn't return %s" %(other.name,item.title)
def text(self): itemcopy = ItemCopy.query(ItemCopy.key==self.item).get() other = UserAccount.query(UserAccount.key==itemcopy.borrower).get() item = Item.query(Item.key==itemcopy.item).get() return "%s wants to extend the due date of '%s' to %s?" %(other.name,item.title,str(self.due_date))
def text(self): other = UserAccount.query(UserAccount.key==self.connection).get() itemcopy = ItemCopy.query(ItemCopy.key==self.item).get() item = Item.query(Item.key==itemcopy.item).get() return "%s has requested to borrow '%s'" %(other.name,item.title)
def text(self): other = UserAccount.query(UserAccount.key==self.connection).get() itemcopy = ItemCopy.query(ItemCopy.key==self.item).get() item = Item.query(Item.key==itemcopy.item).get() return "You have requested to borrow '%s' from %s" %(item.title,other.name)