Exemple #1
0
 def test_term_decompress(self):
     self.assertEqual(TermModel.decompress(value=[4, 5]), {
         1: [[[], []]],
         2: [[], []],
         4: [],
         5: []
     })
Exemple #2
0
def invalidate_after_terms_set_changed(sender, instance, **kwargs):
    if getattr(instance, "_during_terms_normalization", False):
        return

    action = kwargs.pop('action', None)
    if action in ["pre_remove", "pre_add"]:
        valid_pk_set = getattr(instance, "_valid_pk_set", None)
        if valid_pk_set is None:
            valid_pk_set = set()
            setattr(instance, "_valid_pk_set", valid_pk_set)

        pk_set = kwargs.pop('pk_set')

        if getattr(instance, "_during_terms_validation", False):
            valid_pk_set.update(pk_set)
            return

        normal_pk_set = set(
            TermModel.objects.filter(pk__in=pk_set).exclude(
                system_flags=TermModel.system_flags.
                external_tagging_restriction).order_by().values_list(
                    'id', flat=True))
        normal_pk_set.difference_update(valid_pk_set)

        pk_set.clear()
        pk_set.update(normal_pk_set)

        if action == "pre_add":
            # normalize terms set
            origin_pk_set = set(instance.terms.values_list('id', flat=True))
            tree = TermModel.decompress(origin_pk_set | pk_set, fix_it=False)
            normal_pk_set = set(
                [x.term.id for x in tree.values() if x.is_leaf])
            # pk set to add
            pk_set_difference = normal_pk_set - origin_pk_set
            pk_set.clear()
            pk_set.update(pk_set_difference)
            # pk set to remove
            pk_set_difference = origin_pk_set - normal_pk_set
            if pk_set_difference:
                instance._during_terms_normalization = True
                instance.terms.remove(*list(pk_set_difference))
                del instance._during_terms_normalization

            if pk_set:
                external_add_terms.send(sender=instance.__class__,
                                        instance=instance,
                                        pk_set=pk_set)
        else:
            if pk_set:
                external_remove_terms.send(sender=instance.__class__,
                                           instance=instance,
                                           pk_set=pk_set)
Exemple #3
0
 def data_mart_available_terms_ids(self):
     """
     Return available terms ids for current data mart and filters
     """
     # дерево терминов вычисляется на этапе фильтрации, помимо витрины данных учитываются дополнительные фильтры
     tree = self.context.get('initial_filter_meta', None)
     if tree is None:
         # при POST запросе потребуется вычислить дерево терминов, поскольку фильтрация не производится
         data_mart = self.data_mart_from_request
         if data_mart is not None:
             tree = TermModel.decompress(data_mart.active_terms_ids)
         else:
             return []
     # Удаляем термины с ограничением на установку извне
     return [key for key, value in tree.expand().items() if not value.term.system_flags.external_tagging_restriction]
Exemple #4
0
def invalidate_after_terms_set_changed(sender, instance, **kwargs):
    if getattr(instance, "_during_terms_normalization", False):
        return

    action = kwargs.pop('action', None)
    if action in ["pre_remove", "pre_add"]:

        valid_pk_set = getattr(instance, "_valid_pk_set", None)
        if valid_pk_set is None:
            valid_pk_set = set()
            setattr(instance, "_valid_pk_set", valid_pk_set)

        pk_set = kwargs.pop('pk_set', None)

        if getattr(instance, "_during_terms_validation", False):
            valid_pk_set.update(pk_set)
            return

        if instance.system_flags.change_terms_restriction:
            # disable updating terms set on change restriction
            pk_set.clear()
            return

        pk_set.difference_update(valid_pk_set)

        if action == "pre_add":
            # normalize terms set
            origin_pk_set = set(instance.terms.values_list('id', flat=True))
            tree = TermModel.decompress(origin_pk_set | pk_set, fix_it=True)
            normal_pk_set = set(
                [x.term.id for x in tree.values() if x.is_leaf])
            # pk set to add
            pk_set_difference = normal_pk_set - origin_pk_set
            pk_set.clear()
            pk_set.update(pk_set_difference)
            # pk set to remove
            pk_set_difference = origin_pk_set - normal_pk_set
            if pk_set_difference:
                instance._during_terms_normalization = True
                instance.terms.remove(*list(pk_set_difference))
                del instance._during_terms_normalization

    elif action in ['post_add', 'post_remove']:
        # clear cache
        keys = get_data_mart_all_active_terms_keys()
        cache.delete_many(keys)
Exemple #5
0
 def test_term_decompress_fix_it_true(self):
     self.assertEqual(TermModel.decompress(value=[4, 5], fix_it=True), {
         1: [[]],
         2: []
     })
Exemple #6
0
 def filter_data_mart_pk(self, name, queryset, value):
     self._data_mart_id = value
     if self.data_mart_id is None:
         return queryset
     return queryset.filter(id__in=TermModel.decompress(self.data_mart_term_ids, fix_it=True).keys())