· 4 years ago · Apr 19, 2021, 03:24 PM
1# encoding: utf-8
2from flask import Flask
3from flask_sqlalchemy import SQLAlchemy
4from marshmallow import fields
5
6from marshmallow_jsonapi.flask import Schema
7from flask_rest_jsonapi import Api, ResourceDetail, ResourceList
8from flask_admin import Admin
9from flask_admin.contrib.sqla import ModelView
10import os
11from datetime import datetime
12
13app = Flask(__name__)
14
15database_location = os.getcwd()+'\\app.db'
16
17a = os.getcwd()
18b = app.root_path
19# set optional bootswatch theme
20app.config['FLASK_ADMIN_SWATCH'] = 'cerulean'
21app.config['SECRET_KEY'] = 'secret_key'
22
23admin = Admin(app, name='Администратор', template_mode='bootstrap3')
24# Add administrative views here
25app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+database_location
26
27
28db = SQLAlchemy(app)
29
30
31class Animals(db.Model):
32 id = db.Column(db.Integer,primary_key=True)
33 type_animal = db.Column(db.String)
34 weight = db.Column(db.Integer)
35 brought_in = db.Column(db.Date)
36
37
38class AnimalsSchema(Schema):
39 class Meta:
40 type_ = 'питомцы'
41 self_view = 'animals_detail'
42 self_view_kwargs = {'id': '<id>'}
43 self_view_many = 'animals_list'
44
45 id = fields.Integer(as_string=True)
46 type_animal = fields.Str(required = True)
47 weight = fields.Integer(as_string=True)
48 brought_in = fields.Date()
49
50class AnimalsList(ResourceList):
51 schema = AnimalsSchema
52 data_layer = {'session': db.session,
53 'model': Animals}
54
55class AnimalsDetail(ResourceDetail):
56 schema = AnimalsSchema
57 data_layer = {'session': db.session,
58 'model': Animals}
59
60
61api = Api(app)
62api.route(AnimalsList, 'animals_list', '/animals')
63api.route(AnimalsDetail, 'animals_detail', '/animals/<int:id>')
64
65admin.add_view(ModelView(Animals, db.session))
66
67
68
69
70if __name__ == '__main__':
71 app.run(debug=True)