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

Save many metafields with a single request #112

Open
flux627 opened this issue Sep 13, 2015 · 10 comments
Open

Save many metafields with a single request #112

flux627 opened this issue Sep 13, 2015 · 10 comments

Comments

@flux627
Copy link
Contributor

flux627 commented Sep 13, 2015

There does not seem to be a way to attach metafields locally, and then simply save the product with the metafields along with it. I'm forced to use add_metafields() or Metafield().save(), both of which adds a single metafield per API call. This is very slow.

If I take a product with 50 metafields, and save it as a new product, it saves almost instantly, like so:

p = shopify.Product.find()[0]
print len(p.metafields())
> 50
p.id = None
p.images = None
p.save()

How can I attach metafields to a product in the same way they are attached when they are fetched?

@kevinhughes27
Copy link
Contributor

Active resource does a lot of work to add methods to add attributes etc but at the end of the day it just sends json to the server. If you either post the json directly not using the library or edit the attributes directly you should be able to do this.

@kevinhughes27
Copy link
Contributor

also see discussion on #76

@flux627
Copy link
Contributor Author

flux627 commented Sep 15, 2015

Based on the discussion in #76, I created the following helper function:

def attach_metafields(target, namespace, dictionary):
    mfs = []
    for key in dictionary.keys():
        value = str(dictionary[key])
        if type(dictionary[key]) is int:
            value_type = "integer"
        else:
            value_type = "string"

        mf_dict = {
            "namespace"   : namespace,
            "key"         : key,
            "value"       : value,
            "value_type"  : value_type,
        }

        mfs.append(mf_dict)

    if hasattr(target.metafields, '__call__'):
        target.metafields = mfs
    elif type(target.metafields) is list:
        target.metafields.extend(mfs)

After saving the target, however, the metafields are not saved on the server. I am attempting this on an existing product. Any idea why it's not working?

@flux627
Copy link
Contributor Author

flux627 commented Sep 15, 2015

Got it to work. I had to attach it to attributes. Here's the updated function:

def attach_metafields(target, namespace, dictionary):
    mfs = []
    for key in dictionary.keys():
        value = str(dictionary[key])
        if type(dictionary[key]) is int:
            value_type = "integer"
        else:
            value_type = "string"

        mf_dict = {
            "namespace"   : namespace,
            "key"         : key,
            "value"       : value,
            "value_type"  : value_type,
        }

        mfs.append(mf_dict)

    if "metafields" in target.attributes.keys():
        target.attributes["metafields"].extend(mfs)
    else:
        target.attributes["metafields"] = mfs

@kevinhughes27
Copy link
Contributor

if you add tests and make a PR I think I can accept this!

@flux627
Copy link
Contributor Author

flux627 commented Sep 15, 2015

I apologize, I'm not very familiar or clear with how this module/pyactiveresource actually work. Is there any one place I can add this so that every resource that supports metafields automatically has access to it? Or does this need to be added to every resource class manually?

Also, I feel like the level of abstraction that my helper function goes is a bit beyond the level abstraction than most of the other functions in this module. Most other functions can be mapped 1-to-1 to the json API documentation, which is useful because this effectively acts as documentation for this module. The functions/attributes that do not have this 1-to-1 mapping are always frustrating, because their existence/usage are non-obvious, requiring me to dig around in the source code and/or futz around to figure out how they work. As useful as this function is, I don't want to contribute to this problem. If you think this isn't actually a problem, I'd love to hear you address these issues (or non-issues).

I'd also love to hear other people's opinions (particularly @gavinballard's) about all this.

@kevinhughes27
Copy link
Contributor

Its a valid point - it will be hard for developers to know about the existence of this function since this lib itself is not really documented only the api is.

Another issue - I don't know if adding metafields like this even works for other resources it might only work for products.

It might be better to just leave as an issue and then people can find it this way?

@flux627
Copy link
Contributor Author

flux627 commented Sep 15, 2015

I just confirmed that this function works for products, images, variants, and customers (did not try the others yet), however with a peculiarity:

You can only create metafields, not update. If any metafield you pass exists, none of them are created or updated, the following error is received: must be unique within this namespace on this resource

I think this is because it is using a POST request when it needs to be PUT. I noticed that all resources have a .put() method, however I'm not sure how it works?

Something I'm still confused about, relating to my OP:

p = shopify.Product.find()[0]
print len(p.metafields())
> 50
print p.attributes.get("metafields")
> None
p.id = None
p.images = None
p.save()
print len(p.metafields())
> 50
p.save()
print p.errors.full_messages()
> []

In the above example, you can see that a new product was saved with metafields, even though "metafields" was not in attributes. This means that the metafields are being stored somewhere else in the object, and in a place where it doesn't raise an error if the object is saved when they already exist. As per my original question in the OP: where is this metafield information stored? I can't figure it out from the source code.

@kevinhughes27
Copy link
Contributor

that is certainly interesting. I'd have to look into it as well since I am not sure how pyactiveresource is doing this either

@flux627
Copy link
Contributor Author

flux627 commented Sep 15, 2015

I don't know what happened, but it's not working anymore. When I save a product with id = None, the metafields are no longer being saved along with it, unless I reassign them like so:

product.attributes["metafields"] = product.metafields()

I'm not sure if I'm crazy or if something changed...

Edit: just to clarify, the helper function above still works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants