Skip to content

Commit

Permalink
Use the dict format of FieldBlockManager everywhere.
Browse files Browse the repository at this point in the history
  • Loading branch information
genglert committed Feb 8, 2021
1 parent 14b247f commit 4b45dc8
Show file tree
Hide file tree
Showing 26 changed files with 402 additions and 234 deletions.
13 changes: 7 additions & 6 deletions creme/activities/forms/mass_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,12 +602,13 @@ class ActivityMassImportForm(ImportForm4CremeEntity):
class Meta:
exclude = ('type', 'sub_type', 'busy')

blocks = ImportForm4CremeEntity.blocks.new(
(
'participants', _('Participants & subjects'),
['my_participation', 'participating_users', 'participants', 'subjects']
),
)
blocks = ImportForm4CremeEntity.blocks.new({
'id': 'participants',
'label': _('Participants & subjects'),
'fields': [
'my_participation', 'participating_users', 'participants', 'subjects',
],
})

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down
30 changes: 18 additions & 12 deletions creme/billing/forms/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,15 @@ class ProductLineMultipleAddForm(_LineMultipleAddForm):
items = MultiCreatorEntityField(label=_('Products'), model=products.get_product_model())

blocks = core_forms.FieldBlockManager(
('general', _('Products choice'), ['items']),
(
'additional',
_('Optional global information applied to your selected products'),
['quantity', 'vat', 'discount_value'],
)
{
'id': 'general',
'label': _('Products choice'),
'fields': ['items'],
}, {
'id': 'additional',
'label': _('Optional global information applied to your selected products'),
'fields': ['quantity', 'vat', 'discount_value'],
},
)

def _get_line_class(self):
Expand All @@ -104,12 +107,15 @@ class ServiceLineMultipleAddForm(_LineMultipleAddForm):
items = MultiCreatorEntityField(label=_('Services'), model=products.get_service_model())

blocks = core_forms.FieldBlockManager(
('general', _('Services choice'), ['items']),
(
'additional',
_('Optional global information applied to your selected services'),
['quantity', 'vat', 'discount_value'],
)
{
'id': 'general',
'label': _('Services choice'),
'fields': ['items'],
}, {
'id': 'additional',
'label': _('Optional global information applied to your selected services'),
'fields': ['quantity', 'vat', 'discount_value'],
},
)

def _get_line_class(self):
Expand Down
8 changes: 5 additions & 3 deletions creme/commercial/forms/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ class _SegmentForm(_AuxForm):
'duplicated_property': _('A property with the name «%(name)s» already exists'),
}

blocks = FieldBlockManager(
('general', _('General information'), ['name', 'product', 'place', 'price', 'promotion']),
)
blocks = FieldBlockManager({
'id': 'general',
'label': _('General information'),
'fields': ['name', 'product', 'place', 'price', 'promotion'],
})

class Meta:
model = MarketSegmentDescription
Expand Down
20 changes: 11 additions & 9 deletions creme/creme_config/forms/bricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,11 @@ class BrickDetailviewLocationsAddForm(_BrickDetailviewLocationsForm):
)

# TODO: manage Meta.fields in '*'
blocks = base.FieldBlockManager(
('general', _('Configuration'), ('role', 'hat', 'top', 'left', 'right', 'bottom')),
)
blocks = base.FieldBlockManager({
'id': 'general',
'label': _('Configuration'),
'fields': ('role', 'hat', 'top', 'left', 'right', 'bottom'),
})

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand All @@ -255,15 +257,15 @@ def __init__(self, *args, **kwargs):
hat_f.initial = hat_f.choices[0][0]

def save(self, *args, **kwargs):
self.role = role = self.cleaned_data['role']
self.role = role = self.cleaned_data['role']
self.superuser = (role is None)
super().save(*args, **kwargs)


class BrickDetailviewLocationsEditForm(_BrickDetailviewLocationsForm):
def __init__(self, role, superuser, *args, **kwargs):
super().__init__(*args, **kwargs)
self.role = role
self.role = role
self.superuser = superuser

self.locations = locations = BrickDetailviewLocation.objects.filter(
Expand Down Expand Up @@ -328,7 +330,7 @@ class BrickHomeLocationsEditionForm(_BrickLocationsForm):

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

self.locations = locations = BrickHomeLocation.objects.filter(
Expand Down Expand Up @@ -529,9 +531,9 @@ def clean(self, *args, **kwargs):
class CustomBrickConfigItemEditForm(_CustomBrickConfigItemBaseForm):
cells = EntityCellsField(label=_('Lines'))

blocks = base.CremeModelForm.blocks.new(
('cells', 'Columns', ['cells']),
)
blocks = base.CremeModelForm.blocks.new({
'id': 'cells', 'label': 'Columns', 'fields': ['cells'],
})

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down
11 changes: 9 additions & 2 deletions creme/creme_config/forms/custom_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,15 @@ class CustomFormGroupForm(CremeModelForm):
cells = CustomFormCellsField(label=_('Fields'))

blocks = FieldBlockManager(
('name', 'Name', ('name',)),
('cells', 'Fields', ('cells',)),
{
'id': 'name',
'label': 'Name',
'fields': ('name',),
}, {
'id': 'cells',
'label': 'Fields',
'fields': ('cells',),
},
)

class Meta:
Expand Down
4 changes: 3 additions & 1 deletion creme/creme_config/forms/generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,9 @@ def replacer(self, new_value):


class DeletionForm(CremeModelForm):
blocks = FieldBlockManager(('general', _('Replacement'), '*'))
blocks = FieldBlockManager({
'id': 'general', 'label': _('Replacement'), 'fields': '*',
})

# TODO: what about deletion.DO_NOTHING ?!
fk_handler_classes = {
Expand Down
44 changes: 27 additions & 17 deletions creme/creme_config/forms/relation_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

################################################################################
# Creme is a free/open-source Customer Relationship Management software
# Copyright (C) 2009-2020 Hybird
# Copyright (C) 2009-2021 Hybird
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
Expand Down Expand Up @@ -38,14 +38,18 @@
SemiFixedRelationType,
)

_CTypesField = partial(MultiEntityCTypeChoiceField, required=False,
label=_('Type constraint'),
help_text=_('No constraint means that all types are accepted.'),
)
_PropertyTypesField = partial(ModelMultipleChoiceField, required=False,
label=_('Properties constraint'),
queryset=CremePropertyType.objects.all(),
)
_CTypesField = partial(
MultiEntityCTypeChoiceField,
required=False,
label=_('Type constraint'),
help_text=_('No constraint means that all types are accepted.'),
)
_PropertyTypesField = partial(
ModelMultipleChoiceField,
required=False,
label=_('Properties constraint'),
queryset=CremePropertyType.objects.all(),
)


class RelationTypeCreateForm(CremeForm):
Expand Down Expand Up @@ -90,16 +94,22 @@ class RelationTypeCreateForm(CremeForm):
)

blocks = FieldBlockManager(
('subject', _('Subject'), ['subject_ctypes', 'subject_properties']),
(
'predicate',
_('Verb/Predicate'),
[
{
'id': 'subject',
'label': _('Subject'),
'fields': ['subject_ctypes', 'subject_properties'],
}, {
'id': 'predicate',
'label': _('Verb/Predicate'),
'fields': [
'subject_predicate', 'subject_is_copiable', 'subject_min_display',
'object_predicate', 'object_is_copiable', 'object_min_display',
'object_predicate', 'object_is_copiable', 'object_min_display',
],
),
('object', _('Object'), ['object_ctypes', 'object_properties']),
}, {
'id': 'object',
'label': _('Object'),
'fields': ['object_ctypes', 'object_properties'],
},
)

def __init__(self, instance=None, *args, **kwargs):
Expand Down
47 changes: 26 additions & 21 deletions creme/creme_config/forms/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

################################################################################
# Creme is a free/open-source Customer Relationship Management software
# Copyright (C) 2017-2020 Hybird
# Copyright (C) 2017-2021 Hybird
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
Expand Down Expand Up @@ -35,20 +35,24 @@


class ImportForm(CremeForm):
config = FileField(label=_('Configuration file'),
help_text=_('A JSON file created with the export button '
'(generally in another Creme instance).'
),
# max_length=
)
config = FileField(
label=_('Configuration file'),
help_text=_(
'A JSON file created with the export button '
'(generally in another Creme instance).'
),
# max_length=
)

error_messages = {
'invalid_json': _('File content is not valid JSON.'),
'invalid_data': _('File content is not valid (%(error)s).'),
'invalid_version': _('The file has an unsupported version.'),
}

blocks = FieldBlockManager(('general', _('Configuration file'), '*'))
blocks = FieldBlockManager({
'id': 'general', 'label': _('Configuration file'), 'fields': '*',
})

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand All @@ -61,9 +65,9 @@ def clean_config(self):
deserialized_data = json_load('\n'.join(line.decode() for line in config))
except Exception as e:
logger.warning('ImportForm: invalid JSON (%s)', e)
raise ValidationError(self.error_messages['invalid_json'],
code='invalid_json',
)
raise ValidationError(
self.error_messages['invalid_json'], code='invalid_json',
)
else:
if not isinstance(deserialized_data, dict):
raise ValidationError(
Expand All @@ -73,25 +77,26 @@ def clean_config(self):
)

if deserialized_data.get('version') != '1.0':
raise ValidationError(self.error_messages['invalid_version'],
code='invalid_version',
)
raise ValidationError(
self.error_messages['invalid_version'], code='invalid_version',
)

validated_data = defaultdict(set)

try:
for importer in self._importers:
importer.validate(deserialized_data=deserialized_data,
validated_data=validated_data,
)
importer.validate(
deserialized_data=deserialized_data,
validated_data=validated_data,
)
except ValidationError:
raise
except Exception as e:
logger.exception('Error in ImportForm.clean_config()')
raise ValidationError(self.error_messages['invalid_data'],
params={'error': e},
code='invalid_data',
)
raise ValidationError(
self.error_messages['invalid_data'],
params={'error': e}, code='invalid_data',
)

return config

Expand Down
49 changes: 38 additions & 11 deletions creme/creme_config/forms/user_role.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,15 @@ class CredentialsGeneralStep(CremeModelForm):
])

blocks = FieldBlockManager(
('general', _('General information'), '*'),
('actions', _('Actions'), [*PERM_FIELDS.keys()]),
{
'id': 'general',
'label': _('General information'),
'fields': '*',
}, {
'id': 'actions',
'label': _('Actions'),
'fields': [*PERM_FIELDS.keys()],
},
)

class Meta:
Expand Down Expand Up @@ -169,8 +176,15 @@ class Meta:
}

blocks = FieldBlockManager(
('general', _('Filter'), ('name', 'use_or')),
('conditions', _('Conditions'), '*'),
{
'id': 'general',
'label': _('Filter'),
'fields': ('name', 'use_or'),
}, {
'id': 'conditions',
'label': _('Conditions'),
'fields': '*',
},
)

step_help_message = _(
Expand Down Expand Up @@ -536,11 +550,17 @@ def save(self, commit=True, *args, **kwargs):

class UserRoleCredentialsGeneralStep(CredentialsGeneralStep):
blocks = FieldBlockManager(
('general', _('First credentials: main information'), '*'),
(
'actions', _('First credentials: actions'),
['can_view', 'can_change', 'can_delete', 'can_link', 'can_unlink'],
),
{
'id': 'general',
'label': _('First credentials: main information'),
'fields': '*',
}, {
'id': 'actions',
'label': _('First credentials: actions'),
'fields': [
'can_view', 'can_change', 'can_delete', 'can_link', 'can_unlink',
],
},
)

def __init__(self, role, *args, **kwargs):
Expand All @@ -567,8 +587,15 @@ def save(self, commit=False, *args, **kwargs):

class UserRoleCredentialsFilterStep(CredentialsFilterStep):
blocks = FieldBlockManager(
('general', _('First credentials: filter'), ('name', 'use_or')),
('conditions', _('First credentials: conditions'), '*'),
{
'id': 'general',
'label': _('First credentials: filter'),
'fields': ('name', 'use_or'),
}, {
'id': 'conditions',
'label': _('First credentials: conditions'),
'fields': '*',
},
)

def __init__(self, role, *args, **kwargs):
Expand Down
Loading

0 comments on commit 4b45dc8

Please sign in to comment.