diff options
| author | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-05-21 14:10:15 +0200 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-06-17 13:21:28 +0200 | 
| commit | e1add4422cd9801a2683b4c9f68b2e9e60a9f28a (patch) | |
| tree | 8d42915f90dc0a3b575604560c5521211f1d7567 /archaeological_operations/models.py | |
| parent | 2b1f1b5e2473f824acf804a0d8ed398721b05104 (diff) | |
| download | Ishtar-e1add4422cd9801a2683b4c9f68b2e9e60a9f28a.tar.bz2 Ishtar-e1add4422cd9801a2683b4c9f68b2e9e60a9f28a.zip  | |
Optimize tables query for operations and sites
Diffstat (limited to 'archaeological_operations/models.py')
| -rw-r--r-- | archaeological_operations/models.py | 76 | 
1 files changed, 69 insertions, 7 deletions
diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py index 6b2cf4ec3..5bc730018 100644 --- a/archaeological_operations/models.py +++ b/archaeological_operations/models.py @@ -114,7 +114,19 @@ class ArchaeologicalSite(DocumentItem, BaseHistorizedItem, QRCodeItem,      APP = "archaeological-operations"      MODEL = "archaeological-site"      SHOW_URL = 'show-site' -    TABLE_COLS = ['reference', 'name', 'towns_label', 'periods', 'remains'] +    TABLE_COLS = ['reference', 'name', 'cached_towns_label', +                  'cached_periods', 'cached_remains'] +    NEW_QUERY_ENGINE = True +    COL_LABELS = { +        'cached_towns_label': _("Towns"), +        'cached_periods': _("Periods"), +        'cached_remains': _("Remains"), +    } +    EXTRA_REQUEST_KEYS = { +        'cached_towns_label': 'cached_towns_label', +        'cached_periods': 'cached_periods', +        'cached_remains': 'remains', +    }      LONG_SLUG = 'archaeologicalsite'      STATISTIC_MODALITIES_OPTIONS = OrderedDict([ @@ -149,8 +161,6 @@ class ArchaeologicalSite(DocumentItem, BaseHistorizedItem, QRCodeItem,      DATED_FIELDS = ['sinking_date'] -    COL_LABELS = {'towns_label': _("Towns")} -      EXTRA_REQUEST_KEYS = {          'towns_label': 'towns',          'collaborators__pk': 'collaborators__pk',  # dynamic_table_documents @@ -242,6 +252,8 @@ class ArchaeologicalSite(DocumentItem, BaseHistorizedItem, QRCodeItem,          ('operation', 'operations__pk'),      ]      HISTORICAL_M2M = ['periods', 'remains', 'towns'] +    CACHED_LABELS = ['cached_label', 'cached_towns_label', 'cached_periods', +                     'cached_remains']      # objects = SiteManager() @@ -293,6 +305,18 @@ class ArchaeologicalSite(DocumentItem, BaseHistorizedItem, QRCodeItem,          verbose_name=_("Main image"), blank=True, null=True)      cached_label = models.TextField(_("Cached name"),                                      null=True, blank=True, db_index=True) +    cached_towns_label = models.TextField( +        _("Cached town label"), blank=True, null=True, +        help_text=_("Generated automatically - do not edit") +    ) +    cached_periods =  models.TextField( +        _("Cached periods label"), blank=True, null=True, +        help_text=_("Generated automatically - do not edit") +    ) +    cached_remains =  models.TextField( +        _("Cached remains label"), blank=True, null=True, +        help_text=_("Generated automatically - do not edit") +    )      history = HistoricalRecords(bases=[HistoryModel]) @@ -379,6 +403,15 @@ class ArchaeologicalSite(DocumentItem, BaseHistorizedItem, QRCodeItem,                  ]))          return name +    def _generate_cached_towns_label(self): +        return self.towns_label() or "-" + +    def _generate_cached_remains(self): +        return " & ".join([str(remain) for remain in self.remains.all()]) or "-" + +    def _generate_cached_periods(self): +        return " & ".join([str(period) for period in self.periods.all()]) or "-" +      # def natural_key(self):      #     return (self.reference, ) @@ -390,7 +423,7 @@ class ArchaeologicalSite(DocumentItem, BaseHistorizedItem, QRCodeItem,          return [town.label_with_areas for town in self.towns.all()]      def towns_label(self): -        return " - ".join(self.towns_codes()) +        return " & ".join(self.towns_codes())      def get_town_centroid(self):          q = self.towns.filter(center__isnull=False).annotate( @@ -535,9 +568,10 @@ class Operation(ClosedItem, DocumentItem, BaseHistorizedItem, QRCodeItem,      APP = "archaeological-operations"      MODEL = "operation"      SHOW_URL = 'show-operation' -    TABLE_COLS = ['code_patriarche', 'year', 'towns_label', 'common_name', -                  'operation_type', 'start_date', 'excavation_end_date', -                  'remains'] +    TABLE_COLS = ['code_patriarche', 'year', 'cached_towns_label', +                  'common_name', 'operation_type__label', 'start_date', +                  'excavation_end_date', 'cached_remains'] +    NEW_QUERY_ENGINE = True      # statistics      STATISTIC_MODALITIES_OPTIONS = OrderedDict([          ('year', _("Year")), @@ -563,6 +597,7 @@ class Operation(ClosedItem, DocumentItem, BaseHistorizedItem, QRCodeItem,          'documentation_deadline__gte', 'finds_deadline__lte',          'finds_deadline__gte']      EXTRA_REQUEST_KEYS = { +        'operation_type__label': 'operation_type__label',          'common_name': 'common_name__icontains',          'cached_label': 'cached_label__icontains',          'comment': 'comment__icontains', @@ -615,6 +650,10 @@ class Operation(ClosedItem, DocumentItem, BaseHistorizedItem, QRCodeItem,          'archaeological_sites__reference':          _("Archaeological sites (reference)"),          'towns_label': _("Towns"), +        'operation_type__label': _("Operation type"), +        'cached_towns_label': _("Towns"), +        'cached_periods': _("Periods"), +        'cached_remains': _("Remains"),      }      BASE_SEARCH_VECTORS = [          SearchVectorConfig("abstract", "local"), @@ -657,6 +696,8 @@ class Operation(ClosedItem, DocumentItem, BaseHistorizedItem, QRCodeItem,              )          },      } +    CACHED_LABELS = ['cached_label', 'cached_towns_label', 'cached_periods', +                     'cached_remains']      objects = OperationManager()      # alternative names of fields for searches @@ -967,6 +1008,18 @@ class Operation(ClosedItem, DocumentItem, BaseHistorizedItem, QRCodeItem,                                                blank=True, null=True)      name_of_the_protagonist = models.TextField(_("Name of the protagonist"),                                                 blank=True, null=True) +    cached_towns_label = models.TextField( +        _("Cached town label"), blank=True, null=True, +        help_text=_("Generated automatically - do not edit") +    ) +    cached_periods =  models.TextField( +        _("Cached periods label"), blank=True, null=True, +        help_text=_("Generated automatically - do not edit") +    ) +    cached_remains =  models.TextField( +        _("Cached remains label"), blank=True, null=True, +        help_text=_("Generated automatically - do not edit") +    )      history = HistoricalRecords(bases=[HistoryModel]) @@ -1113,6 +1166,15 @@ class Operation(ClosedItem, DocumentItem, BaseHistorizedItem, QRCodeItem,          cached_label = settings.JOINT.join(items)          return cached_label +    def _generate_cached_towns_label(self): +        return self.towns_label() or "-" + +    def _generate_cached_remains(self): +        return " & ".join([str(remain) for remain in self.remains.all()]) or "-" + +    def _generate_cached_periods(self): +        return " & ".join([str(period) for period in self.periods.all()]) or "-" +      def _get_associated_cached_labels(self):          return list(self.context_record.all())  | 
