Skip to content

Commit

Permalink
Merge ea2468e into 3661f9f
Browse files Browse the repository at this point in the history
  • Loading branch information
querti committed Apr 2, 2020
2 parents 3661f9f + ea2468e commit 05a9bf3
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 45 deletions.
1 change: 1 addition & 0 deletions constraints-django111.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Django==1.11.29
25 changes: 20 additions & 5 deletions examples/state/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Django settings for foo project.
from django import VERSION

DEBUG = True
TEMPLATE_DEBUG = DEBUG
Expand Down Expand Up @@ -65,11 +66,25 @@

ROOT_URLCONF = 'state.urls'

TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
# The way to specify the template dirs differs between newer and older versions of Django
if VERSION[0:3] < (1, 9, 0):
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
if VERSION[0:3] >= (1, 9, 0):
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
),
'APP_DIRS': True
}
]

INSTALLED_APPS = (
'django.contrib.auth',
Expand Down
19 changes: 17 additions & 2 deletions kobo/django/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from django.utils.translation import ugettext_lazy as _
from django.core import exceptions
from django.utils.text import capfirst
from django.forms.widgets import Select
from django.forms.fields import CallableChoiceIterator

import kobo.django.forms
from kobo.types import StateEnum
Expand Down Expand Up @@ -42,11 +44,12 @@ class MyModel(models.Model):

class StateEnumField(models.IntegerField):
'''StateEnum DB encapsulation'''

widget = Select

def __init__(self, state_machine, *args, **kwargs):
super(StateEnumField, self).__init__(*args, **kwargs)
self.state_machine = state_machine
self.widget = Select
if self.has_default:
self.state_machine.set_state(self.default)

Expand Down Expand Up @@ -98,7 +101,19 @@ def to_python(self, value):

def _get_choices(self):
return tuple(self.state_machine.get_next_states_mapping(current=self.state_machine.get_state_id()))
choices = property(_get_choices, kobo.django.forms.StateChoiceFormField._set_choices)

def _set_choices(self, value):
# Setting choices also sets the choices on the widget.
# choices can be any iterable, but we call list() on it because
# it will be consumed more than once.
if callable(value):
value = CallableChoiceIterator(value)
else:
value = list(value)

self._choices = self.widget.choices = value

choices = property(_get_choices, _set_choices)


def get_db_prep_value(self, value, connection, prepared=False):
Expand Down
2 changes: 1 addition & 1 deletion kobo/hub/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Migration(migrations.Migration):
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('archive', models.BooleanField(default=False, help_text="When a task is archived, it disappears from admin interface and cannot be accessed by taskd.<br />Make sure that archived tasks are finished and you won't need them anymore.")),
('state', models.PositiveIntegerField(default=0, help_text='Current task state.', choices=[(0, b'FREE'), (1, b'ASSIGNED'), (2, b'OPEN'), (3, b'CLOSED'), (4, b'CANCELED'), (5, b'FAILED'), (6, b'INTERRUPTED'), (7, b'TIMEOUT'), (8, b'CREATED')])),
('state', models.PositiveIntegerField(default=0, help_text='Current task state.', choices=[(0, 'FREE'), (1, 'ASSIGNED'), (2, 'OPEN'), (3, 'CLOSED'), (4, 'CANCELED'), (5, 'FAILED'), (6, 'INTERRUPTED'), (7, 'TIMEOUT'), (8, 'CREATED')])),
('label', models.CharField(help_text='Label, description or any reason for this task.', max_length=255, blank=True)),
('exclusive', models.BooleanField(default=False, help_text='Exclusive tasks have highest priority. They are used e.g. when shutting down a worker.')),
('method', models.CharField(help_text='Method name represents appropriate task handler.', max_length=255)),
Expand Down
11 changes: 7 additions & 4 deletions kobo/hub/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
else:
from django.core.urlresolvers import reverse
from django.http import HttpResponse, StreamingHttpResponse, HttpResponseForbidden
from django.shortcuts import render_to_response, get_object_or_404
from django.shortcuts import render, get_object_or_404
from django.template import RequestContext
from django.utils.translation import ugettext_lazy as _
from django.views.generic import RedirectView
Expand Down Expand Up @@ -184,7 +184,8 @@ def _rendered_log_response(request, task, log_name):
"json_url": reverse("task/log-json", args=[task.id, log_name]),
}

return render_to_response("task/log.html", context, context_instance=RequestContext(request))
#return render_to_response("task/log.html", context, context_instance=RequestContext(request))
return render(request, "task/log.html", context)


def task_log(request, id, log_name):
Expand Down Expand Up @@ -262,8 +263,10 @@ def krb5login(request, redirect_field_name=REDIRECT_FIELD_NAME):
middleware = 'kobo.django.auth.middleware.LimitedRemoteUserMiddleware'
if middleware not in settings.MIDDLEWARE_CLASSES:
raise ImproperlyConfigured("krb5login view requires '%s' middleware installed" % middleware)
redirect_to = request.REQUEST.get(redirect_field_name, "")
redirect_to = request.POST.get(redirect_field_name, "")
if not redirect_to:
redirect_to = request.GET.get(redirect_field_name, "")
if not redirect_to:
redirect_to = reverse("home/index")
return RedirectView.as_view(url=redirect_to)(request)
return RedirectView.as_view(url=redirect_to, permanent=True)(request)

5 changes: 4 additions & 1 deletion tests/fields_test/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

import json
import unittest
import pytest

from django.core.exceptions import ValidationError
from django import VERSION
from .models import DummyDefaultModel, DummyModel, DummyNotHumanModel


Expand Down Expand Up @@ -77,7 +79,8 @@ def test_complex_assignment_with_save(self):

self.assertEqual(d.field, data)


@unittest.skipUnless(VERSION[0:3] < (1, 9, 0),
"Automatic fixture loading is not possible since syncdb was removed.")
class TestFixturesJSONField(unittest.TestCase):
"""
DO NOT ADD ANYTHING INTO DATABASE IN THIS TESTCASE
Expand Down
23 changes: 19 additions & 4 deletions tests/settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Settings for Django testcases against kobo hub
import os
import kobo
from django import VERSION

KOBO_DIR = os.path.normpath(
os.path.join(os.path.dirname(__file__), '..', 'kobo')
Expand Down Expand Up @@ -45,10 +46,24 @@
# - automatic lookup of templates under kobo/hub isn't working for some reason,
# not sure why, but might be related to use of deprecated arguments in
# render_to_string (FIXME)
TEMPLATE_DIRS = (
os.path.join(KOBO_DIR, 'admin/templates/hub/templates'),
os.path.join(KOBO_DIR, 'hub/templates'),
)
#
# The way to specify the template dirs differs between newer and older versions of Django
if VERSION[0:3] < (1, 9, 0):
TEMPLATE_DIRS = (
os.path.join(KOBO_DIR, 'admin/templates/hub/templates'),
os.path.join(KOBO_DIR, 'hub/templates'),
)
if VERSION[0:3] >= (1, 9, 0):
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': (
os.path.join(KOBO_DIR, 'admin/templates/hub/templates'),
os.path.join(KOBO_DIR, 'hub/templates')
),
'APP_DIRS': True
}
]

ROOT_URLCONF = 'tests.hub_urls'

Expand Down
3 changes: 2 additions & 1 deletion tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
teardown_module = runner.stop


class TestTaskSearchForm(django.test.TestCase):
class TestTaskSearchForm(django.test.TransactionTestCase):

def setUp(self):
self._fixture_teardown()
user = User.objects.create(username='testuser')
user2 = User.objects.create(username='anothertestuser')
arch = Arch.objects.create(name='testarch')
Expand Down
18 changes: 11 additions & 7 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
teardown_module = runner.stop


class TestArch(django.test.TestCase):
class TestArch(django.test.TransactionTestCase):

def test_export(self):
arch = Arch.objects.create(name='i386', pretty_name='32 bit')
Expand All @@ -66,7 +66,7 @@ def test_worker_count(self):
self.assertEquals(arch.worker_count, 1)


class TestChannel(django.test.TestCase):
class TestChannel(django.test.TransactionTestCase):

def test_export(self):
channel = Channel.objects.create(name='test')
Expand All @@ -90,9 +90,10 @@ def test_worker_count(self):


@override_settings(VERSION='1.0.0')
class TestWorker(django.test.TestCase):
class TestWorker(django.test.TransactionTestCase):

def setUp(self):
self._fixture_teardown()
self._arch = Arch.objects.create(name='i386', pretty_name='32 bit')
self._channel = Channel.objects.create(name='test')
self._user = User.objects.create(username='testuser')
Expand Down Expand Up @@ -236,9 +237,10 @@ def test_update_worker(self):
self.assertEquals(data['task_count'], 0)


class TestWorkerManager(django.test.TestCase):
class TestWorkerManager(django.test.TransactionTestCase):

def setUp(self):
self._fixture_teardown()
self._arch = Arch.objects.create(name='i386', pretty_name='32 bit')
self._channel = Channel.objects.create(name='test')
self._user = User.objects.create(username='testuser')
Expand Down Expand Up @@ -273,9 +275,10 @@ def test_ready(self):
self.assertEquals(workers[0].id, worker2.id)


class TestTaskManager(django.test.TestCase):
class TestTaskManager(django.test.TransactionTestCase):

def setUp(self):
self._fixture_teardown()
self._arch = Arch.objects.create(name='test-arch', pretty_name='Test Arch')
self._channel = Channel.objects.create(name='test-channel')
self._user = User.objects.create(username='test-user')
Expand Down Expand Up @@ -488,7 +491,7 @@ def test_created(self):
self.assertEquals(tasks[2].id, t3.id)


class TestTaskLog(django.test.TestCase):
class TestTaskLog(django.test.TransactionTestCase):

def test_get_chunk_from_cached_file(self):
task = PropertyMock(
Expand Down Expand Up @@ -887,9 +890,10 @@ def test_get_item_gz_file(self):


@override_settings(TASK_DIR='/task-dir')
class TestTask(django.test.TestCase):
class TestTask(django.test.TransactionTestCase):

def setUp(self):
self._fixture_teardown()
self._arch = Arch.objects.create(name='noarch', pretty_name='noarch')
self._channel = Channel.objects.create(name='default')
self._user = User.objects.create(username='testuser', is_superuser=True)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_task_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

TASK_ID = 123

class TestTaskLogs(django.test.TestCase):
class TestTaskLogs(django.test.TransactionTestCase):
def cleanDataDirs(self):
"""Delete the log directory for the test task."""
log_dir = os.path.join(django.conf.settings.TASK_DIR, '0', '0', str(TASK_ID))
Expand All @@ -44,6 +44,7 @@ def tearDown(self):
super(TestTaskLogs, self).tearDown()

def setUp(self):
self._fixture_teardown()
super(TestTaskLogs, self).setUp()

self.cleanDataDirs()
Expand Down
3 changes: 2 additions & 1 deletion tests/test_taskmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,10 @@ def run(self):
raise Exception()


class TestTaskManager(django.test.TestCase):
class TestTaskManager(django.test.TransactionTestCase):

def setUp(self):
self._fixture_teardown()
super(TestTaskManager, self).setUp()

TaskContainer.register_plugin(DummyForegroundTask)
Expand Down
7 changes: 4 additions & 3 deletions tests/test_view_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def gzip_decompress(data):
return gzfile.read()


class TestViewLog(django.test.TestCase):
class TestViewLog(django.test.TransactionTestCase):
def __init__(self, *args, **kwargs):
super(TestViewLog, self).__init__(*args, **kwargs)
# This is cached since it is a bit slow to build
Expand All @@ -110,6 +110,7 @@ def tearDown(self):
super(TestViewLog, self).tearDown()

def setUp(self):
self._fixture_teardown()
super(TestViewLog, self).setUp()

self.cleanDataDirs()
Expand Down Expand Up @@ -245,7 +246,7 @@ def test_view_zipped_big_html_context(self):
def render(*args, **kwargs):
return HttpResponse(status=200)

with mock.patch('kobo.hub.views.render_to_response', side_effect=render) as render_mock:
with mock.patch('kobo.hub.views.render', side_effect=render) as render_mock:
self.get_log('zipped_big.log')

mock_call = render_mock.mock_calls[0]
Expand All @@ -254,7 +255,7 @@ def render(*args, **kwargs):
self.assertEqual(mock_call[0], '')

call_args = mock_call[1]
(template_name, context) = call_args
(_, template_name, context) = call_args

self.assertEqual(template_name, 'task/log.html')

Expand Down

0 comments on commit 05a9bf3

Please sign in to comment.