Example #1
0
def edit_user(user_id=None):
    """Update a user based on the post parameters given and output a
    json-encoded user with the new attributes."""
    session = db.db_session()
    query = session.query(User).filter(User.id == user_id)
    user_data = request.form
    try:
        cur_user = query[0]
    except IndexError:
        session.close()
        abort(404)
    res = dict()
    res['name'] = user_data['name']
    res['full_name'] = user_data['full_name']
    res['password'] = hashlib.sha256(user_data['password']).hexdigest()
    query.update(res)
    res['id'] = cur_user.id
    del res['password']
    try:
        session.commit()
    except IntegrityError:
        session.rollback()
        abort(400)
    finally:
        session.close()
    return jsonify(res)
Example #2
0
def edit_phrase(phrase_id=None):
    """Edit a phrase based on the given post parameters. This function does its
    best to cope with null parameters to categories, precategories, genres,
    etc. To remove associations, pass 'null' as that parameter via POST."""
    session = db.db_session()
    query = session.query(Phrase).filter(Phrase.id == phrase_id)
    phrase_data = request.form
    try:
        cur_phrase = query[0]
    except IndexError:
        session.close()
        abort(404)
    for key, value in phrase_data.items():
        if value.lower() == 'null':
            setattr(cur_phrase, key, None)
        else:
            setattr(cur_phrase, key, value)
    query = session.query(User).filter(User.name == request.authorization.username)
    cur_phrase.user_id = query[0].id
    session.add(cur_phrase)
    try:
        session.commit()
    except IntegrityError:
        session.rollback()
        session.close()
        abort(400)
    res = _phrase_to_dict(cur_phrase)
    session.close()
    return jsonify(res)
Example #3
0
def edit_badword(badword_id=None):
    """Edit a badword based on the POST parameters and return a json-encoded
    result."""
    session = db.db_session()
    query = session.query(Badword).filter(Badword.id == badword_id)
    badword_data = request.form
    try:
        cur_badword = query[0]
    except IndexError:
        session.close()
        abort(404)
    res = dict()
    res['word'] = badword_data['word']
    query = session.query(User).filter(User.name == request.authorization.username)
    res['user_id'] = query[0].id
    try:
        query.update(res)
        res['id'] = cur_badword.id
        res['phrase_id'] = cur_badword.phrase_id
        session.commit()
    except IntegrityError:
        session.rollback()
        abort(400)
    finally:
        session.close()
    return jsonify(res)
Example #4
0
def count_phrases_per_user():
    """Count all of the phrases in the database on a per-user basis."""
    ret = dict()
    session = db.db_session()
    query = session.query(User)
    for cur_user in query:
        num = session.query(Phrase).filter(Phrase.user_id == cur_user.id).count()
        ret[cur_user.name] = num
    session.close()
    return jsonify(ret)
Example #5
0
def random_phrase():
    """Redirect to a random phrase."""
    session = db.db_session()
    query = session.query(Phrase).filter(Phrase.approved == 0).order_by(func.random()).limit(1)
    try:
        cur_phrase = query[0]
    except IndexError:
        abort(404)
    finally:
        session.close()
    return redirect("%s/phrases/%d" % (conf.app_root, cur_phrase.id), 301)
Example #6
0
def phrases_random_buzzworthy():
    """Return a json-encoded list of buzzworthy phrases. This is going to choke and
    probably die once there is a very large database."""
    session = db.db_session()
    query = session.query(Phrase).filter(Phrase.buzzworthy == 1).filter(Phrase.stage == 1).order_by(func.random()).limit(1)
    try:
        cur_phrase = query[0]
    except IndexError:
        abort(404)
    finally:
        session.close()
    return redirect("%s/phrases/%d" % (conf.app_root, cur_phrase.id), 301)
Example #7
0
def phrase(phrase_id=None):
    """Return a json-encoded phrase identified by the provided id."""
    session = db.db_session()
    query = session.query(Phrase).filter(Phrase.id == phrase_id)
    try:
        cur_phrase = query[0]
    except IndexError:
        session.close()
        abort(404)
    res = _phrase_to_dict(cur_phrase)
    session.close()
    return jsonify(res)
Example #8
0
def difficulties():
    """Return a json-encoded list of difficulties."""
    session = db.db_session()
    res = dict()
    query = session.query(Difficulty)
    for cur_difficulty in query:
        cur_res = dict()
        cur_res['id'] = cur_difficulty.id
        cur_res['name'] = cur_difficulty.name
        res[cur_difficulty.id] = cur_res
    session.close()
    return jsonify(res)
Example #9
0
def packs():
    """Return a json-encoded list of packs."""
    session = db.db_session()
    res = dict()
    query = session.query(Pack)
    for cur_pack in query:
        cur_res = dict()
        cur_res['id'] = cur_pack.id
        cur_res['name'] = cur_pack.name
        res[cur_pack.id] = cur_res
    session.close()
    return jsonify(res)
Example #10
0
def genres():
    """Return a json-encoded list of genres."""
    session = db.db_session()
    res = dict()
    query = session.query(Genre)
    for cur_genre in query:
        cur_res = dict()
        cur_res['id'] = cur_genre.id
        cur_res['name'] = cur_genre.name
        res[cur_genre.id] = cur_res
    session.close()
    return jsonify(res)
Example #11
0
def precategories():
    """Return a json-encoded list of pre-categories."""
    session = db.db_session()
    res = dict()
    query = session.query(PreCategory)
    for cur_precategory in query:
        cur_res = dict()
        cur_res['id'] = cur_precategory.id
        cur_res['name'] = cur_precategory.name
        res[cur_precategory.id] = cur_res
    session.close()
    return jsonify(res)
Example #12
0
def genre(genre_id=None):
    """Return a json-encoded genre identified by the given id."""
    session = db.db_session()
    query = session.query(Genre).filter(Genre.id == genre_id)
    try:
        cur_genre = query[0]
    except IndexError:
        session.close()
        abort(404)
    res = dict()
    res['id'] = cur_genre.id
    res['name'] = cur_genre.name
    session.close()
    return jsonify(res)
Example #13
0
def users():
    """Output a json-encoded list of users. We do not include the password
    hashes for security reasons."""
    res = dict()
    session = db.db_session()
    query = session.query(User)
    for cur_user in query:
        cur_res = dict()
        cur_res['id'] = cur_user.id
        cur_res['name'] = cur_user.name
        cur_res['full_name'] = cur_user.full_name
        res[cur_user.id] = cur_res
    session.close()
    return jsonify(res)
Example #14
0
def delete_pack(pack_id=None):
    """Delete a pack identified by the given id and return the former name."""
    session = db.db_session()
    query = session.query(Pack).filter(Pack.id == pack_id)
    try:
        cur_pack = query[0]
    except IndexError:
        session.close()
        abort(404)
    name = cur_pack.name
    session.delete(cur_pack)
    session.commit()
    session.close()
    return jsonify(name=name)
Example #15
0
def delete_user(user_id=None):
    """Delete a user with the specifed user_id and return the old name."""
    session = db.db_session()
    query = session.query(User).filter(User.id == user_id)
    try:
        cur_user = query[0]
    except IndexError:
        session.close()
        abort(404)
    name = cur_user.name
    session.delete(cur_user)
    session.commit()
    session.close()
    return jsonify(name=name)
Example #16
0
def pack(pack_id=None):
    """Return a json-encoded pack based on the provided id."""
    session = db.db_session()
    query = session.query(Pack).filter(Pack.id == pack_id)
    try:
        cur_pack = query[0]
    except IndexError:
        session.close()
        abort(404)
    res = dict()
    res['id'] = cur_pack.id
    res['name'] = cur_pack.name
    session.close()
    return jsonify(res)
Example #17
0
def delete_difficulty(difficulty_id=None):
    """Delete a difficulty based on the given id and return the former name."""
    session = db.db_session()
    query = session.query(Difficulty).filter(Difficulty.id == difficulty_id)
    try:
        cur_difficulty = query[0]
    except IndexError:
        session.close()
        abort(404)
    name = cur_difficulty.name
    session.delete(cur_difficulty)
    session.commit()
    session.close()
    return jsonify(name=name)
Example #18
0
def delete_badword(badword_id=None):
    """Delete a bad word identified by the given id and return the former bad word."""
    session = db.db_session()
    query = session.query(Badword).filter(Badword.id == badword_id)
    try:
        cur_badword = query[0]
    except IndexError:
        session.close()
        abort(404)
    word = cur_badword.word
    session.delete(cur_badword)
    session.commit()
    session.close()
    return jsonify(word=word)
Example #19
0
def delete_genre(genre_id=None):
    """Add a genre based on the given id and return the former name."""
    session = db.db_session()
    query = session.query(Genre).filter(Genre.id == genre_id)
    try:
        cur_genre = query[0]
    except IndexError:
        session.close()
        abort(404)
    name = cur_genre.name
    session.delete(cur_genre)
    session.commit()
    session.close()
    return jsonify(name=name)
Example #20
0
def category(category_id=None):
    """Return a json-encoded category corresponding to category_id."""
    session = db.db_session()
    query = session.query(Category).filter(Category.id == category_id)
    try:
        cur_category = query[0]
    except IndexError:
        abort(404)
    finally:
        session.close()
    res = dict()
    res['id'] = cur_category.id
    res['name'] = cur_category.name
    return jsonify(res)
Example #21
0
def add_category():
    """Add a new category based on the provided post parameters and return the
    json-encoded result."""
    session = db.db_session()
    cur_category = request.form
    new_category = Category(cur_category['name'])
    session.add(new_category)
    try:
        session.commit()
    except IntegrityError:
        session.rollback()
        abort(400)
    return jsonify(id=new_category.id,
                   name=new_category.name)
Example #22
0
def precategory(precategory_id=None):
    """Return a json-encoded pre-category identified by precategory_id."""
    session = db.db_session()
    query = session.query(PreCategory).filter(PreCategory.id == precategory_id)
    try:
        cur_precategory = query[0]
    except IndexError:
        abort(404)
    finally:
        session.close()
    res = dict()
    res['id'] = cur_precategory.id
    res['name'] = cur_precategory.name
    return jsonify(res)
Example #23
0
def delete_category(category_id=None):
    """Delete a category based on category_id and return the old name."""
    session = db.db_session()
    query = session.query(Category).filter(Category.id == category_id)
    try:
        cur_category = query[0]
    except IndexError:
        session.close()
        abort(404)
    name = cur_category.name
    session.delete(cur_category)
    session.commit()
    session.close()
    return jsonify(name=name)
Example #24
0
def check_auth(username, password):
    """This function is called to check if a username/password combination is
    valid."""
    session = db.db_session()
    result = session.query(User).filter(User.name == username)
    if result.count() != 1:
        session.rollback()
        session.close()
        return False
    else:
        truePass = result[0].password
        tryPass = hashlib.sha256(password).hexdigest()
        session.close()
        return truePass == tryPass
Example #25
0
def difficulty(difficulty_id=None):
    """Return a json-encoded difficulty identified by the given id."""
    session = db.db_session()
    query = session.query(Difficulty).filter(Difficulty.id == difficulty_id)
    try:
        cur_difficulty = query[0]
    except IndexError:
        session.close()
        abort(404)
    res = dict()
    res['id'] = cur_difficulty.id
    res['name'] = cur_difficulty.name
    session.close()
    return jsonify(res)
Example #26
0
def user(user_id=None):
    """Output a json-encoded user corresponding to user_id."""
    session = db.db_session()
    query = session.query(User).filter(User.id == user_id)
    try:
        cur_user = query[0]
    except IndexError:
        abort(404)
    res = dict()
    res['id'] = cur_user.id
    res['name'] = cur_user.name
    res['full_name'] = cur_user.full_name
    session.close()
    return jsonify(res)
Example #27
0
def add_genre():
    """Add a genre based on the given post parameters and return the
    json-encoded result."""
    session = db.db_session()
    cur_genre = request.form
    new_genre = Genre(cur_genre['name'])
    session.add(new_genre)
    try:
        session.commit()
    except IntegrityError:
        abort(400)
    finally:
        session.close()
    return jsonify(id=new_genre.id,
                   name=new_genre.name)
Example #28
0
def delete_phrase(phrase_id=None):
    """Delete a phrase identified by the given id and return the former
    phrase."""
    session = db.db_session()
    query = session.query(Phrase).filter(Phrase.id == phrase_id)
    try:
        cur_phrase = query[0]
    except IndexError:
        session.close()
        abort(404)
    str_phrase = cur_phrase.phrase
    session.delete(cur_phrase)
    session.commit()
    session.close()
    return jsonify(phrase=str_phrase)
Example #29
0
def get_badword(badword_id=None):
    """Edit a badword based on the POST parameters and return a json-encoded
    result."""
    session = db.db_session()
    query = session.query(Badword).filter(Badword.id == badword_id)
    try:
        cur_badword = query[0]
    except IndexError:
        session.close()
        abort(404)
    res = dict()
    res['id'] = cur_badword.id
    res['word'] = cur_badword.word
    res['phrase_id'] = cur_badword.phrase_id
    session.close()
    return jsonify(res)
Example #30
0
def add_pack():
    """Add a pack based on the POST parameters and return a json-encoded
    result."""
    session = db.db_session()
    cur_pack = request.form
    new_pack = Pack(cur_pack['name'])
    session.add(new_pack)
    try:
        session.commit()
    except IntegrityError:
        session.rollback()
        abort(400)
    finally:
        session.close()
    return jsonify(id=new_pack.id,
                   name=new_pack.name)