Skip to content

Commit 8d9649e

Browse files
author
valdergallo
committed
fix tests and add autopep8
1 parent 335bb34 commit 8d9649e

File tree

11 files changed

+62
-49
lines changed

11 files changed

+62
-49
lines changed

.travis.yml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
language: python
22
python:
3-
- 2.7
4-
script: python setup.py test
3+
- 3.7
4+
script: python setup.py pytest
55
env:
6-
- DJANGO=1.4
7-
- DJANGO=1.5
8-
- DJANGO=1.6
9-
- DJANGO=1.7
10-
- DJANGO=1.8
11-
- DJANGO=1.9
12-
- DJANGO=1.10
136
- DJANGO=1.11
7+
- DJANGO=2.1
8+
- DJANGO=3.0
149
install:
1510
- pip install -q Django==$DJANGO -r requirements.txt
1611
branches:

compress_field/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
__version__ = '0.10.2'
2+
__version__ = "0.10.2"
33

44
from .models import ZipFileField
5-
from .fieldfiles import ZipCompressFieldFile
5+
from .fieldfiles import ZipCompressFieldFile

compress_field/base.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import absolute_import, unicode_literals
3+
34
try:
45
from celery.task import task
56
except ImportError:
@@ -15,27 +16,28 @@
1516
from django.db.models.fields.files import FieldFile
1617

1718

18-
FILE_COMPRESS_DELETE_OLD_FILE = getattr(settings,
19-
'FILE_COMPRESS_DELETE_OLD_FILE', True)
19+
FILE_COMPRESS_DELETE_OLD_FILE = getattr(settings, "FILE_COMPRESS_DELETE_OLD_FILE", True)
2020

2121

2222
class CompressFieldFile(FieldFile):
2323
compress_ext = None
2424

2525
def _is_compressed(self):
2626
basename, ext = os.path.splitext(self.name)
27-
compress_ext = '.' + self.compress_ext
27+
compress_ext = "." + self.compress_ext
2828
return compress_ext == ext
29+
2930
is_compressed = property(_is_compressed)
3031

3132
def _base_name(self):
3233
return os.path.basename(self.file.name)
34+
3335
base_name = property(_base_name)
3436

3537
def _compress_name(self):
36-
if not hasattr(self, '_avaliable_compress_name'):
38+
if not hasattr(self, "_avaliable_compress_name"):
3739
basename, ext = os.path.splitext(self.name)
38-
compress_name = basename + ('.' + self.compress_ext)
40+
compress_name = basename + ("." + self.compress_ext)
3941
self._avaliable_compress_name = self.storage.get_available_name(
4042
compress_name
4143
)
@@ -75,12 +77,14 @@ def compress_wrapper(self, delete_old_file):
7577

7678
def compress(self, async_task=True, delete_old_file=FILE_COMPRESS_DELETE_OLD_FILE):
7779
if self.is_compressed:
78-
return 'This file is alredy compress'
80+
return "This file is alredy compress"
7981

8082
if async_task and task:
8183
from .tasks import task_compress_wrapper
84+
8285
wrapper = task_compress_wrapper.delay(
83-
self.instance, self.field.name, delete_old_file)
86+
self.instance, self.field.name, delete_old_file
87+
)
8488
else:
8589
wrapper = CompressFieldFile.compress_wrapper(self, delete_old_file)
8690

compress_field/fieldfiles.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,27 @@
33
import zipfile
44
import sys
55
from .base import CompressFieldFile
6+
67
try:
78
compression = zipfile.ZIP_DEFLATED
89
except ImportError:
910
compression = zipfile.ZIP_STORED
1011

1112

1213
class ZipCompressFieldFile(CompressFieldFile):
13-
compress_ext = 'zip'
14+
compress_ext = "zip"
1415

1516
def compress_content(self):
1617
compress_file_fullname = self.compress_full_name
1718

1819
# to work with py2.6 or lower
1920
if sys.version_info < (2, 7):
20-
compress_file_fullname = open(compress_file_fullname, 'w+b')
21+
compress_file_fullname = open(compress_file_fullname, "w+b")
2122

2223
if not zipfile.is_zipfile(self.file.name):
2324
ziped = zipfile.ZipFile(
24-
compress_file_fullname, 'w', compression=compression)
25+
compress_file_fullname, "w", compression=compression
26+
)
2527
ziped.write(self.file.name, self.base_name)
2628
ziped.close()
2729
return ziped

compress_field/models.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ class ZipFileField(models.FileField):
88
"""
99
ZipField - auto zip content after file was saved on server
1010
"""
11+
1112
attr_class = ZipCompressFieldFile
1213

1314
def south_field_triple(self):
1415
try:
1516
from south.modelsinspector import introspector
16-
cls_name = '{0}.{1}'.format(
17-
self.__class__.__module__,
18-
self.__class__.__name__)
17+
18+
cls_name = "{0}.{1}".format(
19+
self.__class__.__module__, self.__class__.__name__
20+
)
1921
args, kwargs = introspector(self)
2022
return cls_name, args, kwargs
2123
except ImportError:
@@ -24,6 +26,7 @@ def south_field_triple(self):
2426

2527
try:
2628
from south.modelsinspector import add_introspection_rules
29+
2730
add_introspection_rules([], ["^compress_field\.models\.(ZipFileField)"])
2831
except ImportError:
2932
pass

compress_field/tasks.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import absolute_import, unicode_literals
3+
34
try:
45
from celery.task import task
56
except ImportError:
67
task = None
78

89
from django.core.cache import cache
10+
911
LOCK_EXPIRE = 60 * 5
1012

1113
try:
1214
from django.conf import settings
1315
except ImportError:
1416
settings = {}
1517

16-
FILE_COMPRESS_QUEUE = getattr(settings, 'FILE_COMPRESS_QUEUE', 'Celery')
18+
FILE_COMPRESS_QUEUE = getattr(settings, "FILE_COMPRESS_QUEUE", "Celery")
1719

1820

1921
# cache.add fails if if the key already exists
2022
def acquire_lock(lock_id):
21-
return cache.add(lock_id, 'true', LOCK_EXPIRE)
23+
return cache.add(lock_id, "true", LOCK_EXPIRE)
2224

2325

2426
# memcache delete is very slow, but we have to use it to take
@@ -27,10 +29,9 @@ def release_lock(lock_id):
2729
return cache.delete(lock_id)
2830

2931

30-
@task(serializer='pickle', queue=FILE_COMPRESS_QUEUE)
32+
@task(serializer="pickle", queue=FILE_COMPRESS_QUEUE)
3133
def task_compress_wrapper(instance, field, delete_old_file):
32-
lock_id = '{0}-io-lock-{1}'.format(
33-
instance.__class__.__name__, instance.id)
34+
lock_id = "{0}-io-lock-{1}".format(instance.__class__.__name__, instance.id)
3435

3536
if acquire_lock(lock_id):
3637
instance_field = getattr(instance, field)
@@ -39,5 +40,5 @@ def task_compress_wrapper(instance, field, delete_old_file):
3940
return True
4041

4142
# task is locked by IO
42-
print('IO Lock Task')
43+
print("IO Lock Task")
4344
return False

conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import os
2+
3+
def pytest_configure():
4+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_test_settings')
5+

example/apps.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class ExampleConfig(AppConfig):
5+
name = 'example'

pytest.ini

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[pytest]
2+
addopts = --cov=compress_field -vrsx
3+
testpaths =
4+
compress_field
5+
tests
6+
example

requirements.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
pytest==3.0.2
2-
pytest-cov==2.3.1
3-
pytest-django==2.9.1
1+
coverage==5.2
2+
pytest==5.4.3
3+
pytest-cov==2.10.0
4+
pytest-django==3.9.0
5+
pytest-runner==5.2

0 commit comments

Comments
 (0)