Skip to content

Commit

Permalink
Merge pull request #157 from AberystwythSystemsBiology/feature/docume…
Browse files Browse the repository at this point in the history
…nt-deletion

Added ability to remove file.
  • Loading branch information
KeironO authored Feb 5, 2022
2 parents 5c363fb + f37a204 commit df649a0
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 3 deletions.
26 changes: 26 additions & 0 deletions services/web/app/document/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,32 @@ def document_file_lock(id, file_id, tokenuser: UserAccount):
return success_with_content_response(document_file_schema.dump(file))


@api.route("/document/LIMBDOC-<id>/<file_id>/remove", methods=["DELETE"])
@token_required
def document_file_remove(id, file_id, tokenuser: UserAccount):
file = DocumentFile.query.filter_by(id=file_id, document_id=id).first()

if not file:
return {"success": False, "messages": "There's an issue here"}, 417

db.session.delete(file)
db.session.commit()
return success_with_content_response(
document_schema.dump(Document.query.filter_by(id=id).first())
)


@api.route("/document/LIMBDOC-<id>/<file_id>", methods=["GET"])
@token_required
def document_file_view(id, file_id):
file = DocumentFile.query.filter_by(id=file_id, document_id=id).first()

if not file:
return {"success": False, "messages": "There's an issue here"}, 417

return success_with_content_response(document_file_schema.dump(file))


@api.route("/document/LIMBDOC-<id>/<file_id>", methods=["GET"])
@token_required
def document_file_get(id, file_id):
Expand Down
26 changes: 26 additions & 0 deletions services/web/app/document/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,32 @@ def _check_document_name(form, field):
return _check_document_name


def check_file_name(name):
def _check_file_name(form, field):
if field.data != name:
raise ValidationError(
"%s is not the name of the file. Please try again." % (field.data)
)

return _check_file_name


def DocumentFileDeletionForm(name):
class StaticForm(FlaskForm):
submit = SubmitField()

setattr(
StaticForm,
"name",
StringField(
"Please enter %s to continue" % (name),
[DataRequired(), check_file_name(name=name)],
),
)

return StaticForm()


def DocumentLockForm(id):
class StaticForm(FlaskForm):
submit = SubmitField("Submit")
Expand Down
56 changes: 55 additions & 1 deletion services/web/app/document/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@
import io

from . import document
from .forms import DocumentCreationForm, DocumentLockForm, UploadFileForm
from .forms import (
DocumentCreationForm,
DocumentLockForm,
UploadFileForm,
DocumentFileDeletionForm,
)

from ..misc import get_internal_api_header
import requests
Expand Down Expand Up @@ -225,3 +230,52 @@ def view_file(id, file_id):
)
else:
return response.content


@document.route("/LIMBDOC-<id>/file/<file_id>/remove", methods=["GET", "POST"])
@login_required
def remove_file(id, file_id):

document_response = requests.get(
url_for("api.document_view_document", id=id, _external=True),
headers=get_internal_api_header(),
)

if document_response.status_code == 200:

response = requests.get(
url_for("api.document_file_view", id=id, file_id=file_id, _external=True),
headers=get_internal_api_header(),
)

if response.status_code == 200:

form = DocumentFileDeletionForm(response.json()["content"]["name"])

if form.validate_on_submit():
remove_response = requests.delete(
url_for(
"api.document_file_remove",
id=id,
file_id=file_id,
_external=True,
),
headers=get_internal_api_header(),
)

if remove_response.status_code == 200:
flash("Document File Successfully Removed")
return redirect(url_for("document.view", id=id))
else:
flash(remove_response.json()["content"])

return render_template(
"document/file/remove.html",
document=document_response.json()["content"],
file=response.json()["content"],
form=form,
)
else:
return response.content
else:
return document_response.content
32 changes: 32 additions & 0 deletions services/web/app/templates/document/file/remove.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{% extends "template.html" %}

{% block title %}Remove File from LIMBDOC-{{ document.id }}{% endblock %}

{% block body %}
<div class="jumbotron">
<div class="container">
<h1><span class="secondary-heading">
<a href="{{ url_for('document.view', id=document.id) }}">
<i class="fas fa-file"></i> LIMBDOC-{{ document.id }}</span>
</a>
Remove File: {{ file.name }}
</h1>

</div>
</div>
<div class="container">

<div class="alert alert-danger text-center">
<b>Danger:</b> This action cannot be undone. This will permanently delete {{ file.name }}, and remove all associations.
</div>

<form method="POST" action="{{ url_for('document.remove_file', id=document.id, file_id=file.id) }}">
{{ form.csrf_token }}
{{ form_field(form.name) }}
{{ form_field(form.submit) }}
</form>



</div>
{% endblock %}
8 changes: 7 additions & 1 deletion services/web/app/templates/document/view.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ <h2>
</th>
<th>Uploader</th>
<th>Created On</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
Expand All @@ -100,11 +101,16 @@ <h2>
<td><code>{{ file.checksum }}</code></td>
<td>{{ render_author_for_table(file.author) }}</td>
<td>{{ file.created_on }}</td>
<td>
<a href="{{ url_for('document.remove_file', id=document.id, file_id=file.id) }}">
<div class="btn btn-sm btn-danger"><i class="fa fa-trash"></i></div>
</a>
</td>
</tr>
{% endfor %}
{% else %}
<tr>
<td class="full-width" style="text-align: center;" colspan="4">No Data Available.</td>
<td class="full-width" style="text-align: center;" colspan="5">No Data Available.</td>
</tr>
{% endif %}
</tbody>
Expand Down
2 changes: 1 addition & 1 deletion services/web/app/templates/donor/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

{% block body %}
<div class="jumbotron">
<div class="container-fluid">
<div class="container">
<h1><i class="fa fa-user-circle"></i> Donors</h1>

<div class="btn-toolbar form-group" role="toolbar" aria-label="Toolbar with button groups">
Expand Down

0 comments on commit df649a0

Please sign in to comment.