GithubHelp home page GithubHelp logo

Comments (10)

kevinhughes27 avatar kevinhughes27 commented on August 22, 2024

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.

from shopify_python_api.

kevinhughes27 avatar kevinhughes27 commented on August 22, 2024

also see discussion on #76

from shopify_python_api.

flux627 avatar flux627 commented on August 22, 2024

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?

from shopify_python_api.

flux627 avatar flux627 commented on August 22, 2024

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

from shopify_python_api.

kevinhughes27 avatar kevinhughes27 commented on August 22, 2024

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

from shopify_python_api.

flux627 avatar flux627 commented on August 22, 2024

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.

from shopify_python_api.

kevinhughes27 avatar kevinhughes27 commented on August 22, 2024

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?

from shopify_python_api.

flux627 avatar flux627 commented on August 22, 2024

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.

from shopify_python_api.

kevinhughes27 avatar kevinhughes27 commented on August 22, 2024

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

from shopify_python_api.

flux627 avatar flux627 commented on August 22, 2024

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.

from shopify_python_api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.