forked from enviPath/enviPy
Download Pathway Functionality (#38)
Co-authored-by: Tim Lorsbach <tim@lorsba.ch> Reviewed-on: enviPath/enviPy#38
This commit is contained in:
@ -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):
|
||||
|
||||
@ -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}'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user