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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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
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)
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)
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)
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)
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)
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)