views.py 3.04 KB
Newer Older
jtchilders's avatar
jtchilders committed
1 2
import os,logging,sys
from django.shortcuts import render,get_object_or_404
3
from balsam.argo.html_forms import JobDisplayForm
jtchilders's avatar
jtchilders committed
4 5
logger = logging.getLogger(__name__)

6
from balsam.argo import models
jtchilders's avatar
jtchilders committed
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

# Create your views here.

def index(request):
   argo_jobs = models.ArgoJob.objects.all()
   context = {'argo_jobs': argo_jobs}
   return render(request, 'argo/index.html', context)


def filter(request):
   argo_jobs = models.ArgoJob.objects.all()
   context = {'argo_jobs': argo_jobs }
   return render(request, 'argo/filter.html', context)


def job_display(request,job_num):
   logger.debug('inside job_display')
   argo_job = None
   argo_subjobs = None
   form = None
   if job_num is not None:
      argo_job = models.ArgoJob.objects.get(id=int(job_num))
      argo_subjob_pks = argo_job.get_subjob_pk_list()
      argo_subjobs = models.ArgoSubJob.objects.filter(pk__in=argo_subjob_pks)
      current_subjob_choices = []
      for i in range(len(argo_subjob_pks)):
         current_subjob_choices.append( (i,str(i)))
      if argo_job is not None:
         form = None
         if request.method == 'POST':
            form = JobDisplayForm(current_subjob_choices,request.POST)
            if form.is_valid():
               if 'delete_from_database' in request.POST:
                  message = 'Job Number ' + str(job_num)
                  if request.POST.get('confirm_delete'):
                     argo_job.delete()
                     message += ' Deleted'
                  else:
                     message += ' not deleted, go back and click the check box to confirm.'
                  context = {'message':message}
                  return render(request,'argo/job_deleted.html',context)
               elif 'save_to_database' in request.POST:
                  update_fields = []
                  new_state    = form.cleaned_data['state']
                  if argo_job.state != new_state:
                     argo_job.state = new_state
                     update_fields.append('state')
                  new_group_identifier = form.cleaned_data['group_identifier']
                  if argo_job.group_identifier != new_group_identifier:
                     argo_job.group_identifier = new_group_identifier
                     update_fields.append('group_identifier')
                  new_email = form.cleaned_data['email']
                  if argo_job.email != new_email:
                     argo_job.email = new_email
                     update_fields.append('email')
                  argo_job.save(update_fields=update_fields)
         else: # not a POST, but a GET
            form = JobDisplayForm(current_subjob_choices)
            form.fields['state'].initial              = argo_job.state
            form.fields['group_identifier'].initial   = argo_job.group_identifier
            form.fields['email'].initial              = argo_job.email
            form.fields['current_subjob'].initial     = current_subjob_choices
   context = {'argo_job':argo_job,'job_num': job_num, 'form':form, 'argo_subjobs': argo_subjobs}
   return render(request,'argo/job_display.html',context)