summaries : Summary tables¶
A summary field is a readonly and otherwise regular database field whose value is computed at certain moments as a summary of other tables. This can be used as an alternative for virtual fields whose value is computed on the fly for each request.
Users also get a button ∑ on each model for which there are slave summaries.
Implementation of Summary Table
Implementation of Summary fields.
The most advanced use of summaries. Is a summary table used to calculate yearly and monthly worked time with clients based on meeting type and various other rules.
Another use of this module is to create summary fields.
Example: Client has two date fields active_from and active_until whose value is automatically computed based on all contracts with that client. They are not virtual fields because we want to sort and filter on them, and because their values aren't so very dynamic, they are the start and end date of the currently active contract.
Also it is likely required to update
if the field type doesn't support a value of 0
The application must declare them as summary fields by defining:
class Client(Summarized, ...): def reset_summary_data(self): self.active_from = None self.active_untill = None def get_summary_collectors(self): yield (self.update_active_from, Contracts.objects.filter(client=self).orderby("start_date")[0:1]) yield (self.update_active_until, Contracts.objects.filter(client=self).orderby("end_date")[0:1]) def update_active_from(self, obj): self.active_from = obj.start_date def update_active_untill(self, obj): self.active_untill = obj.end_date
Note that when a new contract is added the client's active_from and active_untill fields are not updated unless you run: Client.compute_summary_values()
Summarized model mixin¶
- class lino.modlib.summaries.Summarized¶
Model mixin for database objects that have summary fields.
Set this to True if all instances of this model should be considered temporary data to be deleted by
Action for updating all the summary fields on this database object.
Set all counters and sums to 0.
compute_summary_values()on a a filtered queryset based on keyword arguments.
To be implemented by subclasses. This must yield a sequence of
(collector, qs)tuples, where collector is a callable and qs a queryset. Lino will call collector for each obj in qs. The collector is responsible for updating that object.
- class lino.modlib.summaries.SlaveSummarized¶
Summarizedmodels that are related to a master.
The master is another database object for which this summary data applies. Implementing subclasses must define a field named
masterwhich must be a foreignkey to the master model.
- class lino.modlib.summaries.MonthlySummarized¶
Summarizedthat will have more than one entries per master, one for each month.
This plugin adds the following django-admin command.
Update the summary tables.
If no arguments are given, run it on all summaries.
Otherwise (not yet implemented) every positional argument is expected to be a model name in the form app_label.ModelName, and only these models are being updated.
This plugin installs a
Return a dict mapping each model which has at least one summary to a list of these summaries.
- class lino.modlib.summaries.UpdateSummariesByMaster¶
Update summary data for this object.