Exemplo n.º 1
0
    def post(self):
        """ Handle the sign-up request. """
        email = self.request.get("email")
        pwd = self.request.get("password")
        verify = self.request.get("verify")

        # there is verification on browser by JS, but validating again won't hurt
        if not self._validate(email, pwd, verify):
            logging.error("How could the invalid input pass the JS test? @auth.SignUpHandler.post()")
            self.redirect("/signup")
            return

        if User.exists(email):
            # telling user that the email has been taken
            self._error("Email registered by others")
        else:
            hashed = encrypt.hash_pwd(email, pwd)
            u = User(email=email,
                     pwd_hashed=hashed,
                     parent=utils.get_key_public('User'))
            u.put()
            self._init_user(u)

            self._set_id_cookie(email)
            self.redirect('/me')
Exemplo n.º 2
0
    def get_by_isbns(cls, isbns):
        """ Query an array of books by isbns """
        # GQL limit: at most 30 at a time
        assert len(isbns) <= 30

        cursor = db.GqlQuery("SELECT * FROM Book WHERE ANCESTOR IS :parent_key AND isbn IN :isbn_list",
                             parent_key=utils.get_key_public('Book'),
                             isbn_list=isbns)
        return cursor.run()
Exemplo n.º 3
0
    def get_by_email(cls, email, key_only=False):
        """ Retrieve the User according to his/her email.
            Returns None if it doesn't exist.
        """
        if email is None:
            return None

        cursor = db.GqlQuery("SELECT * FROM User WHERE ANCESTOR IS :parent_key AND email = :val LIMIT 1",
                             parent_key=utils.get_key_public('User'),
                             val=email)
        return cursor.get(keys_only=key_only)
Exemplo n.º 4
0
    def _collect(self, isbn, time):
        """ Collect relevant data of a book.
            @param u: user.
            @param isbn
            @param time: updated time
            @returns: a _SortData object
        """
        result = _SortData()
        result.isbn = isbn
        result.updated_time = time
        cursor = db.GqlQuery("SELECT rating_avg, rating_num, pages FROM Book " +
                             "WHERE ANCESTOR IS :parent_key AND isbn = :val",
                             parent_key=utils.get_key_public('Book'),
                             val=isbn)
        b = cursor.get()
        result.public_rating = b.rating_avg
        result.rated_amount = b.rating_num
        result.pages = b.pages

        rating = elements.Rating.get_by_user_isbn(self._user, isbn)
        if rating:
            result.user_rating = rating.score
        return result
Exemplo n.º 5
0
def parse_book_shared_info(json, douban_id=None):
    """ Construct a books.book.Book object (contains only shared information)
        @param json: provided json object to parse.
        @raise ParseJsonError
    """
    # isbn
    isbn = json.get('isbn13')
    if not isbn:
        isbn = json.get('isbn10')
    # end of isbn

    b = books.book.Book(source=datasrc.DOUBAN,
                        isbn=isbn,
                        parent=utils.get_key_public('Book'))
    b.douban_id = json.get('id')

    # title & subtitle
    b.title = json.get('title')
    b.subtitle = json.get('subtitle')
    b.title_original = json.get('origin_title')

    # author & their introduction & translators
    b.authors = json.get('author')
    b.authors_intro = json.get('author_intro')
    b.translators = json.get('translator')

    # summary
    b.summary = json.get('summary')

    # ratings
    _tmp = json.get('rating')
    if _tmp:
        try:
            b.rating_max = int(_tmp['max'])
            b.rating_avg = float(_tmp['average'])
            if b.rating_avg == 0.0:
                # 0.0 means the ratings are too few to be meaningful
                b.rating_avg = None
            b.rating_num = int(_tmp['numRaters'])
        except Exception:
            # Notify that here is error.
            raise errors.ParseJsonError(msg="Parsing rating failed.",
                                        res_id=douban_id)
    # end of ratings

    # image url & douban url
    _tmp = _parse_book_img_url(json)
    if _tmp and 'book-default' not in _tmp:
        # the default image link is useless
        b.img_link = db.Link(_tmp)

    _tmp = json.get('alt')
    if _tmp:
        b.douban_url = db.Link(_tmp)
    # end of image url & douban url

    # publisher & published date & pages
    _tmp = json.get('publisher')
    if _tmp:
        # some data from Douban may contain '\n', unreasonable!
        b.publisher = _tmp.replace('\n', ' ')

    b.published_date = json.get('pubdate')
    _tmp = json.get('pages')
    if _tmp:
        unit_str = [u'页']
        unit_order = ['after']
        try:
            value_float = _parse_book_amount_unit(_tmp, unit_str, unit_order)[0]
            if value_float is None:
                # in case the page_string is just a number-string
                if _tmp.isdigit():
                    # some books may be a collection, the 'pages' may be u'共12册'
                    b.pages = int(_tmp)
            else:
                b.pages = int(value_float)
        except Exception:
            raise errors.ParseJsonError(msg="Parsing pages failed.",
                                        res_id=douban_id)
    # end of publisher & published date & pages

    # tags from others
    if 'tags' in json:
        try:
            b.tags_others_count, b.tags_others_name = _parse_book_tags_others(json)
        except Exception:
            raise errors.ParseJsonError(msg="Parsing tags failed.",
                                        res_id=douban_id)
    # end of tags from others

    # price
    _tmp = json.get('price')
    if _tmp:
        unit_str = [u"美元", u'元', '$', 'USD', 'JPY', u'円', 'NTD', 'TWD', 'EUR']
        unit_order = ["after", "after", "before", "before", "before", "after", "before", "before", "before"]
        try:
            b.price_amount, b.price_unit = _parse_book_amount_unit(_tmp, unit_str, unit_order)
            if b.price_amount is None:
                # in case the price_string is just a number string
                b.price_amount = float(_tmp.strip())
        except Exception:
            raise errors.ParseJsonError(msg="Parsing price failed.",
                                        res_id=douban_id)
    # end of price
    return b
Exemplo n.º 6
0
 def _get_property(cls, isbn, name):
     cursor = db.GqlQuery("SELECT " + name + " FROM Book WHERE ANCESTOR IS :parent_key AND isbn = :val",
                          parent_key=utils.get_key_public('Book'),
                          val=isbn)
     return cursor.get()
Exemplo n.º 7
0
 def get_by_isbn(cls, isbn, key_only=False):
     """ Query via douban_id """
     cursor = db.GqlQuery("SELECT * FROM Book WHERE ANCESTOR IS :parent_key AND isbn = :val LIMIT 1",
                          parent_key=utils.get_key_public('Book'),
                          val=isbn)
     return cursor.get(keys_only=key_only)
Exemplo n.º 8
0
 def get_by_douban_id(cls, douban_id):
     """ Query via douban_id """
     cursor = db.GqlQuery("SELECT * FROM Book WHERE ANCESTOR IS :parent_key AND douban_id = :val LIMIT 1",
                          parent_key=utils.get_key_public('Book'),
                          val=douban_id)
     return cursor.get()