Skip to content
This repository has been archived by the owner on Jul 9, 2020. It is now read-only.

[feature] Implement copy/clone templates. #131, openwisp/openwisp-controller#134 #135

Closed
wants to merge 1 commit into from

Conversation

pawelplsi
Copy link
Contributor

Another PR to openwisp/openwisp-controller will implement also the wanted organization-selection feature when templates are cloned by multi-organization administrator.

Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good progress, see my comments.

django_netjsonconfig/admin.py Outdated Show resolved Hide resolved
@@ -100,5 +103,21 @@ def clean(self, *args, **kwargs):
if self.type == 'vpn' and not self.config:
self.config = self.vpn.auto_client(auto_cert=self.auto_cert)

def clone(self, user):
clone = copy(self)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an official way to clone model instances, please see https://docs.djangoproject.com/en/2.2/topics/db/queries/#copying-model-instances.

Copy link
Contributor Author

@pawelplsi pawelplsi Dec 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've read this, but this official way requires modification of the original object instance, IMO it would be quite confusing if a function named 'clone' behaved that way, it seems natural that function like that does not affect the original object, what do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure how the is the orginal one affected in the official way?
I may be missing something, can you please explain that, thanks! 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The official way is to just change the primary key to None, do some changes and save() the original object instance. Change of the pk makes it a new object in database and when you are operating directly on the objects, it's good approach but lets see what happened if I implemented the helper clone() function this way:
Let's have some original Template
clone = original.clone()
The clone is created, but now, the original instance is equal to clone.
In the clone action implementation it doesn't make a difference since it does not longer use the original object, but do You not think that behavior may be confusing a bit if one wanted to reuse that clone function?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! I took me a really long while, been reading your message for 15 mins but I think now I get it! 😄
The self object would be changed!
CC: @nemesisdesign

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for being incomprehensible,
If you really want to do it similarly to the official way, It's also possible to change copy(self) to
self.__class__.objects.get(pk=self.pk), but IMO the copy() function is a way more readable.

django_netjsonconfig/base/template.py Outdated Show resolved Hide resolved
django_netjsonconfig/base/template.py Outdated Show resolved Hide resolved
@atb00ker
Copy link
Member

Please fix the travis build as well! 😄

@coveralls
Copy link

coveralls commented Dec 21, 2019

Coverage Status

Coverage increased (+0.002%) to 99.908% when pulling 762f484 on pawelplsi:master into f757fac on openwisp:master.

Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good progress, see my comments

django_netjsonconfig/admin.py Outdated Show resolved Hide resolved
django_netjsonconfig/base/template.py Outdated Show resolved Hide resolved
django_netjsonconfig/admin.py Outdated Show resolved Hide resolved
django_netjsonconfig/admin.py Outdated Show resolved Hide resolved
Copy link
Member

@atb00ker atb00ker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was testing and I got:

ValidationError at /admin/django_netjsonconfig/template/
{'name': ['Template with this Name already exists.']}

Why?
I was cloning a template named:
"default" when a template "default (CLONE)" existed from previous cloning.
Although it's not an application breaking problem, one can simply use "default (CLONE)" to create "default (CLONE) (CLONE)" but can you please look into it?

@pawelplsi
Copy link
Contributor Author

pawelplsi commented Dec 23, 2019

Fixed, now the next clone would be automaticaly named default (Clone 2), etc.

atb00ker
atb00ker previously approved these changes Dec 24, 2019
Copy link
Member

@atb00ker atb00ker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 👍

@nemesifier
Copy link
Member

I resolved conflicts and applied some minor improvements in #140. Will merge once openwisp/openwisp-controller#139 is ready. Follow up on my review on that one please @pawelplsi.

@nemesifier nemesifier closed this Jan 3, 2020
nemesifier added a commit that referenced this pull request Jan 3, 2020
nemesifier added a commit that referenced this pull request Jan 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants