Example #1
0
    def __init__(self, base_book_qs=None, *args, **kwargs):
        super(BookCreationForm, self).__init__(*args, **kwargs)

        # time to build metadata fields
        self.metadata_fields = config.get_configuration('CREATE_BOOK_METADATA', [])

        # TODO: extract this to a separate method to use it also in booktype.apps.edit.forms.MetadataForm
        for field, label, standard in METADATA_FIELDS:
            field_name = '%s.%s' % (standard, field)
            if field_name not in self.metadata_fields:
                continue

            self.fields[field_name] = forms.CharField(
                label=label, required=False)

            c_field = self.fields[field_name]
            BaseBooktypeForm.apply_class(c_field, 'form-control')

            # apply widgets if needed
            if field in self.Meta.widgets:
                c_field.widget = self.Meta.widgets[field]

        if base_book_qs is not None:
            self.fields['base_book'].queryset = base_book_qs
        else:
            logger.warn("base_book_qs queryset parameter was not provided. Using empty queryset")
Example #2
0
    def __init__(self, *args, **kwargs):
        super(AdditionalMetadataForm, self).__init__(*args, **kwargs)
        book = kwargs.get('book')
        additional_fields = getattr(settings, 'ADDITIONAL_METADATA', {})

        for field, attrs in additional_fields.items():
            field_name = '%s.%s' % (self.META_PREFIX, field)
            try:
                if 'TYPE' not in attrs.keys():
                    logger.error(
                        _('%(field)s must have TYPE attribute') %
                        {'field': field})
                    continue

                field_class = getattr(forms, attrs.get('TYPE'))
                field_attrs = attrs.get('ATTRS', {})
                default_widget = field_class.widget.__name__

                widget_class = getattr(forms.widgets,
                                       attrs.get('WIDGET', default_widget))
                widget_atts = attrs.get('WIDGET_ATTRS', {})

                # build the widget
                field_attrs['widget'] = widget_class(attrs=widget_atts)
                field_attrs['label'] = field_attrs.get('label', field.title())

                # now create the field
                self.fields[field_name] = field_class(**field_attrs)

                # add bootstrap class if is not choice field
                if attrs.get('TYPE') != 'ChoiceField':
                    c_field = self.fields[field_name]
                    BaseBooktypeForm.apply_class(c_field, 'form-control')

            except Exception as err:
                exc_type, exc_obj, exc_tb = sys.exc_info()
                logger.error(
                    _('Unable to create field %(field)s. Reason: %(error)s | type: %(exc_type)s | Line: %(line_no)s '
                      ) % {
                          'field': field,
                          'error': err,
                          'exc_type': exc_type,
                          'line_no': exc_tb.tb_lineno
                      })

        # maybe we should move this later if needed
        def unslugify(value):
            return value.replace('-', ' ').replace('_', ' ')

        # now add the stored field
        for meta in Info.objects.filter(
                book=book, name__startswith=self.META_PREFIX).order_by('id'):
            keyname = meta.name.split('.')[1]
            if keyname not in additional_fields.keys():
                self.fields[meta.name] = forms.CharField(
                    widget=forms.widgets.Textarea(
                        attrs={'class': 'form-control meta-dynamic in_db'}),
                    label=unslugify(keyname.capitalize()))
Example #3
0
    def __init__(self, *args, **kwargs):
        super(AdditionalMetadataForm, self).__init__(*args, **kwargs)
        book = kwargs.get('book')
        additional_fields = getattr(settings, 'ADDITIONAL_METADATA', {})

        for field, attrs in additional_fields.items():
            field_name = '%s.%s' % (self.META_PREFIX, field)
            try:
                if 'TYPE' not in attrs.keys():
                    logger.error(
                        _('%(field)s must have TYPE attribute') % {'field': field})
                    continue

                field_class = getattr(forms, attrs.get('TYPE'))
                field_attrs = attrs.get('ATTRS', {})
                default_widget = field_class.widget.__name__

                widget_class = getattr(forms.widgets, attrs.get('WIDGET', default_widget))
                widget_atts = attrs.get('WIDGET_ATTRS', {})

                # build the widget
                field_attrs['widget'] = widget_class(attrs=widget_atts)
                field_attrs['label'] = field_attrs.get('label', field.title())

                # now create the field
                self.fields[field_name] = field_class(**field_attrs)

                # add bootstrap class if is not choice field
                if attrs.get('TYPE') != 'ChoiceField':
                    c_field = self.fields[field_name]
                    BaseBooktypeForm.apply_class(c_field, 'form-control')

            except Exception as err:
                exc_type, exc_obj, exc_tb = sys.exc_info()
                logger.error(
                    _('Unable to create field %(field)s. Reason: %(error)s | type: %(exc_type)s | Line: %(line_no)s ') %
                    {
                        'field': field, 'error': err,
                        'exc_type': exc_type, 'line_no': exc_tb.tb_lineno
                    }
                )

        # maybe we should move this later if needed
        def unslugify(value):
            return value.replace('-', ' ').replace('_', ' ')

        # now add the stored field
        for meta in Info.objects.filter(book=book, name__startswith=self.META_PREFIX).order_by('id'):
            keyname = meta.name.split('.')[1]
            if keyname not in additional_fields.keys():
                self.fields[meta.name] = forms.CharField(
                    widget=forms.widgets.Textarea(attrs={'class': 'form-control meta-dynamic in_db'}),
                    label=unslugify(keyname.capitalize())
                )
Example #4
0
    def __init__(self, *args, **kwargs):
        super(MetadataForm, self).__init__(*args, **kwargs)

        for field, label, standard in METADATA_FIELDS:
            field_name = '%s.%s' % (standard, field)
            self.fields[field_name] = forms.CharField(
                label=label, required=False)

            c_field = self.fields[field_name]
            BaseBooktypeForm.apply_class(c_field, 'form-control')

            # apply widgets if needed
            if field in self.Meta.widgets:
                c_field.widget = self.Meta.widgets[field]