better user data validation for gridftp upload view
[qcg-portal.git] / qcg / views.py
index c34af9b..adc4676 100644 (file)
@@ -1,22 +1,25 @@
 # coding=utf-8
 from datetime import datetime, timedelta
+
 from django.conf import settings
+from django.contrib import messages
 from django.contrib.auth import REDIRECT_FIELD_NAME
 from django.contrib.auth.decorators import login_required
+from django.core.exceptions import SuspiciousOperation
 from django.core.urlresolvers import reverse
 from django.db.models import Q
 from django.http import HttpResponse, QueryDict
-from django.shortcuts import render, get_object_or_404
+from django.shortcuts import render, get_object_or_404, redirect
+from django.utils.html import format_html
 from django.utils.http import urlencode
 from django.utils.timezone import UTC
 from django_openid_auth.views import make_consumer
 from openid.extensions import ax
-from pyqcg import QCG
-from pyqcg.description import JobDescription
-from pyqcg.utils import Credential
 
+from filex.forms import HostPathNameForm, RenameForm, ArchiveForm, HostPathForm
 from qcg.forms import FiltersForm, ColumnsForm, JobDescriptionForm, EnvFormSet
-from qcg.utils import update_user_data, paginator_context
+from qcg.utils import paginator_context
+from qcg.service import update_user_data, submit_job
 
 
 def index(request):
@@ -56,8 +59,7 @@ def parse_date(string):
 
 @login_required
 def jobs_list(request):
-    # QCG.start()
-    # update_user_data(request.user, request.session['proxy'])
+    update_user_data(request.user, request.session['proxy'])
 
     tasks = request.user.tasks.order_by('-job__submission_time', '-submission_time') \
         .select_related('job').prefetch_related('allocations__nodes')
@@ -153,66 +155,47 @@ def task_details(request, job_id, task_id):
 @login_required
 def job_new(request):
     if request.method == 'POST':
-        QCG.start()
-
         form = JobDescriptionForm(request.POST)
         env_formset = EnvFormSet(request.POST)
 
         if form.is_valid() and env_formset.is_valid():
-            print form.cleaned_data
-
-            desc = JobDescription(Credential(request.session['proxy']))
-
-            application = form.cleaned_data['application']
-            if '/' in application:
-                app, ver = application.split('1', 1)
-                desc.application.set_name(app)
-                desc.application.set_version(ver)
-            else:
-                desc.application.set_name(application)
-
-            # TODO script
-            # TODO executable
-            desc.arguments = form.cleaned_data['arguments']
-            desc.note = form.cleaned_data['note']
-            desc.grant = form.cleaned_data['grant']
-            desc.host_candidates = [(form.cleaned_data['host'], 'type?')]  # FIXME WTF ?!?
-            desc.properties = form.cleaned_data['properties']
-            # TODO queue
-            # TODO procs
-            # TODO nodes
-            desc.wall_time = timedelta(seconds=form.cleaned_data['wall_time'])  # FIXME odpowiedni typ pola
-            desc.memory = form.cleaned_data['memory']
-            desc.memory_per_slot = form.cleaned_data['memory_per_slot']
-            desc.variable = [(env['name'], env['value']) for env in env_formset.cleaned_data if not env['DELETE']]
-            desc.modules = form.cleaned_data['modules']
-            desc.natives = form.cleaned_data['native']
-            desc.reservation = ("LOCAL", form.cleaned_data['reservation'])
-            # TODO input
-            # TODO stage_in
-            # TODO stage_out
-            # TODO monitoring
-            # TODO notify_type
-            # TODO notify_address
-            # TODO watch_output_type
-            # TODO watch_output_address
-            # TODO watch_output_pattern
-            # TODO preprocess_type
-            # TODO preprocess_cmd
-            # TODO preprocess_script
-            # TODO postprocess_type
-            # TODO postprocess_cmd
-            # TODO postprocess_script
-            desc.persistent = form.cleaned_data['persistent']
-
-            for prop in ('arguments', 'note', 'grant', 'host_candidates', 'wall_time', 'memory',
-                         'memory_per_slot', 'properties', 'variable', 'modules', 'natives', 'persistent', 'reservation'):
-                print prop, type(getattr(desc, prop)), repr(getattr(desc, prop))
-
-            print desc.xml_description
+            params = form.cleaned_data
+            params['env_variables'] = [(env['name'], env['value'])
+                                       for env in env_formset.cleaned_data if env and not env['DELETE']]
+
+            job_id = submit_job(params, request.session['proxy'])
+
+            messages.success(request,
+                             format_html('<span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span> '
+                                         'Zlecono zadanie <em>{}</em>.', job_id))
 
+            return redirect('jobs')
+
+        print repr(form.errors)
+        print repr(env_formset.errors)
     else:
         form = JobDescriptionForm()
         env_formset = EnvFormSet()
 
-    return render(request, 'qcg/job_new.html', {'form': form, 'env_formset': env_formset})
+    errors = form.errors or (env_formset.is_bound and not env_formset.is_valid)
+
+    return render(request, 'qcg/job_new.html', {'form': form, 'env_formset': env_formset, 'errors': errors})
+
+
+@login_required
+def gridftp(request):
+    return render(request, 'qcg/gridftp.html',
+                  {'new_dir_form': HostPathNameForm(), 'rename_form': RenameForm(),  'archive_form': ArchiveForm()})
+
+
+@login_required
+def gridftp_upload(request):
+    form = HostPathForm(request.GET)
+
+    if not form.is_valid():
+        raise SuspiciousOperation('Invalid parameters for `gridftp_upload`!')
+
+    return render(request, 'qcg/gridftp_upload.html',
+                  {'url': reverse('filex:upload') + '?' + urlencode(form.cleaned_data),
+                   'host': form.cleaned_data['host'], 'path': form.cleaned_data['path'],
+                   'sep': '/' if form.cleaned_data['path'].startswith('~') else ''})