# 7. Views¶

A view is a set of filters and projections that together define a particular view on the datatable. Views can be predefined by the system administrator and shared with users. This approach allows users to quickly get at standard reports, and further explore them by adding additional filters and projections on top of the already defined ones in the view.

Preset or stored views are presented in a hierarchical menu. The view configuration define:

• the human readable name of a view and its description
• what filters are active and with what values
• what projections are active and using what sortorder
• what hierarchical category the view belongs to

It is important to stress that programmatically defined views are installed to a users’ configuration in the database at account creation time. Changes to config/views.py will only be seen by users after the new views have been pushed to the users’ in database configuration.

## 7.1. General structure¶

Global views are defined in config/views.py. These views can be made available to individual users by installing the views into their user accounts. See Administration for more information. config/views.py has the following form:

from pyramid.i18n import TranslationString as _

categories = {
'categoryname': {},
}

all = [{
'name': 'viewname',
'filters': { }, # filter settings
'flattable': { }, # projection settings when view is a flat table
'group': { },     # projection settings when view is a group by
'pivot': { },     # projection settings when view is a pivot
'category': 'categoryname'
},]


## 7.2. Category definition¶

Views are organized in the UI in categories. Categories are defined as a Python dictionary, where each dictionary key points to a category definition. The category definition can have these attributes:

• name – presentation name as displayed to the end-user. Typically constructed using i18n _ functioncall to allow for internationalisation.
• description – presentation description as displayed to the end-user. Typically constructed using the i18n _ functioncall.
• level – integer, position in the hierarchy, starts at 0.
• parent – when a category is a child, the parent of this child.
• locked – Python boolean indicating that this view definition cannot be modified by the end-user. The locked nature is also to select views for updating when a system administrator refreshes system views for a particular user.

Category definition examples

 categories = {
'predefined': {
'name': _(u"Predefined"),
'description': _(u"Predefined presets"),
'level': 0,
'parent': None,
'locked': True
},
'benchmark': {
'name': _(u"Benchmark"),
'description': _(u"Benchmark"),
'level': 1,
'parent': 'predefined',
'locked': True
},
'benchmarkdiab': {
'name': _(u"Diabetes"),
'description': _(u"Diabetes"),
'level': 2,
'parent': 'benchmark',
'locked': True
}
}


## 7.3. View definition¶

A view is constructed from a view definition. The following attributes may be used:

• name – presentation name as displayed to the end-user. Typically constructed using the i18n _ functioncall to allow for internationalisation.
• description – presentation description as displayed to the end-user. Typically constructed using the i18n _ functioncall.
• category – view category, a Python string referencing the category as defined in the previous section.
• filters – a Python dictionary that states for all active filter id s their selected values. Note that the selected values can be found as items of the listorder of a filter. As a special case the string most_recent can be used to select the most recent value as returned by a distinctlist filter.

For the projection definitions one of three attributes must be used:

• flattable – indicating that this view is a flat table without further aggregations. This is a Python dictionary with the following members:
• order – a Python array defining the active projections.
• sortorder – a Python array of tuples that defines the sortorder for a projection id. Note that only those projections that need a sort order need to be named here.
• group – this view is a group by. The dictionary has the following members:
• datafields – a Python array of tuples that defines the (projection, aggregation) to be used
• fields – a Python array that defines the projections to be grouped by. These will end up as individual rows with the defined fields as first columns shown in the row.
• sortorder – active sort order, as defined above.
• groupfractions – a Python dictionary defining the active group fractions. When considering a population partitioned over a number of groups, this shows per group what percentage of the total is represented here considering an additional group filter. So for example when considering an arbitrary grouping of patients, group filters can be used to return the percentage of patients with gender Male in a particular group.
• groupfractionorder – a Python array defining the order in which groupfractions are shown.
• pivot – this view is a group by, further partitioned over a set of (column) values. This dictionary has the following members:
• rows – a Python array defining the projections to group rows on.
• columns – a Python array defining the projections to group columns on. Note that each projection here is evaluated per row.
• datafields – a Python list of tuples defining the projections and aggregates to show in a row/column junction.
• groupfractions, groupfractionorder – defined as for group.

Some view examples:

all = [
{'name': _(u"Patient Overview"),
'filters': { 'startdate': 'most_recent' },
'flattable' : { 'order':['preferencedate', 'ppatientnumber', 'ppatientname',
'pyearofbirth','pgender','ptotalchol','pbmi', 'phba1c',
'pcockroft'],
'sortorder': [('ppatientname', 'ASC')]},
'description': _(u"An overview of patients with year of birth, " \
"totalalcohol, bmi, HbA1c mmol/mol and cockcroft " \
"scores."),
'category': 'predefined' },

{'name': _(u"Patient Count"),
'group': {  'datafields': [('ppatientnumber', 'count')],
'fields': ['ppp_name_diabetes'],
'groupfractions': {  },
'sortorder': [('ppatientnumber', 'DESC')]},
'description': _(u"Show number of patients per principal practitioner"),
'category': 'predefined'},

{'name': _(u"LDL per practitioner"),
'filters': {  'ldl': [u'Determined']},
'pivot': {  'columns': ['preferencedate'],
'datafields': [('pldl_cholesterol', 'avg')],
'groupfractions': {  },
'rows': ['ppp_name_diabetes']},
'description': _(u"Show the average ldl per primary practitioner"),
'category': 'predefined'
}]