._.
This commit is contained in:
parent
90000926e3
commit
ea3bdafaba
@ -56,6 +56,8 @@ class DocumentCategory(base):
|
|||||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
title = Column(VARCHAR(100))
|
title = Column(VARCHAR(100))
|
||||||
|
|
||||||
|
documents = relationship('Document', back_populates='category')
|
||||||
|
|
||||||
|
|
||||||
class Document(base):
|
class Document(base):
|
||||||
__tablename__ = 'documents'
|
__tablename__ = 'documents'
|
||||||
@ -67,6 +69,10 @@ class Document(base):
|
|||||||
|
|
||||||
category_id = Column(Integer, ForeignKey('document_categories.id'))
|
category_id = Column(Integer, ForeignKey('document_categories.id'))
|
||||||
|
|
||||||
|
category = relationship('DocumentCategory', back_populates='documents')
|
||||||
|
|
||||||
|
comments = relationship('Comment', back_populates='document')
|
||||||
|
|
||||||
|
|
||||||
class Comment(base):
|
class Comment(base):
|
||||||
__tablename__ = 'comments'
|
__tablename__ = 'comments'
|
||||||
@ -79,6 +85,8 @@ class Comment(base):
|
|||||||
user_id = Column(Integer, ForeignKey('users.id'))
|
user_id = Column(Integer, ForeignKey('users.id'))
|
||||||
document_id = Column(Integer, ForeignKey('documents.id'))
|
document_id = Column(Integer, ForeignKey('documents.id'))
|
||||||
|
|
||||||
|
document = relationship('Document', back_populates='comments')
|
||||||
|
|
||||||
|
|
||||||
class MaterialType(base):
|
class MaterialType(base):
|
||||||
__tablename__ = 'material_types'
|
__tablename__ = 'material_types'
|
||||||
|
|||||||
90
API/main.py
90
API/main.py
@ -1,8 +1,96 @@
|
|||||||
from data.connect import init_db, connect
|
import datetime
|
||||||
|
import random
|
||||||
|
|
||||||
|
import jwt
|
||||||
|
|
||||||
|
from API.data.connect import Document, DocumentCategory, Comment
|
||||||
|
from data.connect import init_db, connect, User
|
||||||
|
from flask import Flask, Response, request, jsonify
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
app.config['SECRET_KEY'] = 'jyeraghueykgaeyugheaughkawefy'
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/api/v1/SignIn', methods=['POST'])
|
||||||
|
def login():
|
||||||
|
try:
|
||||||
|
data = request.json
|
||||||
|
|
||||||
|
with connect() as session:
|
||||||
|
user = session.query(User).filter(User.email == data['name'], User.password == data['password']).first()
|
||||||
|
|
||||||
|
if user is None:
|
||||||
|
return Response(status=403)
|
||||||
|
|
||||||
|
token = jwt.encode({
|
||||||
|
'sub': user.email,
|
||||||
|
'exp': datetime.datetime.utcnow() + datetime.timedelta(minutes=180)
|
||||||
|
}, app.config['SECRET_KEY'], algorithm='HS256')
|
||||||
|
return jsonify({'token': token})
|
||||||
|
except Exception:
|
||||||
|
return Response(status=400)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/api/v1/Documents', methods=['GET'])
|
||||||
|
def get_documents():
|
||||||
|
try:
|
||||||
|
token = request.headers.get('Authorization')
|
||||||
|
|
||||||
|
if not token:
|
||||||
|
return Response(403)
|
||||||
|
|
||||||
|
try:
|
||||||
|
data = jwt.decode(token, app.config['SECRET_KEY'], algorithms=['HS256'])
|
||||||
|
except jwt.ExpiredSignatureError:
|
||||||
|
return jsonify({'message': 'Token has expired!'}), 401
|
||||||
|
except jwt.InvalidTokenError:
|
||||||
|
return jsonify({'message': 'Invalid token!'}), 401
|
||||||
|
|
||||||
|
resp = []
|
||||||
|
|
||||||
|
with connect() as session:
|
||||||
|
documents = session.query(Document).all()
|
||||||
|
|
||||||
|
for document in documents:
|
||||||
|
# category = session.query(DocumentCategory).filter(DocumentCategory.id == document.category_id).first()
|
||||||
|
# comments = session.query(Comment).filter(Comment.document_id == document.id).all()
|
||||||
|
|
||||||
|
category = document.category
|
||||||
|
comments = document.comments
|
||||||
|
|
||||||
|
resp.append({
|
||||||
|
'id': document.id,
|
||||||
|
'title': document.title,
|
||||||
|
'date_created': document.date_created,
|
||||||
|
'date_updated': document.date_updated,
|
||||||
|
'category': document.category.title,
|
||||||
|
'has_comments': True if len(document.comments) else False
|
||||||
|
})
|
||||||
|
|
||||||
|
return resp
|
||||||
|
|
||||||
|
except Exception:
|
||||||
|
return Response(status=400)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/protected', methods=['GET'])
|
||||||
|
def protected():
|
||||||
|
token = request.headers.get('Authorization')
|
||||||
|
if not token:
|
||||||
|
return jsonify({'message': 'Token is missing!'}), 403
|
||||||
|
|
||||||
|
try:
|
||||||
|
data = jwt.decode(token, app.config['SECRET_KEY'], algorithms=['HS256'])
|
||||||
|
return jsonify({'message': f'Welcome {data["sub"]}!'})
|
||||||
|
except jwt.ExpiredSignatureError:
|
||||||
|
return jsonify({'message': 'Token has expired!'}), 401
|
||||||
|
except jwt.InvalidTokenError:
|
||||||
|
return jsonify({'message': 'Invalid token!'}), 401
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
init_db()
|
init_db()
|
||||||
|
app.run()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user