Skip to content

Commit

Permalink
add compat module, fix 1.10 warning
Browse files Browse the repository at this point in the history
  • Loading branch information
GeyseR committed Sep 7, 2016
1 parent 30be946 commit 78879b5
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 37 deletions.
39 changes: 39 additions & 0 deletions django_any/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import django

if django.VERSION >= (1, 8):
from django.utils.lorem_ipsum import paragraphs
else:
from django.contrib.webdesign.lorem_ipsum import paragraphs


ipaddress_field_defined = django.VERSION < (1, 9)


def get_model_onetoone_fields(model):
if django.VERSION >= (1, 8):
return [
(relation.name, relation)
for relation in model._meta.get_fields()
if relation.one_to_one and relation.auto_created
]
else:
# procceed reversed relations
return [
(relation.var_name, relation.field)
for relation in model._meta.get_all_related_objects()
if relation.field.unique # TODO and not relation.field.rel.parent_link ??
]


def get_model_private_fields(model):
if django.VERSION >= (1, 10):
return model._meta.private_fields
else:
return model._meta.virtual_fields


def get_related_field_model(field):
if django.VERSION >= (1, 9):
return field.target_field.model
else:
return field.related_field.model
8 changes: 3 additions & 5 deletions django_any/contrib/default.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# -*- coding: utf-8 -*-
from inspect import isfunction, ismethod
import django
from django.db.models.fields import NOT_PROVIDED
from django.db.models.fields.related import ForeignKey, OneToOneField

from django_any import compat
from django_any.models import any_model


Expand All @@ -16,10 +17,7 @@ def any_model_with_defaults(cls, **attrs):
# for stuff like default=datetime.now
default = default()
if isinstance(field, (ForeignKey, OneToOneField)):
if django.VERSION >= (1, 9):
Model = field.target_field.model
else:
Model = field.related_field.model
Model = compat.get_related_field_model(field)
if not isinstance(default, Model):
try:
default = Model.objects.get(pk=default)
Expand Down
5 changes: 3 additions & 2 deletions django_any/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import random
from datetime import date, datetime, time

import django
from django import forms
from django.utils import formats

from django_any import compat
from django_any import xunit
from django_any.functions import valid_choices, split_model_kwargs, \
ExtensionMethod
Expand Down Expand Up @@ -242,7 +243,7 @@ def integer_field_data(field, **kwargs):
return str(xunit.any_int(min_value=min_value, max_value=max_value))


if django.VERSION < (1, 9):
if compat.ipaddress_field_defined:
@any_form_field.register(forms.IPAddressField)
def ipaddress_field_data(field, **kwargs):
"""
Expand Down
33 changes: 9 additions & 24 deletions django_any/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,31 @@
"""
Values generators for common Django Fields
"""
import django

from django.contrib.contenttypes.models import ContentType
import re, os, random
from decimal import Decimal
from datetime import date, datetime, time
from string import ascii_letters, digits, hexdigits
from random import choice

from django.conf import settings
from django.core.exceptions import ValidationError
from django.core import validators
from django.core.exceptions import ValidationError
from django.core.validators import validate_ipv4_address
from django.contrib.contenttypes.models import ContentType
from django.db import models, IntegrityError
from django.db.models import Q
from django.db.models.fields.files import FieldFile

if django.VERSION >= (1, 8):
from django.utils.lorem_ipsum import paragraphs
else:
from django.contrib.webdesign.lorem_ipsum import paragraphs

from django.core.validators import validate_ipv4_address
try:
from django.core.validators import validate_ipv6_address, validate_ipv46_address
except ImportError:
validate_ipv6_address = None
validate_ipv46_address = None

from django_any import compat
from django_any import xunit
from django_any.compat import paragraphs
from django_any.functions import valid_choices, split_model_kwargs, ExtensionMethod

any_field = ExtensionMethod()
Expand Down Expand Up @@ -295,7 +291,7 @@ def get_some_file(path):
return result


if django.VERSION < (1, 9):
if compat.ipaddress_field_defined:
@any_field.register(models.IPAddressField)
def any_ipaddress_field(field, **kwargs):
"""
Expand Down Expand Up @@ -344,7 +340,7 @@ def any_genericipaddress_field(field, **kwargs):
raise Exception('Unexpected validators')

if protocol == 'ipv4':
if django.VERSION < (1, 9):
if compat.ipaddress_field_defined:
return any_ipaddress_field(field)
else:
return any_genericipaddress_field(field)
Expand Down Expand Up @@ -494,7 +490,7 @@ def _fill_model_fields(model, **kwargs):
model_fields, fields_args = split_model_kwargs(kwargs)

# fill virtual fields
for field in model._meta.virtual_fields:
for field in compat.get_model_private_fields(model):
if field.name in model_fields:
object = kwargs[field.name]
model_fields[field.ct_field] = kwargs[field.ct_field]= ContentType.objects.get_for_model(object)
Expand Down Expand Up @@ -527,18 +523,7 @@ def _fill_model_fields(model, **kwargs):
else:
setattr(model, field.name, any_field(field, **fields_args[field.name]))

# TODO dunno what I did here but seems like it works (I hope)
if django.VERSION >= (1, 8):
onetoone = [(relation.name, relation)
for relation in model._meta.get_fields()
if relation.one_to_one and relation.auto_created]
else:
# procceed reversed relations
onetoone = [(relation.var_name, relation.field) \
for relation in model._meta.get_all_related_objects() \
if relation.field.unique] # TODO and not relation.field.rel.parent_link ??

for field_name, field in onetoone:
for field_name, field in compat.get_model_onetoone_fields(model):
if field_name in model_fields:
# TODO support any_model call
setattr(model, field_name, kwargs[field_name])
Expand Down
12 changes: 7 additions & 5 deletions django_any/tests/test_contrib_defaults.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# -*- coding: utf-8; -*-
import django
from django.db import models
from django.test import TestCase
import datetime
from decimal import Decimal
from django_any.contrib.default import any_model_with_defaults

from django.db import models
from django.test import TestCase
from django.db.models.fields import NOT_PROVIDED

from django_any import compat
from django_any.contrib.default import any_model_with_defaults


class SimpleModelWithDefaults(models.Model):
big_integer_field = models.BigIntegerField(default=8223372036854775807)
Expand All @@ -19,7 +21,7 @@ class SimpleModelWithDefaults(models.Model):
email_field = models.EmailField(default='admin@dev.null')
float_field = models.FloatField(default=0.7)
integer_field = models.IntegerField(default=42)
if django.VERSION < (1, 9):
if compat.ipaddress_field_defined:
ip_field = models.IPAddressField(default='127.0.0.1')
else:
ip_field = models.GenericIPAddressField(default='127.0.0.1')
Expand Down
5 changes: 4 additions & 1 deletion django_any/tests/test_model_creation_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import django
from django.db import models
from django.test import TestCase

from django_any import compat
from django_any.models import any_model


class SimpleModel(models.Model):
big_integer_field = models.BigIntegerField()
char_field = models.CharField(max_length=5)
Expand All @@ -18,7 +21,7 @@ class SimpleModel(models.Model):
email_field = models.EmailField()
float_field = models.FloatField()
integer_field = models.IntegerField()
if django.VERSION < (1, 9):
if compat.ipaddress_field_defined:
ip_field = models.IPAddressField()
else:
ip_field = models.GenericIPAddressField()
Expand Down

0 comments on commit 78879b5

Please sign in to comment.