[Python-modules-commits] [python-model-mommy] 01/03: Import python-model-mommy_1.3.0.orig.tar.gz
Edward Betts
edward at moszumanska.debian.org
Sat Nov 12 11:43:46 UTC 2016
This is an automated email from the git hooks/post-receive script.
edward pushed a commit to branch master
in repository python-model-mommy.
commit bcf3a42633c85bbbb2b0a91985c352bd10d05c1d
Author: Edward Betts <edward at 4angle.com>
Date: Sat Nov 12 11:38:07 2016 +0000
Import python-model-mommy_1.3.0.orig.tar.gz
---
MANIFEST.in | 2 +
PKG-INFO | 2 +-
model_mommy.egg-info/PKG-INFO | 2 +-
model_mommy.egg-info/SOURCES.txt | 1 +
model_mommy/__init__.py | 2 +-
model_mommy/exceptions.py | 8 ++++
model_mommy/generators.py | 23 ++++++++++-
model_mommy/mommy.py | 88 ++++++++++++++++++++++++----------------
model_mommy/recipe.py | 9 ++--
tox.ini | 27 ++++++++++++
10 files changed, 119 insertions(+), 45 deletions(-)
diff --git a/MANIFEST.in b/MANIFEST.in
index 37f5d47..94001dd 100644
--- a/MANIFEST.in
+++ b/MANIFEST.in
@@ -2,3 +2,5 @@ include model_mommy/mock_file.txt
include model_mommy/mock-img.jpeg
include README.rst
include requirements.txt
+include tox.ini
+recursive-include tests *.py
diff --git a/PKG-INFO b/PKG-INFO
index 8461898..9f7e340 100644
--- a/PKG-INFO
+++ b/PKG-INFO
@@ -1,6 +1,6 @@
Metadata-Version: 1.1
Name: model_mommy
-Version: 1.2.6
+Version: 1.3.0
Summary: Smart object creation facility for Django.
Home-page: http://github.com/vandersonmota/model_mommy
Author: vandersonmota
diff --git a/model_mommy.egg-info/PKG-INFO b/model_mommy.egg-info/PKG-INFO
index fab6884..1aa1c36 100644
--- a/model_mommy.egg-info/PKG-INFO
+++ b/model_mommy.egg-info/PKG-INFO
@@ -1,6 +1,6 @@
Metadata-Version: 1.1
Name: model-mommy
-Version: 1.2.6
+Version: 1.3.0
Summary: Smart object creation facility for Django.
Home-page: http://github.com/vandersonmota/model_mommy
Author: vandersonmota
diff --git a/model_mommy.egg-info/SOURCES.txt b/model_mommy.egg-info/SOURCES.txt
index 884f79a..803c2f4 100644
--- a/model_mommy.egg-info/SOURCES.txt
+++ b/model_mommy.egg-info/SOURCES.txt
@@ -2,6 +2,7 @@ MANIFEST.in
README.rst
requirements.txt
setup.py
+tox.ini
model_mommy/__init__.py
model_mommy/exceptions.py
model_mommy/generators.py
diff --git a/model_mommy/__init__.py b/model_mommy/__init__.py
index 689e927..4687d80 100644
--- a/model_mommy/__init__.py
+++ b/model_mommy/__init__.py
@@ -1,5 +1,5 @@
#coding:utf-8
-__version__ = '1.2.6'
+__version__ = '1.3.0'
__title__ = 'model_mommy'
__author__ = 'Vanderson Mota'
__license__ = 'Apache 2.0'
diff --git a/model_mommy/exceptions.py b/model_mommy/exceptions.py
index dac4060..5c05516 100644
--- a/model_mommy/exceptions.py
+++ b/model_mommy/exceptions.py
@@ -19,3 +19,11 @@ class AmbiguousModelName(Exception):
class InvalidQuantityException(Exception):
pass
+
+
+class CustomMommyNotFound(Exception):
+ pass
+
+
+class InvalidCustomMommy(Exception):
+ pass
diff --git a/model_mommy/generators.py b/model_mommy/generators.py
index 091f803..1900d7b 100644
--- a/model_mommy/generators.py
+++ b/model_mommy/generators.py
@@ -11,6 +11,7 @@ and value is the value for that argument.
"""
import string
+import warnings
from decimal import Decimal
from os.path import abspath, join, dirname
from random import randint, choice, random
@@ -84,8 +85,11 @@ def gen_float():
def gen_decimal(max_digits, decimal_places):
num_as_str = lambda x: ''.join([str(randint(0, 9)) for i in range(x)])
- return Decimal("%s.%s" % (num_as_str(max_digits - decimal_places - 1),
+ if decimal_places:
+ return Decimal("%s.%s" % (num_as_str(max_digits - decimal_places - 1),
num_as_str(decimal_places)))
+ return Decimal(num_as_str(max_digits))
+
gen_decimal.required = ['max_digits', 'decimal_places']
@@ -163,5 +167,20 @@ def gen_content_type():
except ImportError:
# Deprecated
from django.db.models import get_models
+ try:
+ return ContentType.objects.get_for_model(choice(get_models()))
+ except AssertionError:
+ warnings.warn('Database access disabled, returning ContentType raw instance')
+ return ContentType()
+
+def gen_uuid():
+ import uuid
+ return uuid.uuid4()
+
+
+def gen_array():
+ return []
+
- return ContentType.objects.get_for_model(choice(get_models()))
+def gen_json():
+ return {}
diff --git a/model_mommy/mommy.py b/model_mommy/mommy.py
index 7400f38..76b4577 100644
--- a/model_mommy/mommy.py
+++ b/model_mommy/mommy.py
@@ -46,6 +46,21 @@ try:
except ImportError:
DurationField = None
+try:
+ from django.db.models import UUIDField
+except ImportError:
+ UUIDField = None
+
+try:
+ from django.contrib.postgres.fields import ArrayField
+except ImportError:
+ ArrayField = None
+
+try:
+ from django.contrib.postgres.fields import JSONField
+except ImportError:
+ JSONField = None
+
from django.core.exceptions import ValidationError
from django.core.validators import validate_ipv4_address
try:
@@ -56,7 +71,8 @@ except ImportError:
validate_ipv46_address = validate_ipv6_address
from . import generators
-from .exceptions import ModelNotFound, AmbiguousModelName, InvalidQuantityException, RecipeIteratorEmpty
+from .exceptions import (ModelNotFound, AmbiguousModelName, InvalidQuantityException, RecipeIteratorEmpty,
+ CustomMommyNotFound, InvalidCustomMommy)
from .utils import import_from_str, import_if_str
from six import string_types, advance_iterator, PY3
@@ -88,7 +104,7 @@ def make(model, _quantity=None, make_m2m=False, **attrs):
It fill the fields with random values or you can specify
which fields you want to define its values by yourself.
"""
- mommy = Mommy(model, make_m2m=make_m2m)
+ mommy = Mommy.create(model, make_m2m=make_m2m)
if _valid_quantity(_quantity):
raise InvalidQuantityException
@@ -105,7 +121,7 @@ def prepare(model, _quantity=None, **attrs):
It fill the fields with random values or you can specify
which fields you want to define its values by yourself.
"""
- mommy = Mommy(model)
+ mommy = Mommy.create(model)
if _valid_quantity(_quantity):
raise InvalidQuantityException
@@ -171,7 +187,12 @@ if BinaryField:
default_mapping[BinaryField] = generators.gen_byte_string
if DurationField:
default_mapping[DurationField] = generators.gen_interval
-
+if UUIDField:
+ default_mapping[UUIDField] = generators.gen_uuid
+if ArrayField:
+ default_mapping[ArrayField] = generators.gen_array
+if JSONField:
+ default_mapping[JSONField] = generators.gen_json
class ModelFinder(object):
'''
@@ -255,6 +276,25 @@ def is_iterator(value):
else:
return hasattr(value, 'next')
+def _custom_mommy_class():
+ """
+ Returns custom mommy class specified by MOMMY_CUSTOM_CLASS in the django
+ settings, or None if no custom class is defined
+ """
+ custom_class_string = getattr(settings, 'MOMMY_CUSTOM_CLASS', None)
+ if custom_class_string is None:
+ return None
+
+ try:
+ mommy_class = import_from_str(custom_class_string)
+
+ for required_function_name in ['make', 'prepare']:
+ if not hasattr(mommy_class, required_function_name):
+ raise InvalidCustomMommy('Custom Mommy classes must have a "%s" function' % required_function_name)
+
+ return mommy_class
+ except ImportError:
+ raise CustomMommyNotFound("Could not find custom mommy class '%s'" % custom_class_string)
class Mommy(object):
attr_mapping = {}
@@ -264,6 +304,14 @@ class Mommy(object):
# rebuilding the model cache for every make_* or prepare_* call.
finder = ModelFinder()
+ @classmethod
+ def create(cls, model, make_m2m=False):
+ """
+ Factory which creates the mommy class defined by the MOMMY_CUSTOM_CLASS setting
+ """
+ mommy_class = _custom_mommy_class() or cls
+ return mommy_class(model, make_m2m)
+
def __init__(self, model, make_m2m=False):
self.make_m2m = make_m2m
self.m2m_dict = {}
@@ -371,7 +419,7 @@ class Mommy(object):
for k, v in attrs.items():
if django.VERSION >= (1, 9):
manager = getattr(instance, k)
- manager.set(v, bulk=False)
+ manager.set(v, bulk=False, clear=True)
else:
setattr(instance, k, v)
@@ -497,33 +545,3 @@ def filter_rel_attrs(field_name, **rel_attrs):
clean_dict[k] = v
return clean_dict
-
-
-### DEPRECATED METHODS (should be removed in the future)
-def make_many(model, quantity=None, **attrs):
- msg = "make_many is deprecated. You should use make with _quantity parameter."
- warnings.warn(msg, DeprecationWarning)
- quantity = quantity or MAX_MANY_QUANTITY
- mommy = Mommy(model)
- return [mommy.make(**attrs) for i in range(quantity)]
-
-
-def make_one(model, make_m2m=False, **attrs):
- msg = "make_one is deprecated. You should use the method make instead."
- warnings.warn(msg, DeprecationWarning)
- mommy = Mommy(model, make_m2m=make_m2m)
- return mommy.make(**attrs)
-
-
-def prepare_one(model, **attrs):
- msg = "prepare_one is deprecated. You should use the method prepare instead."
- warnings.warn(msg, DeprecationWarning)
- mommy = Mommy(model)
- return mommy.prepare(**attrs)
-
-
-def make_many_from_recipe(mommy_recipe_name, quantity=None, **new_attrs):
- msg = "make_many_from_recipe is deprecated. You should use the method make_recipe with the _quantity parameter instead."
- warnings.warn(msg, DeprecationWarning)
- quantity = quantity or MAX_MANY_QUANTITY
- return [make_recipe(mommy_recipe_name, **new_attrs) for x in range(quantity)]
diff --git a/model_mommy/recipe.py b/model_mommy/recipe.py
index 798b53e..c709ea6 100644
--- a/model_mommy/recipe.py
+++ b/model_mommy/recipe.py
@@ -55,7 +55,7 @@ class Recipe(object):
recipe_attrs = mommy.filter_rel_attrs(k, **a)
mapping[k] = v.recipe.make(**recipe_attrs)
elif isinstance(v, related):
- mapping[k] = v.prepare()
+ mapping[k] = v.make()
mapping.update(new_attrs)
mapping.update(rel_fields_attrs)
return mapping
@@ -151,10 +151,9 @@ class related(object):
else:
raise TypeError('Not a recipe')
- def prepare(self):
+ def make(self):
"""
- Django related manager saves related set.
- No need to persist at first
+ Persists objects to m2m relation
"""
- return [m.prepare() for m in self.related]
+ return [m.make() for m in self.related]
diff --git a/tox.ini b/tox.ini
new file mode 100644
index 0000000..54a2442
--- /dev/null
+++ b/tox.ini
@@ -0,0 +1,27 @@
+[tox]
+envlist = {py27}-django{15,16,17,18,19,110}-{postgresql,sqlite}, {py26}-django{15,16}, py34-django{15,16,17}-{postgresql,sqlite}, {py35,py34}-django{18,19,110}-{postgresql,sqlite}
+
+[testenv]
+setenv=
+ PYTHONPATH = {toxinidir}
+basepython =
+ py26: python2.6
+ py27: python2.7
+ py35: python3.5
+ py34: python3.4
+deps =
+ Pillow>=2.3.1
+ mock==1.0.1
+ six>=1.3.0
+ django-test-without-migrations
+ django15: Django>=1.5,<1.6
+ django16: Django>=1.6,<1.7
+ django17: Django>=1.7,<1.8
+ django18: Django>=1.8,<1.9
+ django19: Django>=1.9,>1.10
+ django110: Django==1.10
+ postgresql: psycopg2
+
+commands=
+ postgresql: {toxinidir}/runtests.py --postgresql
+ sqlite: {toxinidir}/runtests.py --use-tz
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-model-mommy.git
More information about the Python-modules-commits
mailing list