general: store document mime type and use it as css class (#15849)
This commit is contained in:
parent
39441a95f5
commit
03baa36769
|
@ -11,7 +11,8 @@ Architecture: all
|
|||
Depends: ${misc:Depends}, ${python:Depends},
|
||||
python-django (>= 1.7),
|
||||
python-django-filters,
|
||||
python-gadjo
|
||||
python-gadjo,
|
||||
python-magic
|
||||
Recommends: python-django-mellon
|
||||
Description: Fargo Document Box (Python module)
|
||||
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('fargo', '0012_auto_20161124_0626'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='document',
|
||||
name='mime_type',
|
||||
field=models.CharField(max_length=256, blank=True),
|
||||
),
|
||||
]
|
|
@ -2,8 +2,14 @@
|
|||
import base64
|
||||
import subprocess
|
||||
import os
|
||||
import re
|
||||
import threading
|
||||
|
||||
try:
|
||||
import magic
|
||||
except ImportError:
|
||||
magic = None
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.db import models
|
||||
|
@ -73,6 +79,13 @@ class UserDocument(models.Model):
|
|||
return reverse('thumbnail', kwargs={'pk': self.id, 'filename': self.filename_encoded})
|
||||
return ''
|
||||
|
||||
@property
|
||||
def css_classes(self):
|
||||
if not self.document.mime_type:
|
||||
return ''
|
||||
return 'mime-%s mime-%s' % (
|
||||
self.document.mime_type.split('/')[0],
|
||||
re.sub('[/\.+-]', '-', self.document.mime_type))
|
||||
|
||||
class Validation(models.Model):
|
||||
'''Validation of a document as special kind for an user,
|
||||
|
@ -141,6 +154,9 @@ class Document(models.Model):
|
|||
upload_to='uploads/',
|
||||
max_length=300,
|
||||
verbose_name=_('file'))
|
||||
mime_type = models.CharField(
|
||||
max_length=256,
|
||||
blank=True)
|
||||
|
||||
objects = managers.DocumentManager()
|
||||
|
||||
|
@ -148,6 +164,11 @@ class Document(models.Model):
|
|||
'''Create content_hash if new'''
|
||||
if not self.content_hash:
|
||||
self.content_hash = utils.sha256_of_file(self.content)
|
||||
if magic is not None:
|
||||
magic_object = magic.open(magic.MIME)
|
||||
magic_object.load()
|
||||
self.mime_type = magic_object.file(self.content.file.name).split(';')[0]
|
||||
magic_object.close()
|
||||
super(Document, self).save(*args, **kwargs)
|
||||
|
||||
def delete(self):
|
||||
|
|
|
@ -21,7 +21,7 @@
|
|||
{% endblock table.thead %}
|
||||
|
||||
{% block table.tbody.row %}
|
||||
<tr class="{{ forloop.counter|divisibleby:2|yesno:"even,odd" }}"
|
||||
<tr class="{{ forloop.counter|divisibleby:2|yesno:"even,odd" }} {{row.record.css_classes}}"
|
||||
data-url="{{row.record.get_download_url}}"
|
||||
> {# avoid cycle for Django 1.2-1.6 compatibility #}
|
||||
{% for column, cell in row.items %}
|
||||
|
|
|
@ -2,3 +2,4 @@ django>=1.7,<1.9
|
|||
django-tables2<1.1
|
||||
django-jsonfield >= 0.9.3, < 1
|
||||
djangorestframework>=3.3,<3.4
|
||||
file-magic
|
||||
|
|
1
setup.py
1
setup.py
|
@ -102,6 +102,7 @@ setup(
|
|||
'django-tables2<1.1',
|
||||
'django-jsonfield >= 0.9.3, < 1',
|
||||
'djangorestframework>=3.3,<3.4',
|
||||
'file-magic',
|
||||
],
|
||||
zip_safe=False,
|
||||
cmdclass={
|
||||
|
|
|
@ -4,7 +4,13 @@ from webtest import TestApp, Upload
|
|||
import pytest
|
||||
import urlparse
|
||||
|
||||
try:
|
||||
import magic
|
||||
except ImportError:
|
||||
magic = None
|
||||
|
||||
from fargo.wsgi import application
|
||||
from fargo.fargo.models import UserDocument
|
||||
|
||||
pytestmark = pytest.mark.django_db
|
||||
|
||||
|
@ -23,7 +29,23 @@ def test_upload(app, john_doe):
|
|||
form['content'] = Upload('monfichier.pdf', 'coin', 'application/pdf')
|
||||
response2 = form.submit().follow()
|
||||
assert 'monfichier.pdf' in response2.content
|
||||
assert '4 bytes' in response2.content
|
||||
if magic is not None:
|
||||
assert UserDocument.objects.get(filename='monfichier.pdf').document.mime_type == 'text/plain'
|
||||
assert ' mime-text ' in response2.content
|
||||
assert ' mime-text-plain' in response2.content
|
||||
|
||||
UserDocument.objects.all().delete()
|
||||
|
||||
response1 = app.get('/')
|
||||
form = response1.form
|
||||
form['content'] = Upload('monfichier.pdf', '%PDF-1.4 ...', 'application/pdf')
|
||||
response2 = form.submit().follow()
|
||||
assert 'monfichier.pdf' in response2.content
|
||||
assert '12 bytes' in response2.content
|
||||
if magic is not None:
|
||||
assert UserDocument.objects.get(filename='monfichier.pdf').document.mime_type == 'application/pdf'
|
||||
assert ' mime-application ' in response2.content
|
||||
assert ' mime-application-pdf' in response2.content
|
||||
|
||||
|
||||
def test_upload_max_size(app, private_settings, john_doe):
|
||||
|
|
Loading…
Reference in New Issue