def home():
    getAnalyticsData()
    with conn.connect() as connection:
        posts = connection.execute(
            'SELECT p.id, title, body, created, hide, author_id, username, u.name'
            ' FROM post p JOIN public."user" u ON p.author_id = u.id'
            ' ORDER BY created DESC').fetchall()

    if not current_user.is_authenticated:
        posts = [post for post in posts if not post['hide']]

    total_page = math.ceil(len(posts) / 5)
    page = request.args.get("page")

    if page:
        if int(page) > total_page:
            page = total_page
        elif int(page) < 1:
            page = 1
        else:
            page = int(page)
        last_post = 5 + 5 * (page - 1) if len(
            posts) >= 5 + 5 * (page - 1) else len(posts)
    else:
        page = 1
        last_post = 5 if total_page > 1 else len(posts)

    git_log = get_git_log('main')
    return render_template('blog/blog.html',
                           posts=posts[(page - 1) * 5:last_post],
                           markdown=markdown,
                           current_page=page,
                           total_page=total_page,
                           git_log=git_log,
                           length=len(git_log) + 1)
Example #2
0
def update(id):
    post = get_post(id)

    if request.method == 'POST':
        title = request.form['title']
        body = request.form['body']
        try:
            hidden = request.form['hide']
        except KeyError:
            hidden = 'False'
        error = None

        if not title:
            error = 'Title is required.'

        if error is not None:
            flash(error)
        else:
            with conn.connect() as connection:
                connection.execute(
                    'UPDATE post SET title = %s, body = %s, hide = %s'
                    ' WHERE id = %s',
                    (title, body, hidden, id)
                )
            return redirect(url_for('home'))

    return render_template('blog/update.html', post=post)
Example #3
0
def create():
    if request.method == 'POST':
        title = request.form['title']
        body = request.form['body']
        try:
            hidden = request.form['hide']
        except KeyError:
            hidden = 'False'
        error = None

        if not title:
            error = 'Titre requis.'

        if error is not None:
            flash(error)
        else:
            with conn.connect() as connection:
                connection.execute(
                    'INSERT INTO post (title, body, author_id, hide)'
                    ' VALUES (%s, %s, %s, %s)',
                    title, body, current_user.get_id(), hidden
                )
            return redirect(url_for('home'))

    return render_template('blog/create.html')
def set_analytics_data(id, json, time_range, type):
    with conn.connect() as connection:
        connection.execute(
            'INSERT INTO spotify_analytics (id, json, time_range, type)'
            ' VALUES (%s, %s, %s, %s)'
            '  ON CONFLICT (id) DO UPDATE'
            '   SET json = EXCLUDED.json, time_range = EXCLUDED.time_range, type = EXCLUDED.type',
            id, json, time_range, type)
def get_analytics_data(time_range, type):
    with conn.connect() as connection:
        analy = connection.execute(
            'SELECT json'
            ' FROM public.spotify_analytics'
            '  WHERE time_range = %s AND type = %s',
            (time_range, type)).fetchone()
        r_list = [row for row in analy]
    return json.loads(r_list[0])
def get_top_artists():
    with conn.connect() as connection:
        stats = connection.execute('SELECT s.artist, count(s.artist)'
                                   ' FROM public.spotify_stat s'
                                   '  GROUP BY artist'
                                   '   ORDER BY count DESC').fetchall()
    data = []
    for row in stats:
        data.append({'artist': row['artist'], 'count': row['count']})
    return data[:50 if len(data) > 50 else len(data)]
def get_project(id):
    with conn.connect() as connection:
        project = connection.execute(
            'SELECT id, title, body, idproject, source'
            ' FROM projects '
            ' WHERE id = %s', (id, )).fetchone()

    if project is None:
        abort(404, f"Le Post d'id {id} n'existe pas.")
    return project
def get_top_tracks():
    with conn.connect() as connection:
        stats = connection.execute(
            'SELECT s.track, s.artist, s.url_track, count(s.track)'
            ' FROM public.spotify_stat s'
            '  GROUP BY track, artist, url_track'
            '   ORDER BY count DESC').fetchall()
    data = []
    for row in stats:
        data.append({
            'title': row['track'],
            'artist': row['artist'],
            'url_track': row['url_track'],
            'count': row['count']
        })
    return data[:50 if len(data) > 50 else len(data)]
Example #9
0
def get_post(id, check_author=True):
    with conn.connect() as connection:
        post = connection.execute(
            'SELECT p.id, title, body, created, hide, author_id, username, u.name'
            ' FROM post p JOIN public."user" u ON p.author_id = u.id'
            ' WHERE p.id = %s',
            (id,)
        ).fetchone()

    if post is None:
        abort(404, f"Le Post d'id {id} n'existe pas.")

    if check_author and current_user is None and post['author_id'] != \
       int(current_user.get_id()):
        abort(403)

    return post
Example #10
0
def refreshStat():
    counter = 0
    url = USER_RECENTLY_PLAYED_ENDPOINT
    params = {'limit': 50}
    headers = {"Authorization": f"Bearer {TOKEN_DATA[1]}"}
    resp = requests.get(url, params=params, headers=headers).json()['items']
    for track in resp:
        played_at = track['played_at']
        title = track['track']['name']
        artist = ", ".join(
            [artist['name'] for artist in track['track']['artists']])
        url_track = track['track']['album']['images'][1]['url']
        with conn.connect() as connection:
            counter += 1
            connection.execute(
                'INSERT INTO spotify_stat (played_at, track, artist, url_track)'
                ' VALUES (%s, %s, %s, %s)'
                '  ON CONFLICT DO NOTHING', played_at, title, artist,
                url_track)
    app.logger.info(f"Updated {counter} records.")
def create():
    if request.method == 'POST':
        title = request.form['title']
        body = request.form['body']
        source = request.form['source']
        idproject = request.form['idproject']

        error = None

        if not title:
            error = 'Titre requis.'

        if error is not None:
            flash(error)
        else:
            with conn.connect() as connection:
                connection.execute(
                    'INSERT INTO projects (title, body, idproject, source)'
                    ' VALUES (%s, %s, %s, %s)', title, body, idproject, source)
            return redirect(url_for('projects.projects'))

    return render_template('projects/create.html')
def update(id):
    project = get_project(id)

    if request.method == 'POST':
        title = request.form['title']
        body = request.form['body']
        source = request.form['source']
        idproject = request.form['idproject']
        error = None

        if not title:
            error = 'Title is required.'

        if error is not None:
            flash(error)
        else:
            with conn.connect() as connection:
                connection.execute(
                    'UPDATE projects SET title = %s, body = %s, source = %s, idproject = %s'
                    ' WHERE id = %s', (title, body, source, idproject, id))
            return redirect(url_for('home'))

    return render_template('projects/update.html', project=project)
Example #13
0
def delete(id):
    get_post(id)
    with conn.connect() as connection:
        connection.execute('DELETE FROM post WHERE id = %s', (id,))
    return redirect(url_for('home'))
def delete(id):
    get_project(id)
    with conn.connect() as connection:
        connection.execute('DELETE FROM projects WHERE id = %s', (id, ))
    return redirect(url_for('projects.projects'))
def get_all_projects():
    with conn.connect() as connection:
        projects = connection.execute(
            'SELECT id, title, body, idproject, source'
            ' FROM projects').fetchall()
    return projects