def __call__(self, environ, start_response): """Invoke the Controller""" # WSGIController.__call__ dispatches to the Controller method # the request is routed to. This routing information is # available in environ['pylons.routes_dict'] try: return WSGIController.__call__(self, environ, start_response) finally: Session.remove()
def setup_app(command, conf, vars): """Place any commands to setup georegistry here""" # If we are not in a testing environment, if not pylons.test.pylonsapp: load_environment(conf.global_conf, conf.local_conf) # Create the tables if they don't already exist Base.metadata.create_all(bind=Session.bind) # If we are not in a testing environment and users do not exist, if not pylons.test.pylonsapp and not Session.query(model.Person).all(): # Show feedback print 'Please create an administrator account.' # Prepare passwordDefault = store.makeRandomString(parameter.PASSWORD_LENGTH_AVERAGE) # Create person = model.Person(raw_input('Username (administrator): ') or 'administrator', model.hashString(getpass.getpass('Password (%s): ' % passwordDefault) or passwordDefault), raw_input('Nickname (Administrator): ') or u'Administrator', raw_input('Email ([email protected]): ') or '*****@*****.**') person.is_super = True Session.add(person) Session.commit()
def validateSRID(srid): 'Make sure we have a valid SRID' if not srid: raise GeoRegistryError('Must specify spatial reference srid') try: srid = int(srid) except ValueError: raise GeoRegistryError('Could not parse srid=%s as an integer' % srid) result = Session.execute('SELECT proj4text FROM spatial_ref_sys WHERE srid=:srid', dict(srid=srid)).fetchone() if not result: raise GeoRegistryError('Could not recognize srid=%s' % srid) return result[0]
def getPersonIDViaKey(): 'Try to get personID via key; otherwise, try to get personID via session' # Load key key = request.params.get('key', '').strip() # If we have a key, if key: # Load person person = Session.query(model.Person).filter_by(key=key).first() # If the person exists, if person: # Return personID via key return person.id # Return personID via session return getPersonID()
def getWritableFeatures(featureIDs, personID): 'Raise GeoRegistryError if personID does not have write access to the featureIDs' # Validate featureIDs try: featureIDs = [int(x) for x in featureIDs if x is not None] except ValueError: raise GeoRegistryError('Could not parse featureIDs=%s as integers' % featureIDs) # If we have no featureIDs, if not featureIDs: return [] # Load features = Session.query(Feature).filter(Feature.id.in_(featureIDs)).all() # Validate missingIDs missingIDs = list(set(featureIDs).difference(x.id for x in features)) if missingIDs: raise GeoRegistryError('Cannot modify featureIDs=%s that do not exist' % missingIDs) # Validate blockedIDs blockedIDs = [x.id for x in features if x.owner_id != personID] if blockedIDs: raise GeoRegistryError('Cannot modify featureIDs=%s because you are not the owner' % blockedIDs) # Return return features
def getTags(string, addMissing=False): 'Return corresponding tags' # Load tagTexts and discard empty lines tagTexts = filter(lambda x: x, (x.strip() for x in string.splitlines())) if not tagTexts: raise GeoRegistryError('Must specify at least one tag in tags') # Check whether tagTexts are too long longTagTexts = filter(lambda x: len(x) > parameter.TAG_LENGTH_MAXIMUM, tagTexts) if longTagTexts: raise GeoRegistryError('Cannot add the following tags because they are too long:\n%s' % '\n'.join(longTagTexts)) # Check whether tags exist missingTagTexts = list(set(tagTexts).difference(tag.text for tag in Session.query(Tag).filter(Tag.text.in_(tagTexts)))) if missingTagTexts: # If we are not supposed to add missing tags, if not addMissing: raise GeoRegistryError('Cannot match the following tags: %s' % missingTagTexts) # Add tags that don't exist Session.execute(tags_table.insert(), [{ 'text': x, } for x in missingTagTexts]) # Commit Session.commit() # Return return Session.query(Tag).filter(Tag.text.in_(tagTexts)).all()
def init_model(engine): 'Call me before using any of the tables or classes in the model' Session.configure(bind=engine)