Exemplo n.º 1
0
    def setUp(self):
        super(RichTextFieldTestCase, self).setUp()

        self.old_signal_receivers = []
        for s in DROP_SIGNALS:
            self.old_signal_receivers.append(s.receivers)
            s.receivers = []

        create_basic_categories(self)
        create_and_place_a_publishable(self)

        self.field = fields.NewmanRichTextField(
            instance=self.publishable,
            model=self.publishable.__class__,
            field_name="description",
        )
Exemplo n.º 2
0
    def test_two_uses_in_two_fields_on_one_object_count_as_one(self):
        l = License(max_applications=2, applications=1)
        l.target = self.category
        l.save()

        text = 'some-text {%% box inline for core.category with pk %s %%}{%% endbox %%}' % self.category.pk
        self.publishable.description = self.field.clean(text)

        field2 = fields.NewmanRichTextField(
            instance=self.publishable,
            model=self.publishable.__class__,
            field_name="title",
        )
        self.publishable.title = field2.clean(text)
        self.publishable.save()

        tools.assert_equals(2, License.objects.get(pk=l.pk).applications)
Exemplo n.º 3
0
    def test_two_dependencies_in_two_fields_get_picked_up(self):
        field2 = fields.NewmanRichTextField(
            instance=self.publishable,
            model=self.publishable.__class__,
            field_name="title",
        )

        text2 = 'some-text {%% box inline for core.category with pk %s %%}{%% endbox %%}' % self.category_nested.pk
        self.publishable.title = field2.clean(text2)

        text = 'some-text {%% box inline for core.category with pk %s %%}{%% endbox %%}' % self.category.pk
        self.publishable.description = self.field.clean(text)

        self.publishable.save()

        tools.assert_equals(2, Dependency.objects.all().count())
        tools.assert_equals(
            sorted([self.category.pk, self.category_nested.pk]), [
                dep.target_id
                for dep in Dependency.objects.order_by('target_id')
            ])
Exemplo n.º 4
0
    def test_object_still_used_in_one_field_doesnt_affect_applications(self):
        l = License(max_applications=2, applications=1)
        l.target = self.category
        l.save()

        dep = Dependency()
        dep.target = self.category
        dep.dependent = self.publishable
        dep.save()

        text = 'some-text {%% box inline for core.category with pk %s %%}{%% endbox %%}' % self.category.pk
        self.publishable.description = self.field.clean(text)

        field2 = fields.NewmanRichTextField(
            instance=self.publishable,
            model=self.publishable.__class__,
            field_name="title",
        )
        self.publishable.title = field2.clean('no box here')
        self.publishable.save()

        tools.assert_equals(1, License.objects.get(pk=l.pk).applications)
Exemplo n.º 5
0
    def test_object_still_used_in_one_field_doesnt_affect_dependencies(self):
        dep = Dependency()
        dep.target = self.category
        dep.dependent = self.publishable
        dep.save()

        text = 'some-text {%% box inline for core.category with pk %s %%}{%% endbox %%}' % self.category.pk

        field2 = fields.NewmanRichTextField(
            instance=self.publishable,
            model=self.publishable.__class__,
            field_name="title",
        )

        self.publishable.title = field2.clean('no box here')
        self.publishable.description = self.field.clean(text)
        self.publishable.save()

        tools.assert_equals(1, Dependency.objects.all().count())
        dep = Dependency.objects.all()[0]
        tools.assert_equals(self.category, dep.target)
        tools.assert_equals(self.publishable, dep.dependent)
Exemplo n.º 6
0
def formfield_for_dbfield_factory(cls, db_field, **kwargs):
    formfield_overrides = dict(FORMFIELD_FOR_DBFIELD_DEFAULTS,
                               **cls.formfield_overrides)
    custom_param_names = ('request', 'user', 'model', 'super_field',
                          'instance')
    custom_params = {}
    # move custom kwargs from kwargs to custom_params
    for key in kwargs:
        if key not in custom_param_names:
            continue
        custom_params[key] = kwargs[key]
        if key == 'request':
            custom_params['user'] = custom_params[key].user
    for key in custom_param_names:
        kwargs.pop(key, None)

    if db_field.choices:
        return db_field.formfield(**kwargs)

    for css_class, rich_text_fields in getattr(cls, 'rich_text_fields',
                                               {}).iteritems():
        if db_field.name in rich_text_fields:
            kwargs.update({
                'required': not db_field.blank,
                'label': db_field.verbose_name,
                'field_name': db_field.name,
                'instance': custom_params.get('instance', None),
                'model': custom_params.get('model'),
            })
            rich_text_field = fields.NewmanRichTextField(**kwargs)
            if css_class:
                rich_text_field.widget.attrs['class'] += ' %s' % css_class
            return rich_text_field

    # date and datetime fields
    if isinstance(db_field, models.DateTimeField):
        kwargs.update({
            'widget': widgets.DateTimeWidget,
        })
        return db_field.formfield(**kwargs)
    elif isinstance(db_field, models.DateField):
        kwargs.update({
            'widget': widgets.DateWidget,
        })
        return db_field.formfield(**kwargs)

    if isinstance(db_field, models.ImageField):
        # we accept only (JPEG) images with RGB color profile.
        kwargs.update({
            'label': db_field.verbose_name,
        })
        return fields.RGBImageField(db_field, **kwargs)

    if db_field.name in cls.raw_id_fields and isinstance(
            db_field, models.ForeignKey):
        kwargs.update({
            'label': db_field.verbose_name,
            'widget': widgets.ForeignKeyRawIdWidget(db_field.rel),
            'required': not db_field.blank,
        })
        return fields.RawIdField(db_field.rel.to.objects.all(), **kwargs)

    if db_field.name in getattr(cls, 'suggest_fields', {}).keys() \
                        and isinstance(db_field, (models.ForeignKey, models.ManyToManyField)):
        kwargs.update({
            'required': not db_field.blank,
            'label': db_field.verbose_name,
            'model': cls.model,
            'lookup': cls.suggest_fields[db_field.name]
        })
        return fields.AdminSuggestField(db_field, **kwargs)

    if isinstance(db_field, models.ForeignKey) and issubclass(
            db_field.rel.to, ContentType):
        kwargs['widget'] = widgets.ContentTypeWidget
        return db_field.formfield(**kwargs)
    elif db_field.name in ('target_id', 'source_id', 'object_id', 'related_id',
                           'obj_id'):
        kwargs['widget'] = widgets.ForeignKeyGenericRawIdWidget
        return db_field.formfield(**kwargs)

    if db_field.name == 'order':
        kwargs['widget'] = widgets.OrderFieldWidget
        return db_field.formfield(**kwargs)

    # magic around restricting category choices in all ForeignKey (related to Category) fields
    if is_category_fk(db_field) and 'model' in custom_params:
        kwargs.update({
            'model': custom_params['model'],
            'user': custom_params['user']
        })
        super_qs = custom_params['super_field'].queryset
        return fields.CategoryChoiceField(super_qs, **kwargs)

    if db_field.__class__ in formfield_overrides:
        kwargs = dict(formfield_overrides[db_field.__class__], **kwargs)
        return db_field.formfield(**kwargs)

    return db_field.formfield(**kwargs)