misc: remove requirement on python-magic

Python package changed between python3 and python2 on Debian
(python-magic is the pypi package python-magic, but python3-magic is the
pypi package file-magic :/)
This commit is contained in:
Benjamin Dauvergne 2020-07-03 13:41:16 +02:00
parent 0a4fa6b6fe
commit 8dc42017ed
5 changed files with 15 additions and 7 deletions

View File

@ -107,7 +107,6 @@ setup(
install_requires=[
'authentic2',
'Pillow',
'python-magic',
'sorl-thumbnail',
],
entry_points={

View File

@ -16,7 +16,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import base64
import magic
import uuid
from django.core.files.base import ContentFile
@ -36,7 +35,7 @@ from django_filters.rest_framework import FilterSet
from authentic2_idp_oidc.utils import make_pairwise_reversible_sub
from authentic2 import api_views
from . import models
from . import models, utils
class Base64ImageField(serializers.FileField):
@ -128,7 +127,7 @@ def validate_cut(self, request, uuid):
'max-size': max_size,
})
accepted_mime_types = _get_cut_validation_accepted_mime_types()
mime_type = magic.from_buffer(content.read(10000), mime=True)
mime_type = utils.mime_type_from_buffer(content.read(10000))
if mime_type not in accepted_mime_types:
errors.append({
'code': 'justificatifs-bad-format',

View File

@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-
import magic
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth import get_user_model
from django_rbac.utils import get_ou_model, get_role_model, get_operation, get_permission_model
@ -239,3 +241,10 @@ def update_roles():
handle_ou(ou_territoire, **vars())
for ou in OU.objects.exclude(slug__in=['usagers', 'territoire']):
handle_ou(**vars())
def mime_type_from_buffer(buf):
if hasattr(magic, 'from_buffer'):
return magic.from_buffer(buf, mime=True)
else:
return magic.detect_from_content(buf).mime_type

View File

@ -15,7 +15,6 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import magic
from contextlib import closing
from django.utils.timezone import now
@ -34,7 +33,7 @@ from authentic2 import hooks
from authentic2.utils import redirect
from .custom_settings import CORE_ATTRIBUTES
from . import models, forms, tables
from . import models, forms, tables, utils
class EditCoreView(EditProfile):
@ -272,7 +271,7 @@ def validation_attachment(request, pk, filename):
raise PermissionDenied
attachment = models.ValidationRequestAttachment.objects.get(pk=pk)
attachment.image.open()
mime_type = magic.from_buffer(attachment.image.read(10000), mime=True)
mime_type = utils.mime_type_from_buffer(attachment.image.read(10000))
attachment.image.open()
return HttpResponse(attachment.image, content_type=mime_type)

View File

@ -39,6 +39,8 @@ deps =
# dependency constraints for authentic
py2: django-appconf<1.0.4
py2: django-filter<2
py2: python-magic
py3: file-magic
drf34: djangorestframework>=3.4,<3.4.1
drf39: djangorestframework>=3.9.2,<3.10
dj111: django<2.0