-
Notifications
You must be signed in to change notification settings - Fork 1
/
views.py
131 lines (118 loc) · 4.84 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import os
from datetime import datetime
from zipfile import ZipFile
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect, FileResponse
from django.shortcuts import render
from django.shortcuts import redirect
from .models import Job
from .forms import JobForm
from .utils import JobProcess
from .version import VERSION
from django.core import management
import subprocess
from pathlib import Path
import logging
import traceback
from django.urls import reverse
ROOT = Path(os.path.realpath(__file__)).parent.parent.parent
from .mailer import send_sfu_email
def read_markdown(md):
with open(md) as f:
return f.read()
def index(request):
cwd = Path(os.path.dirname(os.path.realpath(__file__)))
info_csv_help_md = cwd / 'docs' / 'info_csv_help.md'
phylodating_help_md = cwd / 'docs' / 'phylodating_help.md'
unrooted_tree_help_md = cwd / 'docs' / 'unrooted_tree_help.md'
rooted_tree_help_md = cwd / 'docs' / 'rooted_tree_help.md'
stats_csv_help_md = cwd / 'docs' / 'stats_csv_help.md'
data_csv_help_md = cwd / 'docs' / 'data_csv_help.md'
divergence_vs_time_help_md = cwd / 'docs' / 'divergence_vs_time_help.md'
info_csv_help_md = read_markdown(info_csv_help_md)
phylodating_help_md = read_markdown(phylodating_help_md)
unrooted_tree_help_md = read_markdown(unrooted_tree_help_md)
rooted_tree_help_md = read_markdown(rooted_tree_help_md)
stats_csv_help_md = read_markdown(stats_csv_help_md)
data_csv_help_md = read_markdown(data_csv_help_md)
divergence_vs_time_help_md = read_markdown(divergence_vs_time_help_md)
form = JobForm()
version = VERSION
return render(
request,
'jobs/index.html',
{
'form': form,
'version': version,
'info_csv_help': info_csv_help_md,
'phylodating_help': phylodating_help_md,
'unrooted_tree_help': unrooted_tree_help_md,
'rooted_tree_help': rooted_tree_help_md,
'stats_csv_help': stats_csv_help_md,
'data_csv_help': data_csv_help_md,
'divergence_vs_time_help': divergence_vs_time_help_md,
}
)
def results(request):
if request.method == 'POST':
try:
form = JobForm(request.POST, request.FILES)
except Exception as e:
logging.error('JobForm could not be instantiated!')
logging.error(traceback.format_exc())
if form.is_valid():
new_job = form.save()
# This is to call the command via code
# management.call_command('submit_job', new_job.id)
subprocess.Popen(['/usr/local/bin/python3', os.path.join(ROOT, 'manage.py'), 'submit_job', str(new_job.id)])
sender = 'BCCFE_Phylodating'
subject = 'Your phylodating job'
url = request.build_absolute_uri(reverse('phylodating:details', args=[new_job.job_id]))
body = f'Hi there, you recently submitted a job using the BCCFE Phylodating webtool. To check the status of your job and/or download the results please use the following URL:\n\n{url}'
if new_job.email:
send_sfu_email(
sender, new_job.email, subject, body, attachment_list=-1, cc_list=-1
)
return redirect(new_job)
else:
return HttpResponse(f'Form is invalid: {form.errors}')
else:
form = JobForm()
return render(request, 'jobs/index.html', {'form': form})
def details(request, job_id):
job = Job.objects.get(job_id=job_id)
readable_status = job.my_choices_dict[job.status]
alert_suffix = job.bootstrap_alerts[job.status]
context = {
'job': job,
'status': readable_status,
'alert_suffix': alert_suffix
}
return render(request, 'jobs/details.html', context)
def download(request, job_id):
job = Job.objects.get(job_id=job_id)
context = {
'job': job
}
response = HttpResponse(content_type='application/zip')
today = datetime.today().strftime('%Y-%m-%d-%H:%M:%S')
response['Content-Disposition'] = f'attachment; filename="phylodating_{job.job_id}_{today}.zip"'
warnings = []
with ZipFile(response, 'w') as z:
for _file in (
job.data_out.path,
job.rooted_tree_out.path,
job.stats_out.path,
job.plot.path
):
try:
z.write(_file, os.path.basename(_file))
except FileNotFoundError:
warnings.append(f'File not generated: "{_file}"')
return response
def download_sample(request):
current_dir = os.path.dirname(os.path.realpath(__file__))
sample_path = os.path.join(current_dir, 'static', 'phylodating_sample.zip')
zip_file = open(sample_path, 'rb')
response = FileResponse(zip_file, as_attachment=True)#filename='phylodating_sample_data.zip')
return response