예제 #1
0
def post():
    form = PostForm()
    if request.method == 'GET':
        if request.args.get('isbn'):
            form.isbn.data = request.args.get('isbn')
    if form.validate_on_submit():
        isbn = form.isbn.data.strip().replace('-','').replace(' ','')
        price = form.price.data
        if price == '':
            price = None
        cond = form.condition.data
        comments = form.comments.data
        courses = form.courses.data.strip().replace(' ','').upper()
        if len(courses) > 9:
            courses = ''
        if not Book.query.get(isbn):
            info = get_amazon_info(isbn)
            image = get_amazon_image(isbn)
            b = Book(isbn=isbn, title=info['title'], author=info['author'], amazon_url=info['url'], image=image, courses=[courses])
            add_commit(db, b)
        else:
            b = Book.query.get(isbn)
            old_courses = list(b.courses)
            old_courses.append(courses)
            b.courses = list(set(old_courses))
            db.session.commit()
        p = Post(uid=current_user.id, timestamp=datetime.utcnow(), isbn=isbn, price=price,condition=cond,comments=comments)
        add_commit(db, p)
        email_subbers(p)
        return redirect(url_for('book',isbn=isbn))
    return render_template('post.html',
                           post_form = form)
예제 #2
0
def info(isbn):
    isbn = isbn.strip().replace('-', '').replace(' ', '')
    if len(isbn) != 13 or not isbn.isdigit():
        return jsonify(data=None)
    i = Book.query.get(isbn)
    if i:
        return jsonify(title=i.title,
                       image=i.image,
                       author=i.author,
                       courses=i.courses)
    time.sleep(1)  # dont send requests too fast to not spam amazon
    img = get_amazon_image(isbn)
    time.sleep(1)
    info = get_amazon_info(isbn)
    if info:
        return jsonify(title=info['title'], image=img, author=info['author'])
    return jsonify(title=None)
예제 #3
0
def post():
    form = PostForm()
    if request.method == 'GET':
        if request.args.get('isbn'):
            form.isbn.data = request.args.get('isbn')
    if form.validate_on_submit():
        isbn = form.isbn.data.strip().replace('-', '').replace(' ', '')
        price = form.price.data
        if price == '':
            price = None
        cond = form.condition.data
        comments = form.comments.data
        courses = form.courses.data.strip().replace(' ', '').upper()
        if len(courses) > 9:
            courses = ''
        if not Book.query.get(isbn):
            info = get_amazon_info(isbn)
            image = get_amazon_image(isbn)
            b = Book(isbn=isbn,
                     title=info['title'],
                     author=info['author'],
                     amazon_url=info['url'],
                     image=image,
                     courses=[courses])
            add_commit(db, b)
        else:
            b = Book.query.get(isbn)
            old_courses = list(b.courses)
            old_courses.append(courses)
            b.courses = list(set(old_courses))
            db.session.commit()
        p = Post(uid=current_user.id,
                 timestamp=datetime.utcnow(),
                 isbn=isbn,
                 price=price,
                 condition=cond,
                 comments=comments)
        add_commit(db, p)
        email_subbers(p)
        return redirect(url_for('book', isbn=isbn))
    return render_template('post.html', post_form=form)
예제 #4
0
def info(isbn):
    isbn = isbn.strip().replace('-','').replace(' ','')
    if len(isbn) != 13 or not isbn.isdigit():
        return jsonify(data=None)
    i = Book.query.get(isbn)
    if i:
        return jsonify(
            title=i.title,
            image=i.image,
            author=i.author,
            courses=i.courses)
    time.sleep(1) # dont send requests too fast to not spam amazon
    img = get_amazon_image(isbn)
    time.sleep(1)
    info = get_amazon_info(isbn)
    if info:
        return jsonify(
            title=info['title'],
            image=img,
            author=info['author'])
    return jsonify(title=None)