[cig-commits] r7786 - in cs/pythia/trunk/cig/web/seismo: events stations

leif at geodynamics.org leif at geodynamics.org
Wed Aug 8 02:08:48 PDT 2007


Author: leif
Date: 2007-08-08 02:08:47 -0700 (Wed, 08 Aug 2007)
New Revision: 7786

Added:
   cs/pythia/trunk/cig/web/seismo/stations/components.py
   cs/pythia/trunk/cig/web/seismo/stations/support.py
Removed:
   cs/pythia/trunk/cig/web/seismo/stations/urls.py
   cs/pythia/trunk/cig/web/seismo/stations/views.py
Modified:
   cs/pythia/trunk/cig/web/seismo/events/__init__.py
   cs/pythia/trunk/cig/web/seismo/events/components.py
   cs/pythia/trunk/cig/web/seismo/stations/__init__.py
Log:
OOP-ized 'stations'.


Modified: cs/pythia/trunk/cig/web/seismo/events/__init__.py
===================================================================
--- cs/pythia/trunk/cig/web/seismo/events/__init__.py	2007-08-08 05:58:48 UTC (rev 7785)
+++ cs/pythia/trunk/cig/web/seismo/events/__init__.py	2007-08-08 09:08:47 UTC (rev 7786)
@@ -0,0 +1,2 @@
+
+from EventBrowser import EventBrowser

Modified: cs/pythia/trunk/cig/web/seismo/events/components.py
===================================================================
--- cs/pythia/trunk/cig/web/seismo/events/components.py	2007-08-08 05:58:48 UTC (rev 7785)
+++ cs/pythia/trunk/cig/web/seismo/events/components.py	2007-08-08 09:08:47 UTC (rev 7786)
@@ -47,6 +47,7 @@
 
 from cig.seismo.events import CMTSolution
 from opal import forms
+from opal.components import WebComponent
 from opal.http import HttpResponseRedirect
 from opal.shortcuts import render_to_response
 from opal.template.context import RequestContext
@@ -59,6 +60,7 @@
 
 
     def __init__(self, home):
+        WebComponent.__init__()
 
         self.home = home # '/specfem3dglobe/events/'
         
@@ -88,27 +90,30 @@
             )
 
 
-    def eventDetail(self, request):
+    def eventDetail(self, request, object_id):
         return self.genericObjectDetail(
             request,
-            queryset = Event.user_objects.all()
+            object_id = object_id,
+            queryset = Event.user_objects.all(),
             )
 
 
-    def editEvent(self, request):
+    def editEvent(self, request, object_id):
         return self.genericUpdateObject(
             request,
+            object_id = object_id,
             model = Event,
             post_save_redirect = self.home,
-            follow: { 'user': False },
+            follow = { 'user': False },
             )
 
 
-    def deleteEvent(self, request):
+    def deleteEvent(self, request, object_id):
         return self.genericDeleteObject(
             request,
-            model: Event,
-            post_delete_redirect: self.home,
+            object_id = object_id,
+            model = Event,
+            post_delete_redirect = self.home,
             )
 
 
@@ -201,27 +206,29 @@
 
     # views -- sources
 
-    def sourceDetail(self, request):
+    def sourceDetail(self, request, object_id):
         return self.genericObjectDetail(
             request,
             queryset = Source.objects.all()
             )
 
 
-    def editSource(self, request):
+    def editSource(self, request, object_id):
         return self.genericUpdateObject(
             request,
+            object_id = object_id,
             model = Source,
             post_save_redirect = self.home,
-            follow: { 'user': False },
+            follow = { 'user': False },
             )
 
 
-    def deleteSource(self, request):
+    def deleteSource(self, request, object_id):
         return self.genericDeleteObject(
             request,
-            model: Source,
-            post_delete_redirect: self.home,
+            object_id = object_id,
+            model = Source,
+            post_delete_redirect = self.home,
             )
 
 

Modified: cs/pythia/trunk/cig/web/seismo/stations/__init__.py
===================================================================
--- cs/pythia/trunk/cig/web/seismo/stations/__init__.py	2007-08-08 05:58:48 UTC (rev 7785)
+++ cs/pythia/trunk/cig/web/seismo/stations/__init__.py	2007-08-08 09:08:47 UTC (rev 7786)
@@ -0,0 +1,2 @@
+
+from StationBrowser import StationBrowser

Added: cs/pythia/trunk/cig/web/seismo/stations/components.py
===================================================================
--- cs/pythia/trunk/cig/web/seismo/stations/components.py	2007-08-08 05:58:48 UTC (rev 7785)
+++ cs/pythia/trunk/cig/web/seismo/stations/components.py	2007-08-08 09:08:47 UTC (rev 7786)
@@ -0,0 +1,186 @@
+#!/usr/bin/env python
+#
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+#
+#                       cig.web.seismo.events
+#
+# Copyright (c) 2007, California Institute of Technology
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+#
+#    * Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+#
+#    * Redistributions in binary form must reproduce the above
+#    copyright notice, this list of conditions and the following
+#    disclaimer in the documentation and/or other materials provided
+#    with the distribution.
+#
+#    * Neither the name of the California Institute of Technology nor
+#    the names of its contributors may be used to endorse or promote
+#    products derived from this software without specific prior
+#    written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+#
+
+
+from models import Station, StationList, StationNetwork
+import support
+
+from opal.components import WebComponent
+from opal.http import HttpResponseRedirect
+from opal.shortcuts import get_object_or_404, render_to_response
+from opal.template.context import RequestContext
+
+
+class StationBrowser(WebComponent):
+
+
+    models = [Station, StationList, StationNetwork]
+
+
+    def __init__(self, home):
+        WebComponent.__init__()
+
+        self.home = home # '/specfem3dglobe/stations/'
+        
+        from opal.contrib.auth.decorators import login_required
+        self.urlpatterns = self.patterns(
+            (r'^$', login_required(self.index)),
+            (r'^create/$', login_required(self.createStationList)),
+            (r'^upload/$', login_required(self.uploadStationList)),
+            (r'^(?P<object_id>\d+)/$', self.stationListDetail),
+            (r'^(?P<object_id>\d+)/edit/$', self.editStationList),
+            (r'^(?P<object_id>\d+)/delete/$', self.deleteStationList),
+            (r'^(?P<object_id>\d+)/gearth\.kml$', self.stationListDetailGEarth),
+            )
+
+        return
+
+
+    def index(request):
+        station_lists = StationList.objects.filter(user__exact=request.user)
+        return render_to_response('stations/index.html',
+                                  {'station_lists': station_lists },
+                                  RequestContext(request, {}))
+
+
+    def createStationList(request):
+        from os.path import dirname
+        from pkg_resources import resource_stream
+
+        actionChoices = (
+            (0, 'Create an empty list of stations.'),
+            (1, 'Create a default list of stations.'),
+            )
+        class Manipulator(forms.Manipulator):
+            def __init__(self):
+                super(Manipulator, self).__init__()
+                self.fields = [
+                    forms.TextField('name', maxlength=100, is_required=True),
+                    forms.RadioSelectField('action', choices=actionChoices, is_required=True)
+                    ]
+
+        manipulator = Manipulator()
+
+        if request.method == 'POST':
+            new_data = request.POST.copy()
+            errors = manipulator.get_validation_errors(new_data)
+            if not errors:
+                manipulator.do_html2python(new_data)
+                stationList = StationList.objects.create(user = request.user,
+                                                         name = new_data['name'])
+                if new_data['action'] == "1":
+                    stream = resource_stream(__name__, "STATIONS")
+                    support.parse_station_list(stationList, stream)
+                url = "%s/%i/" % (dirname(dirname(request.path)), stationList.id)
+                return HttpResponseRedirect(url)
+        else:
+            errors = {}
+            new_data = {'action': 1}
+
+        form = forms.FormWrapper(manipulator, new_data, errors)
+        return render_to_response('stations/stationlist_create.html',
+                                  {'form': form},
+                                  RequestContext(request, {}))
+
+
+    def uploadStationList(request):
+        from os.path import dirname
+
+        manipulator = support.UploadStationListManipulator()
+
+        if request.method == 'POST':
+            new_data = request.POST.copy()
+            new_data.update(request.FILES)
+            errors = manipulator.get_validation_errors(new_data)
+            if not errors:
+                manipulator.do_html2python(new_data)
+                stationList = manipulator.save(new_data, request.user)
+                url = "%s/%i/" % (dirname(dirname(request.path)), stationList.id)
+                return HttpResponseRedirect(url)
+        else:
+            errors = new_data = {}
+
+        form = forms.FormWrapper(manipulator, new_data, errors)
+        return render_to_response('stations/stationlist_upload.html',
+                                  {'form': form},
+                                  RequestContext(request, {}))
+
+
+    def stationListDetail(self, request, object_id):
+        return self.genericObjectDetail(
+            request,
+            object_id = object_id,
+            queryset = StationList.objects.all(),
+            )
+
+
+    def editStationList(self, request, object_id):
+        return self.genericUpdateObject(
+            request,
+            object_id = object_id,
+            model = StationList,
+            post_save_redirect = self.home,
+            follow = { 'user': False },
+            )
+
+
+    def deleteStationList(self, request, object_id):
+        return self.genericDeleteObject(
+            request,
+            object_id = object_id,
+            model = StationList,
+            post_delete_redirect = self.home,
+            )
+
+
+    def stationListDetailGEarth(self, request, object_id):
+        stationList = get_object_or_404(StationList, id=object_id)
+        return support.station_list_gearth(
+            request,
+            object_id = object_id,
+            queryset = stationList.station_set.all(),
+            extra_context = {'name': stationList.name}
+            )
+
+
+# end of file


Property changes on: cs/pythia/trunk/cig/web/seismo/stations/components.py
___________________________________________________________________
Name: svn:executable
   + *

Copied: cs/pythia/trunk/cig/web/seismo/stations/support.py (from rev 7785, cs/pythia/trunk/cig/web/seismo/stations/views.py)
===================================================================
--- cs/pythia/trunk/cig/web/seismo/stations/views.py	2007-08-08 05:58:48 UTC (rev 7785)
+++ cs/pythia/trunk/cig/web/seismo/stations/support.py	2007-08-08 09:08:47 UTC (rev 7786)
@@ -0,0 +1,136 @@
+#!/usr/bin/env python
+#
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+#
+#                      cig.web.seismo.stations
+#
+# Copyright (c) 2006, California Institute of Technology
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+#
+#    * Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+#
+#    * Redistributions in binary form must reproduce the above
+#    copyright notice, this list of conditions and the following
+#    disclaimer in the documentation and/or other materials provided
+#    with the distribution.
+#
+#    * Neither the name of the California Institute of Technology nor
+#    the names of its contributors may be used to endorse or promote
+#    products derived from this software without specific prior
+#    written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+#
+
+
+from opal import forms
+from models import Station, StationList, StationNetwork
+
+
+# support code
+
+
+def parse_station_list(stationList, stream):
+
+    # discard the count
+    stream.readline()
+    
+    for line in stream:
+
+        # parse the line
+        code, network, latitude, longitude, elevation, bur = line.split()
+        latitude, longitude = float(latitude), float(longitude)
+        elevation, bur = float(elevation), float(bur)
+
+        if stationList is None:
+            continue
+
+        # get/create the network entry
+        network, created = StationNetwork.objects.get_or_create(code = network, defaults = { 'name': "" })
+
+        # create the station
+        station = Station.objects.create(stationList = stationList,
+                                         code = code,
+                                         name = "",
+                                         network = network,
+                                         status = 1,
+                                         latitude = latitude,
+                                         longitude = longitude,
+                                         elevation = elevation,
+                                         bur = bur)
+    return
+
+
+class UploadStationListManipulator(forms.Manipulator):
+    
+    def __init__(self):
+        super(UploadStationListManipulator, self).__init__()
+        self.fields = [
+            forms.TextField('name', maxlength=100, is_required=True),
+            forms.FileUploadField(field_name='stations', is_required=True),
+            ]
+
+    def get_validation_errors(self, new_data):
+        from StringIO import StringIO
+        
+        errors = super(UploadStationListManipulator, self).get_validation_errors(new_data)
+        
+        if not errors.get('stations'):
+            try:
+                stations = new_data['stations']['content']
+                stream = StringIO(stations)
+                parse_station_list(None, stream)
+            except Exception:
+                errors['stations'] = ['Please select a file in STATIONS format.']
+
+        return errors
+
+    def save(self, new_data, user):
+        from StringIO import StringIO
+
+        # Create the new station list.
+        stationList = StationList.objects.create(user = user,
+                                                 name = new_data['name'])
+        
+        # Parse the uploaded STATIONS file.
+        stations = new_data['stations']['content']
+        stream = StringIO(stations)
+        parse_station_list(stationList, stream)
+
+        return stationList
+
+
+# move to shared location
+def gearth_object_list(request, **kwds):
+    from opal.views.generic.list_detail import object_list
+    return object_list(request,
+                       mimetype='application/vnd.google-earth',
+                       **kwds)
+
+
+def station_list_gearth(request, **kwds):
+    return gearth_object_list(request,
+                              template_name='stations/station_list_gearth.kml',
+                              **kwds)
+
+
+# end of file

Deleted: cs/pythia/trunk/cig/web/seismo/stations/urls.py
===================================================================
--- cs/pythia/trunk/cig/web/seismo/stations/urls.py	2007-08-08 05:58:48 UTC (rev 7785)
+++ cs/pythia/trunk/cig/web/seismo/stations/urls.py	2007-08-08 09:08:47 UTC (rev 7786)
@@ -1,74 +0,0 @@
-#!/usr/bin/env python
-#
-# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-#
-#                      cig.web.seismo.stations
-#
-# Copyright (c) 2006, California Institute of Technology
-# All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions
-# are met:
-#
-#
-#    * Redistributions of source code must retain the above copyright
-#    notice, this list of conditions and the following disclaimer.
-#
-#    * Redistributions in binary form must reproduce the above
-#    copyright notice, this list of conditions and the following
-#    disclaimer in the documentation and/or other materials provided
-#    with the distribution.
-#
-#    * Neither the name of the California Institute of Technology nor
-#    the names of its contributors may be used to endorse or promote
-#    products derived from this software without specific prior
-#    written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
-# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
-# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
-# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
-# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-# POSSIBILITY OF SUCH DAMAGE.
-#
-# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-#
-
-
-from opal.conf.urls.defaults import *
-from models import Station, StationList, StationNetwork
-
-
-stationlist_delete_args = {
-    'model': StationList,
-    'post_delete_redirect': '/specfem3dglobe/stations/',
-    }
-
-
-stationlist_create_update_args = {
-    'model': StationList,
-    'post_save_redirect': '/specfem3dglobe/stations/',
-    'follow': { 'user': False },
-    }
-
-
-urlpatterns = patterns('',
-    (r'^$', 'cig.web.seismo.stations.views.index'),
-    (r'^create/$', 'cig.web.seismo.stations.views.create'),
-    (r'^default/$', 'cig.web.seismo.stations.views.default'),
-    (r'^upload/$', 'cig.web.seismo.stations.views.upload'),
-    (r'^(?P<object_id>\d+)/$', 'opal.views.generic.list_detail.object_detail', {'queryset': StationList.objects.all()}),
-    (r'^(?P<object_id>\d+)/edit/$', 'opal.views.generic.create_update.update_object', stationlist_create_update_args),
-    (r'^(?P<object_id>\d+)/delete/$', 'opal.views.generic.create_update.delete_object', stationlist_delete_args),
-    (r'^(?P<object_id>\d+)/gearth\.kml$','cig.web.seismo.stations.views.stationlist_detail_gearth'),
-)
-
-
-# end of file

Deleted: cs/pythia/trunk/cig/web/seismo/stations/views.py
===================================================================
--- cs/pythia/trunk/cig/web/seismo/stations/views.py	2007-08-08 05:58:48 UTC (rev 7785)
+++ cs/pythia/trunk/cig/web/seismo/stations/views.py	2007-08-08 09:08:47 UTC (rev 7786)
@@ -1,221 +0,0 @@
-#!/usr/bin/env python
-#
-# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-#
-#                      cig.web.seismo.stations
-#
-# Copyright (c) 2006, California Institute of Technology
-# All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions
-# are met:
-#
-#
-#    * Redistributions of source code must retain the above copyright
-#    notice, this list of conditions and the following disclaimer.
-#
-#    * Redistributions in binary form must reproduce the above
-#    copyright notice, this list of conditions and the following
-#    disclaimer in the documentation and/or other materials provided
-#    with the distribution.
-#
-#    * Neither the name of the California Institute of Technology nor
-#    the names of its contributors may be used to endorse or promote
-#    products derived from this software without specific prior
-#    written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
-# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
-# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
-# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
-# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-# POSSIBILITY OF SUCH DAMAGE.
-#
-# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-#
-
-
-from opal import forms
-from opal.contrib.auth.decorators import login_required
-from opal.http import HttpResponseRedirect
-from opal.shortcuts import get_object_or_404, render_to_response
-from opal.template.context import RequestContext
-from models import Station, StationList, StationNetwork
-
-
-def index(request):
-    station_lists = StationList.objects.filter(user__exact=request.user)
-    return render_to_response('stations/index.html',
-                              {'station_lists': station_lists },
-                              RequestContext(request, {}))
-index = login_required(index)
-
-
-def create(request):
-    from os.path import dirname
-    from pkg_resources import resource_stream
-
-    actionChoices = (
-        (0, 'Create an empty list of stations.'),
-        (1, 'Create a default list of stations.'),
-        )
-    class Manipulator(forms.Manipulator):
-        def __init__(self):
-            super(Manipulator, self).__init__()
-            self.fields = [
-                forms.TextField('name', maxlength=100, is_required=True),
-                forms.RadioSelectField('action', choices=actionChoices, is_required=True)
-                ]
-    
-    manipulator = Manipulator()
-    
-    if request.method == 'POST':
-        new_data = request.POST.copy()
-        errors = manipulator.get_validation_errors(new_data)
-        if not errors:
-            manipulator.do_html2python(new_data)
-            stationList = StationList.objects.create(user = request.user,
-                                                     name = new_data['name'])
-            if new_data['action'] == "1":
-                stream = resource_stream(__name__, "STATIONS")
-                parse_station_list(stationList, stream)
-            url = "%s/%i/" % (dirname(dirname(request.path)), stationList.id)
-            return HttpResponseRedirect(url)
-    else:
-        errors = {}
-        new_data = {'action': 1}
-
-    form = forms.FormWrapper(manipulator, new_data, errors)
-    return render_to_response('stations/stationlist_create.html',
-                              {'form': form},
-                              RequestContext(request, {}))
-create = login_required(create)
-
-
-def upload(request):
-    from os.path import dirname
-    
-    manipulator = UploadStationListManipulator()
-    
-    if request.method == 'POST':
-        new_data = request.POST.copy()
-        new_data.update(request.FILES)
-        errors = manipulator.get_validation_errors(new_data)
-        if not errors:
-            manipulator.do_html2python(new_data)
-            stationList = manipulator.save(new_data, request.user)
-            url = "%s/%i/" % (dirname(dirname(request.path)), stationList.id)
-            return HttpResponseRedirect(url)
-    else:
-        errors = new_data = {}
-
-    form = forms.FormWrapper(manipulator, new_data, errors)
-    return render_to_response('stations/stationlist_upload.html',
-                              {'form': form},
-                              RequestContext(request, {}))
-upload = login_required(upload)
-
-
-def stationlist_detail_gearth(request, object_id):
-    stationList = get_object_or_404(StationList, id=object_id)
-    kwds = dict(queryset = stationList.station_set.all(),
-                extra_context = {'name': stationList.name})
-    return station_list_gearth(request, **kwds)
-
-
-# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-# support code
-
-
-def parse_station_list(stationList, stream):
-
-    # discard the count
-    stream.readline()
-    
-    for line in stream:
-
-        # parse the line
-        code, network, latitude, longitude, elevation, bur = line.split()
-        latitude, longitude = float(latitude), float(longitude)
-        elevation, bur = float(elevation), float(bur)
-
-        if stationList is None:
-            continue
-
-        # get/create the network entry
-        network, created = StationNetwork.objects.get_or_create(code = network, defaults = { 'name': "" })
-
-        # create the station
-        station = Station.objects.create(stationList = stationList,
-                                         code = code,
-                                         name = "",
-                                         network = network,
-                                         status = 1,
-                                         latitude = latitude,
-                                         longitude = longitude,
-                                         elevation = elevation,
-                                         bur = bur)
-    return
-
-
-class UploadStationListManipulator(forms.Manipulator):
-    
-    def __init__(self):
-        super(UploadStationListManipulator, self).__init__()
-        self.fields = [
-            forms.TextField('name', maxlength=100, is_required=True),
-            forms.FileUploadField(field_name='stations', is_required=True),
-            ]
-
-    def get_validation_errors(self, new_data):
-        from StringIO import StringIO
-        
-        errors = super(UploadStationListManipulator, self).get_validation_errors(new_data)
-        
-        if not errors.get('stations'):
-            try:
-                stations = new_data['stations']['content']
-                stream = StringIO(stations)
-                parse_station_list(None, stream)
-            except Exception:
-                errors['stations'] = ['Please select a file in STATIONS format.']
-
-        return errors
-
-    def save(self, new_data, user):
-        from StringIO import StringIO
-
-        # Create the new station list.
-        stationList = StationList.objects.create(user = user,
-                                                 name = new_data['name'])
-        
-        # Parse the uploaded STATIONS file.
-        stations = new_data['stations']['content']
-        stream = StringIO(stations)
-        parse_station_list(stationList, stream)
-
-        return stationList
-
-
-# move to shared location
-def gearth_object_list(request, **kwds):
-    from opal.views.generic.list_detail import object_list
-    return object_list(request,
-                       mimetype='application/vnd.google-earth',
-                       **kwds)
-
-
-def station_list_gearth(request, **kwds):
-    return gearth_object_list(request,
-                              template_name='stations/station_list_gearth.kml',
-                              **kwds)
-
-
-# end of file



More information about the cig-commits mailing list