diff --git a/documents/resources.py b/documents/resources.py index c6a6b71..d4bbfed 100644 --- a/documents/resources.py +++ b/documents/resources.py @@ -72,6 +72,17 @@ def get_document_data(document_id): else: return jsonify({'message':'unauthorized request.'}), 403 +@documents.route('/', methods=['DELETE']) +@utils.server_error_check +@utils.authorize_user +def delete_document(document_id,payload): + user_id = payload['user_id'] + user_id_from_document_row = str(state_machine.get_document_by_id(document_id).user_id) + if user_id != user_id_from_document_row: + return jsonify({'message':'unauthorized request.'}), 403 + state_machine.delete_document(document_id) + return jsonify({"message":"Document deleted."}), 200 + @documents.route('//share', methods=['POST']) @utils.server_error_check @utils.authorize_user diff --git a/documents/state_machine.py b/documents/state_machine.py index 393ceab..4b196c3 100644 --- a/documents/state_machine.py +++ b/documents/state_machine.py @@ -66,6 +66,11 @@ def update_document_data_by_document_id(document_id, data_from_user): db.session.commit() return {'data updated'} +def delete_document(document_id): + document = db.session.query(Document).filter(Document.id == document_id).first() + db.session.delete(document) + db.session.commit() + def upsert_shared_document(document_id, email): shared_document = SharedDocument( document_id = document_id, diff --git a/models/SharedDocument.py b/models/SharedDocument.py index d753dd1..a29dcf6 100644 --- a/models/SharedDocument.py +++ b/models/SharedDocument.py @@ -8,7 +8,7 @@ class SharedDocument(db.Model): __tablename__ = 'shared_documents' id = db.Column(UUID(as_uuid=True), primary_key=True, unique=True, server_default=sqlalchemy.text("uuid_generate_v4()"),) - document_id = db.Column(UUID(as_uuid=True), sqlalchemy.ForeignKey(Document.id), unique=False, nullable=False) + document_id = db.Column(UUID(as_uuid=True), sqlalchemy.ForeignKey(Document.id, ondelete='CASCADE'), unique=False, nullable=False) email = db.Column(db.String(255), unique=False, nullable=False) public = db.Column(sqlalchemy.Boolean, unique=False, nullable=True) created_at = db.Column(db.DateTime(), nullable=False, server_default=db.func.now())