Exemple #1
0
def delete_thread(thread_uid):
    """Delete given thread and all its comments.

    :param thread_uid: Thread unique ID.
    """
    thread = storage.get_thread(thread_uid)
    if not thread:
        abort(404)

    if thread['author'] != g.username:
        abort(403)

    if request.method == 'GET':
        return render_template('delete_thread.html',
                               thread=thread,
                               thread_uid=thread_uid)

    if request.form.get('confirm') != 'confirm':
        return error('delete_thread:confirm')

    storage.delete_thread(thread_uid)
    flash('Thread Succesfully Deleted: {0}'.format(thread['subject']),
          'success')

    return to_threads()
Exemple #2
0
def start_thread():
    """Start new thread with or without first comment."""
    subject = request.form.get('subject') or ''
    comment = request.form.get('comment') or ''
    if not subject:
        return error('start_thread:subject')

    storage.start_thread(g.username, subject, comment)
    flash('New Thread Started: {0}'.format(subject), 'success')

    return to_threads()
Exemple #3
0
def index():
    """Setup username and log into the Comments app."""
    error = None

    # Username already supplied, redirect to threads list
    if g.username:
        return to_threads()

    if request.method == 'POST':
        username = request.form.get('username') or ''
        if len(username) > 2:
            prefix = current_app.config['KEY_PREFIX']
            username_key = USERNAME_KEY.format(prefix)
            session[username_key] = username

            flash('Welcome, {0}!'.format(username), 'success')
            return to_threads()

        error = True

    status = 400 if error else 200
    return make_response(render_template('index.html', error=error), status)