diff --git a/epdb/models.py b/epdb/models.py index 72d6466d..dcd7cad2 100644 --- a/epdb/models.py +++ b/epdb/models.py @@ -920,6 +920,46 @@ class Pathway(EnviPathModel, AliasMixin, ScenarioMixin): 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 @transaction.atomic def create(package: 'Package', smiles: str, name: Optional[str] = None, description: Optional[str] = None): diff --git a/epdb/views.py b/epdb/views.py index a7f085ce..e635fad6 100644 --- a/epdb/views.py +++ b/epdb/views.py @@ -1254,6 +1254,14 @@ def package_pathway(request, package_uuid, pathway_uuid): if request.GET.get("last_modified", False): 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['title'] = f'enviPath - {current_package.name} - {current_pathway.name}' diff --git a/templates/actions/objects/pathway.html b/templates/actions/objects/pathway.html index 399adaa5..b1083fcf 100644 --- a/templates/actions/objects/pathway.html +++ b/templates/actions/objects/pathway.html @@ -9,19 +9,24 @@