Coverage for lino/mixins/uploadable.py : 50%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# Copyright 2010-2015 Luc Saffre # License: BSD (see file COPYING for details)
"""Represents an uploadable file.
.. attribute:: file
Pointer to the file itself (a `Django FileField <https://docs.djangoproject.com/en/1.8/ref/models/fields/#filefield>`_).
.. attribute:: mimetype
The `Media type <https://en.wikipedia.org/wiki/Media_type>`_ of the file. See also `this thread <http://stackoverflow.com/questions/643690/maximum-mimetype-length-when-storing-type-in-db>`_ about length of MIME type field.
"""
# file_field_class = models.FileField
# verbose_name = _("upload") # verbose_name_plural = _("uploads")
_("File"), blank=True, upload_to='uploads/%Y/%m') _("MIME type"), blank=True, max_length=255, editable=False)
#~ from django.core.files.base import ContentFile if not 'file' in request.FILES: logger.debug("No 'file' has been submitted.") return uf = request.FILES['file'] # an UploadedFile instance #~ cf = ContentFile(request.FILES['file'].read()) #~ print f #~ raise NotImplementedError #~ dir,name = os.path.split(f.name) #~ if name != f.name: #~ print "Aha: %r contains a path! (%s)" % (f.name,__file__)
self.size = uf.size self.mimetype = uf.content_type
""" Certain Python versions or systems don't manage non-ascii filenames, so we replace any non-ascii char by "_" """
#~ logger.info('20121004 handle_uploaded_files() %r',uf.name) name = uf.name.encode('ascii', 'replace') name = name.replace('?', '_')
# Django magics: self.file = name # assign a string ff = self.file # get back a FileField instance ! #~ print 'uf=',repr(uf),'ff=',repr(ff)
#~ if not ispure(uf.name): #~ raise Exception('uf.name is a %s!' % type(uf.name))
ff.save(name, uf, save=False)
# The expression `self.file` # now yields a FieldFile instance that has been created from `uf`. # see Django FileDescriptor.__get__()
logger.info("Wrote uploaded file %s", ff.path) |