Exemplo n.º 1
0
def bookmark():
    ''' Accepts a JSON payload with the URL to bookmark and saves the bookmark. '''
    # if request.content_type != 'application/json':
    #     resp = Response(status='Unsupported Media Type')
    #     resp.status_code = 415
    #     return resp

    bookmark_url = request.form['bookmark_url']
    comic_name = get_comic_name_from_url(bookmark_url)

    db = get_db()
    existing = db.execute('SELECT * FROM bookmarks WHERE comic = ?',
                          (comic_name, )).fetchone()

    result = None
    if existing is None:
        result = db.execute('INSERT INTO bookmarks VALUES (?, ?, ?)',
                            (comic_name, g.user['id'], bookmark_url))
        db.commit()
    else:
        result = db.execute('UPDATE bookmarks SET link = ?  WHERE comic = ?',
                            (bookmark_url, existing['comic']))
        db.commit()

    return render_template('comics/bookmark.html', bookmark=result)
Exemplo n.º 2
0
def add_user_command(username, password):
    ''' Adds a new user to the database. '''
    db = get_db()
    db.execute(
        'INSERT INTO users (name, password) VALUES (?, ?)',
        (username, generate_password_hash(password))
    )
    db.commit()
Exemplo n.º 3
0
def load_logged_in_user():
    user_id = session.get('user_id')

    if user_id is None:
        g.user = None
    else:
        g.user = get_db().execute(
            'SELECT * FROM users WHERE id = ?', (user_id,)
        ).fetchone()
Exemplo n.º 4
0
def get_num_unread(comic_name):
    ''' Computes the number of strips unread for `comic`. '''
    db = get_db()
    return db.execute(
        '''
        SELECT count(*) FROM (SELECT * FROM strips WHERE comic = ?) as stripsForComic
        LEFT OUTER JOIN (SELECT * FROM reads WHERE userId=?) AS userReads
        ON stripsForComic.id=userReads.stripId
        WHERE stripId IS NULL
        ''', (comic_name, g.user['id'])).fetchone()['count(*)']
Exemplo n.º 5
0
def get_comic_name_from_url(url):
    ''' Attempts to match the given URL to an existing comic in the DB. '''
    db = get_db()
    comics = db.execute('''SELECT * FROM comics''').fetchall()

    for comic in comics:
        if comic['name'].lower() in url:
            return comic['name']

    return None
Exemplo n.º 6
0
def get_bookmark(comic_name):
    ''' Returns the bookmark for the comic or None if no such bookmark exists. '''
    db = get_db()
    result = db.execute(
        '''
        SELECT link FROM bookmarks
        WHERE comic = ?
        LIMIT 1
        ''', (comic_name, )).fetchone()

    if result is None:
        return None

    return result['link']
Exemplo n.º 7
0
def index():
    db = get_db()
    comics = db.execute('''
        SELECT * FROM comics
        ORDER BY name ASC
        ''').fetchall()
    comics = [dict(comic) for comic in comics]

    for comic in comics:
        comic['numUnread'] = get_num_unread(comic['name'])
        comic['bookmark'] = get_bookmark(
            comic['name']) if comic['style'] == 'serial' else None

    return render_template('comics/index.html', comics=comics)
Exemplo n.º 8
0
def add_comic_command(name, author, style, link, icon_url=None):
    '''
    Adds a new comic to the database.

    STYLE must be either "serial" or "episodic".
    '''
    if style not in ['serial', 'episodic']:
        click.echo('Invalid input. style must be either "serial" or "episodic".', err=True)
        sys.exit(1)

    db = get_db()
    db.execute(
        'INSERT INTO comics (name, author, link, iconUrl, style) VALUES (?, ?, ?, ?, ?)',
        (name, author, link, icon_url, style)
    )
    db.commit()
    click.echo(f'Added {name} to the database.')
Exemplo n.º 9
0
def strips(comic_name=None):
    db = get_db()
    strips = db.execute(
        '''
        SELECT stripsForComic.*, (stripId IS NOT NULL) as wasRead
        FROM (SELECT * FROM strips WHERE comic = ?) AS stripsForComic
        LEFT OUTER JOIN (SELECT * FROM reads WHERE userId = ?) AS userReads
        ON stripsForComic.id = userReads.stripId
        ORDER BY stripsForComic.datePublished DESC
        ''', (comic_name, g.user['id'])).fetchall()
    strips = [dict(strip) for strip in strips]

    for strip in strips:
        strip['wasRead'] = bool(strip['wasRead'])

    comic = db.execute(
        '''
        SELECT * FROM comics WHERE name = ? LIMIT 1
        ''', (comic_name, )).fetchone()
    comic = dict(comic)
    comic['bookmark'] = get_bookmark(comic_name)

    return render_template('comics/strips.html', strips=strips, comic=comic)
Exemplo n.º 10
0
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        db = get_db()
        error = None

        user = db.execute(
            'SELECT * FROM users WHERE name = ?', (username,)
        ).fetchone()

        if user is None:
            error = 'Incorrect username.'
        elif not check_password_hash(user['password'], password):
            error = 'Incorrect password.'

        if error is None:
            session.clear()
            session['user_id'] = user['id']
            return redirect(url_for('index'))

        flash(error)

    return render_template('auth/login.html')