jazzband / django-downloadview

Serve files with Django.
https://django-downloadview.readthedocs.io
Other
380 stars 58 forks source link

TypeError: int() argument must be a string or a number, not 'datetime.datetime' #104

Closed zerc closed 9 years ago

zerc commented 9 years ago

Hello!

Some times i catched this exception when downloaded file. After some digging i found why.

This happens when request has If-Modified-Since header.

Exception raised here: https://github.com/benoitbryon/django-downloadview/blob/master/django_downloadview/views/base.py#L119

def was_modified_since(self, file_instance, since):
        """Return True if ``file_instance`` was modified after ``since``.
        Uses file wrapper's ``was_modified_since`` if available, with value of
        ``since`` as positional argument.
        Else, fallbacks to default implementation, which uses
        :py:func:`django.views.static.was_modified_since`.
        Django's ``was_modified_since`` function needs a datetime and a size.
        It is passed ``modified_time`` and ``size`` attributes from file
        wrapper. If file wrapper does not support these attributes
        (``AttributeError`` or ``NotImplementedError`` is raised), then
        the file is considered as modified and ``True`` is returned.
        """
        try:
            return file_instance.was_modified_since(since)
        except (AttributeError, NotImplementedError):
            try:
                modification_time = file_instance.modified_time
                size = file_instance.size
            except (AttributeError, NotImplementedError):
                return True
            else:
                return was_modified_since(since, modification_time, size)

because modification_time it is realy datetime object for default storage (FileSystemStorage)

https://github.com/django/django/blob/master/django/core/files/storage.py#L324

    def modified_time(self, name):
        return datetime.fromtimestamp(os.path.getmtime(self.path(name)))

I can to contribute if needed.

benoitbryon commented 9 years ago

Thanks for the report!

I can to contribute if needed.

It would be very nice :)

zerc commented 9 years ago

Oh sorry for this spam

benoitbryon commented 9 years ago

Hi @zerc and @benesch ! I merged your pull-requests and released django-downloadview 1.8 :) Thank you!

benesch commented 9 years ago

Thanks so much! Looks great.