Download Pathway Functionality (#38)

Co-authored-by: Tim Lorsbach <tim@lorsba.ch>
Reviewed-on: enviPath/enviPy#38
This commit is contained in:
2025-07-31 01:30:16 +12:00
parent aec61151ce
commit 79b4b1586c
5 changed files with 96 additions and 7 deletions

View File

@ -920,6 +920,46 @@ class Pathway(EnviPathModel, AliasMixin, ScenarioMixin):
return json.dumps(res) return json.dumps(res)
def to_csv(self) -> str:
import csv
import io
rows = []
rows.append([
'SMILES',
'name',
'depth',
'probability',
'rule_names',
'rule_ids',
'parent_smiles',
])
for n in self.nodes.order_by('depth'):
cs = n.default_node_label
row = [cs.smiles, cs.name, n.depth]
edges = self.edges.filter(end_nodes__in=[n])
if len(edges):
for e in edges:
_row = row.copy()
_row.append(e.kv.get('probability'))
_row.append(','.join([r.name for r in e.edge_label.rules.all()]))
_row.append(','.join([r.url for r in e.edge_label.rules.all()]))
_row.append(e.start_nodes.all()[0].default_node_label.smiles)
rows.append(_row)
else:
row += [None, None, None, None]
rows.append(row)
buffer = io.StringIO()
writer = csv.writer(buffer)
writer.writerows(rows)
buffer.seek(0)
return buffer.getvalue()
@staticmethod @staticmethod
@transaction.atomic @transaction.atomic
def create(package: 'Package', smiles: str, name: Optional[str] = None, description: Optional[str] = None): def create(package: 'Package', smiles: str, name: Optional[str] = None, description: Optional[str] = None):

View File

@ -1254,6 +1254,14 @@ def package_pathway(request, package_uuid, pathway_uuid):
if request.GET.get("last_modified", False): if request.GET.get("last_modified", False):
return JsonResponse({'modified': current_pathway.modified.strftime('%Y-%m-%d %H:%M:%S')}) return JsonResponse({'modified': current_pathway.modified.strftime('%Y-%m-%d %H:%M:%S')})
if request.GET.get("download", False) == "true":
filename = f"{current_pathway.name.replace(' ', '_')}_{current_pathway.uuid}.csv"
csv_pw = current_pathway.to_csv()
response = HttpResponse(csv_pw, content_type='text/csv')
response['Content-Disposition'] = f'attachment; filename="{filename}"'
return response
context = get_base_context(request) context = get_base_context(request)
context['title'] = f'enviPath - {current_package.name} - {current_pathway.name}' context['title'] = f'enviPath - {current_package.name} - {current_pathway.name}'

View File

@ -8,9 +8,14 @@
<i class="glyphicon glyphicon-plus"></i> Add Reaction</a> <i class="glyphicon glyphicon-plus"></i> Add Reaction</a>
</li> </li>
<li role="separator" class="divider"></li> <li role="separator" class="divider"></li>
<li>
<a class="button" data-toggle="modal" data-target="#download_pathway_modal">
<i class="glyphicon glyphicon-floppy-save"></i> Download Pathway</a>
</li>
<li role="separator" class="divider"></li>
<li> <li>
<a class="button" data-toggle="modal" data-target="#edit_pathway_modal"> <a class="button" data-toggle="modal" data-target="#edit_pathway_modal">
<i class="glyphicon glyphicon-plus"></i> Edit Pathway</a> <i class="glyphicon glyphicon-edit"></i> Edit Pathway</a>
</li> </li>
{# <li>#} {# <li>#}
{# <a class="button" data-toggle="modal" data-target="#add_pathway_edge_modal">#} {# <a class="button" data-toggle="modal" data-target="#add_pathway_edge_modal">#}

View File

@ -0,0 +1,35 @@
{% load static %}
<!-- Download Pathway -->
<div id="download_pathway_modal" class="modal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title">Download Pathway</h3>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
By clicking on Download the Pathway will be converted into a CSV and directly downloaded.
<form id="download-pathway-modal-form" accept-charset="UTF-8" action="{{ pathway.url }}"
data-remote="true" method="GET">
<input type="hidden" name="download" value="true"/>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id="download-pathway-modal-submit">Download</button>
</div>
</div>
</div>
</div>
<script>
$(function () {
$('#download-pathway-modal-submit').click(function (e) {
e.preventDefault();
$('#download-pathway-modal-form').submit();
});
})
</script>

View File

@ -50,6 +50,7 @@
{% block action_modals %} {% block action_modals %}
{% include "modals/objects/add_pathway_node_modal.html" %} {% include "modals/objects/add_pathway_node_modal.html" %}
{% include "modals/objects/add_pathway_edge_modal.html" %} {% include "modals/objects/add_pathway_edge_modal.html" %}
{% include "modals/objects/download_pathway_modal.html" %}
{% include "modals/objects/edit_pathway_modal.html" %} {% include "modals/objects/edit_pathway_modal.html" %}
{% include "modals/objects/delete_pathway_node_modal.html" %} {% include "modals/objects/delete_pathway_node_modal.html" %}
{% include "modals/objects/delete_pathway_edge_modal.html" %} {% include "modals/objects/delete_pathway_edge_modal.html" %}