jonasundderwolf / django-image-cropping

Django helper application to easily and non-destructively crop arbitrarily large images in admin and frontend.
Other
553 stars 131 forks source link

fix Django 1.4 compatibility issue #3

Closed joshkersey closed 12 years ago

joshkersey commented 12 years ago
Django Version: 1.4
Exception Type:  TypeError
Exception Value: __init__() takes at least 3 arguments (3 given)
Exception Location: /image_cropping/widgets.py in __init__, line 60
    super(CropForeignKeyWidget, self).__init__(*args, **kwargs)

The signature for ForeignKeyRawIdWidget.__init__() in Django 1.4 is:

class ForeignKeyRawIdWidget(forms.TextInput):
    def __init__(self, rel, admin_site, attrs=None, using=None):

The required argument admin_site is new to Django 1.4. The formfield method of CropForeignKey in fields.py doesn't include the admin_site argument:

def formfield(self, *args, **kwargs):
    kwargs['widget'] = CropForeignKeyWidget(self.rel, field_name=self.field_name,
        using=kwargs.get('using'))
    return super(CropForeignKey, self).formfield(*args, **kwargs)

This patch inspects the arguments of ForeignKeyRawIdWidget.__init__() to see if admin_site is a required argument and patches it into the __init__ kwargs for CropForeignKeyWidget. If admin_site is required it uses django.contrib.admin.sites.site as the value.

class CropForeignKeyWidget(ForeignKeyRawIdWidget, CropWidget):
    def __init__(self, *args, **kwargs):
        if 'admin_site' in inspect.getargspec(ForeignKeyRawIdWidget.__init__)[0]:  # Django 1.4
            kwargs['admin_site'] = site
        self.field_name = kwargs.pop('field_name')
        super(CropForeignKeyWidget, self).__init__(*args, **kwargs)

Could be argued whether this belongs in the Widget init or the CropForeignKey formfield.

jonasvp commented 12 years ago

Hi, thanks for your patch! We're already working on a branch with Django 1.4 compatibility which solves it in a cleaner manner but your fix is great for now.

I'll merge your patch and release a 0.4.4. The new version 0.5 will deprecate the CropForeignKey and necessitate an admin class mixin for the same functionality. Cleaner, but some code changes necessary.

/cc @anrie