def _edit_tags(self): tags_str = self.request.get('tags') self.edited = True helper = TagHelper(self.user) if tags_str: # remove duplication src = tags_str.split(' ') tags_arr = [] for t in src: if t and t not in tags_arr: tags_arr.append(t) tags = elements.Tags.get_by_user_isbn(self.user, self.isbn) if tags: for name in tags.names: helper.remove(name, tags.isbn) for name in tags_arr: helper.add(name, tags.isbn) tags.names = tags_arr else: tags = elements.Tags(user=self.user, isbn=self.isbn, parent=utils.get_key_private('Tags', self.user), names=tags_arr) for name in tags_arr: helper.add(name, self.isbn) tags.put() else: # to delete any tags t = elements.Tags.get_by_user_isbn(self.user, self.isbn) if t: t.delete() for name in t.names: helper.remove(name, t.isbn)
def _edit_rating(self): rating_str = self.request.get('rating') if rating_str: self.edited = True r = elements.Rating.get_by_user_isbn(self.user, self.isbn) try: rating_num = int(rating_str) except Exception: logging.error("Error parsing the str for Rating: " + rating_str) return helper = books.SortHelper(self.user) if rating_num == 0: # 0 means clear r.delete() helper.delete_user_rating(self.isbn) else: if r: r.score = rating_num r.max_score = 5 r.min_score = 0 else: r = elements.Rating(user=self.user, isbn=self.isbn, parent=utils.get_key_private("Rating", self.user), score=rating_num, max_score=5, min_score=0) r.put() helper.set_user_rating(self.isbn, r.score) return
def get_by_user_isbn(cls, user, isbn): """ Query via User & ISBN """ cursor = db.GqlQuery("SELECT * FROM Comment WHERE ANCESTOR IS :parent_key" + " AND user = :u AND isbn = :i LIMIT 1", parent_key=utils.get_key_private('Comment', user), u=user, i=isbn) return cursor.get()
def get_all_booklists(cls, user): """ Query all booklists of a user. """ cursor = db.GqlQuery("SELECT * FROM BookList WHERE ANCESTOR IS :parent_key " + "AND user = :u", parent_key=utils.get_key_private('BookList', user), u=user) # since there are at most 3 lists now return cursor.run(limit=len(LIST_NAMES))
def get_by_user_name(cls, user, name): """ Query via User & BookList's name. """ cursor = db.GqlQuery("SELECT * FROM BookList WHERE ANCESTOR IS :parent_key " + "AND user = :u AND name = :n LIMIT 1", parent_key=utils.get_key_private('BookList', user), u=user, n=name) return cursor.get()
def get_or_create(cls, user, name): """ Query via User & BookList's name. If it is not there, create one and save it into datastore. """ bl = cls.get_by_user_name(user, name) if not bl: bl = BookList(name=name, user=user, parent=utils.get_key_private('BookList', user)) bl.put() return bl
def get_by_isbns(cls, user, isbns): """ Query a list of tags by a list of isbns. @param isbns: a list of isbns. If there are more than 30 items in it, it will use the first 30 ones. @returns: an iterable object returned by datastore. """ # at most 30 fetches at one time in datastore isbns = isbns[:30] cursor = db.GqlQuery("SELECT names FROM Tags WHERE ANCESTOR IS :parent_key" + " AND user = :u AND isbn IN :lt", parent_key=utils.get_key_private('Tags', user), u=user, lt=isbns) return cursor.run()
def _edit_comment(self): comment_str = self.request.get('comment') self.edited = True if comment_str: c = elements.Comment.get_by_user_isbn(self.user, self.isbn) if c: c.comment = comment_str else: c = elements.Comment(user=self.user, isbn=self.isbn, parent=utils.get_key_private('Comment', self.user), comment=comment_str) c.put() else: # to delete any comment c = elements.Comment.get_by_user_isbn(self.user, self.isbn) if c: c.delete() return
def _init_memcache(self): """ Add data into memcache. """ cursor = db.GqlQuery("SELECT * FROM Tags WHERE ANCESTOR IS :parent_key AND user = :u", parent_key=utils.get_key_private('Tags', self._user), u=self._user) results = {} for tag in cursor.run(): for name in tag.names: if name in results: results[name].append(tag.isbn) else: results[name] = [tag.isbn] # set all tags' names, in a cas way while True: if self._get() is None: if self._client.add(self._key, results): break else: if self._client.cas(self._key, results): break return
def parse_book_related_info(json, user): """ Parsing not only the shared book information, but also user's ratings, tags etc. @param json: the provided json to parse @param user: the corresponding user @return: a books.BookRelated object Used in get_book_list() """ results = books.BookRelated() results.book = parse_book_shared_info(json.get('book'), json.get('book_id')) isbn = results.book.isbn # comment comment_string = json.get('comment') if comment_string: c = elements.Comment(user=user, isbn=isbn, parent=utils.get_key_private('Comment', user)) c.comment = comment_string results.comment = c # end of comment # booklist name status = json.get('status') if status: # specify which list the book is in, if any results.booklist_name = { 'reading': books.booklist.LIST_READING, 'read': books.booklist.LIST_DONE, 'wish': books.booklist.LIST_INTERESTED }.get(status) # end of booklist name # updated time time_string = json.get('updated') if time_string: try: time = datetime.datetime.strptime(time_string, "%Y-%m-%d %H:%M:%S") except ValueError: pass else: results.updated_time = time # end of updated time # tags tags_array = json.get('tags') if tags_array: t = elements.Tags(user=user, isbn=isbn, parent=utils.get_key_private('Tags', user)) t.names = tags_array results.tags = t # end of tags # rating rating_obj = json.get('rating') if rating_obj: r = elements.Rating(user=user, isbn=isbn, parent=utils.get_key_private('Rating', user)) r.score = int(rating_obj.get('value')) r.max_score = int(rating_obj.get('max')) r.min_score = int(rating_obj.get('min')) results.rating = r # end of rating return results