def post(self, _id=None): claims = get_jwt_claims() if not claims['is_admin']: return {'message': 'Admin rights required.'} data = DryBread.parser.parse_args() drybread = DryBreadModel(**data) try: drybread.save_to_db() except: return { 'message': 'Error occured when saving drybread to database.' }, 500 return drybread.json(), 201
def load_tables(): if len(DryBreadModel.find_all()) == 0: print('Database does not exist/is empty. Creating from file...') data = dict() with open('tmp_database.json', 'r', encoding='utf-8') as f: data = json.loads(f.read()) for e in data['suchary']: if e['type'] <= cfg['JOKE_TYPE']: db = DryBreadModel(e['q'], e['a']) db.save_to_db() print('drybreads loaded:', [x.json() for x in db.find_all()]) ####users database from api.models.user import UserModel admin = UserModel('admin', 'admin', UserModel.ROLE_ADMIN) admin.save_to_db()
def delete(self, _id): claims = get_jwt_claims() if not claims['is_admin']: return {'message': 'Admin rights required.'} drybread = DryBreadModel.find_by_id(_id) if drybread: try: drybread.delete_from_db() except: return { 'message', 'Error occured when removing drybread from database.' }, 500 return {'message': 'Drybread deleted.'} return {'message': 'Drybread not found.'}
def put(self, _id): claims = get_jwt_claims() if not claims['is_admin']: return {'message': 'Admin rights required.'} data = DryBread.parser.parse_args() drybread = DryBreadModel.find_by_id(_id) if drybread: drybread.question = data['question'] drybread.answer = data['answer'] else: drybread = DryBreadModel(**data) try: drybread.save_to_db() except: return {'message', 'Error occured when updating drybread.'}, 500 return drybread.json()
def update_daily_drybread(): global daily_drybread daily_drybread = DryBreadModel.get_random_drybread().json() flash('Drybread has been successfully updated.') return redirect(url_for('index'))
def initialize(): db.create_all() from fill_tables import load_tables load_tables() global daily_drybread daily_drybread = DryBreadModel.get_random_drybread().json()
def get(self): claims = get_jwt_claims() if not claims['is_admin']: return {'message': 'Admin rights required.'} return {'drybreads': [x.json() for x in DryBreadModel.find_all()]}
def get(self, _id): drybread = DryBreadModel.find_by_id(_id) if drybread: return drybread.json() return {'message': 'Drybread not found.'}, 404