Esempio n. 1
0
def edit_book(book_id):
    user_id = user.get_id()
    app_user = AppUser.query.get(stormpathUserHash(user_id))
    book = Book.query.get(book_id)

    if book in app_user.author.books:
        if request.method == 'GET':
            return render_template('edit_book.html', book=book)

        genres = []
        for g in request.form.get('genres').split(','):
            genre_name = g.strip().title()
            if not genre_name.isspace():
                genre = Genre.query.filter_by(name=genre_name).first()
                if not genre:
                    genre = Genre(genre_name)
                    db.session.add(genre)
                genres.append(genre)

        book.genres = genres
        book.title = request.form.get('title')
        book.isbn = request.form.get('isbn')
        book.publisher = request.form.get('publisher')
        book.description = request.form.get('description')
        db.session.add(book)

        db.session.commit()

        return redirect(url_for('author_routes.author_dashboard'))

    return redirect(url_for('index'))
Esempio n. 2
0
def add_book():
    user_id = user.get_id()
    app_user = AppUser.query.get(stormpathUserHash(user_id))

    if app_user.is_author:
        if request.method == 'GET':
            return render_template('add_book.html', author=app_user.author)

        book_file = request.files.get('epub_file', None)
        # POST is a epub file upload
        if book_file.content_type == 'application/epub+zip' or book_file.content_type == 'application/octet-stream':
            book_upload = BookUploader(book_file.filename, book_file)
            book_location = book_upload.file_dir[:-1]

        # fetch genres too!
        book_data = {
            'author': app_user.author,
            'title': request.form.get('title'),
            'publisher': request.form.get('publisher'),
            'epub_url': book_location
        }

        book = Book.book_from_dict(**book_data)
        db.session.add(book)
        db.session.commit()

        print book
        return render_template('add_book.html', author=app_user.author)

    return redirect(url_for('index'))
Esempio n. 3
0
def purchase(book_id):
    user_href = user.get_id()
    app_user = AppUser.query.get(stormpathUserHash(user_href))

    book = Book.query.get(book_id)
    app_user.purchase_book(book)

    return redirect(url_for('user_routes.library'))
Esempio n. 4
0
def purchase(book_id):
    user_href = user.get_id()
    app_user = AppUser.query.get(stormpathUserHash(user_href))

    book = Book.query.get(book_id)
    app_user.purchase_book(book)

    return redirect(url_for('user_routes.library'))
Esempio n. 5
0
    def test_user_can_login(self):
        _user = User.from_login(
                TestConfig.USER_EMAIL,
                TestConfig.USER_PASSWORD,
            )
        login_user(_user)

        user_id = user.get_id()
        app_user = AppUser.query.get(stormpathUserHash(user_id))
        assert app_user.user_href == user_id
Esempio n. 6
0
    def test_user_can_login(self):
        _user = User.from_login(
            TestConfig.USER_EMAIL,
            TestConfig.USER_PASSWORD,
        )
        login_user(_user)

        user_id = user.get_id()
        app_user = AppUser.query.get(stormpathUserHash(user_id))
        assert app_user.user_href == user_id
Esempio n. 7
0
def add_book():
    user_id = user.get_id()
    app_user = AppUser.query.get(stormpathUserHash(user_id))

    if app_user.is_author:
        if request.method == 'GET':
            return render_template('add_book.html', author=app_user.author)

        book_file = request.files.get('epub_file', None)
        cover_file = request.files.get('cover_file', None)

        # POST is a epub file upload
        if book_file.content_type == 'application/epub+zip' or book_file.content_type == 'application/octet-stream':
            book_upload = BookUploader(book_file.filename, book_file,
                                       cover_file)
            epub_url = S3URL + 'epubs/' + book_upload.epub_key
            cover_url = CLOUDFRONTURL + book_upload.cover_key

        genres = []
        for g in request.form.get('genres').split(','):
            genre_name = g.strip().title()
            if not genre_name.isspace():
                genre = Genre.query.filter_by(name=genre_name).first()
                if not genre:
                    genre = Genre(genre_name)
                    db.session.add(genre)
                genres.append(genre)

        book_data = {
            'author': app_user.author,
            'isbn': request.form.get('isbn'),
            'title': request.form.get('title'),
            'publisher': request.form.get('publisher'),
            'description': request.form.get('description'),
            'genres': genres,
            'epub_url': epub_url,
            'cover_large': cover_url
        }

        book = Book.book_from_dict(**book_data)
        db.session.add(book)
        db.session.commit()

        return redirect(url_for('author_routes.author_dashboard'))

    return redirect(url_for('index'))
Esempio n. 8
0
def bootstrapTestDB(db):
    """
        Takes an created SQLAlchemy db and bootstraps the tables
        with dummy data
    """
    books_copy, authors_copy = deepcopy(books), deepcopy(authors)

    # load authors
    for author_data in authors:
        db.session.add(Author.author_from_dict(**author_data))
    db.session.commit()

    # load genres
    for book_data in books_copy:
        for genre in book_data['genres']:
            g = Genre.query.filter_by(name=genre).first()
            if not g:
                db.session.add(Genre(genre))
                db.session.flush()
    db.session.commit()

    # load books
    for book_data in books_copy:
        book_data['genres'] = [
            Genre.query.filter_by(name=genre_item).first()
            for genre_item in book_data['genres']
        ]
        book_data['author'] = Author.query.filter_by(
            name=book_data['author']).first()
        db.session.add(Book.book_from_dict(**book_data))
    # commit the changes
    db.session.commit()

    #load users
    for author_data in authors_copy:
        author = Author.query.filter_by(name=author_data['name']).first()
        db.session.add(
            AppUser(stormpathUserHash(author_data['user_href']),
                    author_data['user_href'], author))
    db.session.commit()
Esempio n. 9
0
def settings():

    user_id = user.get_id()
    app_user = AppUser.query.get(stormpathUserHash(user_id))

    if request.method == 'GET':
        return render_template('author_settings.html', author=app_user.author)

    if not app_user.is_author:
        author = Author.author_from_dict(**request.form.to_dict())
        db.session.add(author)
        db.session.commit()
        app_user.become_author(author)

    ## TODO Handle edit author attributes
    """
    if app_user.is_author:
        author_name = request.form.get('author_name')
        if author_name != app_user.author.name:
            app_user.author.update_name(author_name)
    """

    return render_template('author_settings.html', author=app_user.author)
Esempio n. 10
0
 def inject_appuser():
     if user.is_authenticated():
         user_id = user.get_id()
         app_user = AppUser.query.get(stormpathUserHash(user_id))
         return dict(app_user=app_user)
     return dict(app_user=None)
Esempio n. 11
0
    def test_author_app_user_relation(self):
        author = Author.query.filter_by(name="Jake Hartnell").first()
        app_user = AppUser.query.get(author.user_id)

        assert author.user_id == stormpathUserHash(app_user.user_href)
        assert author.user_id == app_user.user_id
Esempio n. 12
0
def author_dashboard():
    user_id = user.get_id()
    app_user = AppUser.query.get(stormpathUserHash(user_id))

    if app_user.is_author:
        return render_template('author_dashboard.html', author=app_user.author)
Esempio n. 13
0
    def test_author_app_user_relation(self):
        author = Author.query.filter_by(name="Jake Hartnell").first()
        app_user = AppUser.query.get(author.user_id)

        assert author.user_id == stormpathUserHash(app_user.user_href)
        assert author.user_id == app_user.user_id
Esempio n. 14
0
@auth_routes.route('/authorize_ios', methods=['POST'])
def authorize_iOS():
    """ User login/auth/session management """

    username = request.form.get('username', '')
    password = request.form.get('password', '')
    user = None

    try:
        user = User.from_login(username, password)
    except StormpathError, err:
        pass

    if user:
        app_user = AppUser.query.get(stormpathUserHash(user.get_id()))
        t = make_secure_token(username + password)
        if app_user.ios_token != t:
            app_user.set_ios_token(t)
        return jsonify({
            'username': user.username,
            'user_id': app_user.user_id,
            'authenticated': True,
            'ios_token': t
        })
    else:
        return jsonify({
            'username': username,
            'authenticated': False,
            'ios_token': None
        })
Esempio n. 15
0
@auth_routes.route('/authorize_ios', methods=['POST'])
def authorize_iOS():
    """ User login/auth/session management """

    username = request.form.get('username', '')
    password = request.form.get('password', '')
    user = None

    try:
        user = User.from_login(username, password)
    except StormpathError, err:
        pass

    if user:
        app_user = AppUser.query.get(stormpathUserHash(user.get_id()))
        t = make_secure_token(username + password)
        if app_user.ios_token != t:
            app_user.set_ios_token(t)
        return jsonify({ 'username': user.username,
                         'user_id': app_user.user_id,
                         'authenticated': True,
                         'ios_token': t
                })
    else:
        return jsonify({ 'username': username,
                 'authenticated': False,
                 'ios_token': None
                })

@auth_routes.route('/logout')
Esempio n. 16
0
def on_load(state):
    stormpath_manager.init_app(state.app)


@login_required
@user_routes.route('/settings', methods=['GET', 'POST'])
def settings():
    if request.method == 'GET':
        return render_template('settings.html')

    is_author = True if request.form.get('is_author') is not None else False

    try:
        user.username = request.form.get('username')
        user.email = request.form.get('email')
        user.given_name = request.form.get('first_name')
        user.surname = request.form.get('last_name')
        user.save()
    except StormpathError, err:
        return render_template('settings.html', error=err.message)

    user_id = user.get_id()
    app_user = AppUser.query.get(stormpathUserHash(user_id))

    if app_user.is_author:
        author_name = request.form.get('author_name')
        if author_name != app_user.author.name:
            app_user.author.update_name(author_name)

    return render_template('settings.html')