-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex_10_upload_with_base64.py
65 lines (54 loc) · 2.17 KB
/
ex_10_upload_with_base64.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from flask import Flask, request,jsonify, send_from_directory
from flask_restful import Resource, Api
from sqlalchemy import Column, Integer, Text, Float, DateTime, create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.sql.expression import func
from flask_restful import Resource, Api
from dataclasses import dataclass
import json
Base = declarative_base() # Basisklasse aller in SQLAlchemy verwendeten Klassen
metadata = Base.metadata
engine = create_engine('sqlite:////home/albert/tmp/binary_metadata.sqlite3') #Welche Datenbank wird verwendet
db_session = scoped_session(sessionmaker(autoflush=True, bind=engine))
Base.query = db_session.query_property() #Dadurch hat jedes Base - Objekt (also auch ein GeoInfo) ein Attribut query für Abfragen
app = Flask(__name__) #Die Flask-Anwendung
api = Api(app) #Die Flask API
@dataclass #Diese ermoeglicht das Schreiben als JSON mit jsonify
class BinaryWithMetadata(Base):
__tablename__ = 'binary_with_metadata' # Abbildung auf diese Tabelle
id: int
name: str
ext: str
data: str
desc : str
when: str
id = Column(Integer, primary_key=True)
name = Column(Text)
ext = Column(Text)
data = Column(Text)
desc = Column(Text)
when = Column(DateTime, default=func.now())
class BinaryWithMetadataREST(Resource):
def get(self, id):
infos = BinaryWithMetadata.query.get(id)
return jsonify(infos)
def put(self,id):
d = request.get_json(force=True)
print(d)
info = BinaryWithMetadata(name=d['name'], ext=d['ext'], data=d['data'], desc=d['desc'])
db_session.add(info)
db_session.flush()
db_session.commit()
return jsonify(info)
api.add_resource(BinaryWithMetadataREST, '/img_meta/<int:id>')
@app.teardown_appcontext
def shutdown_session(exception=None):
print("Shutdown Session")
db_session.remove()
def init_db():
# Erzeugen der Tabellen für die Klassen, die oben deklariert sind (muss nicht sein, wenn diese schon existiert)
Base.metadata.create_all(bind=engine)
if __name__ == '__main__':
init_db()
app.run(debug=True, port="5001")