onadata.apps.viewer.models package

Subpackages

Submodules

onadata.apps.viewer.models.column_rename module

ColumnRename model

class onadata.apps.viewer.models.column_rename.ColumnRename(*args, **kwargs)

Bases: Model

ColumnRename model

exception DoesNotExist

Bases: ObjectDoesNotExist

exception MultipleObjectsReturned

Bases: MultipleObjectsReturned

column_name

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

classmethod get_dict()

Returns a dictionary where xpath is key and column_name is value

id

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

objects = <django.db.models.manager.Manager object>
xpath

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

onadata.apps.viewer.models.data_dictionary module

DataDictionary model.

class onadata.apps.viewer.models.data_dictionary.DataDictionary(*args, **kwargs)

Bases: XForm

DataDictionary model class.

exception DoesNotExist

Bases: DoesNotExist

exception MultipleObjectsReturned

Bases: MultipleObjectsReturned

file_name()

Returns the XML filename based on the self.id_string.

metadata_set

Accessor to the related objects manager on the one-to-many relation created by GenericRelation.

In the example:

class Post(Model):
    comments = GenericRelation(Comment)

post.comments is a ReverseGenericManyToOneDescriptor instance.

save(*args, **kwargs)

Sets additional form properties before saving to the DB

tagged_items

Accessor to the related objects manager on the one-to-many relation created by GenericRelation.

In the example:

class Post(Model):
    comments = GenericRelation(Comment)

post.comments is a ReverseGenericManyToOneDescriptor instance.

onadata.apps.viewer.models.data_dictionary.create_follow_up_form(sender, instance=None, created=False, **kwargs)

Create a FollowUpForm for a form that consumes entities

Check if a form consumes data from a dataset that is an EntityList. If so, we create a FollowUpForm

onadata.apps.viewer.models.data_dictionary.create_registration_form(sender, instance=None, created=False, **kwargs)

Create a RegistrationForm for a form that defines entities

Create an EntityList if it does not exist. If it exists, use the the existing EntityList

onadata.apps.viewer.models.data_dictionary.disable_registration_form(sender, instance=None, created=False, **kwargs)

Disable registration form if form no longer contains entities definitions

onadata.apps.viewer.models.data_dictionary.invalidate_caches(sender, instance=None, created=False, **kwargs)

Invalidate caches

onadata.apps.viewer.models.data_dictionary.is_newline_error(error)

Return True is e is a new line error based on the error text. Otherwise return False.

onadata.apps.viewer.models.data_dictionary.process_xlsform(xls, default_name)

Process XLSForm file and return the survey dictionary for the XLSForm.

onadata.apps.viewer.models.data_dictionary.save_project(sender, instance=None, created=False, **kwargs)

Receive XForm project to update date_modified field of the project and on the next XHR request the form will be included in the project data.

onadata.apps.viewer.models.data_dictionary.set_object_permissions(sender, instance=None, created=False, **kwargs)

Apply the relevant object permissions for the form to all users who should have access to it.

onadata.apps.viewer.models.data_dictionary.sheet_to_csv(xls_content, sheet_name)

Writes a csv file of a specified sheet from a an excel file

Parameters:
  • xls_content – Excel file contents

  • sheet_name – the name of the excel sheet to generate the csv file

Returns:

a (StringIO) csv file object

onadata.apps.viewer.models.data_dictionary.upload_to(instance, filename, username=None)

Return XLSForm file upload path.

onadata.apps.viewer.models.export module

Export model.

class onadata.apps.viewer.models.export.Export(*args, **kwargs)

Bases: ExportBaseModel

Class representing a data export from an XForm

exception DoesNotExist

Bases: ObjectDoesNotExist

exception MultipleObjectsReturned

Bases: MultipleObjectsReturned

created_on

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

error_message

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

export_type

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

export_url

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

classmethod exports_outdated(xform, export_type, options=None)

Return True if export is outdated or there is no export matching the export_type with the specified options.

filedir

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

filename

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

get_export_type_display(*, field=<django.db.models.fields.CharField: export_type>)
get_next_by_created_on(*, field=<django.db.models.fields.DateTimeField: created_on>, is_next=True, **kwargs)
get_previous_by_created_on(*, field=<django.db.models.fields.DateTimeField: created_on>, is_next=False, **kwargs)
id

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

internal_status

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

classmethod is_filename_unique(xform, filename)

Return True if the filename is unique.

objects = <django.db.models.manager.Manager object>
options

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

save(*args, **kwargs)

Save the current instance. Override this in a subclass if you want to control the saving process.

The ‘force_insert’ and ‘force_update’ parameters can be used to insist that the “save” must be an SQL insert or update (or equivalent for non-SQL backends), respectively. Normally, they should not be set.

set_filename(filename)

Set the filename of an export and mark internal_status as Export.SUCCESSFUL.

task_id

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

time_of_last_submission

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

xform

Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.

In the example:

class Child(Model):
    parent = ForeignKey(Parent, related_name='children')

Child.parent is a ForwardManyToOneDescriptor instance.

xform_id
class onadata.apps.viewer.models.export.ExportBaseModel(*args, **kwargs)

Bases: Model

Export base model class

CSV_EXPORT = 'csv'
CSV_ZIP_EXPORT = 'csv_zip'
EXPORT_MIMES = {'csv': 'csv', 'csv_zip': 'zip', 'geojson': 'geo+json', 'kml': 'vnd.google-earth.kml+xml', 'osm': 'osm', 'sav': 'sav', 'sav_zip': 'zip', 'xls': 'vnd.ms-excel', 'xlsx': 'vnd.openxmlformats', 'zip': 'zip'}
EXPORT_OPTION_FIELDS = ['binary_select_multiples', 'dataview_pk', 'group_delimiter', 'include_images', 'include_labels', 'include_labels_only', 'include_hxl', 'language', 'query', 'remove_group_name', 'show_choice_labels', 'include_reviews', 'split_select_multiples', 'value_select_multiples', 'win_excel_utf8']
EXPORT_TYPES = [('xlsx', 'Excel'), ('csv', 'CSV'), ('zip', 'ZIP'), ('kml', 'kml'), ('csv_zip', 'CSV ZIP'), ('sav_zip', 'SAV ZIP'), ('sav', 'SAV'), ('external', 'Excel'), ('osm', 'osm'), ('gsheets', 'Google Sheets'), ('geojson', 'geojson')]
EXPORT_TYPE_DICT = {'csv': 'CSV', 'csv_zip': 'CSV ZIP', 'external': 'Excel', 'geojson': 'geojson', 'gsheets': 'Google Sheets', 'kml': 'kml', 'osm': 'osm', 'sav': 'SAV', 'sav_zip': 'SAV ZIP', 'xlsx': 'Excel', 'zip': 'ZIP'}
EXTERNAL_EXPORT = 'external'
FAILED = 2
GEOJSON_EXPORT = 'geojson'
GOOGLE_SHEETS_EXPORT = 'gsheets'
KML_EXPORT = 'kml'
MAX_EXPORTS = 10
class Meta

Bases: object

abstract = False
OSM_EXPORT = 'osm'
PENDING = 0
SAV_EXPORT = 'sav'
SAV_ZIP_EXPORT = 'sav_zip'
SUCCESSFUL = 1
XLSX_EXPORT = 'xlsx'
ZIP_EXPORT = 'zip'
created_on

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

error_message

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

export_type

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

export_url

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

filedir

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

filename

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

property filepath

Return the file path of an export file, None if the file does not exist.

property full_filepath

Return the full filepath of an export file, None if the file does not exist.

get_export_type_display(*, field=<django.db.models.fields.CharField: export_type>)
get_next_by_created_on(*, field=<django.db.models.fields.DateTimeField: created_on>, is_next=True, **kwargs)
get_previous_by_created_on(*, field=<django.db.models.fields.DateTimeField: created_on>, is_next=False, **kwargs)
internal_status

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

property is_pending

Return True if an export status is pending.

property is_successful

Return True if an export status successful.

options

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

property status

Return the status [FAILED|PENDING|SUCCESSFUL] of an export.

task_id

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

time_of_last_submission

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

exception onadata.apps.viewer.models.export.ExportConnectionError

Bases: Exception

ExportConnectionError exception class.

exception onadata.apps.viewer.models.export.ExportTypeError

Bases: Exception

ExportTypeError exception class.

class onadata.apps.viewer.models.export.GenericExport(*args, **kwargs)

Bases: ExportBaseModel

Generic Export model.

exception DoesNotExist

Bases: ObjectDoesNotExist

exception MultipleObjectsReturned

Bases: MultipleObjectsReturned

content_object

Provide a generic many-to-one relation through the content_type and object_id fields.

This class also doubles as an accessor to the related object (similar to ForwardManyToOneDescriptor) by adding itself as a model attribute.

content_type

Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation.

In the example:

class Child(Model):
    parent = ForeignKey(Parent, related_name='children')

Child.parent is a ForwardManyToOneDescriptor instance.

content_type_id
created_on

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

error_message

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

export_type

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

export_url

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

classmethod exports_outdated(instance, export_type, options=None)

Return True if export is outdated or there is no export matching the export_type with the specified options.

filedir

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

filename

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

get_export_type_display(*, field=<django.db.models.fields.CharField: export_type>)
get_next_by_created_on(*, field=<django.db.models.fields.DateTimeField: created_on>, is_next=True, **kwargs)
classmethod get_object_content_type(instance)

Returns the content_type of the instance.

get_previous_by_created_on(*, field=<django.db.models.fields.DateTimeField: created_on>, is_next=False, **kwargs)
id

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

internal_status

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

classmethod is_filename_unique(instance, filename)

Return True if the filename is unique, False otherwise

object_id

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

objects = <django.db.models.manager.Manager object>
options

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

save(*args, **kwargs)

Save the current instance. Override this in a subclass if you want to control the saving process.

The ‘force_insert’ and ‘force_update’ parameters can be used to insist that the “save” must be an SQL insert or update (or equivalent for non-SQL backends), respectively. Normally, they should not be set.

task_id

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

time_of_last_submission

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

onadata.apps.viewer.models.export.export_delete_callback(sender, **kwargs)

Delete export file when an export object is deleted.

onadata.apps.viewer.models.export.get_export_options_query_kwargs(options)

Get dict with options JSONField lookups for export options field

onadata.apps.viewer.models.parsed_instance module

ParsedInstance model

exception onadata.apps.viewer.models.parsed_instance.ParseError

Bases: Exception

Raise when an exception happens when parsing the XForm XML submission.

class onadata.apps.viewer.models.parsed_instance.ParsedInstance(*args, **kwargs)

Bases: Model

ParsedInstance - parsed XML submission, represents the XML submissions as a python

object.

DEFAULT_BATCHSIZE = 1000
DEFAULT_LIMIT = 1000000
exception DoesNotExist

Bases: ObjectDoesNotExist

exception MultipleObjectsReturned

Bases: MultipleObjectsReturned

STATUS = '_status'
USERFORM_ID = '_userform_id'
add_note(note)

Add a note for the instance.

classmethod dicts(xform)

Iterates over a forms submissions.

end_time

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

get_notes()

Returns a list of notes data objects.

id

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

instance

Accessor to the related object on the forward side of a one-to-one relation.

In the example:

class Restaurant(Model):
    place = OneToOneField(Place, related_name='restaurant')

Restaurant.place is a ForwardOneToOneDescriptor instance.

instance_id
lat

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

lng

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

objects = <django.db.models.manager.Manager object>
remove_note(note_id)

Deletes the note with the pk as note_id

save(*args, **kwargs)

Save the current instance. Override this in a subclass if you want to control the saving process.

The ‘force_insert’ and ‘force_update’ parameters can be used to insist that the “save” must be an SQL insert or update (or equivalent for non-SQL backends), respectively. Normally, they should not be set.

start_time

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

to_dict()

Returns a python dictionary object of a submission.

to_dict_for_mongo()

Return the XForm XML submission as a python object.

onadata.apps.viewer.models.parsed_instance.build_sql_where(xform, query, start=None, end=None)

Build SQL WHERE clause

onadata.apps.viewer.models.parsed_instance.datetime_from_str(text)

Parses a datetime from a string and returns the datetime object.

onadata.apps.viewer.models.parsed_instance.dict_for_mongo(item)

Validates the keys of a python object.

onadata.apps.viewer.models.parsed_instance.exclude_deleting_submissions_clause(xform_id: int) tuple[str, list[int]]

Return SQL clause to exclude submissions whose deletion is in progress

Parameters:

xform_id – XForm ID

Returns:

SQL and list of submission IDs under deletion

onadata.apps.viewer.models.parsed_instance.get_etag_hash_from_query(sql=None, params=None)

Returns md5 hash from the date_modified field or

onadata.apps.viewer.models.parsed_instance.get_name_from_survey_element(element)

Returns the abbreviated xpath of an element.

onadata.apps.viewer.models.parsed_instance.get_sql_with_params(xform, query=None, fields=None, sort=None, start=None, end=None, start_index=None, limit=None, json_only: bool = True)

Returns the SQL and related parameters

onadata.apps.viewer.models.parsed_instance.query_count(xform, query=None, date_created_gte=None, date_created_lte=None)

Count number of instances matching query

onadata.apps.viewer.models.parsed_instance.query_data(xform, query=None, sort=None, start=None, end=None, start_index=None, limit=None, json_only: bool = True)

Query the submissions table and returns the results

onadata.apps.viewer.models.parsed_instance.query_fields_data(xform, fields, query=None, sort=None, start=None, end=None, start_index=None, limit=None)

Query the submissions table and return json fields data

Module contents

Viewer models.