|
1 | 1 | from __future__ import unicode_literals |
2 | 2 |
|
3 | | -import threading |
4 | 3 | import copy |
5 | | -import warnings |
| 4 | +import importlib |
| 5 | +import threading |
6 | 6 |
|
7 | | -import django |
8 | 7 | from django.db import models, router |
9 | 8 | from django.db.models.fields.proxy import OrderWrt |
10 | | -from django.db.models.fields.related import RelatedField |
11 | 9 | from django.conf import settings |
12 | 10 | from django.contrib import admin |
13 | 11 | from django.utils import six |
|
20 | 18 | from django.apps import apps |
21 | 19 | except ImportError: # Django < 1.7 |
22 | 20 | from django.db.models import get_app |
23 | | -try: |
24 | | - import importlib |
25 | | -except ImportError: # Python < 2.7 |
26 | | - from django.utils import importlib |
27 | 21 | try: |
28 | 22 | from south.modelsinspector import add_introspection_rules |
29 | 23 | except ImportError: # south not present |
@@ -139,19 +133,14 @@ def copy_fields(self, model): |
139 | 133 | field.__class__ = models.IntegerField |
140 | 134 | if isinstance(field, models.ForeignKey): |
141 | 135 | old_field = field |
142 | | - field_arguments = {} |
| 136 | + field_arguments = {'db_constraint': False} |
143 | 137 | if (getattr(old_field, 'one_to_one', False) or |
144 | 138 | isinstance(old_field, models.OneToOneField)): |
145 | 139 | FieldType = models.ForeignKey |
146 | 140 | else: |
147 | 141 | FieldType = type(old_field) |
148 | | - if django.get_version() >= "1.6": |
149 | | - field_arguments['db_constraint'] = False |
150 | 142 | if getattr(old_field, 'to_fields', []): |
151 | 143 | field_arguments['to_field'] = old_field.to_fields[0] |
152 | | - elif (django.get_version() < "1.6" and |
153 | | - old_field.rel.field_name != 'id'): |
154 | | - field_arguments['to_field'] = old_field.rel.field_name |
155 | 144 | if getattr(old_field, 'db_column', None): |
156 | 145 | field_arguments['db_column'] = old_field.db_column |
157 | 146 | field = FieldType( |
@@ -181,17 +170,16 @@ def get_extra_fields(self, model, fields): |
181 | 170 | def revert_url(self): |
182 | 171 | """URL for this change in the default admin site.""" |
183 | 172 | opts = model._meta |
184 | | - try: |
185 | | - app_label, model_name = opts.app_label, opts.model_name |
186 | | - except AttributeError: # Django < 1.7 |
187 | | - app_label, model_name = opts.app_label, opts.module_name |
| 173 | + app_label, model_name = opts.app_label, opts.model_name |
188 | 174 | return ('%s:%s_%s_simple_history' % |
189 | 175 | (admin.site.name, app_label, model_name), |
190 | 176 | [getattr(self, opts.pk.attname), self.history_id]) |
191 | 177 |
|
192 | 178 | def get_instance(self): |
193 | | - return model(**dict([(field.attname, getattr(self, field.attname)) |
194 | | - for field in fields.values()])) |
| 179 | + return model(**{ |
| 180 | + field.attname: getattr(self, field.attname) |
| 181 | + for field in fields.values() |
| 182 | + }) |
195 | 183 |
|
196 | 184 | return { |
197 | 185 | 'history_id': models.AutoField(primary_key=True), |
@@ -261,81 +249,6 @@ def get_history_user(self, instance): |
261 | 249 | return None |
262 | 250 |
|
263 | 251 |
|
264 | | -class CustomForeignKeyField(models.ForeignKey): |
265 | | - |
266 | | - def __init__(self, *args, **kwargs): |
267 | | - warnings.warn("CustomForeignKeyField is deprecated.", |
268 | | - DeprecationWarning) |
269 | | - super(CustomForeignKeyField, self).__init__(*args, **kwargs) |
270 | | - self.db_constraint = False |
271 | | - self.generate_reverse_relation = False |
272 | | - |
273 | | - def get_attname(self): |
274 | | - return self.name |
275 | | - |
276 | | - def get_one_to_one_field(self, to_field, other): |
277 | | - # HACK This creates a new custom foreign key based on to_field, |
278 | | - # and calls itself with that, effectively making the calls |
279 | | - # recursive |
280 | | - temp_field = self.__class__(to_field.rel.to._meta.object_name) |
281 | | - for key, val in to_field.__dict__.items(): |
282 | | - if (isinstance(key, six.string_types) |
283 | | - and not key.startswith('_')): |
284 | | - setattr(temp_field, key, val) |
285 | | - field = self.__class__.get_field( |
286 | | - temp_field, other, to_field.rel.to) |
287 | | - return field |
288 | | - |
289 | | - def get_field(self, other, cls): |
290 | | - # this hooks into contribute_to_class() and this is |
291 | | - # called specifically after the class_prepared signal |
292 | | - to_field = copy.copy(self.rel.to._meta.pk) |
293 | | - field = self |
294 | | - if isinstance(to_field, models.OneToOneField): |
295 | | - field = self.get_one_to_one_field(to_field, other) |
296 | | - elif isinstance(to_field, models.AutoField): |
297 | | - field.__class__ = convert_auto_field(to_field) |
298 | | - else: |
299 | | - field.__class__ = to_field.__class__ |
300 | | - excluded_prefixes = ("_", "__") |
301 | | - excluded_attributes = ( |
302 | | - "rel", |
303 | | - "creation_counter", |
304 | | - "validators", |
305 | | - "error_messages", |
306 | | - "attname", |
307 | | - "column", |
308 | | - "help_text", |
309 | | - "name", |
310 | | - "model", |
311 | | - "unique_for_year", |
312 | | - "unique_for_date", |
313 | | - "unique_for_month", |
314 | | - "db_tablespace", |
315 | | - "db_index", |
316 | | - "db_column", |
317 | | - "default", |
318 | | - "auto_created", |
319 | | - "null", |
320 | | - "blank", |
321 | | - ) |
322 | | - for key, val in to_field.__dict__.items(): |
323 | | - if (isinstance(key, six.string_types) |
324 | | - and not key.startswith(excluded_prefixes) |
325 | | - and key not in excluded_attributes): |
326 | | - setattr(field, key, val) |
327 | | - return field |
328 | | - |
329 | | - def do_related_class(self, other, cls): |
330 | | - field = self.get_field(other, cls) |
331 | | - transform_field(field) |
332 | | - field.rel = None |
333 | | - |
334 | | - def contribute_to_class(self, cls, name): |
335 | | - # HACK: remove annoying descriptor (don't super()) |
336 | | - RelatedField.contribute_to_class(self, cls, name) |
337 | | - |
338 | | - |
339 | 252 | def transform_field(field): |
340 | 253 | """Customize field appropriately for use in historical model""" |
341 | 254 | field.name = field.attname |
|
0 commit comments