Satisfy python2 tests

This commit is contained in:
2020-09-29 19:02:55 +02:00
parent 10ea6affaf
commit 09bfbeedc4
3 changed files with 29 additions and 10 deletions

View File

@@ -15,7 +15,7 @@ from django.db import models
from django.template.loader import get_template
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import get_language, ugettext_lazy as _
from django_countries.fields import CountryField
from django_countries.fields import Country, CountryField
from .. import choices
from .. import config
@@ -326,12 +326,22 @@ class Event(models.Model):
for field in fields:
field_name = field.name
from_value = getattr(event, field_name)
if (isinstance(from_value, datetime.datetime) or
isinstance(from_value, datetime.date) or
isinstance(from_value, datetime.time) or
isinstance(from_value, Country)):
from_value = str(from_value)
to_value = getattr(self, field_name)
if (isinstance(to_value, datetime.datetime) or
isinstance(to_value, datetime.date) or
isinstance(to_value, datetime.time) or
isinstance(to_value, Country)):
to_value = str(to_value)
if from_value != to_value:
change = {
'field': field_name,
'refer': str(from_value),
'current': str(to_value),
'refer': from_value,
'current': to_value,
}
changes.append(change)
diff_text = json.dumps(changes)

View File

@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.conf import settings
from django.db import models

View File

@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import datetime
import json
@@ -36,19 +37,26 @@ class EventsTestCase(EventMixin, TestCase):
event.save()
event.country = 'FR'
event.save()
event.trainer_familyname += '-Ömlaut'
event.max_participants = 8
event.save()
changes = event.changes
self.assertEqual(changes.count(), 2)
self.assertEqual(changes.count(), 3)
subchanges = json.loads(changes.first().content)
subchanges = json.loads(changes.get(pk=1).content)
self.assertEqual(len(subchanges), 3)
self.assertIn({'field': 'alt_first_day', 'refer': 'None', 'current': '2019-03-02'}, subchanges)
self.assertIn({'field': 'alt_first_day', 'refer': None, 'current': '2019-03-02'}, subchanges)
self.assertIn({'field': 'sport', 'refer': 'W', 'current': 'M'}, subchanges)
self.assertIn({'field': 'ski_lift', 'refer': 'False', 'current': 'True'}, subchanges)
self.assertIn({'field': 'ski_lift', 'refer': False, 'current': True}, subchanges)
subchanges = json.loads(changes.last().content)
self.assertEqual(len(subchanges), 2)
subchanges = json.loads(changes.get(pk=2).content)
self.assertEqual(len(subchanges), 1)
self.assertIn({'field': 'country', 'refer': 'DE', 'current': 'FR'}, subchanges)
self.assertIn({'field': 'max_participants', 'refer': '0', 'current': '8'}, subchanges)
subchanges = json.loads(changes.get(pk=3).content)
self.assertEqual(len(subchanges), 2)
self.assertIn({'field': 'trainer_familyname', 'refer': 'Weißalles', 'current': 'Weißalles-Ömlaut'}, subchanges)
self.assertIn({'field': 'max_participants', 'refer': 0, 'current': 8}, subchanges)