Commit e29df255 authored by BAIRE Anthony's avatar BAIRE Anthony
Browse files

add Job.data_dir to deduplicate code

parent b6398dcf
......@@ -50,8 +50,8 @@ def job_response(job, request):
4: 'archived',
5: 'deleted',
6: 'aborting'}
odir = os.path.join(DATASTORE, str(job.id))
files = {f: get_link(job.id, odir, f, request) for f in os.listdir(odir)
job_dir = job.data_dir
files = {f: get_link(job.id, job_dir, f, request) for f in os.listdir(job_dir)
if os.path.isfile(os.path.join(odir,f)) and not f.startswith(".")}
return {str(job.id): {'status': status[job.state],
'files': files
......@@ -105,7 +105,7 @@ def jobs(request):
log.debug('No usable versions')
return HttpResponse("This app is not yet published", status=404)
upload_data(request.FILES.values(), job.id)
upload_data(request.FILES.values(), job)
# run the Job validators
try:
......
......@@ -60,24 +60,24 @@ def get_ssh_data(key):
return fp_encoded, comment
def upload_data(uploaded_files, job_id):
def upload_data(uploaded_files, job):
"""
Upload any data according to a specific job id
Args:
uploaded_files: iterable that yields
django.core.files.uploadedfile.UploadedFile objects
job_id (int): job ID.
job (Job): job
Examples:
>>> upload_data(self.request.FILES.getlist('files'), job.id)
>>> upload_data(self.request.FILES.getlist('files'), job)
Returns:
Nothing
"""
job_dir = os.path.join(settings.DATASTORE, str(job_id))
job_dir = job.data_dir
os.makedirs(job_dir)
for file_data in uploaded_files:
......
from __future__ import unicode_literals
import os
from django.conf import settings
from django.contrib.auth.models import User, AnonymousUser
from django.core.validators import MinLengthValidator, MinValueValidator, \
RegexValidator
......@@ -482,6 +484,13 @@ class Job(TimeStampModel):
"""
return self.state not in (Job.RUNNING, Job.ABORTING)
@property
def data_dir(self):
"""Return the system path to the job data directory"""
return os.path.join(settings.DATASTORE, str(self.id))
# def __str__(self):
# return self.webapp
......
......@@ -656,7 +656,7 @@ class JobDetail(LoginRequiredMixin, DetailView):
if job.state == Job.DONE:
# job is done
# -> read the `allgo.log` file
log_file = os.path.join(dirname, 'allgo.log')
log_file = os.path.join(job.data_dir, 'allgo.log')
try:
with open(log_file, 'r', errors="replace") as log_data:
logs = log_data.read()
......@@ -735,7 +735,7 @@ class JobCreate(SuccessMessageMixin, CreateView):
obj.save()
# Upload files if there are any
upload_data(self.request.FILES.getlist('files'), obj.id)
upload_data(self.request.FILES.getlist('files'), obj)
# start the job
obj.state = Job.WAITING
......@@ -871,7 +871,7 @@ class JobDelete(LoginRequiredMixin, DeleteView):
# delete the data dir if present
# FIXME: if this fail then we have dangling files staying in the way
job_dir = os.path.join(settings.DATASTORE, str(pk))
job_dir = job.data_dir
if os.path.exists(job_dir):
shutil.rmtree(job_dir)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment