# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django import forms
from django.conf import settings
from django.core import signing
from django.core.exceptions import ValidationError
from django.core.files.storage import default_storage
from django.core.files.uploadedfile import InMemoryUploadedFile, TemporaryUploadedFile
from django.core.urlresolvers import reverse_lazy
from django.forms import Field
from django.utils.translation import ugettext_lazy as _

from djng import app_settings
from .drop_widgets import DropImageWidget


class FloatField(forms.FloatField):
    """
    The internal ``django.forms.FloatField`` does not handle the step value in its number widget.
    """
    def __init__(self, *args, **kwargs):
        self.step = kwargs.pop('step', None)
        super(FloatField, self).__init__(*args, **kwargs)

    def widget_attrs(self, widget):
        attrs = super(FloatField, self).widget_attrs(widget)
        attrs.update(step=self.step)
        return attrs


class ImageField(Field):
    storage = app_settings.upload_storage
    signer = signing.Signer()

    def __init__(self, *args, **kwargs):
        accept = kwargs.pop('accept', 'image/*')
        fileupload_url = kwargs.pop('fileupload_url', reverse_lazy('fileupload'))
        area_label = kwargs.pop('area_label', _("Drop or click here to upload"))
        attrs = {
            'accept': accept,
            'ngf-pattern': accept,
            'fileupload-url': fileupload_url,
        }
        kwargs.update(widget=DropImageWidget(attrs=attrs, area_label=area_label))
        super(ImageField, self).__init__(*args, **kwargs)

    def to_python(self, value):
        try:
            if 'previous_image' in value:
                # Delete previous image
                previous_image = self.signer.unsign(value['previous_image'])
                self.remove_images(previous_image)
            if isinstance(value['temp_name'], bool):
                # Nothing changed
                return
            temp_name = self.signer.unsign(value['temp_name'])
        except signing.BadSignature:
            raise ValidationError("Got bogus upstream data")
        try:
            temp_file = self.storage.open(temp_name, 'rb')
            file_size = self.storage.size(temp_name)
            if file_size < settings.FILE_UPLOAD_MAX_MEMORY_SIZE:
                obj = InMemoryUploadedFile(
                    file=temp_file,
                    field_name=None,
                    name=value['image_name'],
                    charset=value['charset'],
                    content_type=value['content_type'],
                    content_type_extra=value['content_type_extra'],
                    size=file_size,
                )
            else:
                obj = TemporaryUploadedFile(
                    value['image_name'],
                    value['content_type'],
                    0,
                    value['charset'],
                    content_type_extra=value['content_type_extra'],
                )
                while True:
                    chunk = temp_file.read(0x10000)
                    if not chunk:
                        break
                    obj.file.write(chunk)
                obj.file.seek(0)
                obj.file.size = file_size
        except IOError:
            obj = None
        except Exception as excp:
            raise ValidationError("File upload failed. {}: {}".format(excp.__class__.__name__, excp))
        else:
            self.storage.delete(temp_name)
        return obj

    def remove_images(self, image_name):
        from easy_thumbnails.models import Source, Thumbnail

        try:
            source = Source.objects.get(name=image_name)
            for thumb in Thumbnail.objects.filter(source=source):
                default_storage.delete(thumb.name)
                thumb.delete()
            source.delete()
        except Source.DoesNotExist:
            pass
        default_storage.delete(image_name)

@@ -394,39 +394,8 @@
 azy%0A
-from django.forms import Field%0A
 from
@@ -990,24 +990,30 @@
 geField(
+forms.
 Field):%0A
     stor
@@ -1000,24 +1000,48 @@
 rms.Field):%0A
+    render_label = True%0A
     storage 

fix failing unit test