Exemple #1
0
    def __init__(self, choice_to_delete, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.choice_to_delete = choice_to_delete

        if choice_to_delete.custom_field.value_class.objects.filter(
            value=choice_to_delete
        ).exists():
            self.fields['to_choice'] = forms.ModelChoiceField(
                label=_('Choose a choice to transfer to'),
                help_text=_(
                    'The selected choice will replace the deleted one '
                    'in entities which use it.'
                ),
                required=False,
                queryset=CustomFieldEnumValue.objects.filter(
                    custom_field=choice_to_delete.custom_field,
                ).exclude(id=choice_to_delete.id),
            )
        else:
            self.fields['info'] = core_fields.ReadonlyMessageField(
                label=_('Information'),
                initial=gettext(
                    'This choice is not used by any entity, you can delete it safely.'
                ),
            )
Exemple #2
0
    def formfield(self, instance, user, **kwargs):
        field_name = self.filter_field_name
        efilter = getattr(instance, field_name)

        if efilter and not efilter.can_view(user)[0]:
            return core_fields.ReadonlyMessageField(
                label=self.verbose_name,
                initial=_(
                    'The filter cannot be changed because it is private.'),
                return_value=self.not_editable_flag,
            )

        mfield = type(instance)._meta.get_field(field_name)

        choice_field = mfield.formfield()
        choice_field.empty_label = pgettext_lazy('creme_core-filter', 'All')
        choice_field.queryset = choice_field.queryset.filter(
            entity_type=getattr(instance, self.ctype_field_name), )
        choice_field.initial = efilter

        if hasattr(choice_field, 'get_limit_choices_to'):
            q_filter = choice_field.get_limit_choices_to()

            if q_filter is not None:
                choice_field.queryset = choice_field.queryset.complex_filter(
                    q_filter)

        return choice_field
Exemple #3
0
 def finalize_recipient_field(name, model):
     if FieldsConfig.objects.get_for_model(model).is_fieldname_hidden(
             'email'):
         fields[name] = core_fields.ReadonlyMessageField(
             label=self.fields[name].label,
             initial=gettext(
                 'Beware: the field «Email address» is hidden ;'
                 ' please contact your administrator.'),
         )
Exemple #4
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        models = [
            *filter(FieldsConfig.objects.is_model_valid, apps.get_models())
        ]
        # NB: we use <FieldsConfig.objects.get_for_models()> to take advantage of its cache ;
        #     it useful because this constructor can be called several times in a request
        #     because of our wizard (which fill the instance by calling all
        #     previous steps' validation).
        # Old code:
        #  used_ct_ids = {*FieldsConfig.objects.values_list('content_type', flat=True)}
        excluded_ct_ids = {
            # Do not want a choice "creme entity" ('description' can be hidden).
            ContentType.objects.get_for_model(CremeEntity).id,

            # Exclude ContentType which already have a configuration
            *(
                fc.content_type_id
                for fc in FieldsConfig.objects.get_for_models(models).values()
                if
                not fc._state.adding  # <True> means the FieldsConfig is in DB
            )
        }
        self.ctypes = ctypes = [
            ct for ct in map(ContentType.objects.get_for_model, models)
            if ct.id not in excluded_ct_ids
        ]

        if ctypes:
            self.fields['ctype'].ctypes = ctypes
        else:
            # TODO: remove the 'submit' button ?
            self.fields['ctype'] = core_fields.ReadonlyMessageField(
                label=_('Related resource'),
                initial=_(
                    'All configurable types of resource are already configured.'
                ),
            )
Exemple #5
0
    def __init__(self, model, field, user, entities, is_bulk=False, **kwargs):
        super().__init__(model,
                         field,
                         user,
                         entities,
                         is_bulk=is_bulk,
                         **kwargs)
        self.fields['field_value'] = type_selector = ActivityTypeField(
            label=_('Type'),
            types=ActivityType.objects.exclude(pk=ACTIVITYTYPE_INDISPO),
        )
        self._mixed_indispo = False
        indispo_count = sum(a.type_id == ACTIVITYTYPE_INDISPO
                            for a in entities)

        if indispo_count:
            if indispo_count == len(entities):
                # All entities are indisponibilities, so we propose to change the sub-type.
                type_selector.types = ActivityType.objects.filter(
                    pk=ACTIVITYTYPE_INDISPO)
            else:
                self._mixed_indispo = True
                # TODO: remove when old view entity.bulk_edit_field() has been removed
                self.fields['beware'] = core_fields.ReadonlyMessageField(
                    label=_('Beware !'),
                    initial=ngettext(
                        'The type of {count} activity cannot be changed because'
                        ' it is an indisponibility.',
                        'The type of {count} activities cannot be changed because'
                        ' they are indisponibilities.',
                        indispo_count).format(count=indispo_count),
                )

        if not is_bulk:
            first = entities[0]
            type_selector.initial = (first.type_id, first.sub_type_id)
Exemple #6
0
    def __init__(self, entity, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.act = entity

        instance = self.instance
        if instance.pk:  # Edition
            fields = self.fields
            efilter = instance.filter

            # TODO: add a method EntityFilter.can_list(self.user) to avoid a query
            if (efilter and
                    not EntityFilter.objects.filter_by_user(self.user).filter(
                        entity_type=instance.ctype,
                        id=efilter.id,
                    ).exists()):
                fields['ec_label'] = core_fields.ReadonlyMessageField(
                    label=fields['entity_counting'].label,
                    initial=_(
                        'The filter cannot be changed because it is private.'),
                )
                del fields['entity_counting']
            else:
                fields[
                    'entity_counting'].initial = instance.ctype_id, instance.filter_id
Exemple #7
0
class PollReplyFillForm(base_forms.CremeForm):
    question = core_fields.ReadonlyMessageField(label=_('Question'),
                                                initial='??')

    def __init__(self, line_node, instance=None, *args, **kwargs):
        "@param line_node Node (see ReplySectionTree) related to a PollReplyLine."
        super().__init__(*args, **kwargs)
        self.poll_reply = instance
        self.line_node = line_node

        fields = self.fields
        number = line_node.number
        question_f = fields['question']

        if number:
            # TODO: use NodeStyle ??
            question_f.initial = f'{number} - {line_node.question}'
            fields['not_applicable'] = forms.BooleanField(
                label=gettext('Not applicable'),
                required=False,
                initial=not line_node.applicable,
            )
        else:
            question_f.label = _('Comment')
            question_f.initial = line_node.question

        answer_field = line_node.answer_formfield
        if answer_field is not None:
            # TODO: set dynamically "required" on client side with the value of 'not_applicable'
            answer_field.required = answer_field.widget.is_required = False
            fields['answer'] = answer_field

    def clean(self):
        cdata = super().clean()
        errors = self._errors

        if (not errors and not cdata.get('not_applicable', False)
                and self.line_node.poll_line_type.editable
                and cdata.get('answer') is None):
            errors['answer'] = self.error_class(
                [gettext('The answer is required.')])

        return cdata

    def save(self, *args, **kwargs):
        line = self.line_node
        cdata = self.cleaned_data
        not_applicable = cdata.get('not_applicable', False)

        if not_applicable:
            answer = None
        elif line.poll_line_type.editable:
            answer = cdata['answer']
        else:
            answer = ''

        line.applicable = not not_applicable
        line.answer = answer

        line.save()

        return self.poll_reply