Esempio n. 1
0
class RTypeBrickItemAddCtypeForm(base.CremeModelForm):
    ctype = EntityCTypeChoiceField(
        label=_('Customised resource'),
        widget=core_widgets.DynamicSelect({'autocomplete': True}),
    )

    class Meta:
        model = RelationBrickItem
        exclude = ('relation_type',)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        instance = self.instance
        ct_field = self.fields['ctype']
        compatible_ctypes = instance.relation_type.object_ctypes.all()

        if compatible_ctypes:
            ct_field.ctypes = compatible_ctypes

        # TODO: iter_ctypes() ??
        used_ct_ids = frozenset(ct.id for ct, cells in instance.iter_cells())
        ct_field.ctypes = (ct for ct in ct_field.ctypes if ct.id not in used_ct_ids)

    def save(self, *args, **kwargs):
        # NB: we should set this in clean(), but it interfere when we re-using
        #     the same instance (see __init__)
        self.instance.set_cells(self.cleaned_data['ctype'], ())

        return super().save(*args, **kwargs)
Esempio n. 2
0
 class Meta:
     model = SetCredentials
     exclude = ('value', )  # fields ??
     widgets = {
         'set_type': creme_widgets.CremeRadioSelect,
         # TODO: always this widget for CTypeForeignKey ??
         'ctype': creme_widgets.DynamicSelect(attrs={'autocomplete': True}),
         'forbidden': creme_widgets.CremeRadioSelect,
     }
Esempio n. 3
0
class CustomBrickConfigItemCreateForm(_CustomBrickConfigItemBaseForm):
    ctype = EntityCTypeChoiceField(
        label=_('Related resource'),
        widget=core_widgets.DynamicSelect(attrs={'autocomplete': True}),
    )

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # TODO: add an 'exclude' argument in creme_entity_content_types() ??
        get_for_model = ContentType.objects.get_for_model
        is_invalid = gui_bricks.brick_registry.is_model_invalid
        self.fields['ctype'].ctypes = (
            get_for_model(model)
            for model in creme_registry.iter_entity_models()
            if not is_invalid(model)
        )

    def clean(self, *args, **kwargs):
        cdata = super().clean(*args, **kwargs)

        if not self._errors:
            self.instance.content_type = self.cleaned_data['ctype']

        return cdata
Esempio n. 4
0
class RTypeBrickAddForm(base.CremeModelForm):
    relation_type = forms.ModelChoiceField(
        RelationType.objects.none(), empty_label=None,
        widget=core_widgets.DynamicSelect(attrs={'autocomplete': True}),
    )

    class Meta(base.CremeModelForm.Meta):
        model = RelationBrickItem

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        existing_type_ids = RelationBrickItem.objects.values_list('relation_type_id', flat=True)

        self.fields['relation_type'].queryset = RelationType.objects.exclude(
            pk__in=existing_type_ids,
        )

    def save(self, *args, **kwargs):
        self.instance.brick_id = gui_bricks.SpecificRelationsBrick.generate_id(
            'creme_config',
            self.cleaned_data['relation_type'].id,
        )
        return super().save(*args, **kwargs)