source: TI05-delivery/ows_framework/trunk/ows_server/ows_server/controllers/csml_api.py @ 3536

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/trunk/ows_server/ows_server/controllers/csml_api.py@3536
Revision 3536, 2.1 KB checked in by cbyrom, 12 years ago (diff)

Upgrade the server code to make consistent with newer pylons codebase - v0.9.6.1.
This mainly involves the replacement of the Response object, and associated methods,
with the inbuild, default response object. Typical changes include:

render_response -> render - with required settings on the response object
made before the render call against the response object

Response(...) -> response.write() - for content + response.headers.. = .. for headers info

  • also included the replacement of depricated functions, as highlighted by

the server logging

Line 
1# Copyright (C) 2007 STFC & NERC (Science and Technology Facilities Council).
2# This software may be distributed under the terms of the
3# Q Public License, version 1.0 or later.
4# http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt
5"""
6Basic CSML controller.
7
8@author: Stephen Pascoe
9"""
10
11from ows_server.lib.base import *
12import os
13from ows_server.lib.csml_util import get_csml_doc, CsmlBundle, get_csml_document
14from elementtree import ElementTree as ET
15
16import tempfile, shutil
17
18class CsmlApiController(BaseController):
19    def index(self, file, format):
20        c.dataset = get_csml_doc(file)
21       
22        if format == 'html':
23            return render('csml_describe')
24        else:
25                response.headers['Content-Type'] = 'text/xml'
26            return response.write(ET.tostring(c.dataset.toXML()))
27   
28    def getFeature(self, file, feature, format):
29        d = get_csml_doc(file)
30        f = d.getFeature(feature)
31        if format == 'html':
32            c.feature_obj = f
33            return render('csml_describe_feature')
34        else:
35            r = ET.Element("CSML_API"); f.toXML(r)
36                response.headers['Content-Type'] = format
37            return response.write(ET.tostring(r))
38
39    def subsetToGridSeries(self, file, feature):
40        d = get_csml_doc(file)
41        f = d.getFeature(feature)
42
43        # Get selector parameters from request
44        domain = f.getDomain()
45        sel = {}
46        for ord in domain:
47            if ord in request.params:
48                sel[ord] = request.params[ord].split(',')
49            else:
50                sel[ord] = ()
51
52        # Create the bundle in a temporary directory
53        tmpdir = tempfile.mkdtemp()
54        b = CsmlBundle(tmpdir)
55        b.add(f.subsetToGridSeries(ncpath=b.join('data.nc'), **sel))
56        bundleFile = b.makeBundle(b.join('bundle.zip'))
57
58        # Write bundle to a response object
59        response.headers['Content-Type'] = 'application/octetstream'
60        r = response.write(open(bundleFile))
61        r.headers['Content-disposition'] = 'attachment; filename=bundle.zip'
62
63        # remove tmpdir
64        shutil.rmtree(tmpdir)
65
66        return r
Note: See TracBrowser for help on using the repository browser.