コード例 #1
0
ファイル: models.py プロジェクト: eculver/django-file-picker
    def save(self, **kwargs):
        # file size
        try:
            self.file_size = self.file.size
        except Exception:
            pass

        # file types from mimetype
        if self.file:
            [self.file_type, self.file_subtype] = parse_types(self.file.path)

        return super(BaseFileModel, self).save(**kwargs)
コード例 #2
0
ファイル: utils.py プロジェクト: eculver/django-file-picker
def render_upload(obj, template_path="file_picker/render/", **options):
    """
    Render a single ``File`` or ``Image`` model instance (``obj``) using the
    appropriate rendering template and the given keyword options, and
    return the rendered HTML.

    The template used to render each upload is selected based on the
    mime-type of the upload. For an upload with mime-type
    "image/jpeg", assuming the default ``template_path`` of
    "file_picker/render", the template used would be the first one
    found of the following: ``file_picker/render/image/jpeg.html``,
    ``file_picker/render/image/default.html``, and
    ``file_picker/render/default.html``

    """
    if obj is None or obj.file is None:
        return settings.NOT_FOUND_STRING

    template_name = options.pop('as', None)

    if template_name:
        templates = [template_name,
                     "%s/default" % template_name.split('/')[0],
                     "default"]
    else:
        [file_type, file_subtype] = parse_types(obj.file.url)
        templates = [join(file_type, file_subtype),
                     join(file_type, "default"),
                     "default"]

    tpl = template.loader.select_template(
        ["%s.html" % join(template_path, p) for p in templates])

    return tpl.render(template.Context({'obj': obj,
                                        'media_url': settings.MEDIA_URL,
                                        'options': options}))