Skip to content

Commit

Permalink
Fixed lint errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
jackton1 committed Mar 24, 2021
1 parent e7696f8 commit 8286951
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 37 deletions.
4 changes: 3 additions & 1 deletion model_clone/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from model_clone.utils import create_copy_of_instance

__all__ = [
"CloneMixin", "CloneModelAdmin", "CloneModelAdminMixin",
"CloneMixin",
"CloneModelAdmin",
"CloneModelAdminMixin",
"create_copy_of_instance",
]
4 changes: 1 addition & 3 deletions model_clone/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ class CloneModelAdminMixin(object):

def change_view(self, request, object_id, form_url="", extra_context=None):
extra_context = extra_context or {}
extra_context[
"include_duplicate_object_link"
] = self.include_duplicate_object_link
extra_context["include_duplicate_object_link"] = self.include_duplicate_object_link
if self.include_duplicate_object_link:
to_field = request.POST.get(TO_FIELD_VAR, request.GET.get(TO_FIELD_VAR))
if to_field and not self.to_field_allowed(request, to_field):
Expand Down
20 changes: 5 additions & 15 deletions model_clone/mixins/clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,7 @@ def check(cls, **kwargs):
"Conflicting configuration.",
hint=(
'Please provide either "_clone_fields"'
+ ' or "_clone_excluded_fields" for model {}'.format(
cls.__name__
)
+ ' or "_clone_excluded_fields" for model {}'.format(cls.__name__)
),
obj=cls,
id="{}.E002".format(ModelCloneConfig.name),
Expand All @@ -221,9 +219,7 @@ def check(cls, **kwargs):
"Conflicting configuration.",
hint=(
'Please provide either "_clone_m2m_fields"'
+ ' or "_clone_excluded_m2m_fields" for model {}'.format(
cls.__name__
)
+ ' or "_clone_excluded_m2m_fields" for model {}'.format(cls.__name__)
),
obj=cls,
id="{}.E002".format(ModelCloneConfig.name),
Expand All @@ -243,9 +239,7 @@ def check(cls, **kwargs):
"Please provide either "
+ '"_clone_m2o_or_o2m_fields"'
+ " or "
+ '"_clone_excluded_m2o_or_o2m_fields" for {}'.format(
cls.__name__
)
+ '"_clone_excluded_m2o_or_o2m_fields" for {}'.format(cls.__name__)
),
obj=cls,
id="{}.E002".format(ModelCloneConfig.name),
Expand Down Expand Up @@ -370,9 +364,7 @@ def make_clone(self, attrs=None, sub_clone=False):
for field in one_to_one_fields:
rel_object = getattr(self, field.related_name, None)
if rel_object:
if hasattr(rel_object, "make_clone") and callable(
rel_object.make_clone
):
if hasattr(rel_object, "make_clone") and callable(rel_object.make_clone):
rel_object.make_clone(
attrs={field.remote_field.name: duplicate}, sub_clone=True
)
Expand All @@ -388,9 +380,7 @@ def make_clone(self, attrs=None, sub_clone=False):
items = []
for item in getattr(self, field.related_name).all():
try:
item_clone = item.make_clone(
attrs={field.remote_field.name: duplicate}
)
item_clone = item.make_clone(attrs={field.remote_field.name: duplicate})
except IntegrityError:
item_clone = item.make_clone(
attrs={field.remote_field.name: duplicate}, sub_clone=True
Expand Down
8 changes: 2 additions & 6 deletions model_clone/tests/test_clone_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ def test_cloning_with_field_overridden(self):

def test_cloning_using_auto_now_field_is_updated(self):
name = "New Book"
instance = Book.objects.create(
name=name, created_by=self.user1, slug=slugify(name)
)
instance = Book.objects.create(name=name, created_by=self.user1, slug=slugify(name))
new_name = "My New Book"
clone = instance.make_clone(attrs={"name": new_name})

Expand Down Expand Up @@ -187,9 +185,7 @@ def test_cloning_unique_fields_is_valid(self):
"{} {} {}".format(first_name, Author.UNIQUE_DUPLICATE_SUFFIX, 1),
)

@patch(
"sample.models.Author.USE_UNIQUE_DUPLICATE_SUFFIX", new_callable=PropertyMock
)
@patch("sample.models.Author.USE_UNIQUE_DUPLICATE_SUFFIX", new_callable=PropertyMock)
def test_cloning_unique_field_with_use_unique_duplicate_suffix_set_to_False(
self,
use_unique_duplicate_suffix_mock,
Expand Down
16 changes: 8 additions & 8 deletions model_clone/tests/test_create_copy_of_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ def setUpTestData(cls):
def test_cloning_model_with_custom_id(self):
instance = Library.objects.create(name="First library", user=self.user1)
clone = create_copy_of_instance(instance, attrs={"user": self.user2})

self.assertNotEqual(instance.pk, clone.pk)
self.assertEqual(clone.user, self.user2)

def test_cloning_unique_fk_field_without_a_fallback_value_is_invalid(self):
name = "New Library"
instance = Library.objects.create(name=name, user=self.user1)

with self.assertRaises(ValidationError):
create_copy_of_instance(instance)

def test_cloning_excluded_field_without_a_fallback_value_is_invalid(self):
name = "New Library"
instance = Book.objects.create(
name=name, created_by=self.user1, slug=slugify(name)
)

instance = Book.objects.create(name=name, created_by=self.user1, slug=slugify(name))

with self.assertRaises(IntegrityError):
create_copy_of_instance(instance, exclude={'slug'}, attrs={"created_by": self.user2})
create_copy_of_instance(
instance, exclude={"slug"}, attrs={"created_by": self.user2}
)
8 changes: 4 additions & 4 deletions model_clone/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ def create_copy_of_instance(instance, exclude=(), save_new=True, attrs=None):
exclude = exclude or [
f.name
for f in instance._meta.fields
if any([all([f.name not in defaults, f.attname not in defaults]), f.has_default(), f.null])
if any(
[all([f.name not in defaults, f.attname not in defaults]), f.has_default(), f.null]
)
]

try:
Expand Down Expand Up @@ -135,9 +137,7 @@ def generate_value(value, suffix, transform, max_length, max_attempts):
yield get_value(value, suffix, transform, max_length, i)

raise StopIteration(
"CloneError: max unique attempts for {} exceeded ({})".format(
value, max_attempts
)
"CloneError: max unique attempts for {} exceeded ({})".format(value, max_attempts)
)


Expand Down

0 comments on commit 8286951

Please sign in to comment.