def test_ctypes03(self): "All accepted" ContentType.objects.clear_cache() with self.assertNumQueries(0): field = EntityCTypeChoiceField() ct1 = self.ct1 self.assertEqual(ct1, field.clean(ct1.id))
def test_ctypes02(self): "Setter" ct1 = self.ct1 field = EntityCTypeChoiceField() field.ctypes = [ct1] clean = field.clean self.assertEqual(ct1, clean(ct1.id)) self.assertFieldValidationError(EntityCTypeChoiceField, 'invalid_choice', clean, self.ct2.id, )
class CustomBrickConfigItemCreateForm(CremeModelForm): ctype = EntityCTypeChoiceField( label=_('Related resource'), widget=DynamicSelect(attrs={'autocomplete': True}), ) class Meta(CremeModelForm.Meta): model = CustomBrickConfigItem def __init__(self, *args, **kwargs): # super(CustomBrickConfigItemCreateForm, self).__init__(*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 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
class RTypeBrickItemAddCtypeForm(CremeModelForm): ctype = EntityCTypeChoiceField( label=_('Customised resource'), widget=DynamicSelect({'autocomplete': True}), ) class Meta: model = RelationBrickItem exclude = ('relation_type', ) def __init__(self, *args, **kwargs): # super(RTypeBrickItemAddCtypeForm, self).__init__(*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 used_ct_ids = frozenset( ct.id for ct, cells in instance.iter_cells()) # TODO: iter_ctypes() ?? ct_field.ctypes = (ct for ct in ct_field.ctypes if ct.id not in used_ct_ids) def save(self, *args, **kwargs): self.instance.set_cells(self.cleaned_data['ctype'], ()) # return super(RTypeBrickItemAddCtypeForm, self).save(*args, **kwargs) return super().save(*args, **kwargs)
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)
def test_invalid(self): self.assertFieldValidationError( EntityCTypeChoiceField, 'invalid_choice', EntityCTypeChoiceField().clean, self.ct3.id, )
def test_not_required(self): ct1 = self.ct1 ct2 = self.ct2 clean = EntityCTypeChoiceField(required=False).clean self.assertEqual(ct1, clean(ct1.id)) self.assertEqual(ct2, clean(ct2.id)) self.assertEqual(None, clean(''))
class CustomBrickConfigItemCreateForm(_CustomBrickConfigItemBaseForm): ctype = EntityCTypeChoiceField( label=_('Related resource'), widget=DynamicSelect(attrs={'autocomplete': True}), ) # class Meta(CremeModelForm.Meta): # model = CustomBrickConfigItem 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
def test_required(self): ct1 = self.ct1 ct2 = self.ct2 clean = EntityCTypeChoiceField().clean self.assertEqual(ct1, clean(ct1.id)) self.assertEqual(ct2, clean(ct2.id)) self.assertFieldValidationError(EntityCTypeChoiceField, 'required', clean, '')
class ButtonMenuAddForm(CremeForm): ctype = EntityCTypeChoiceField( label=_('Related resource'), # help_text=_('The buttons related to this type of resource ' # 'will be chosen by editing the configuration' # ), ) def __init__(self, *args, **kwargs): # super(ButtonMenuAddForm, self).__init__(*args, **kwargs) super().__init__(*args, **kwargs) used_ct_ids = set( ButtonMenuItem.objects.exclude( content_type=None).distinct().values_list('content_type_id', flat=True)) ct_field = self.fields['ctype'] ct_field.ctypes = (ct for ct in ct_field.ctypes if ct.id not in used_ct_ids) def save(self): bmi = ButtonMenuItem(content_type=self.cleaned_data['ctype'], button_id='', order=1) generate_string_id_and_save(ButtonMenuItem, [bmi], _PREFIX)
def test_ctypes01(self): "Constructor" ct1 = self.ct1 with self.assertNumQueries(0): field = EntityCTypeChoiceField(ctypes=[ct1]) clean = field.clean self.assertEqual(ct1, clean(ct1.id)) self.assertFieldValidationError(EntityCTypeChoiceField, 'invalid_choice', clean, self.ct2.id, )
def formfield(self, instance, user, **kwargs): has_perm = user.has_perm_to_create get_ct = ContentType.objects.get_for_model return EntityCTypeChoiceField( label=self.verbose_name, # TODO: accept models too ? ctypes=[ # ctype # for ctype in self.registry.ctypes # if has_perm(ctype.model_class()) get_ct(model) for model in self.registry.models if has_perm(model) ], )
class CustomFieldsCTAddForm(CustomFieldsBaseForm): content_type = EntityCTypeChoiceField( label=_('Related resource'), help_text=_('The other custom fields for this type of resource ' 'will be chosen by editing the configuration'), widget=DynamicSelect({'autocomplete': True})) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) used_ct_ids = { *CustomField.objects.values_list('content_type_id', flat=True) } ct_field = self.fields['content_type'] ct_field.ctypes = (ct for ct in ct_field.ctypes if ct.id not in used_ct_ids)
class RecurrentGeneratorCreateForm(RecurrentGeneratorEditForm): ct = EntityCTypeChoiceField(label=_('Type of resource used as template')) def __init__(self, *args, **kwargs): from ..registry import recurrent_registry super().__init__(*args, **kwargs) has_perm = self.user.has_perm_to_create self.fields['ct'].ctypes = [ ctype for ctype in recurrent_registry.ctypes if has_perm(ctype.model_class()) ] def save(self, *args, **kwargs): self.instance.ct = self.cleaned_data['ct'] return super().save(*args, **kwargs)
class ButtonMenuAddForm(CremeForm): ctype = EntityCTypeChoiceField(label=_('Related resource')) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) used_ct_ids = { *ButtonMenuItem.objects.exclude(content_type=None) .distinct() .values_list('content_type_id', flat=True) } ct_field = self.fields['ctype'] ct_field.ctypes = (ct for ct in ct_field.ctypes if ct.id not in used_ct_ids) def save(self, commit=True): bmi = ButtonMenuItem(content_type=self.cleaned_data['ctype'], button_id='', order=1) if commit: bmi.save() return bmi