def create(str_pk,
               text,
               subject_ctypes=(),
               is_custom=False,
               generate_pk=False,
               is_copiable=True):
        """Helps the creation of new CremePropertyType.
        @param subject_ctypes: Sequence of CremeEntity classes/ContentType objects.
        @param generate_pk: If True, str_pk is used as prefix to generate pk.
        """
        if not generate_pk:
            property_type = CremePropertyType.objects.update_or_create(
                id=str_pk,
                defaults={
                    'text': text,
                    'is_custom': is_custom,
                    'is_copiable': is_copiable,
                })[0]
        else:
            from creme.creme_core.utils.id_generator import generate_string_id_and_save
            property_type = CremePropertyType(text=text,
                                              is_custom=is_custom,
                                              is_copiable=is_copiable)
            generate_string_id_and_save(CremePropertyType, [property_type],
                                        str_pk)

        get_ct = ContentType.objects.get_for_model
        property_type.subject_ctypes.set([
            model if isinstance(model, ContentType) else get_ct(model)
            for model in subject_ctypes
        ])

        return property_type
Example #2
0
    def save(self, *args, **kwargs):
        instance = self.instance
        ct = self.cleaned_data['ctype']
        instance.content_type = ct

        # super(CustomBrickConfigItemCreateForm, self).save(commit=False)
        super().save(commit=False)
        generate_string_id_and_save(
            CustomBrickConfigItem, [instance],
            'creme_core-user_customblock_{}-{}'.format(ct.app_label, ct.model))

        return instance
Example #3
0
    def save(self, *args, **kwargs):
        instance = self.instance

        if not instance.id:
            super().save(commit=False, *args, **kwargs)
            generate_string_id_and_save(
                ActivitySubType,
                [instance],
                'creme_config-useractivitydetailesubtype',
            )
        else:
            super().save(*args, **kwargs)

        return instance
Example #4
0
    def save(self):  # TODO: *args, **kwargs
        instance = self.instance

        if not instance.id:
            super().save(commit=False)
            generate_string_id_and_save(
                ActivityType,
                [instance],
                'creme_config-useractivitytype',
            )
        else:
            super().save()

        return instance
Example #5
0
    def save(self, *args, **kwargs):
        instance = self.instance
        conditions = self.get_cleaned_conditions()
        efilter = instance.efilter

        if conditions:
            role = instance.role
            cdata = self.cleaned_data
            name = cdata['name']
            use_or = cdata['use_or']
            ctype = instance.ctype or ContentType.objects.get_for_model(
                CremeEntity)

            if efilter is None:
                efilter = EntityFilter(
                    name=name,
                    entity_type=ctype,
                    filter_type=self.efilter_type,
                    use_or=use_or,
                )
                generate_string_id_and_save(
                    EntityFilter,
                    [efilter],
                    f'creme_core-credentials_{role.id}-',
                )
                instance.efilter = efilter
            else:
                update_model_instance(
                    efilter,
                    entity_type=ctype,
                    name=name,
                    use_or=use_or,
                )

            efilter.set_conditions(
                conditions,
                check_cycles=
                False,  # There cannot be a cycle without sub-filter.
                check_privacy=False,  # No sense here.
            )
            super().save(*args, **kwargs)
        elif efilter:
            instance.efilter = None
            super().save(*args, **kwargs)
            efilter.delete()
        else:
            super().save(*args, **kwargs)

        return instance
Example #6
0
    def save(self, commit=True, *args, **kwargs):
        instance = self.instance

        if instance.pk:
            super().save(*args, **kwargs)
        else:
            super().save(commit=False)

            if commit:
                ct = instance.content_type
                generate_string_id_and_save(
                    CustomBrickConfigItem, [instance],
                    f'creme_core-user_customblock_{ct.app_label}-{ct.model}',
                )

        return instance
Example #7
0
    def save(self, *args, **kwargs):
        instance = self.instance
        ct = self._entity_type

        super().save(commit=False, *args, **kwargs)
        generate_string_id_and_save(
            EntityFilter, [instance],
            f'creme_core-userfilter_{ct.app_label}-{ct.model}',
        )

        instance.set_conditions(
            self.get_cleaned_conditions(),
            # There cannot be a cycle because we are creating the filter right now
            check_cycles=False,
            check_privacy=False,  # Already checked in clean()
        )

        return instance
Example #8
0
    def save(self):
        button_ids = self.cleaned_data['button_ids']
        ct = self.ct
        BMI_objects = ButtonMenuItem.objects
        BMI_get = BMI_objects.get
        items_2_save = []

        if not button_ids:
            # No pk to BMI objects --> can delete() on queryset directly
            BMI_objects.filter(content_type=ct).delete()
            # No button for this content type -> fake button_id
            items_2_save.append(
                ButtonMenuItem(content_type=ct, button_id='', order=1))
        else:
            old_ids = {bmi.button_id for bmi in self.set_buttons}
            new_ids = set(button_ids)
            buttons_2_del = old_ids - new_ids
            buttons_2_add = new_ids - old_ids

            # No pk to BCI objects --> can delete() on queryset directly
            BMI_objects.filter(content_type=ct,
                               button_id__in=buttons_2_del).delete()

            offset = 1 if ct is None else 1000  # Default conf before ct's conf

            for i, button_id in enumerate(button_ids):
                if button_id in buttons_2_add:
                    items_2_save.append(
                        ButtonMenuItem(content_type=ct,
                                       button_id=button_id,
                                       order=i + offset))
                else:
                    bmi = BMI_get(content_type=ct, button_id=button_id)

                    if bmi.order != i + offset:
                        bmi.order = i + offset
                        bmi.save()

        generate_string_id_and_save(ButtonMenuItem, items_2_save, _PREFIX)
Example #9
0
    def create(
            subject_desc: tuple,
            object_desc: tuple,
            is_custom: bool = False,
            generate_pk: bool = False,
            is_internal: bool = False,
            is_copiable: Union[bool, Tuple[bool, bool]] = (True, True),
            minimal_display: Tuple[bool, bool] = (False, False),
    ) -> Tuple['RelationType', 'RelationType']:
        """
        @param subject_desc: Tuple
               (string_pk, predicate_string
                [, sequence_of_cremeEntityClasses [, sequence_of_propertyTypes]]
               )
        @param object_desc: See subject_desc.
        @param generate_pk: If True, 'string_pk' args are used as prefix to generate pks.
        """
        # In case sequence_of_cremeEntityClasses or sequence_of_propertyType not given.
        padding = ((), ())

        subject_desc += padding
        object_desc  += padding

        if isinstance(is_copiable, bool):
            is_copiable = (is_copiable, is_copiable)

        pk_subject   = subject_desc[0]
        pk_object    = object_desc[0]
        pred_subject = subject_desc[1]
        pred_object  = object_desc[1]

        if not generate_pk:
            update_or_create = RelationType.objects.update_or_create
            defaults = {'is_custom': is_custom, 'is_internal': is_internal}
            sub_relation_type = update_or_create(
                id=pk_subject,
                defaults={
                    **defaults,
                    'predicate': pred_subject,
                    'is_copiable': is_copiable[0],
                    'minimal_display': minimal_display[0],
                },
            )[0]
            obj_relation_type = update_or_create(
                id=pk_object,
                defaults={
                    **defaults,
                    'predicate': pred_object,
                    'is_copiable': is_copiable[1],
                    'minimal_display': minimal_display[1],
                },
            )[0]
        else:
            from creme.creme_core.utils.id_generator import (
                generate_string_id_and_save,
            )

            sub_relation_type = RelationType(
                predicate=pred_subject, is_custom=is_custom, is_internal=is_internal,
                is_copiable=is_copiable[0], minimal_display=minimal_display[0],
            )
            obj_relation_type = RelationType(
                predicate=pred_object,  is_custom=is_custom, is_internal=is_internal,
                is_copiable=is_copiable[1], minimal_display=minimal_display[1],
            )

            generate_string_id_and_save(RelationType, [sub_relation_type], pk_subject)
            generate_string_id_and_save(RelationType, [obj_relation_type], pk_object)

        sub_relation_type.symmetric_type = obj_relation_type
        obj_relation_type.symmetric_type = sub_relation_type

        # Delete old m2m (TODO: just remove useless ones ???)
        for rt in (sub_relation_type, obj_relation_type):
            rt.subject_ctypes.clear()
            rt.subject_properties.clear()

        get_ct = ContentType.objects.get_for_model

        for subject_ctype in subject_desc[2]:
            sub_relation_type.subject_ctypes.add(get_ct(subject_ctype))

        for object_ctype in object_desc[2]:
            obj_relation_type.subject_ctypes.add(get_ct(object_ctype))

        for subject_prop in subject_desc[3]:
            sub_relation_type.subject_properties.add(subject_prop)

        for object_prop in object_desc[3]:
            obj_relation_type.subject_properties.add(object_prop)

        sub_relation_type.save()
        obj_relation_type.save()

        return sub_relation_type, obj_relation_type
Example #10
0
 def save(self):
     bmi = ButtonMenuItem(content_type=self.cleaned_data['ctype'],
                          button_id='',
                          order=1)
     generate_string_id_and_save(ButtonMenuItem, [bmi], _PREFIX)