Пример #1
0
def register():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        mongo = get_db()
        error = None

        if not username:
            error = 'Username is Required!'
        elif not password:
            error = 'Password is Required!'
        elif mongo.db.users.find_one({"username": username}) is not None:
            error = 'Username {} is taken!'.format(username)

        if error is None:
            mongo.db.users.insert_one({
                "username":
                username,
                "password":
                generate_password_hash(password)
            })
            return redirect(url_for('auth.login'))

        flash(error)
    return render_template('auth/register.html')
Пример #2
0
def get_plan(id, check_author=True):
    id = ObjectId(id)
    plan = get_db().db.plans.find_one({"_id": id})
    if plan is None:
        abort(404, "Post id {} doesn't exist.".format(id))

    if check_author and plan['author_id'] != g.user['_id']:
        abort(403)

    return plan
Пример #3
0
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        mongo = get_db()
        error = None
        user = mongo.db.users.find_one({"username": username})
        if user is None or not check_password_hash(user['password'], password):
            error = 'Invalid Username and Password combination'

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

        flash(error)
    return render_template('auth/login.html')
Пример #4
0
def create():
    if request.method == 'POST':
        title = request.form['title']
        body = request.form['body']
        error = None

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

        if error is not None:
            flash(error)
        else:
            mongo = get_db()
            mongo.db.plans.insert({
                'title': title,
                'body': body,
                'author_id': g.user['_id'],
                'username': g.user['username']
            })
            return redirect(url_for('plan.index'))
    return render_template('plan/create.html')
Пример #5
0
def update(id):
    plan = get_plan(id)

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

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

        if error is not None:
            flash(error)
        else:
            mongo = get_db()
            print('scream')
            mongo.db.plans.update({"_id": ObjectId(id)},
                                  {"$set": {
                                      "title": title,
                                      "body": body
                                  }})
            return redirect(url_for('plan.index'))

    return render_template('plan/update.html', plan=plan)
Пример #6
0
def load_logged_in_user():
    user_id = ObjectId(session.get('user_id'))
    if user_id is None:
        g.user = None
    else:
        g.user = get_db().db.users.find_one({'_id': user_id})
Пример #7
0
def delete(id):
    get_plan(id)
    mongo = get_db()
    mongo.db.plans.remove({"_id": ObjectId(id)})
    return redirect(url_for('plan.index'))
Пример #8
0
def index():
    mongo = get_db()
    plans = mongo.db.plans.find()
    return render_template("plan/index.html", plans=plans)