def clone(self, scenarioID):
     'Show form to create a new item based on datasets and parameters from existing scenario'
     # Make sure the user is logged in
     personID = h.getPersonID()
     if not personID:
         return redirect(url('person_login', targetURL=h.encodeURL(request.path)))
     # Make sure the user has access to the scenario
     scenario = Session.query(model.Scenario).filter(model.getScopeFilter(personID)).filter(model.Scenario.id==scenarioID).first()
     if not scenario:
         return redirect(url('new_scenario'))
     # Load
     scenarioInput = scenario.input
     # Prepare
     c.scenario = scenario
     c.metricModel = metric.getModel(request.GET.get('metricModel', scenarioInput['metric model name']))
     c.metricConfiguration = scenarioInput['metric configuration']
     c.networkModel = network.getModel(request.GET.get('networkModel', scenarioInput['network model name']))
     c.networkConfiguration = scenarioInput['network configuration']
     # Return
     return render('/scenarios/new.mako')
 def index(self, format='html'):
     'GET /scenarios: Show all items in the collection'
     # Initialize
     personID = h.getPersonID()
     refresh = request.GET.get('refresh', 0)
     scope = request.GET.get('scope', str(model.scopePrivate))
     # Load
     scenarioQuery = Session.query(model.Scenario)
     if not personID:
         scenarioQuery = scenarioQuery.filter_by(scope=model.scopePublic)
     elif scope == '-':
         scenarioQuery = scenarioQuery.filter_by(owner_id=personID)
     elif scope == '*':
         scenarioQuery = scenarioQuery.filter(model.getScopeFilter(personID))
     else:
         scenarioQuery = scenarioQuery.filter_by(owner_id=personID).filter_by(scope=model.scopePrivate)
     c.scenarios = scenarioQuery.options(orm.eagerload(model.Scenario.owner)).order_by(model.Scenario.when_created.desc()).all()
     # If this is not a refresh request,
     if not refresh:
         return render('/scenarios/index.mako')
     # If this is a refresh request,
     else:
         return render('/scenarios/scenarios.mako')
 def check(self, scenarioID):
     # Initialize
     personID = h.getPersonID()
     # Load
     scenario = Session.query(model.Scenario).filter(model.Scenario.id==scenarioID).filter(model.getScopeFilter(personID)).first()
     # Return
     return dict(isOk=0 if not scenario or scenario.isQueued() else 1)
 def show(self, id, format='html'):
     'GET /scenarios/id: Show a specific item'
     # If the output format is not supported, 
     if format not in ['html', 'zip', 'geojson', 'json']: 
         return 'Unsupported output format: ' + format 
     try:
         id = int(id)
     except ValueError:
         return redirect(url('scenarios'))
     # Load
     personID = h.getPersonID()
     c.scenario = Session.query(model.Scenario).filter(model.Scenario.id==id).filter(model.getScopeFilter(personID)).first()
     # If user does not have access to the scenario,
     if not c.scenario:
         c.status = model.statusFailed
         if format == 'html':
             return render('/scenarios/show.mako')
         elif format == 'zip':
             return ''
         elif format == 'geojson':
             return geojson.dumps(geojson.FeatureCollection([]))
         elif format == 'json':
             return cjson.encode({})
     # If the scenario has an error,
     if c.scenario.status == model.statusFailed:
         c.traceback = c.scenario.output['traceback']
         c.status = model.statusFailed
         if format == 'html':
             return render('/scenarios/show.mako')
         elif format == 'zip':
             return forward(FileApp(c.scenario.getFolder() + '.zip'))
         elif format == 'geojson':
             return geojson.dumps(geojson.FeatureCollection([]))
         elif format == 'json':
             return c.scenario.exportJSON()
     # If the scenario has not been processed,
     if c.scenario.isQueued():
         c.status = model.statusPending
         if format == 'html':
             return render('/scenarios/show.mako')
         elif format == 'zip':
             return forward(FileApp(c.scenario.getFolder() + '.zip'))
         elif format == 'geojson':
             return geojson.dumps(geojson.FeatureCollection([]))
         elif format == 'json':
             return c.scenario.exportJSON()
     # Prepare
     c.status = model.statusDone
     c.scenarioInput = c.scenario.input
     c.scenarioOutput = c.scenario.output
     transform_point = geometry_store.get_transform_point(geometry_store.proj4LL, geometry_store.proj4SM)
     # If the user wants HTML,
     if format == 'html':
         # Render scenario
         c.metricModel = metric.getModel(c.scenarioInput['metric model name'])
         scenarioStatistics = c.scenarioOutput['statistics']
         nodeStatistics = scenarioStatistics['node']
         # Prepare map
         centerX, centerY = transform_point(nodeStatistics['mean longitude'], nodeStatistics['mean latitude'])
         box1X, box1Y = transform_point(nodeStatistics['minimum longitude'], nodeStatistics['maximum latitude'])
         box2X, box2Y = transform_point(nodeStatistics['maximum longitude'], nodeStatistics['minimum latitude'])
         # Render map
         datasetStore = c.scenario.getDataset()
         c.mapFeatures = datasetStore.exportGeoJSON(transform_point)
         c.mapCenter = '%s, %s' % (centerX, centerY)
         c.mapBox = '%s, %s, %s, %s' % (box1X, box1Y, box2X, box2Y)
         # Render nodes
         c.nodes = list(datasetStore.cycleNodes())
         c.populationQuartiles = scenarioStatistics['metric']['population quartiles']
         # Render scenarios
         c.scenarios = Session.query(model.Scenario).filter(model.getScopeFilter(personID)).filter(model.Scenario.status==model.statusDone).filter(model.Scenario.id!=c.scenario.id).order_by(model.Scenario.id.desc()).all()
         # Return
         return render('/scenarios/show.mako')
     elif format == 'zip':
         return forward(FileApp(c.scenario.getFolder() + '.zip'))
     elif format == 'geojson':
         return c.scenario.getDataset().exportGeoJSON(transform_point)
     elif format == 'json':
         return c.scenario.exportJSON(request.params.get('nodeID'))