Merge branch 'versions/next' into feature/db_migration

Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
Roberto Rosario
2018-08-04 01:59:17 -04:00
9 changed files with 1885 additions and 12 deletions

View File

@@ -205,3 +205,18 @@ class UserLoginTestCase(BaseTestCase):
response = self.client.get(reverse('documents:document_list'))
self.assertEqual(response.status_code, 200)
def test_username_login_redirect(self):
TEST_REDIRECT_URL = '/about/'
response = self.client.post(
'{}?next={}'.format(
reverse(settings.LOGIN_URL), TEST_REDIRECT_URL
), {
'username': TEST_ADMIN_USERNAME,
'password': TEST_ADMIN_PASSWORD,
'remember_me': False
}, follow=True
)
self.assertEqual(response.redirect_chain, [(TEST_REDIRECT_URL, 302)])

View File

@@ -10,6 +10,7 @@ from django.contrib.auth.views import (
from django.http import HttpResponseRedirect
from django.shortcuts import redirect, resolve_url
from django.urls import reverse
from django.utils.http import is_safe_url
from django.utils.translation import ugettext_lazy as _
from stronghold.decorators import public
@@ -26,6 +27,7 @@ def login_view(request):
Control how the use is to be authenticated, options are 'email' and
'username'
"""
success_url_allowed_hosts = set()
kwargs = {'template_name': 'authentication/login.html'}
if setting_login_method.value == 'email':
@@ -33,10 +35,25 @@ def login_view(request):
else:
kwargs['authentication_form'] = UsernameAuthenticationForm
allowed_hosts = {request.get_host()}
allowed_hosts.update(success_url_allowed_hosts)
redirect_to = request.POST.get(
REDIRECT_FIELD_NAME, request.GET.get(REDIRECT_FIELD_NAME, '')
)
url_is_safe = is_safe_url(
url=redirect_to,
allowed_hosts=allowed_hosts,
require_https=request.is_secure(),
)
url = redirect_to if url_is_safe else ''
if not request.user.is_authenticated:
extra_context = {
'appearance_type': 'plain',
REDIRECT_FIELD_NAME: resolve_url(settings.LOGIN_REDIRECT_URL)
REDIRECT_FIELD_NAME: url or resolve_url(settings.LOGIN_REDIRECT_URL)
}
result = login(request, extra_context=extra_context, **kwargs)

View File

@@ -2,9 +2,12 @@ from __future__ import unicode_literals
from django.test import TestCase
from documents.tests import GenericDocumentTestCase
from ..models import Transformation
from ..transformations import (
BaseTransformation, TransformationResize, TransformationRotate,
TransformationZoom
BaseTransformation, TransformationCrop, TransformationResize,
TransformationRotate, TransformationZoom
)
TRANSFORMATION_RESIZE_WIDTH = 123
@@ -20,7 +23,7 @@ TRANSFORMATION_ZOOM_PERCENT = 49
TRANSFORMATION_ZOOM_CACHE_HASH = '1b333603674469e0'
class TransformationTestCase(TestCase):
class TransformationBaseTestCase(TestCase):
def test_cache_uniqness(self):
transformation_1 = TransformationResize(width=640, height=640)
@@ -101,3 +104,50 @@ class TransformationTestCase(TestCase):
(transformation_rotate, transformation_resize, transformation_zoom)
), TRANSFORMATION_COMBINED_CACHE_HASH
)
class TransformationTestCase(GenericDocumentTestCase):
def test_crop_transformation_optional_arguments(self):
document_page = self.document.pages.first()
Transformation.objects.add_for_model(
obj=document_page, transformation=TransformationCrop,
arguments={'top': '10'}
)
self.assertTrue(document_page.generate_image().startswith('page'))
def test_crop_transformation_invalid_arguments(self):
document_page = self.document.pages.first()
Transformation.objects.add_for_model(
obj=document_page, transformation=TransformationCrop,
arguments={'top': 'x', 'left': '-'}
)
self.assertTrue(document_page.generate_image().startswith('page'))
def test_crop_transformation_non_valid_range_arguments(self):
document_page = self.document.pages.first()
Transformation.objects.add_for_model(
obj=document_page, transformation=TransformationCrop,
arguments={'top': '-1000', 'bottom': '100000000'}
)
self.assertTrue(document_page.generate_image().startswith('page'))
def test_crop_transformation_overlapping_ranges_arguments(self):
document_page = self.document.pages.first()
Transformation.objects.add_for_model(
obj=document_page, transformation=TransformationCrop,
arguments={'top': '1000', 'bottom': '1000'}
)
Transformation.objects.add_for_model(
obj=document_page, transformation=TransformationCrop,
arguments={'left': '1000', 'right': '10000'}
)
self.assertTrue(document_page.generate_image().startswith('page'))

View File

@@ -88,10 +88,48 @@ class TransformationCrop(BaseTransformation):
def execute_on(self, *args, **kwargs):
super(TransformationCrop, self).execute_on(*args, **kwargs)
return self.image.crop(
(self.left, self.top, self.right, self.bottom)
left = float(self.left or '0')
top = float(self.top or '0')
right = self.image.size[0] - float(self.right or '0')
bottom = self.image.size[1] - float(self.bottom or '0')
if left < 0:
left = 0
if left > self.image.size[0] - 1:
left = self.image.size[0] - 1
if top < 0:
top = 0
if top > self.image.size[1] - 1:
top = self.image.size[1] - 1
if right < 0:
right = 0
if right > self.image.size[0] - 1:
right = self.image.size[0] - 1
if bottom < 0:
bottom = 0
if bottom > self.image.size[1] - 1:
bottom = self.image.size[1] - 1
if left > right:
left = right - 1
if top > bottom:
top = bottom - 1
logger.debug(
'left: %f, top: %f, right: %f, bottom: %f', left, top, right,
bottom
)
return self.image.crop((left, top, right, bottom))
class TransformationFlip(BaseTransformation):
arguments = ()