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

add code to pyactive resource errors #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

add code to pyactive resource errors #19

wants to merge 1 commit into from

Conversation

chshkhr
Copy link

@chshkhr chshkhr commented Jul 16, 2017

Errors.code helps to differentiate actions when Shopify replies with error

@kevinhughes27
Copy link
Contributor

Can I ask a bit more information about how you are using this downstream? Almost every useful error from Shopify will be 422 (although that does assume you are using this with our API which very well may not be the case)

@chshkhr
Copy link
Author

chshkhr commented Jul 17, 2017

Hi, when code in [429, 503, 504] I'm repeating the same request to Shopify API

@@ -830,6 +832,7 @@ def save(self):
self.errors.from_xml(err.response.body)
elif self.klass.format == formats.JSONFormat:
self.errors.from_json(err.response.body)
self.errors.code = err.code;
Copy link
Contributor

Choose a reason for hiding this comment

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

the description of this method above leads me to think that this should only be called for ResourceInvalid errors and that network errors are handled elsewhere. Also can you add a test showing this working

Copy link
Author

Choose a reason for hiding this comment

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

Really I already use ShopifyAPI modified this way. It's up to you to accept this pull request. I'm sorry I can't write test to show this working. Only part of code from real project: ` while -1 < repeat < MAX_REPEAT:
if repeat > 0:
time.sleep(repeat * repeat)

    try:
        styleno = style.find('StyleNo').text
        title = (style.find('CustomText5').text or
                 style.find('Description4').text or
                 styleno)

        modif_time = style.find('RecModified').text
        styleid = style.find('StyleId').text
        if oldProductID:
            try:
                product = shopify.Product.find(oldProductID)
            except Exception as e:  # PyCharm BUG: do not remove "as e"
                product = shopify.Product()
            else:
                if recreate:
                    oldProductID = None
                    print('\t\tDeleting...')
                    product.destroy()
                    product = shopify.Product()
        else:
            product = shopify.Product()

        product.title = title

        product.body_html = style.find('CustomText1').text or \
                            title + ' <font color="RED">Empty CustomText1!</font>'

        # ??? Style SKU	- Style No

        product.option1 = style.find('AttributeSet1').text or 'Empty AttributeSet1'
        product.option2 = style.find('AttributeSet2').text or 'Empty AttributeSet2'
        product.option3 = style.find('AttributeSet3').text or 'Empty AttributeSet3'

        product.vendor = style.find('PrimaryVendor').text or 'Empty PrimaryVendor'

        # League - (CustomLookup3) Team - (CustomLookup1)
        product.tags = ', '.join(filter(None, (
            style.find('CustomLookup3').text,
            style.find('CustomLookup1').text,
        )))

        # Copy Items to Variants
        items = style.iter('Item')
        variants = []
        style_qty = 0
        for item in items:
            varcount += 1
            if varcount > MAX_VARIANTS:
                break
            sku = item.find('PLU').text or f'Empty PLU {varcount}'
            variant = dict()
            if oldProductID and product.variants:
                for v in product.variants:
                    if v.sku == sku:
                        variant['id'] = v.id
                        break
            variant['sku'] = sku
            # ??? 'barcode'

            # "option1" goes instead of "title": if "option1" not filled Shopify creates Empty Variant
            variant['option1'] = ' '.join(filter(None, (
                style.find('CustomText5').text,
                item.find('Attribute1').text,
                item.find('Attribute2').text,
                sku,
                # item.find('Attribute3').text,
            )))  # or styleno + ' Empty CustomText5+Attribute1+Attribute2+Attribute3'
            itemid = item.find('ItemId').text
            variant['ItemId'] = itemid

            variant['price'] = float(item.find('BasePrice').text)

            variant['inventory_management'] = 'shopify'
            variant['inventory_policy'] = 'deny'  #'continue'  #'deny' - default
            variant['inventory_quantity'] = 0
            with db.cursor() as qry:
                qry.execute('SELECT Qty FROM Items WHERE ItemId = %s', (itemid,))
                val = qry.fetchone()
                if val:
                    var_qty = val['Qty']
                    if var_qty and var_qty >= 0:
                        var_qty = int(var_qty)
                        variant['inventory_quantity'] = var_qty
                        style_qty += var_qty
            variants.append(variant)

        if publish_zero_qty or style_qty > 0 or oldProductID:  # new with qty>0 or old
            product.variants = variants
            if not channel_active or style_qty == 0:
                product.published_at = None
            product.save()

    except Exception as e:
        err_count += 1
        err_mes = e
        err_code = -1  # means "it's Exception"
        repeat = MAX_REPEAT  # do not retry on any Exception
        err_delta = 1

    else:
        if product.errors:
            err_mes = product.errors.full_messages()
            err_code = product.errors.code
            if err_code in [429, 503, 504]:
                if repeat == 0:
                    repeat = 1
                elif repeat >= MAX_REPEAT:
                    err_count += 1
            else:
                err_count += 1`

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

Successfully merging this pull request may close these issues.

2 participants