added command to laod products from woo.enreda.coop, required extra env variables
This commit is contained in:
@@ -12,12 +12,13 @@ https://docs.djangoproject.com/en/2.2/ref/settings/
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import dotenv
|
|
||||||
|
|
||||||
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
|
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
|
||||||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
|
||||||
# Get variables from .env
|
# Get variables from .env
|
||||||
|
import dotenv
|
||||||
|
|
||||||
dotenv.read_dotenv(
|
dotenv.read_dotenv(
|
||||||
dotenv=os.path.join(BASE_DIR, '../.env'),
|
dotenv=os.path.join(BASE_DIR, '../.env'),
|
||||||
override=True
|
override=True
|
||||||
|
|||||||
45
core/management/commands/addWCdata.py
Normal file
45
core/management/commands/addWCdata.py
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
import logging
|
||||||
|
import os
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
from django.core.management.base import BaseCommand
|
||||||
|
|
||||||
|
from companies.models import Company
|
||||||
|
|
||||||
|
from utils.woocommerce import migrate_shop_products
|
||||||
|
|
||||||
|
import dotenv
|
||||||
|
|
||||||
|
dotenv.read_dotenv(
|
||||||
|
dotenv=os.path.join(settings.BASE_DIR, '../.env'),
|
||||||
|
override=True
|
||||||
|
)
|
||||||
|
|
||||||
|
logging.basicConfig(
|
||||||
|
filename='logs/addtestdata.log',
|
||||||
|
filemode='w',
|
||||||
|
format='%(levelname)s:%(message)s',
|
||||||
|
level=logging.INFO,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
|
||||||
|
url = "https://woo.enreda.coop/"
|
||||||
|
help = 'Load data from example site https://woo.enreda.coop/ '
|
||||||
|
|
||||||
|
def handle(self, *args, **kwargs):
|
||||||
|
print("Migrate data from Enreda WooCommerce...\n")
|
||||||
|
# find or create company instance
|
||||||
|
enreda, created = Company.objects.get_or_create(company_name='enreda', web_link=self.url)
|
||||||
|
# set credentials
|
||||||
|
credentials = {
|
||||||
|
'key': os.getenv('WC_KEY'),
|
||||||
|
'secret': os.getenv('WC_SECRET')
|
||||||
|
}
|
||||||
|
enreda.credentials = credentials
|
||||||
|
enreda.save()
|
||||||
|
# migrate shop
|
||||||
|
print(f"Starting migration...")
|
||||||
|
products_created = migrate_shop_products(self.url, credentials['key'], credentials['secret'] )
|
||||||
|
print(f"Products created: {len(products_created)}")
|
||||||
@@ -10,3 +10,6 @@ AWS_SECRET_ACCESS_KEY_S3 = ''
|
|||||||
S3_BUCKET=''
|
S3_BUCKET=''
|
||||||
AWS_ACCESS_KEY_ID_SES = ''
|
AWS_ACCESS_KEY_ID_SES = ''
|
||||||
AWS_SECRET_ACCESS_KEY_SES = ''
|
AWS_SECRET_ACCESS_KEY_SES = ''
|
||||||
|
# WooCommerce test credentials
|
||||||
|
WC_KEY = ''
|
||||||
|
WC_SECRET = ''
|
||||||
@@ -27,6 +27,11 @@ def get_wcapi_instance(url, key, secret, version="wc/v3"):
|
|||||||
|
|
||||||
|
|
||||||
def migrate_shop_products(url, key, secret, version="wc/v3"):
|
def migrate_shop_products(url, key, secret, version="wc/v3"):
|
||||||
|
"""Tries to connect to WooCommerce site @ url with given credentials
|
||||||
|
|
||||||
|
If succesful, returns list of Product instances created
|
||||||
|
|
||||||
|
"""
|
||||||
# get wcapi
|
# get wcapi
|
||||||
wcapi = get_wcapi_instance(url, key, secret, version)
|
wcapi = get_wcapi_instance(url, key, secret, version)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user