Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP implement clone method #37

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions appnexus/clone_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
clone_services_unrequired_fields = {
"Advertiser": ["id", "name", "profile_id"],
}


class CloneMixin():

def clone(self, **kwargs):
try:
payload = {}
for field in self.__dict__.keys():
if field not in clone_services_unrequired_fields[type(self).__name__]:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if field not in clone_services_unrequired_fields[type(self).__name__]:
if field not in self._unrequired_fields:

payload[field] = self.__dict__[field]
return self.create(payload, **kwargs)
except KeyError:
raise NotImplementedError("Clone method isn't yet available for {} service."
.format(self.service_name))
Copy link
Contributor

Choose a reason for hiding this comment

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

The message isn't correlated to the actual error.



__all__ = ["CloneMixin", "clone_services_unrequired_fields"]
3 changes: 3 additions & 0 deletions appnexus/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from thingy import Thingy

from appnexus.client import AppNexusClient, client, services_list
from appnexus.clone_mixin import CloneMixin, clone_services_unrequired_fields
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for a module just for this mixin. :)

from appnexus.utils import classproperty, normalize_service_name

logger = logging.getLogger("appnexus-client")
Expand Down Expand Up @@ -172,6 +173,8 @@ def create_models(services_list):
if service_name in ("AdQualityRule", "Advertiser", "Campaign",
"Creative", "LineItem", "PaymentRule"):
ancestors.append(ProfileMixin)
if service_name in clone_services_unrequired_fields.keys():
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if service_name in clone_services_unrequired_fields.keys():
if service_name in ("Advertiser"):

ancestors.append(CloneMixin)
Copy link
Contributor

@ramnes ramnes Jul 31, 2019

Choose a reason for hiding this comment

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

Suggested change
ancestors.append(CloneMixin)
ancestors.append(CloneMixin)
attrs["_unrequired_fields"] = clone_services_unrequired_fields[service_name]

model = type(service_name, tuple(ancestors), {})
Copy link
Contributor

@ramnes ramnes Jul 31, 2019

Choose a reason for hiding this comment

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

Suggested change
model = type(service_name, tuple(ancestors), {})
model = type(service_name, tuple(ancestors), attrs)

globals().setdefault(service_name, model)

Expand Down