From 59b8b2324de770546bc2318f64f41b96c411ec9e Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Sun, 23 Jun 2019 13:04:53 +0200 Subject: [PATCH 1/2] manager: translate state names (#34234) --- .../authentic2/manager/user_import.html | 4 +-- src/authentic2/manager/user_import.py | 27 +++++++++++++++---- tests/test_user_manager.py | 2 +- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/authentic2/manager/templates/authentic2/manager/user_import.html b/src/authentic2/manager/templates/authentic2/manager/user_import.html index e33de979..e3c6b917 100644 --- a/src/authentic2/manager/templates/authentic2/manager/user_import.html +++ b/src/authentic2/manager/templates/authentic2/manager/user_import.html @@ -56,8 +56,8 @@ {% for report in reports %} - {% if report.state != 'running' %}{{ report.created }}{% else %}{{ report.created }}{% endif %} - {{ report.state }} {% if report.state == 'error' %}"{{ report.exception }}"{% endif %} {% if not report.is_running %}!failed before finishing!{% endif %} + {% if report.state != report.STATE_RUNNING %}{{ report.created }}{% else %}{{ report.created }}{% endif %} + {{ report.state_display }} {% if report.state == report.STATE_ERROR %}"{{ report.exception }}"{% endif %} {% if not report.is_running %}!failed before finishing!{% endif %} {% if not report.simulate %}{% endif %} {% if report.simulate %}
{% csrf_token %}
{% endif %} diff --git a/src/authentic2/manager/user_import.py b/src/authentic2/manager/user_import.py index 49c09800..43db2157 100644 --- a/src/authentic2/manager/user_import.py +++ b/src/authentic2/manager/user_import.py @@ -34,6 +34,7 @@ from django.db import connection from django.utils import six from django.utils.functional import cached_property from django.utils.timezone import utc +from django.utils.translation import ugettext_lazy as _ from authentic2.utils import gettid @@ -123,6 +124,17 @@ class UserImport(object): class Report(object): + STATE_WAITING = 'waiting' + STATE_RUNNING = 'running' + STATE_FINISHED = 'finished' + STATE_ERROR = 'error' + STATES = { + STATE_WAITING: _('Waiting'), + STATE_RUNNING: _('Running'), + STATE_FINISHED: _('Finished'), + STATE_ERROR: _('Error'), + } + def __init__(self, user_import, uuid): self.user_import = user_import self.uuid = uuid @@ -140,6 +152,11 @@ class Report(object): data = pickle.load(fd) return data + @property + def state_display(self): + state = self.data['state'] + return self.STATES.get(state, state) + @property @contextlib.contextmanager def data_update(self): @@ -155,11 +172,11 @@ class Report(object): with report.data_update as data: data['encoding'] = user_import.meta['encoding'] data['ou'] = user_import.meta.get('ou') - data['state'] = 'waiting' + data['state'] = cls.STATE_WAITING return report def run(self, start=True, simulate=False): - assert self.data.get('state') == 'waiting' + assert self.data.get('state') == self.STATE_WAITING with self.data_update as data: data['simulate'] = simulate @@ -171,7 +188,7 @@ class Report(object): importer = UserCsvImporter() start = datetime.datetime.now() with self.data_update as data: - data['state'] = 'running' + data['state'] = self.STATE_RUNNING data['pid'] = os.getpid() data['tid'] = gettid() try: @@ -181,14 +198,14 @@ class Report(object): simulate=simulate) except Exception as e: logger.exception('error during report %s:%s run', self.user_import.uuid, self.uuid) - state = 'error' + state = self.STATE_ERROR try: exception = six.text_type(e) except Exception: exception = repr(repr(e)) else: exception = None - state = 'finished' + state = self.STATE_FINISHED finally: duration = datetime.datetime.now() - start try: diff --git a/tests/test_user_manager.py b/tests/test_user_manager.py index 976176f4..334713a8 100644 --- a/tests/test_user_manager.py +++ b/tests/test_user_manager.py @@ -271,7 +271,7 @@ x,x,x,x'''.encode(encoding), def wait_finished(): new_resp = response.click('Users Import') - if new_resp.pyquery('tr[data-uuid="%s"] td.state span' % uuid).text() == 'finished': + if new_resp.pyquery('tr[data-uuid="%s"] td.state span' % uuid).text() == 'Finished': return new_resp simulate = reports[0] -- 2.20.1