ga_dynamic_models Package

aux Module

This module contains functions that may make your life easier, shortcuts for defining certain things in models and APIs. These are generally executed by the parser as the definition is parsed, as opposed to the names in ‘utils’, which generally contains helper functions for creating parse-able entities.

ga_dynamic_models.aux.universal_filter(model)

Make APIs allow filtering on ALL fields. This is dangerous for large tables.

parser Module

This is not terribly well documented, but the Parser transforms a dictionary contained in the database into a Model or Resource class. It may call methods, import modules, and do all kinds of hocus-pocus to do its job, so when letting users declare models on their own, make sure you do so in a secure way and validate that models are actual real models.

This can be generalized to a class declaration, so long as the class declaration is done in the Django ORM style. Thus you could also use this to declare models in MongoEngine or a similar ORM. YOu cannot define new methods, but you can define class attributes all you want. For more on how to do this, see ga_dynamic_models.utils.

class ga_dynamic_models.parser.Parser(module_name, result_metaclass=<type 'type'>)

Bases: object

parse(name, bases, fields, meta, **kwargs)

utils Module

Utilities for creating dynamic models.

This module is insane. What it does, effectively, is construct Models from objects listed in a MongoDB database. Quite a lot of functionality you’d expect could only be achieved through actual physical declaration of a class can be done here. One thing that’s essential right now, thoguh is that the WSGI container must be restarted after a model is declared. There’s probably a way around it, but right now I don’t know it. A task for doing this, if you’re using this with Geoanalytics, is declared in tasks.py and can be called via Celery’s task mechanism.

A few meta-“types” we’ll define here. A callable is represented in JSON as:

{ 'type' : 'callable',
  'module' : fully_qualified_module_name,
  'callable' : name of the callable in question - cannot be dotted,
  'parameters' : list of JSON types or callables.  Callables will be called from the inside out
}

A module-level attribute is represented as:

{ 'type' : 'attribute',
  'module' : ...,
  'attribute' : attribute name
}

A class level attribute is represented as:

{ 'type' : 'class_attribute',
  'module' : ...,
  'cls' : ...,
  'attribute' : name
}

A class level method is represented as:

{ 'type' : 'class_method',
  'module' : ...,
  'cls' : ...,
  'method' : name
  'parameters' : { 'positionals' : list of JSON types or callables.  Callables will be called from the inside out,
                   'keywords' : same }
}

A date is represented as any valid date string from ga_ows.utils.parsetime like this:

{ 'type' : 'datetime',
  'value' : timestring
}

In principle, there are model mixins, multiple inheritance, custom field types, all of that. In practice that won’t happen, likely, but why not support it?

A model can be constructed from a dictionary modeled on the following structure:

{
  'name' : string,
  'bases' : [ attribute<model_subclass>, ... ],
  'fields' : {
        field_name : { callable<field_subclass>, ...
  },
  'meta' : {
      'abstract' : boolean,
      'app_label' : string<app name>,
      'db_table' : string<table name>,
      'db_tablespace' : string<tablespace name>,
      'get_latest_by' : string<field name>,
      'managed' : boolean<managed by django>,
      'order_with_respect_to' : string<foreign key field name>
  },
}

Additionally, look for how to expose modules in wms/wfs in ows.py and in Tastypie in api.py.

Usage is similar to this:

mymodel = simple_geomodel("MyModel", managed=False, db_table='my_model',
    mychar = simple_geofield("CharField", max_length=255, default=''),
    mygeom = simple_geofield("PolygonField", srid=3157, null=True),
    ...
)

declare_model(mymodel)

We refer to an item in this quite a lot. An item is defined as anything that can be created by calling:

  • method
  • queryset
  • attribute
  • callable
  • class_method
  • class_attribute
  • attributes

or a bare string or number. The caveat is that if you want a floating point number, you must have a decimal point, as JSON doesn’t distinguish between floats and integers.

ga_dynamic_models.utils.attribs(module, *ls)

Part of the grammar of dynamic models. A dotted chain of attributes.

Parameters:
  • module – The dotted name of the module containing the attribute
  • ls – The list of attribute names or **method**s to call to get the result desired
Returns:

A JSON serializable dict

ga_dynamic_models.utils.attribute(module, name)

Part of the grammar of dynamic models. A single attribute of a module.

Parameters:
  • module – THe name of the module.
  • name – The name of the attribute
Returns:

A JSON serializable dict.

ga_dynamic_models.utils.callable(module, name, *args, **kwargs)

Part of the grammar of dynamic models. Call a python callable with arguments.

Parameters:
  • module – The dotted name of the module containing the attribute.
  • name – The name of the attribute.
  • args – The positional arguments as **item**s to pass to the function call.
  • kwargs – The keyword arguments as **item**s to pass to the function call
Returns:

A JSON serializable dict

ga_dynamic_models.utils.class_attribute(module, cls, attribute)

Part of the grammar of dynamic models. An attribute of a class

Parameters:
  • module – The name of the module containing the class
  • cls – The name of the class
  • attribute – The name of the attribute
Returns:

A JSON serializable dict

ga_dynamic_models.utils.class_method(module, cls, method, *args, **kwargs)

Part of the grammar of dynamic models. A class method call

Parameters:
  • module – The name of the module containing the class
  • cls – The name of the class
  • method – The name of the class method
  • args – The arguments to pass as **item**s
  • kwargs – The keyword arguments to pass as **item**s
Returns:

A JSON serializable dict.

ga_dynamic_models.utils.declare_model(model, replace=False, user=None, syncdb=False)

Adds the model to the database and calls syncdb.

Parameters:
  • model – A model as defined by simple_model, simple_geomodel, or model.
  • replace – Whether or not to replace the model if it already exists.
  • user – The user who owns the model
  • syncdb – Whether to call syncdb after the model is declared.
Returns:

A JSON serializable dict.

ga_dynamic_models.utils.declare_resource(resource, replace=False, user=None)

Declares a TastyPie API and inserts it into the DB, if it exists.

Parameters:
  • resource – The resource, as created by simple_model_resource, simple_geo_resource, etc.
  • replace – Whether or not to replace the model if it already exists.
  • user – The user who owns the resource
Returns:

ga_dynamic_models.utils.drop_model(model, user=None)

Drop a model from the database. Drops the table as well.

Parameters:
  • model – THe model name to drop
  • user – The user who owns the model, if relevant.
Returns:

ga_dynamic_models.utils.drop_resource(resource, user=None)

Drops a TastyPie API resource from the DB, if it exists.

Parameters:
  • resource – The resource name to drop.
  • user – The user requesting the drop, if relevant.
Returns:

ga_dynamic_models.utils.get_connection()

Get the MongoDB connection associated with this app.

Returns:A MongoDB database.
ga_dynamic_models.utils.get_model(model)

Get a Model class that’s stored in this app.

Parameters:model – The name of the model to return.
Returns:The model class as a Python class.
ga_dynamic_models.utils.method(method, *parameters)

Part of the grammar of dynamic models. Declare a method.

Parameters:
  • method – The name of the method
  • parameters – A list of **item**s.
Returns:

A JSON serializable dict.

ga_dynamic_models.utils.model(name, bases, fields, **meta)

Part of the grammar of dynamic models. A model class declaration. This function should encapsulate any model definition.

Parameters:
  • name – The name of the model to declare
  • bases – The base classes of the model
  • fields – The fields of the model as a dict of name => field
  • meta – The metadata attributes of the model. Keyword => item.
Returns:

A JSON serializable dict.

ga_dynamic_models.utils.queryset(module, model, *extra)

Part of the grammar of dynamic models. A model queryset. Must be handled special because of the redefinition of __getattribute__ in ModelBase

Parameters:
  • module – The dotted name of a module containing the model
  • model – The classname of a model
  • extra – Any methods that should be called to further winnow the queryset
Returns:

A JSON serializable dict.

ga_dynamic_models.utils.resource(name, bases, fields, **meta)

Part of the grammar of dynamic models. A TastyPie resource declaration. This function should encapsulate any API declaration.

Parameters:
  • name – The name of the resource to declare.
  • bases – The base classes of the resource
  • fields – The fields of the resource
  • meta – The metadata attributes of the resource.
Returns:

A JSON seralizable dict.

ga_dynamic_models.utils.simple_field(kind, *args, **kwargs)

A simplification of the **callable* function that creates a field.

Parameters:
  • kind – The kind of field, should be in django.db.models.
  • args – The positional arguments to the field as **item**s
  • kwargs – The keyword arguments to the field as **item**s
Returns:

ga_dynamic_models.utils.simple_geo_resource(module, model, resource_name, **meta)

Part of the grammar of dynamic models. A simplification for common TastyPie resource declarations. Calls resource to declare a GeoModelResource (as defined in ga_ows.tastyhacks ). A GeoResource sends GeoJSOn instead of JSON.

Parameters:
  • module – The name of the module the model is in.
  • model – The model to make the API from
  • resource_name – The endpoint name of the resource
  • meta – The meta attributes of the resource
Returns:

A JSON serializable dict.

ga_dynamic_models.utils.simple_geofield(kind, *args, **kwargs)

A simplification of the **callable* function that creates a field.

Parameters:
  • kind – The kind of field, should be in django.contrib.gis.db.models.
  • args – The positional arguments to the field as **item**s
  • kwargs – The keyword arguments to the field as **item**s
Returns:

ga_dynamic_models.utils.simple_geomodel(name, managed=True, db_table=None, **fields)

A simplification of the model function that declares a GeoDjango model.

Parameters:
  • name – The name of the model
  • managed – default True. The model’s “managed” attribute. See Django’s model reference.
  • db_table – The database table to point at. Safe to leeave this as None unless you have a specific table.
  • fields – The fields to put in the model, as keyword arguments. See simple_field or simple_geofield
Returns:

A JSON Serializable dict.

ga_dynamic_models.utils.simple_model(name, managed=True, db_table=None, **fields)

A simplification of the model function that declares a Django model.

Parameters:
  • name – The name of the model
  • managed – default True. The model’s “managed” attribute. See Django’s model reference.
  • db_table – The database table to point at. Safe to leeave this as None unless you have a specific table.
  • fields – The fields to put in the model, as keyword arguments. See simple_field.
Returns:

A JSON Serializable dict.

ga_dynamic_models.utils.simple_model_resource(module, model, resource_name, **meta)

A simplification for common TastyPie resource declarations. Calls resource to declare a ModelResource.

Parameters:
  • module – The name of the module the model is in.
  • model – The model to make the API from.
  • resource_name – The endpoint name of the resource.
  • meta – The meta attributes of the resource.
Returns:

A JSON serlizable dict.

Table Of Contents

Previous topic

ga_dynamic_models

Next topic

views Package

This Page