source: cows_wps/trunk/cows_wps/controllers/submitter.py @ 6949

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows_wps/trunk/cows_wps/controllers/submitter.py@6949
Revision 6949, 4.5 KB checked in by astephen, 10 years ago (diff)

various fixes to get user picked up and cookies working.

Line 
1import os
2import re
3import glob
4import logging
5import urllib
6import urllib2
7import Cookie
8import datetime
9
10from pylons import request, response, session, tmpl_context as c
11from pylons.controllers.util import abort, redirect_to
12from routes import url_for
13
14
15from cows.exceptions import *
16from cows.pylons.ows_controller import render_ows_exception
17
18from cows_wps.controllers import *
19from cows_wps import utils
20from cows_wps.renderer.ui_renderer import *
21from cows_wps.model.managers import requestManager
22from cows_wps.process_handler.context.process_context import ProcessContext
23from cows_wps.renderer import xml_renderer
24
25from cows_wps.utils.parse_wps_config import wps_config_dict
26from cows_wps.utils.parse_capabilities_config import caps_config_dict
27
28
29from cows_wps.model.managers import requestManager
30
31log = logging.getLogger(__name__)
32
33
34class SubmitterController(BaseController):
35
36
37    def index(self):
38        wps_request_url = str(request.params.getone("wps_request_url"))
39        proc_id = str(request.params.getone("proc_id"))
40
41        async = self._isAsync(proc_id)
42       
43        # If quick process then just forward
44        if async == False:
45#            return urllib.unquote(wps_request_url)
46            return self._submitToWPSAndRedirectToJobViewer(wps_request_url)
47
48        # Now we are only dealing with async requests
49        cost_only_request_url = wps_request_url + "&Costonly=true"
50
51        wps_resp_div = "wps_response_div"
52        resp = '<div>You have requested to run a "%s" job that will be run on the offline batch processing system. These jobs are controlled by a queuing system and it may take some time for your outputs to be produced.<br><br></div>' % proc_id
53
54        resp += '<div id="%s">Estimates of the size of the expected outputs and the duration of the job are being calculated...</div>' % wps_resp_div
55        resp += """<script type="text/javascript">
56
57        function init() {
58                makeWPSRequest('%s', '%s');
59        }
60
61</script>""" % (cost_only_request_url, wps_resp_div)
62
63        submitter_url = url_for(controller='submitter', action='conf')
64        resp += """<br><form id="confirmation" name="confirmation" action="%s" method="GET">
65<input type="hidden" name="wps_request_url" value="%s" />
66<input type="submit" name="submit" id="submit" value="Submit" disabled="disabled" />
67</form>""" % (submitter_url, urllib.unquote(wps_request_url))
68
69        renderer = UIPageRenderer()
70        resp = renderer.render("Confirmation page",
71                               [("Information about your asynchronous job", resp)])
72
73        return resp
74
75
76    def _submitToWPSAndRedirectToJobViewer(self, wps_request_url):
77        "Submits request and gets statusLocation then redirects to job viewer controller."
78        base_url = "http://" + request.url[7:].split("/")[0]
79        full_url = base_url + wps_request_url
80        #return full_url
81
82#        full_url = "http://badc.nerc.ac.uk/data/rules.html"
83        clean_request_url = base_url + urllib.unquote(wps_request_url)
84        req = urllib2.Request(url = clean_request_url)
85        cookies = request.cookies
86
87        expiration = datetime.datetime.now() + datetime.timedelta(days=30)
88
89        for cookie in cookies.items():
90            nc = Cookie.SimpleCookie()
91            (k, v) = cookie
92            log.warn("COOKIE: %s = %s" % (k, v))
93            nc[k] = v
94            nc[k]["domain"] = "ceda-wps1.badc.rl.ac.uk"
95            nc[k]["path"] = "/"
96            nc[k]["expires"] = expiration.strftime("%a, %d-%b-%Y %H:%M:%S PST")
97            req.add_header('Cookie', nc.output())
98
99       # return clean_request_url
100#        getter = urllib.urlopen(clean_request_url)
101        getter = urllib2.urlopen(req)
102        xml = getter.read()
103        getter.close()
104#        return str(xml)
105
106
107        m = re.search('statusLocation="(.+?)"', xml)
108 #       if m: return "Matched:  " + str(m.groups())
109 #       return "BAD"
110        status_url = m.groups()[0]
111
112        jobviewer_url = url_for(controller='jobviewer', status_url = status_url)
113        return redirect_to(jobviewer_url)
114
115
116    def conf(self):
117        "Called once confirmation is complete and just calls WPS and forwards."
118        wps_request_url = str(request.params.getone("wps_request_url"))
119        return self._submitToWPSAndRedirectToJobViewer(wps_request_url)
120       
121
122    def _isAsync(self, proc_id):
123        "Returns boolean by looking up if WPS is async."
124        process_conf = caps_config_dict[proc_id]
125        process_type = process_conf["wps_interface"]["process_type"]
126
127        if process_type.find("async") == 0:
128            return True
129        else:
130            return False
131       
Note: See TracBrowser for help on using the repository browser.