Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions documents/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ def get_document_data(document_id):
else:
return jsonify({'message':'unauthorized request.'}), 403

@documents.route('/<document_id>', 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('/<document_id>/share', methods=['POST'])
@utils.server_error_check
@utils.authorize_user
Expand Down
5 changes: 5 additions & 0 deletions documents/state_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion models/SharedDocument.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down