Esempio n. 1
0
def web_settings():
    user = get_user()

    if user:
        return render_template('user/settings.html')
    else:
        return render_template('status/error.html', code='logged_out')
Esempio n. 2
0
def web_account():
    user = get_user()

    if user:
        return render_template('user/account.html', user=user)
    else:
        return render_template('status/error.html', code='logged_out')
Esempio n. 3
0
def api_delete_put():
    user = get_user()

    if not user:
        return make_response('NO USER', 422)
    else:
        delete_user(user)
        return make_response('OK', 200)
Esempio n. 4
0
def api_favourites_get():
    user = get_user()

    if not user:
        return make_response('NO USER', 422)
    else:
        posts = get_favourites(user)
        return jsonify(posts)
Esempio n. 5
0
def web_delete_confirm():
    user = get_user()

    if not user:
        return render_template('status/error.html', code='logged_out')
    else:
        delete_user(user)
        return redirect('/logout/', code=302)
Esempio n. 6
0
def api_account_get():
    user = get_user()

    keys = 'username', 'email', 'verified'

    if not user:
        return make_response('NO USER', 422)
    else:
        return jsonify({key: user[key] for key in keys})
Esempio n. 7
0
def web_post(post_link):
    user = get_user()
    post = find_post_by_link(post_link)
    is_favourite = check_favourite(user, post)

    return render_template('posts/post.html',
                           post=post,
                           is_favourite=is_favourite,
                           tags=post['tags'].split(','))
Esempio n. 8
0
def api_favourite_delete():
    user = get_user()
    post_id = request.form.get('post')
    post = find_post_by_id(post_id)

    if not post:
        return make_response('NO POST', 422)
    if not user:
        return make_response('NO USER', 422)
    else:
        return remove_post(user, post)
Esempio n. 9
0
def api_favourite_get():
    user = get_user()
    post_id = request.form.get('post')
    post = find_post_by_id(post_id)
    added = check_favourite(user, post)

    if not post:
        return make_response('NO POST', 422)
    if not user:
        return make_response('NO USER', 422)
    if added:
        return make_response('IS FAVOURITE', 200)
    else:
        return make_response('NOT FAVOURITE', 200)
Esempio n. 10
0
def web_settings_processor():
    username = request.form.get('username')
    email = request.form.get('email')
    repeat_email = request.form.get('repeat-email')
    new_password = request.form.get('new-password')
    repeat_new_password = request.form.get('repeat-new-password')

    user = get_user()
    new_password_check = new_password == repeat_new_password
    email_check = email == repeat_email
    username_exists = check_username(username)
    email_exists = check_email(email)
    email_syntax_ok = syntax_check.check_email_syntax(email)
    username_syntax_ok = syntax_check.check_username_syntax(username)
    password_syntax_ok = syntax_check.check_password_syntax(new_password)

    if not user:
        return redirect('/logout/', code=302)
    elif email and not email_syntax_ok:
        return render_template('status/error.html', code='wrong_email_syntax')
    elif username and not username_syntax_ok:
        return render_template('status/error.html',
                               code='wrong_username_syntax')
    elif new_password and not password_syntax_ok:
        return render_template('status/error.html',
                               code='wrong_password_syntax')
    if not new_password_check:
        return render_template('status/error.html',
                               code='passwords_do_not_match')
    elif not email_check:
        return render_template('status/error.html', code='emails_do_not_match')
    elif username_exists:
        return render_template('status/error.html', code='username_exists')
    elif email_exists:
        return render_template('status/error.html', code='email_exists')

    if username:
        update_user(user, username=username)
    if email:
        update_user(user, email=email)
    if new_password:
        cookie_id = generate_hash()
        update_user(user, new_password=new_password, cookie_id=cookie_id)
        resp = make_response(
            render_template('status/success.html', code='edit_success'))
        resp.set_cookie(COOKIE_NAME, cookie_id, max_age=60 * 60 * 24 * 30)
        return resp

    return render_template('status/success.html', code='edit_success')
Esempio n. 11
0
def web_favourites():
    user = get_user()
    if not user:
        return render_template('status/error.html', code='logged_out')

    posts = get_favourites(user)
    if not posts:
        return render_template('posts/favourites.html', code='no_posts')
    current_page = request.args.get('page')

    if not current_page:
        current_page = 0
    else:
        current_page = int(current_page)
    pages_number = len(posts) // 10 + 1

    if len(posts) > 10:
        posts = posts[current_page * 10:current_page * 10 + 10]

    return render_template('posts/favourites.html',
                           posts=posts,
                           current_page=current_page,
                           pages_number=pages_number)
Esempio n. 12
0
def api_settings_post():
    username = request.form.get('username')
    email = request.form.get('email')
    new_password = request.form.get('new-password')

    user = get_user()
    username_exists = check_username(username)
    email_exists = check_email(email)
    email_syntax_ok = syntax_check.check_email_syntax(email)
    username_syntax_ok = syntax_check.check_username_syntax(username)
    password_syntax_ok = syntax_check.check_password_syntax(new_password)

    if not user:
        return make_response('NO USER', 422)
    elif email and not email_syntax_ok:
        return make_response('INVALID EMAIL SYNTAX', 422)
    elif username and not username_syntax_ok:
        return make_response('INVALID USERNAME SYNTAX', 422)
    elif new_password and not password_syntax_ok:
        return make_response('INVALID PASSWORD SYNTAX', 422)
    elif username_exists:
        return make_response('USERNAME EXISTS', 422)
    elif email_exists:
        return make_response('EMAIL EXISTS', 422)

    if username:
        update_user(user, username=username)
    if email:
        update_user(user, email=email)
    if new_password:
        cookie_id = generate_hash()
        update_user(user, new_password=new_password, cookie_id=cookie_id)
        resp = make_response(make_response('OK', 200))
        resp.set_cookie(COOKIE_NAME, cookie_id, max_age=60 * 60 * 24 * 30)
        return resp

    return make_response('OK', 200)
Esempio n. 13
0
def web_del_post():
    user = get_user()
    post_id = request.form.get('post')
    post = find_post_by_id(post_id)

    return remove_post(user, post)