Exemplo n.º 1
0
def users(username):
    if not username:
        if request.method == 'DELETE':
            abort(400)  # don't allow deletes with out user
        if session.get('username', False):
            username = session.get('username')
        else:
            abort(400)
    if request.method == 'GET':
        q = mongo.db.users.find_one({'username': username}, {'hash': False})
        if q:
            q['_id'] = str(q['_id'])
            if 'theme' not in q:
                q['theme'] = 'cerulean'
            return jsonify(q)
        else:
            abort(404)
    elif request.method == 'DELETE':
        return jsonify({'success': remove_user(username)})
    elif request.method == 'POST':
        fields = {}
        if not request.json:
            abort(400)
        if 'theme' in request.json:
            if request.json['theme'] in app.config['THEMES']:
                fields['theme'] = request.json['theme']
            else:
                abort(400)
        if 'email' in request.json:
            fields['email'] = request.json['email']
        q = mongo.db.users.update({'username': username},
                                  {"$set": fields}, upsert=False)
        if q:
            if session and 'theme' in fields:
                session['theme'] = fields['theme']
            return jsonify({'success': True})
        else:
            return jsonify({'success': False})
Exemplo n.º 2
0
def users(username):
    if not username:
        if request.method == 'DELETE':
            abort(400)  # don't allow deletes with out user
        if session.get('username', False):
            username = session.get('username')
        else:
            abort(400)
    if request.method == 'GET':
        q = list(r.table("users").filter({"username": username}).without("hash").run(rdb.conn))[0]
        if q:
            if 'theme' not in q:
                q['theme'] = 'cerulean'
            return jsonify(q)
        else:
            abort(404)
    elif request.method == 'DELETE':
        return jsonify({'success': remove_user(username)})
    elif request.method == 'POST':
        fields = {}
        if not request.json:
            abort(400)
        if 'theme' in request.json:
            if request.json['theme'] in app.config['THEMES']:
                fields['theme'] = request.json['theme']
            else:
                abort(400)
        if 'email' in request.json:
            fields['email'] = request.json['email']
        q = r.table("users").filter({"username": username}).update(fields).run(rdb.conn) 
        if q["replaced"] != 0 :
            if session and 'theme' in fields:
                session['theme'] = fields['theme']
            return jsonify({'success': True})
        else:
            return jsonify({'success': False})
Exemplo n.º 3
0
def users(username):
    if not username:
        if request.method == 'DELETE':
            abort(400)  # don't allow deletes with out user
        if session.get('username', False):
            username = session.get('username')
        else:
            abort(400)
    if request.method == 'GET':
        q = list(r.table("users").filter({"username": username}).without("hash").run(rdb.conn))[0]
        if q:
            if 'theme' not in q:
                q['theme'] = 'cerulean'
            return jsonify(q)
        else:
            abort(404)
    elif request.method == 'DELETE':
        return jsonify({'success': remove_user(username)})
    elif request.method == 'POST':
        fields = {}
        if not request.json:
            abort(400)
        if 'theme' in request.json:
            if request.json['theme'] in app.config['THEMES']:
                fields['theme'] = request.json['theme']
            else:
                abort(400)
        if 'email' in request.json:
            fields['email'] = request.json['email']
        q = r.table("users").filter({"username": username}).update(fields).run(rdb.conn) 
        if q["replaced"] != 0 :
            if session and 'theme' in fields:
                session['theme'] = fields['theme']
            return jsonify({'success': True})
        else:
            return jsonify({'success': False})