Пример #1
0
    def post(self):
        """ Handling requests for editing data. """
        email = auth.get_email_from_cookies(self.request.cookies)
        self.user = auth.user.User.get_by_email(email)
        if not self.user:
            self.redirect('/login')
            return

        self.isbn = self.request.path.split('/book/')[1]
        try:
            utils.validate_isbn(self.isbn)
        except Exception:
            msg = "Invalid ISBN: " + self.isbn
            params = {'msg': msg}
            self.redirect('/error?' + urllib.urlencode(params))
            return

        if not self.user.is_douban_connected():
            self.redirect('/auth/douban')
            return

        edit_type = self.request.get('type')

        # the booklists this book was previously in
        from_lists = [bl for bl in BookList.get_all_booklists(self.user) if self.isbn in bl.isbns()]

        if edit_type == 'booklist':
            self._edit_booklist(from_lists)
            self._finish_editing()
            return

        self.edited = False
        if edit_type == 'rating':
            self._edit_rating()
        elif edit_type == 'comment':
            self._edit_comment()
        elif edit_type == 'tags':
            self._edit_tags()
        elif edit_type == 'tongji':
            self._edit_tongji()

        if self.edited:
            if from_lists:
                # already in some lists, now edited, sync to douban, edit
                self._sync_edit("PUT")
            else:
                # previously not in any list, now edited, add it to Done List
                done_list = BookList.get_or_create(self.user, books.booklist.LIST_DONE)
                done_list.add_isbn(self.isbn, front=True)
                # sync to douban, add
                self._sync_edit("POST")

        self._finish_editing()
Пример #2
0
    def _fetch_parse(self, user, list_type=None):
        """ It may cause problems to fetch and then parse. Do it together..
            @param list_type: which booklist to import.
        """
        if list_type is None:
            list_type = self.request.get('list_type')
            if not list_type:
                return

        try:
            datas = douban.get_book_list(user, list_type)
        except utils.errors.ParseJsonError as err:
            logging.error("ERROR while importing from Douban, user_key: " + user.key() +
                          " list_type: " + list_type)
            logging.error(err)
            self._log(err)
            return

        bl = BookList.get_or_create(user, list_type)
        bl.start_importing(len(datas))
        # also clear those in memcache
        helper = SortHelper(user)
        helper.clear(list_type)

        for related in datas:
            # also added into memcache in merge_into_datastore()
            b = related.merge_into_datastore(user, update_book=False)
            if b:
                # when already such book there, b will be None
                try:
                    url, datas = tongji.get_by_isbn(b.isbn)
                    b.set_tongji_info(url, datas)
                except Exception as err:
                    logging.error("ERROR while saving TJ info, isbn: " + b.isbn)
                    logging.error(err)
                    self._log(err)

        # after all, finish importing
        bl = BookList.get_or_create(user, list_type)
        bl.douban_amount = None
        bl.put()
        return
Пример #3
0
    def _edit_booklist(self, from_lists):
        """ Change or delete belonging booklist.
            @param from_lists: The booklists this book was previously in.
        """
        target_list_name = self.request.get('booklist')
        if target_list_name:
            if target_list_name == "remove":
                # remove from any booklists
                for bl in from_lists:
                    bl.remove_isbn(self.isbn)

                # also remove any Rating, Tags, Comment
                r = elements.Rating.get_by_user_isbn(self.user, self.isbn)
                if r:
                    r.delete()
                t = elements.Tags.get_by_user_isbn(self.user, self.isbn)
                if t:
                    t.delete()
                c = elements.Comment.get_by_user_isbn(self.user, self.isbn)
                if c:
                    c.delete()

                # sync memcache
                helper = books.SortHelper(self.user)
                helper.delete(self.isbn)

                # sync to douban, delete all related
                self._sync_edit("DELETE")
            else:
                # change to one booklist
                target_list = BookList.get_or_create(self.user, target_list_name)
                if from_lists:
                    for bl in from_lists:
                        bl.remove_isbn(self.isbn)
                    target_list.add_isbn(self.isbn, front=True)
                    # sync to douban, modify
                    self._sync_edit("PUT")
                else:
                    target_list.add_isbn(self.isbn, front=True)
                    # sync to douban, add
                    self._sync_edit("POST")

                # sync memcache
                helper = books.SortHelper(self.user)
                helper.set_by_isbn(target_list_name, self.isbn)
        return