Skip to content

Commit b6782b4

Browse files
committed
connect signals in tests instead; import sorting; doc tweaks
1 parent b541fa3 commit b6782b4

File tree

5 files changed

+50
-42
lines changed

5 files changed

+50
-42
lines changed

docs/advanced.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,9 @@ source model. This is possible by combining the ``bases`` functionality with the
391391
392392
# define your signal handler/callback anywhere outside of models.py
393393
def add_history_ip_address(sender, **kwargs):
394-
thread = threading.local()
395394
history_instance = kwargs['history_instance']
396-
# (thread.request for use only in conjunction with the simple_history middleware)
397-
history_instance.ip_address = thread.request.META['REMOTE_ADDR']
395+
# thread.request for use only when the simple_history middleware is enabled
396+
history_instance.ip_address = HistoricalRecords.thread.request.META['REMOTE_ADDR']
398397
399398
400399
.. code-block:: python
@@ -512,7 +511,7 @@ This may be useful when you want to construct timelines and need to get only the
512511
513512
Using signals
514513
------------------------------------
515-
django-simple-history includes signals that helps you provide custom behaviour when saving a historical record. Arguments passed to the signals include the following:
514+
`django-simple-history` includes signals that help you provide custom behavior when saving a historical record. Arguments passed to the signals include the following:
516515
517516
instance
518517
The source model instance being saved

simple_history/tests/apps.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

simple_history/tests/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
from django.apps import apps
66
from django.conf import settings
7-
from django.dispatch import receiver
87
from django.db import models
8+
from django.dispatch import receiver
99
from django.urls import reverse
1010

1111
from simple_history import register

simple_history/tests/tests/test_models.py

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010
from django.core.files.base import ContentFile
1111
from django.db import models
1212
from django.db.models.fields.proxy import OrderWrt
13-
from django.test import TestCase, override_settings
13+
from django.test import override_settings, TestCase
1414
from django.urls import reverse
1515

1616
from simple_history.models import (
1717
HistoricalRecords,
1818
ModelChange
1919
)
20+
from simple_history.signals import pre_create_historical_record
2021
from simple_history.tests.tests.utils import middleware_override_settings
2122
from simple_history.utils import update_change_reason
2223
from ..external.models import ExternalModel2, ExternalModel4
@@ -45,16 +46,17 @@
4546
HistoricalChoice,
4647
HistoricalCustomFKError,
4748
HistoricalPoll,
49+
HistoricalPollWithHistoricalIPAddress,
4850
HistoricalState,
4951
Library,
5052
MultiOneToOne,
5153
Person,
5254
Place,
5355
Poll,
5456
PollInfo,
55-
PollWithHistoricalIPAddress,
5657
PollWithExcludeFields,
5758
PollWithExcludedFKField,
59+
PollWithHistoricalIPAddress,
5860
Province,
5961
Restaurant,
6062
SelfFK,
@@ -1052,14 +1054,32 @@ def test_changed_value_lost(self):
10521054
instance = historical.instance
10531055
self.assertEqual(instance.place, new_place)
10541056

1057+
def add_static_history_ip_address(sender, **kwargs):
1058+
history_instance = kwargs['history_instance']
1059+
history_instance.ip_address = '192.168.0.1'
1060+
1061+
1062+
class ExtraFieldsStaticIPAddressTestCase(TestCase):
1063+
def setUp(self):
1064+
pre_create_historical_record.connect(
1065+
add_static_history_ip_address,
1066+
sender=HistoricalPollWithHistoricalIPAddress,
1067+
dispatch_uid='add_static_history_ip_address'
1068+
)
1069+
1070+
def tearDown(self):
1071+
pre_create_historical_record.disconnect(
1072+
add_static_history_ip_address,
1073+
sender=HistoricalPollWithHistoricalIPAddress,
1074+
dispatch_uid='add_static_history_ip_address'
1075+
)
10551076

1056-
class ExtraFieldsTestCase(TestCase):
10571077
def test_extra_ip_address_field_populated_on_save(self):
10581078
poll = PollWithHistoricalIPAddress.objects.create(
10591079
question="Will it blend?", pub_date=today
10601080
)
10611081

1062-
poll_history = poll.history.order_by('history_date')[0]
1082+
poll_history = poll.history.first()
10631083

10641084
self.assertEquals('192.168.0.1', poll_history.ip_address)
10651085

@@ -1072,13 +1092,31 @@ def test_extra_ip_address_field_not_present_on_poll(self):
10721092
poll.ip_address
10731093

10741094

1095+
def add_dynamic_history_ip_address(sender, **kwargs):
1096+
history_instance = kwargs['history_instance']
1097+
history_instance.ip_address = \
1098+
HistoricalRecords.thread.request.META['REMOTE_ADDR']
1099+
1100+
10751101
@override_settings(**middleware_override_settings)
1076-
class ExtraFieldsIPAddressTestCase(TestCase):
1102+
class ExtraFieldsDynamicIPAddressTestCase(TestCase):
1103+
def setUp(self):
1104+
pre_create_historical_record.connect(
1105+
add_dynamic_history_ip_address,
1106+
sender=HistoricalPollWithHistoricalIPAddress,
1107+
dispatch_uid='add_dynamic_history_ip_address'
1108+
)
1109+
1110+
def tearDown(self):
1111+
pre_create_historical_record.disconnect(
1112+
add_dynamic_history_ip_address,
1113+
sender=HistoricalPollWithHistoricalIPAddress,
1114+
dispatch_uid='add_dynamic_history_ip_address'
1115+
)
1116+
10771117
def test_signal_is_able_to_retrieve_request_from_thread(self):
10781118
data = {
1079-
# workaround for bad interplay with signals and mocking; see
1080-
# simple_history.tests.tests.utils.add_history_ip_address
1081-
'question': 'read IP from request',
1119+
'question': 'Will it blend?',
10821120
'pub_date': '2018-10-30'
10831121
}
10841122

simple_history/tests/tests/utils.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,3 @@
1515
settings.MIDDLEWARE_CLASSES + [request_middleware]
1616
)
1717
}
18-
19-
20-
def add_history_ip_address(sender, **kwargs):
21-
history_instance = kwargs['history_instance']
22-
if history_instance.question == 'read IP from request':
23-
from simple_history.models import HistoricalRecords
24-
history_instance.ip_address = \
25-
HistoricalRecords.thread.request.META['REMOTE_ADDR']
26-
else:
27-
history_instance.ip_address = '192.168.0.1'

0 commit comments

Comments
 (0)