Application models#
These are object model for data dumps to define behaviors for data collection during dump creation.
- class diskette.core.applications.ApplicationConfig(name, models=[], excludes=None, natural_foreign=False, natural_primary=False, comments=None, filename=None, is_drain=None, allow_drain=False, dump_command=None)[source]#
Application model to validate and store application details.
TODO: Another name would better to avoid mental clash with Django “AppConfig”. ApplicationModel (+2) ? ApplicationDataDef (0)? ApplicationDefinition (+3) ? DataDefinition (+3) ?
- Parameters:
name (string) – Application name, almost anything but it may be slugified for internal usages so avoid too much longer text and special characters.
models (list) – List of labels. A label is either an application label like
auth
or a full model label likeauth.user
. Labels are validated, they must exists in Django application registry.
- Keyword Arguments:
filename (string) – The filename to use if application dump is to be written in a file. The filename also determine the format used to dump data. If you want another format that the default one you will have to define it from the filename even you don’t plan to write dump to a file. Finally if not given, the filename will be automatically defined with slugified
name
with default format.excludes (list) – The list of excluded model labels that won’t be collected into the application dump. Currently, the excluded labels are not validated.
natural_foreign (boolean) – Enable usage of natural foreign key.
natural_primary (boolean) – Enable usage of natural primary key.
comments (string) – Free text not used internally.
allow_drain (boolean) – Define if application allows its excluded models to be drained. Default is
False
to avoid implicit draining of data that may not be wanted.dump_command (string) – Custom dump command to use for this specific application instead of default
dumpdata
. If you have models that usedjango-polymorphic
you should give valuepolymorphic_dumpdata
here.
- is_drain#
Declare application as a special drain application. This should always be
False
for a common application,True
value is reserved toDrainApplicationConfig
.- Type:
boolean
- property models#
List all fully qualified model labels to include, either implicitely and explicitely from given
models
argument.NOTE: models, excludes and retention attributes may be cached since they have no reason to change.
- Returns:
Fully qualified model labels.
- Return type:
list
- property excludes#
List all fully qualified model labels to exclude, either implicitely and explicitely from given
_excludes
argument.- Returns:
Fully qualified model labels.
- Return type:
list
- property retention#
List all fully qualified model labels that are not allowed to be drained from this application.
Included models are never allowed to be drained and exclusions models may be allowed if
allow_drain
is enabled.- Returns:
Fully qualified model labels that won’t be allowed to be drained. This means labels from
models
on defaut and possibly theexcludes
one also if application allows for drainage.- Return type:
list
- get_filename(format_extension=None)[source]#
Automatically determine filename from Application name and with a format extension name.
- Keyword Arguments:
format_extension (string) – Custom extension to use if given.
- Returns
string: Filename.
- as_config()[source]#
Returns Application configuration suitable for dump history.
- Keyword Arguments:
name (boolean) – To include or not the name into the dict.
commented (boolean) – To include or not the comments into the dict.
- Returns
dict: Application data.
- class diskette.core.applications.DjangoAppLookupStore(registry=None)[source]#
Django application store collects applications and store them with their useful parameters.
This is registry built on top of Django apps to implement methods to easily perform queries alike with Django ORM. It helps to find detailed inclusions and exclusions with application models.
- registry#
The store registry built as a
datalookup.Dataset
.- Type:
Dataset
- Keyword Arguments:
registry (list) – List of dictionnaries, each dictionnary is an application item with its parameters. If not given, it will be filled automatically from enabled applications using
django.apps
.
- as_dict()[source]#
Return the store items with their parameters as a dictionnary.
- Returns:
Store items as a dictionnary.
- Return type:
dict
- as_json()[source]#
Return the store items with their parameters as JSON.
- Returns:
Store items as JSON.
- Return type:
string
- classmethod normalize_label(model, app=None)[source]#
Return normalized name for a model or FQM label.
- Parameters:
model (string or object) – Model name as a string or an object that inherits from
django.db.models.base.ModelBase
.- Keyword Arguments:
app (string or object) – Either a model object, AppConfig object or a string for an application label.
- Returns:
A fully qualified model name composed from app name and model name.
- Return type:
string
- classmethod is_fully_qualified_labels(labels)[source]#
Validate given labels are fully qualified model labels.
- Parameters:
labels (list) – List of label to validate.
- Returns:
A list of invalid labels.
- Return type:
list
- get_registry_app_models(app_id, app)[source]#
Return model labels for given application.
Internally used to build appstore registry.
- Parameters:
app_id (integer) – An unique id to represent related application.
app (AppConfig) – The application object from Django apps.
- Returns:
A list of dictionnaries for models. Model dictionnary will contains the following items:
- id
A simple integer built from the loop, since it is only done internally it should be ensured to be unique.
- app_id
Given application id.
- unique_id
A special field to help for ordering on models since datalookup does not provide a way to do this. This is a join of app id and model id, both formatted on 4 digits filled by zero.
- app
Application label.
- name
Model class name.
- object
Model class.
- label
Fully qualified model label.
- Return type:
list
- get_registry()[source]#
Store all model labels from enabled applications into a registry.
- Returns:
A list of dictionnaries for applications. Application dictionnary will contains the following items:
- id
A simple integer built from the loop, since it is only done internally it should be ensured to be unique.
- verbose_name
Application verbose name.
- label
Application label.
- pythonpath
Python path to the application itself.
- models
A list of application models as returned from method
DjangoAppLookupStore.get_registry_app_models
.
- Return type:
list
- get_app(label)[source]#
Getter to retrieve a single application from a label.
- Parameters:
label (string) – Application label.
- Raises:
MultipleObjectExistsFromAppstore – If there is more than one application object with the same label. This is something that should never happen because of how Django manage applications.
DoesNotExistsFromAppstore – If there is not any application with the given label.
- Returns:
Datalookup node for retrieved application.
- Return type:
node
- get_model(label)[source]#
Getter to retrieve a single application model from a label.
- Parameters:
label (string) – Fully qualified model label.
- Raises:
MultipleObjectExistsFromAppstore – If there is more than one application object with the same label. This is something that should never happen because of how Django manage applications.
DoesNotExistsFromAppstore – If there is not any application with the given label.
- Returns:
Datalookup node for retrieved application.
- Return type:
node
- get_app_model_labels(app)[source]#
Return model labels for given application.
- Parameters:
app (sring) – Application label.
- Returns:
Fully qualified model labels.
- Return type:
list
- get_all_model_labels()[source]#
Return all model labels from all enabled applications.
- Returns:
Fully qualified model labels.
- Return type:
list
- check_unexisting_labels(labels)[source]#
Check if given labels exists as app or models in store.
- Parameters:
labels (list) – A list of application or FQM label (string) to check.
- Returns:
Respectively a list of not found apps and a list of not found models.
- Return type:
Tuple
- get_models_inclusions(labels, excludes=None)[source]#
Returns model nodes for inclusions.
- Parameters:
labels (string or list) – A list of App label (string), fully qualified model labels (string) or AppConfig.
- Keyword Arguments:
excludes (list) – List of fully qualified model labels to exclude.
- Returns:
List of Datalookup nodes.
- Return type:
list
- get_models_exclusions(labels, excludes=None)[source]#
Returns model nodes for exclusions.
Exclusion gather the explicit exclude labels and the intersection between inclusions and missing models related to implied app from inclusion labels.
- Parameters:
labels (string or list) – App label or fully qualified model labels.
- Keyword Arguments:
excludes (list) – List of fully qualified model labels.
- Returns:
List of Datalookup nodes.
- Return type:
list
- class diskette.core.applications.DrainApplicationConfig(*args, **kwargs)[source]#
Special application to drain remaining models from apps.
On default a drain will dump anything that have not been defined from apps. Its base goal is to dump data from undefined applications.
- drain_excluded#
If enabled, the drain will accept to drain exclusion from applications which allow it. Else the drain will exclude also the application exclusion. Default is disabled.
- Type:
boolean
- property excludes#
Just returns exclude labels as given since drain excludes are meaningful enough.
- Returns:
Fully qualified model labels.
- Return type:
list