Skip to content

Commit

Permalink
Merge pull request #37 from nikolaik/python3
Browse files Browse the repository at this point in the history
Python3 and Django 1.8 support
  • Loading branch information
leplatrem committed Jul 29, 2015
2 parents 08c0c58 + 1e061c1 commit 19a19ba
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 24 deletions.
14 changes: 11 additions & 3 deletions .travis.yml
Expand Up @@ -3,23 +3,31 @@ language: python
python:
- 2.6
- 2.7
- 3.4

env:
- DJANGO_VERSION=1.5
- DJANGO_VERSION=1.6
- DJANGO_VERSION=1.7
- DJANGO_VERSION=1.8
matrix:
exclude:
- python: 2.6
env: DJANGO_VERSION=1.7
- python: 2.6
env: DJANGO_VERSION=1.8

install:
- "git clone git://github.com/n1k0/casperjs.git"
- "sudo ln -s `pwd`/casperjs/bin/casperjs /usr/local/bin"
# This is a dependency of our Django test script
- pip install argparse --use-mirrors
- pip install argparse --use-mirrors # This is a dependency of our Django test script
- pip install -q Django==$DJANGO_VERSION --use-mirrors
- pip install mock
- pip install flake8 coverage --use-mirrors
- pip install django-timedeltafield

before_script:
- "pep8 --ignore=E501 screamshot"
- pep8 --ignore=E501 screamshot
- flake8 --ignore=E501 screamshot

script:
Expand Down
19 changes: 15 additions & 4 deletions quicktest.py
Expand Up @@ -2,9 +2,9 @@
import sys
import argparse

from django import VERSION
from django.conf import settings


class QuickDjangoTest(object):
"""
A quick way to run the Django test suite without a fully-configured project.
Expand All @@ -30,7 +30,7 @@ def __init__(self, *args, **kwargs):

def run_tests(self):
"""
Fire up the Django test suite developed for version 1.2
Fire up the Django test suite
"""
settings.configure(
DEBUG = True,
Expand All @@ -52,8 +52,19 @@ def run_tests(self):
},
INSTALLED_APPS = self.INSTALLED_APPS + self.apps
)
from django.test.simple import DjangoTestSuiteRunner
failures = DjangoTestSuiteRunner().run_tests(self.apps, verbosity=1)
if VERSION >= (1, 7):
import django
django.setup()
try:
# Django < 1.8
from django.test.simple import DjangoTestSuiteRunner
test_runner = DjangoTestSuiteRunner(verbosity=1)
except ImportError:
# Django >= 1.8
from django.test.runner import DiscoverRunner
test_runner = DiscoverRunner(verbosity=1)

failures = test_runner.run_tests(self.apps)
if failures: # pragma: no cover
sys.exit(failures)

Expand Down
4 changes: 3 additions & 1 deletion screamshot/models.py
@@ -1,3 +1,4 @@
from django.utils.encoding import python_2_unicode_compatible
import io
import imghdr
from hashlib import md5
Expand Down Expand Up @@ -48,6 +49,7 @@ def get_available_name(self, name):
return name


@python_2_unicode_compatible
class WebPageScreenshot(models.Model):
"""Straightforward implementation of screamshots as model objects.
in additions to fields used for screamshot.utils.casperjs_capture
Expand Down Expand Up @@ -118,7 +120,7 @@ class Meta:
verbose_name = _("Web page screenshot")
verbose_name_plural = _("Web pages screenshots")

def __unicode__(self):
def __str__(self):
return self.title

def save(self, *args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion screamshot/urls.py
@@ -1,6 +1,6 @@
from django.conf.urls import patterns, url

from views import capture
from .views import capture


urlpatterns = patterns('screamshot.views',
Expand Down
25 changes: 14 additions & 11 deletions screamshot/utils.py
@@ -1,15 +1,19 @@
from django.utils import six
import os
import logging
import subprocess
from tempfile import NamedTemporaryFile
import json
from urlparse import urljoin
from mimetypes import guess_type, guess_all_extensions

try:
from urllib.parse import urljoin
except ImportError:
# Python 2
from urlparse import urljoin
from django.core.exceptions import ImproperlyConfigured, ValidationError
from django.core.urlresolvers import reverse
from django.core.validators import URLValidator
from StringIO import StringIO
from io import StringIO
from django.template.loader import render_to_string
from django.conf import settings

Expand Down Expand Up @@ -98,13 +102,12 @@ def casperjs_capture(stream, url, method=None, width=None, height=None,
"""
Captures web pages using ``casperjs``
"""
if isinstance(stream, six.string_types):
output = stream
else:
with NamedTemporaryFile('wb+', suffix='.%s' % render, delete=False) as f:
output = f.name
try:
if isinstance(stream, basestring):
output = stream
else:
with NamedTemporaryFile('rwb', suffix='.%s' % render, delete=False) as f:
output = f.name

cmd = CASPERJS_CMD + [url, output]

# Extra command-line options
Expand Down Expand Up @@ -138,7 +141,7 @@ def casperjs_capture(stream, url, method=None, width=None, height=None,
else:
if stream != output:
# From file to stream
with open(output) as out:
with open(output, 'rb') as out:
stream.write(out.read())
stream.flush()
finally:
Expand Down Expand Up @@ -227,7 +230,7 @@ def parse_render(render):
render = 'png'
else:
render = render.lower()
for k, v in formats.iteritems():
for k, v in formats.items():
if '.%s' % render in v:
render = k
break
Expand Down
8 changes: 4 additions & 4 deletions screamshot/views.py
@@ -1,13 +1,13 @@
import base64
import logging
from StringIO import StringIO
from io import BytesIO

from django.core.urlresolvers import NoReverseMatch
from django.http import HttpResponse, HttpResponseBadRequest
from django.utils.translation import ugettext as _

from utils import (casperjs_capture, CaptureError, UnsupportedImageFormat,
image_mimetype, parse_url)
from .utils import (casperjs_capture, CaptureError, UnsupportedImageFormat,
image_mimetype, parse_url)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -44,7 +44,7 @@ def capture(request):
except ValueError:
height = None

stream = StringIO()
stream = BytesIO()
try:
casperjs_capture(stream, url, method=method.lower(), width=width,
height=height, selector=selector, data=data,
Expand Down

0 comments on commit 19a19ba

Please sign in to comment.