def login(username, password): if current_user.is_authenticated: return Errors("Already logedin", 400).to_json() user = db.session.query(Character).filter_by(name=username).first() if user and bycrpt.check_password_hash(user.password, password): login_user(user) return jsonify({'success': True, "msg": "Login {}".format(username)}) return Errors("Could Not Login", 404).to_json()
def get_apps(): if request.method == 'GET': return Applicant.find_all_app() if request.method == 'POST': response = request.get_json() if request.is_json else "Not valid" if response == 'Not valid': return Errors('Expecting A JSON Object', 400).to_json() return add_application(response) return Errors('Not a Valid HTTP Request on This Route', 405).to_json()
def create_character(req): try: if current_user.is_authenticated: return Errors("Already logedin", 400).to_json() password = bycrpt.generate_password_hash( req['password']).decode('utf-8') admin = Character(name=req['username'], password=password) admin.save_to_db() return admin.to_json() except KeyError: return Errors("Username Already Taken", 400).to_json()
def modify_applicants(app_id): if request.method == 'PUT': response = request.get_json() if request.is_json else "Not valid" if response == 'Not valid': return Errors('Expecting A JSON Object', 400).to_json() return update_application(app_id, response) if request.method == 'DELETE': return delete_application(app_id) if request.method == 'GET': return Applicant.find_by_id(app_id) return Errors('Not a Valid HTTP Request on This Route', 405).to_json()
def create_admin(req): try: if current_user.is_authenticated: return Errors("Already logedin", 400).to_json() if valid_username(req['username']): password = bcrypt.generate_password_hash( req['password']).decode('utf-8') admin = Admin(username=req['username'], password=password) admin.save_to_db() return admin.to_json() return Errors("Username Already Taken", 400).to_json() except KeyError: errors.append({'msg': 'Missing Important Keys'})
def delete(table, id): try: res = db.session.query(table).filter_by(id=id).one() res.delete_db() return jsonify({"msg": "Success"}), 200 except NoResultFound: return Errors("Unable to find area", 400).to_json()
def add_item(id, req): try: res = db.session.query(Character).filter_by(id=id).one() w1 = Items(name=req['name'], desc=req['desc'], objs=res) w1.save_to_db() return jsonify({'msg': "success", 'data': res.to_json()}) except KeyError: return Errors("Missing Key Values", 400).to_json()
def all_item(cls): try: items = cls.query.all() return jsonify({ 'success': True, 'count': len(items), 'data': list(map(lambda x: x.to_json(), items)) }), 200 except AttributeError: return Errors('Unable To Find items', 404).to_json()
def find_by_id(cls, id): try: item = cls.query.filter_by(id=id).first() return jsonify({ 'success': True, 'count': 1, 'data': item.to_json() }), 200 except AttributeError: return Errors('Unable To Find item', 404).to_json()
def all_areas(cls): try: area = cls.query.all() return jsonify({ 'success': True, 'count': len(area), 'data': list(map(lambda x: x.to_json(), area)) }), 200 except AttributeError: return Errors('Unable To Find area', 404).to_json()
def all_weapons(cls): try: weapon = cls.query.all() return jsonify({ 'success': True, 'count': len(weapon), 'data': list(map(lambda x: x.to_json(), weapon)) }), 200 except AttributeError: return Errors('Unable To Find weapon', 404).to_json()
def add(table, res): try: if table is Items: print("Calles") o1 = table(res['name'], res['desc']) if table is Character: o1 = table(res['name']) if table is Weapons: o1 = table(res['name'], res['desc'], res['power']) o1.save_to_db() return jsonify({'msg': 'success', 'data': o1.to_json()}) except KeyError: return Errors("Required Keys Missing", 400).to_json()
def find_by_lastname(cls, lastname): lastname = lastname.lower() try: response = cls.query.filter_by(last_name=lastname).all() return jsonify({ 'success': True, 'count': len(response), 'data': list(map(lambda x: x.to_json(), response)) }), 200 except AttributeError: return Errors( 'Unable To Search Application with Last Name: {}'.format( lastname), 404).to_json()
def find_by_school(cls, school): school = school.lower() try: response = cls.query.filter_by(school=school).all() return jsonify({ 'success': True, 'count': len(response), 'data': list(map(lambda x: x.to_json(), response)) }), 200 except AttributeError: return Errors( 'Unable To Search Applications with School'.format(school), 404).to_json()
def retrieve_app_name(last_name): if request.method == 'GET': return Applicant.find_by_lastname(last_name) return Errors('Not a Valid HTTP Request on This Route', 405).to_json()
def retrieve_app_schools(school): if request.method == 'GET': return Applicant.find_by_school(school) return Errors('Not a Valid HTTP Request on This Route', 405).to_json()