This commit is contained in:
Андрей Дувакин 2025-01-05 11:44:35 +05:00
parent ea3bdafaba
commit 301692dd35
2 changed files with 175 additions and 6 deletions

View File

@ -12,6 +12,8 @@ class Post(base):
id = Column(Integer, primary_key=True, autoincrement=True)
title = Column(VARCHAR(100))
users = relationship('User', back_populates='post')
class Department(base):
__tablename__ = 'departments'
@ -41,6 +43,10 @@ class User(base):
post_id = Column(Integer, ForeignKey('posts.id'))
department_id = Column(Integer, ForeignKey('departments.id'))
post = relationship('Post', back_populates='users')
comments = relationship('Comment', back_populates='user')
class DepartmentUser(base):
__tablename__ = 'department_users'
@ -86,6 +92,7 @@ class Comment(base):
document_id = Column(Integer, ForeignKey('documents.id'))
document = relationship('Document', back_populates='comments')
user = relationship('User', back_populates='comments')
class MaterialType(base):

View File

@ -20,7 +20,11 @@ def login():
user = session.query(User).filter(User.email == data['name'], User.password == data['password']).first()
if user is None:
return Response(status=403)
return {
'timestamp': int(datetime.datetime.now().timestamp()),
'message': 'Неправильные авторизационные даныне',
'errorCode': 1304
}, 403
token = jwt.encode({
'sub': user.email,
@ -28,7 +32,11 @@ def login():
}, app.config['SECRET_KEY'], algorithm='HS256')
return jsonify({'token': token})
except Exception:
return Response(status=400)
return {
'timestamp': int(datetime.datetime.now().timestamp()),
'message': 'Неправильно сформированный запрос',
'errorCode': 1400
}, 400
@app.route('/api/v1/Documents', methods=['GET'])
@ -37,14 +45,26 @@ def get_documents():
token = request.headers.get('Authorization')
if not token:
return Response(403)
return {
'timestamp': int(datetime.datetime.now().timestamp()),
'message': 'Неправильные авторизационные даныне',
'errorCode': 1304
}, 403
try:
data = jwt.decode(token, app.config['SECRET_KEY'], algorithms=['HS256'])
except jwt.ExpiredSignatureError:
return jsonify({'message': 'Token has expired!'}), 401
return {
'timestamp': int(datetime.datetime.now().timestamp()),
'message': 'Неправильные авторизационные даныне',
'errorCode': 1401
}, 401
except jwt.InvalidTokenError:
return jsonify({'message': 'Invalid token!'}), 401
return {
'timestamp': int(datetime.datetime.now().timestamp()),
'message': 'Неправильные авторизационные даныне',
'errorCode': 1401
}, 403
resp = []
@ -70,7 +90,149 @@ def get_documents():
return resp
except Exception:
return Response(status=400)
return {
'timestamp': int(datetime.datetime.now().timestamp()),
'message': 'Неправильно сформированный запрос',
'errorCode': 1400
}, 400
@app.route('/api/v1/Document/<int:documentId>/Comments', methods=['GET'])
def get_comments(documentId):
try:
token = request.headers.get('Authorization')
if not token:
return {
'timestamp': int(datetime.datetime.now().timestamp()),
'message': 'Неправильные авторизационные даныне',
'errorCode': 1304
}, 403
try:
data = jwt.decode(token, app.config['SECRET_KEY'], algorithms=['HS256'])
except jwt.ExpiredSignatureError:
return {
'timestamp': int(datetime.datetime.now().timestamp()),
'message': 'Неправильные авторизационные даныне',
'errorCode': 1401
}, 401
except jwt.InvalidTokenError:
return {
'timestamp': int(datetime.datetime.now().timestamp()),
'message': 'Неправильные авторизационные даныне',
'errorCode': 1401
}, 403
resp = []
with connect() as session:
comments = session.query(Comment).filter(Comment.document_id == documentId).all()
if not comments:
return {
'timestamp': int(datetime.datetime.now().timestamp()),
'message': 'Документ не найден',
'errorCode': 1404
}, 404
for comment in comments:
resp.append({
'id': comment.id,
'document_id': comment.document_id,
'text': comment.text,
'date_created': comment.date_created,
'date_updated': comment.date_updated,
'author': {
'name': f'{comment.user.last_name} {comment.user.first_name}',
'position': comment.user.post.title,
}
})
return resp
except Exception:
return {
'timestamp': int(datetime.datetime.now().timestamp()),
'message': 'Неправильно сформированный запрос',
'errorCode': 1400
}, 400
@app.route('/api/v1/Document/<int:documentId>/Comments', methods=['POST'])
def create_comment(documentId):
try:
token = request.headers.get('Authorization')
if not token:
return {
'timestamp': int(datetime.datetime.now().timestamp()),
'message': 'Неправильные авторизационные даныне',
'errorCode': 1304
}, 403
try:
data = jwt.decode(token, app.config['SECRET_KEY'], algorithms=['HS256'])
except jwt.ExpiredSignatureError:
return {
'timestamp': int(datetime.datetime.now().timestamp()),
'message': 'Неправильные авторизационные даныне',
'errorCode': 1401
}, 401
except jwt.InvalidTokenError:
return {
'timestamp': int(datetime.datetime.now().timestamp()),
'message': 'Неправильные авторизационные даныне',
'errorCode': 1401
}, 403
resp = []
comment_data = request.json
if not comment_data:
return {
'timestamp': int(datetime.datetime.now().timestamp()),
'message': 'Неправильно сформированный запрос',
'errorCode': 1400
}, 400
with connect() as session:
document = session.query(Document).filter(Document.id == documentId).first()
if document is None:
return {
'timestamp': int(datetime.datetime.now().timestamp()),
'message': 'Документ не найден',
'errorCode': 1404
}, 404
user = session.query(User).filter(User.id == comment_data['user_id']).first()
if user is None:
return {
'timestamp': int(datetime.datetime.now().timestamp()),
'message': 'Пользователь не найден',
'errorCode': 1404
}, 404
comment = Comment(
text=comment_data['text'],
document_id=document.id,
date_created=datetime.datetime.now(),
date_updated=datetime.datetime.now(),
user_id=user.id,
)
session.add(comment)
session.commit()
return Response(status=200)
except Exception:
return {
'timestamp': int(datetime.datetime.now().timestamp()),
'message': 'Неправильно сформированный запрос',
'errorCode': 1400
}, 400
@app.route('/protected', methods=['GET'])