Merge branch 'devel'
[qcg-portal.git] / qcg / utils.py
index 43fac0e..57cf23a 100644 (file)
@@ -1,13 +1,18 @@
 # coding=utf-8
+
+from functools import wraps
+
 import os
 import string
 import random
 
+from django.contrib.auth.decorators import login_required
+from django.core.cache import caches
 from django.core.paginator import Paginator
 from django.utils.formats import date_format
 from django.utils.timezone import localtime
+from django.views.decorators.cache import cache_control
 from pyqcg import QCG
-from pyqcg.utils import Credential
 from pyqcg.description import JobDescription
 
 from filex.ftp import FTPOperation
@@ -15,8 +20,9 @@ from qcg import constants
 
 
 from django.utils import encoding
-# for Debugging
-from pprint import pprint
+
+resources_cache = caches['resources']
+
 
 def get_attributes(obj, attrs):
     return {name: getattr(obj, name) for name in attrs if getattr(obj, name) is not None}
@@ -65,7 +71,7 @@ def generate_md_inputfile(params):
     opis=params['note'][:80]
     md_input.append(encoding.smart_str(opis, encoding='ascii', errors='ignore'))
     # Dane kontrolne obliczen
-    md_input.append('SEED=-3059743 PDBREFONE_LETTER MD EXTCONF RESCALE_MODE=2')
+    md_input.append('SEED=-3059743 PDBREF ONE_LETTER MD EXTCONF RESCALE_MODE=2')
     ctl_data='nstep='+str(params['nstep'])+' ntwe='+str(params['ntwe'])
     ctl_data+=' ntwx='+str(params['ntwx'])+' dt='+str(params['dt'])+' damax='+str(params['damax'])+'lang=0 tbf'
     md_input.append('{:<79}&'.format(ctl_data))
@@ -101,8 +107,7 @@ def generate_md_inputfile(params):
 
 def to_job_desc(params, proxy):
     QCG.start()
-    desc = JobDescription(Credential(proxy))
-    desc.sequence=None
+    desc = JobDescription()
 
     direct_map = ('env_variables', 'executable', 'arguments', 'note', 'grant', 'hosts', 'properties', 'queue', 'procs',
                   'wall_time', 'memory', 'memory_per_slot', 'modules', 'input', 'stage_in', 'native', 'notify',
@@ -135,14 +140,8 @@ def to_job_desc(params, proxy):
         desc.set_reservation(params['reservation'])
     if params['watch_output']:
         desc.set_watch_output(params['watch_output'], params['watch_output_pattern'])
-    if params['sequence']:
-        desc.sequence=params['sequence']
 
     # TODO monitoring
-
-    print "Hello from to_job_desc function"
-    pprint(params)
-    print desc.sequence
     
     return desc
 
@@ -154,10 +153,9 @@ def to_form_data(xml):
     QCG.start()
     desc = JobDescription()
     desc.xml_description = xml
-    #desc.sequence=None
 
     direct_map = ('env_variables', 'executable', 'arguments', 'note', 'grant', 'hosts', 'properties', 'queue', 'procs',
-                  'wall_time', 'memory', 'memory_per_slot', 'modules', 'input', 'stage_in', 'native', 'persistent')
+                  'wall_time', 'modules', 'input', 'stage_in', 'native', 'persistent')
 
     params = {}
     for name in direct_map:
@@ -195,8 +193,34 @@ def to_form_data(xml):
         else:
             params['postprocess_type'] = JobDescriptionForm.Process.CMD
             params['postprocess_cmd'] = desc.postprocess
+    if desc.memory:
+        params['memory'] = int(desc.memory)
+    if desc.memory_per_slot:
+        params['memory_per_slot'] = int(desc.memory_per_slot)
 
-    
-    print "Hello from to_form_data function"
-    pprint(params)
     return params
+
+
+def restricted(view):
+    return wraps(view)(cache_control(no_cache=True, must_revalidate=True, no_store=True)(login_required(view)))
+
+
+def cached_resources(proxy):
+    hosts = resources_cache.get('hosts')
+    if hosts is None:
+        # prevent circular import errors
+        from qcg.service import fetch_resources
+
+        hosts, _, applications, modules = map(make_choices, fetch_resources(proxy))
+        resources_cache.set('hosts', hosts)
+        resources_cache.set('applications', applications)
+        resources_cache.set('modules', modules)
+    else:
+        applications = resources_cache.get('applications')
+        modules = resources_cache.get('modules')
+
+    return hosts, applications, modules
+
+
+def make_choices(iterable):
+    return ((None, ''),) + tuple((item, item) for item in sorted(iterable))