コード例 #1
0
    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
コード例 #2
0
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()
コード例 #3
0
 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.'}
コード例 #4
0
    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()
コード例 #5
0
ファイル: app.py プロジェクト: RusksTeam/db-generator
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'))
コード例 #6
0
ファイル: app.py プロジェクト: RusksTeam/db-generator
def initialize():
    db.create_all()
    from fill_tables import load_tables
    load_tables()
    global daily_drybread
    daily_drybread = DryBreadModel.get_random_drybread().json()
コード例 #7
0
 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()]}
コード例 #8
0
 def get(self, _id):
     drybread = DryBreadModel.find_by_id(_id)
     if drybread:
         return drybread.json()
     return {'message': 'Drybread not found.'}, 404