Ejemplo n.º 1
0
def check_for_new_etsy_orders():
    logger.info("searching etsy orders with status 'open'...")
    etsy = EtsyAPI(settings.ETSY_SHOP_ID, settings.ETSY_CLIENT_KEY, settings.ETSY_CLIENT_SECRET, settings.ETSY_RESOURCE_OWNER_KEY, settings.ETSY_RESOURCE_OWNER_SECRET)
    countries = etsy.get_countries()
    orders = etsy.get_new_orders()
    for o in orders:
        receipt_id = o['receipt_id']
        country = list(filter(lambda c: c['country_id'] == o['country_id'], countries))[0]
        with transaction.atomic():
            order, created = C2OOrder.objects.get_or_create(store_platform = 'etsy', store_order_id = receipt_id)
            if created:
                logger.info("found new order with etsy id '%s', saving...", receipt_id)
                order.luma_id = C2OOrder.get_new_luma_id()
                order.notes = blank_if_none(o.get('message_from_buyer', ''))
                order.customer_name	= o['name']
                order.customer_email = o['buyer_email']
                order.customer_telephone = ''
                order.address_delivery_name	= o['name']
                order.address_company_name = ''
                order.address_address_line_1 = blank_if_none(o.get('first_line', ''))
                order.address_address_line_2 = blank_if_none(o.get('second_line', ''))
                order.address_city = blank_if_none(o.get('city', ''))
                order.address_county = blank_if_none(o.get('state', ''))
                order.address_postcode = blank_if_none(o.get('zip', ''))
                order.address_country = country['name']
                order.save()
                link = settings.HOST + '/orders/new_c2o_orders/'
                for item in etsy.get_order_items(receipt_id):
                    product = safe_get(C2OProduct.objects.filter(etsy_listing_id = item['listing_id']))
                    if not product:
                        logger.warning("Can't find an Etsy product for %s, title: '%s' - listing_id: '%s'" % (order, item['title'], item['listing_id']))
                        send_admin_email("Order #%s - product missing!" % order.luma_id, "Can't find an Etsy product for order #%s, title: '%s' - listing_id: '%s'" % (order.luma_id, item['title'], item['listing_id']))
                        continue
                    C2OOrderItem.objects.create(order = order,
                                                product_id = product.unique_id,
                                                size = convert_to_c2o_size(item['size']),
                                                quantity = item['quantity'])
                send_admin_email("New order from Etsy", "Your new Etsy order can be found here: %s" % link)
            else:
                if order.status == C2OOrder.STATUS_NEW:
                    logger.warning("new order with etsy id '%s' already exists!", receipt_id)
                    send_admin_email("New Order duplicated from Etsy", "Order with etsy id '%s' already exists!" % receipt_id)
Ejemplo n.º 2
0
def update_order_status(order):
    logger.info("checking status for %s...", order)
    api = C2OApi(settings.C2O_API_KEY)
    success, messages = api.check_order_status(order)
    if success:
        if order.c2o_status == 'Dispatched':
            try:
                order.status = C2OOrder.STATUS_DISPATCHED
                logger.info("%s c2o status is now %s!", order, order.c2o_status)
                send_admin_email("Order #%s dispatched" % order.luma_id, "C2O service has dispatched order #%s.\nTracking link is: %s" % (order.luma_id, order.tracking_link))
                order.save()

                if order.store_platform == 'etsy':
                    logger.info("marking Etsy order status as shipped...")
                    etsy = EtsyAPI(settings.ETSY_SHOP_ID, settings.ETSY_CLIENT_KEY, settings.ETSY_CLIENT_SECRET, settings.ETSY_RESOURCE_OWNER_KEY, settings.ETSY_RESOURCE_OWNER_SECRET)
                    etsy.mark_order_as_shipped(order.store_order_id)

            except:
                logger.error("Can't update order dispatch status: %s", get_exception_trace())
    else:
        logger.info("could not fetch status: %s", '\n'.join(messages))
    sys.path.append(project_home)

# set environment variable to tell django where your settings.py is
os.environ['DJANGO_SETTINGS_MODULE'] = 'lumacart_console.prod_settings'
django.setup()
logger = logging.getLogger("project")

# add your project directory to the sys.path
from lumacart_console.catalogue.models import C2OProduct
from lumacart_console.orders.etsy import EtsyAPI
from lumacart_console.utils import get_exception_trace, snake_string
from django.conf import settings

try:
    logger.info("Syncing products from Etsy...")
    etsy = EtsyAPI(settings.ETSY_SHOP_ID, settings.ETSY_CLIENT_KEY, settings.ETSY_CLIENT_SECRET, settings.ETSY_RESOURCE_OWNER_KEY, settings.ETSY_RESOURCE_OWNER_SECRET)
    items = etsy.get_shop_active_items()
    logger.info("Found %d items", len(items))
    saved = 0
    for i in items:
        title = html.unescape(i['title'])
        slug_title = snake_string(title)
        product, created = C2OProduct.objects.get_or_create(unique_id=slug_title)
        if created:
            product.sku_name = "Gildan Men's Ring Spun, SoftStyle T-Shirt"
        product.etsy_listing_id = i['listing_id']
        product.title = title
        product.description = i['description']
        product.save()
        saved += 1
    logger.info("Saved %d products", saved)