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 feedback(self): 'Send feedback' # Load text = request.POST.get('text', '').strip() # If there is text, if text: # Initialize personID = h.getPersonID() headerByValue = {} # If the person is logged in, if personID: # Load person = Session.query(model.Person).get(personID) nickname = person.nickname headerByValue['reply-to'] = email.utils.formataddr((nickname, person.email)) # If th person is not logged in, else: nickname = 'Anonymous' # Send it subject = '[%s] Feedback from %s' % (parameter.SITE_NAME, nickname) try: smtp.sendMessage( config['safe']['mail support'], config['safe']['mail support'], subject, text, headerByValue) except: return dict(isOk=0, message='Error sending message') # Return return dict(isOk=1) # Return return dict(isOk=0)
def update(self, id): 'PUT /scenarios/id: Update an existing item' # Initialize personID = h.getPersonID() # Load scenario = Session.query(model.Scenario).filter(model.Scenario.id==id).first() # If the scenario doesn't exist, if not scenario: return dict(isOk=0, message='Scenario %s does not exist' % id) # If the user is not the owner, if personID != scenario.owner_id: return dict(isOk=0, message='You are not the owner of scenario %s' % id) # Load scenarioName = request.params.get('scenarioName', '').strip() if not scenarioName: return dict(isOk=0, message='Please enter a scenario name') try: scenarioScope = int(request.params.get('scenarioScope')) except ValueError: return dict(isOk=0, message='Scenario scope must be an integer') if scenarioScope not in [model.scopePrivate, model.scopePublic]: return dict(isOk=0, message='Scenario scope can either be %s=private or %s=public' % (model.scopePrivate, model.scopePublic)) # Update scenario.name = scenarioName scenario.scope = scenarioScope # Commit Session.commit() # Return return dict(isOk=1)
def index(self, format="html"): "GET /: Show landing page if not logged in" # Initialize personID = h.getPersonID() # If not logged in, if not personID: return render("/landing/index.mako") # Take them to the scenarios, else: return redirect(url("scenario_index"))
def update_(self): "Send update confirmation email" # Load personID = h.getPersonID() # If the person is not logged in, if not personID: return dict(isOk=0) # Prepare person = Session.query(model.Person).get(personID) # Return return changeAccount(dict(request.POST), "update", "/people/confirm.mako", person)
def delete(self, id): 'DELETE /scenarios/id: Delete an existing item' # Initialize personID = h.getPersonID() # Load scenario = Session.query(model.Scenario).filter(model.Scenario.id==id).first() # If the scenario doesn't exist, if not scenario: return dict(isOk=0, message='Scenario %s does not exist' % id) # If the user is not the owner, if personID != scenario.owner_id: return dict(isOk=0, message='You are not the owner of scenario %s' % id) # Delete Session.delete(scenario) Session.commit() # Return return dict(isOk=1)
def update(self): 'Show account update page' # Load personID = h.getPersonID() # If the person is not logged in, if not personID: # Return return redirect(url('person_login', targetURL=h.encodeURL('/'))) # Render c.isNew = False person = Session.query(model.Person).get(personID) # Return return formencode.htmlfill.render(render('/people/change.mako'), { 'username': person.username, 'nickname': person.nickname, 'email': person.email, 'email_sms': person.email_sms, })
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 update(self): "Show account update page" # Load personID = h.getPersonID() # If the person is not logged in, if not personID: # Return return redirect(url("person_login", targetURL=h.encodeURL("/"))) # Render c.isNew = False person = Session.query(model.Person).get(personID) # Return return formencode.htmlfill.render( render("/people/change.mako"), { "username": person.username, "nickname": person.nickname, "email": person.email, "email_sms": person.email_sms, }, )
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 create(self): 'POST /scenarios: Create a new item' # Initialize personID = h.getPersonID() if not personID: return redirect(url('person_login', targetURL=h.encodeURL(h.url('new_scenario')))) # Load try: demographicDatabase_h = int(request.POST.get('demographicDatabase_h', 0)) except ValueError: demographicDatabase_h = 0 if not demographicDatabase_h and 'demographicDatabase' not in request.POST: return cjson.encode(dict(isOk=0, message='The demographicDatabase field is required')) scenarioName = request.POST.get('scenarioName') or 'Untitled' try: scenarioScope = int(request.POST.get('scenarioScope', model.scopePrivate)) except ValueError: scenarioScope = model.scopePrivate metricModelName = request.POST.get('metricModelName', metric.getModelNames()[0]) networkModelName = request.POST.get('networkModelName', network.getModelNames()[0]) callbackURL = request.POST.get('callbackURL') # Create scenario scenario = model.Scenario(personID, scenarioName, scenarioScope) Session.add(scenario) Session.commit() scenarioFolder = scenario.getFolder() if os.path.exists(scenarioFolder): shutil.rmtree(scenarioFolder) store.makeFolderSafely(scenarioFolder) # If the user is using an existing demographicDatabase, if demographicDatabase_h: # Copy source in case it is deleted sourceScenario = Session.query(model.Scenario).get(demographicDatabase_h) sourceScenarioFolder = sourceScenario.getFolder() demographicFileName = sourceScenario.input['demographic file name'] demographicPath = os.path.join(scenarioFolder, demographicFileName) shutil.copyfile(os.path.join(sourceScenarioFolder, demographicFileName), demographicPath) # If the user is uploading a new demographicDatabase, else: # Save original demographicDatabase in case the user wants it later demographicDatabase = request.POST['demographicDatabase'] demographicFileExtension = os.path.splitext(demographicDatabase.filename)[1] demographicFileName = 'demographics' + demographicFileExtension demographicPath = os.path.join(scenarioFolder, demographicFileName) shutil.copyfileobj(demographicDatabase.file, open(demographicPath, 'wb')) demographicDatabase.file.close() # Store input configurationByName = extractConfigurationByName(request.POST, scenarioFolder) scenario.input = { 'demographic file name': str(demographicFileName), 'metric model name': metricModelName, 'metric configuration': configurationByName.get('metric', {}), 'network model name': networkModelName, 'network configuration': configurationByName.get('network', {}), 'callback url': callbackURL, 'host url': request.host_url, } Session.commit() store.zipFolder(scenarioFolder + '.zip', scenarioFolder) # Redirect redirect(url('scenario', id=scenario.id))
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'))