Commit 46834c68 authored by David Seaward's avatar David Seaward
Browse files

Refactor WooCommerce calls as a class method.


Signed-off-by: David Seaward's avatarDavid Seaward <david.seaward@puri.sm>
parent 60a0e33f
Pipeline #54233 passed with stage
in 31 seconds
......@@ -43,7 +43,11 @@ class SubscriptionError(Exception):
class SubscriptionManager:
woo = None
@classmethod
def woo_get(cls, query):
woo = get_woo_connection()
return woo.get(query)
@classmethod
def create_subscription(cls, user, account_type=AccountType.BASIC,
......@@ -169,16 +173,13 @@ class SubscriptionManager:
@classmethod
def search_subscriptions(cls, user, status=None):
if cls.woo is None:
cls.woo = get_woo_connection()
result = []
query = 'subscriptions?customer={}'.format(user.get_woocommerce_id())
if status is not None:
query += '&status={}'.format(status.value)
r = cls.woo.get(query)
cls.woo = None
r = cls.woo_get(query)
if r.status_code == 200:
q_subs = r.json()
......@@ -197,12 +198,9 @@ class SubscriptionManager:
@classmethod
def get_subscription(cls, sub_id):
if cls.woo is None:
cls.woo = get_woo_connection()
query = 'subscriptions/{}'.format(sub_id)
r = cls.woo.get(query)
cls.woo = None
r = cls.woo_get(query)
if r.status_code == 200:
q_sub = r.json()
......@@ -217,6 +215,12 @@ class SubscriptionManager:
class Subscription:
@classmethod
def woo_get(cls, query):
woo = get_woo_connection()
return woo.get(query)
def __init__(
self, user, subscription_id, product_id, variation_id,
variation_name, next_payment_date, line_item_id, order_id,
......@@ -241,8 +245,6 @@ class Subscription:
@classmethod
def parse_subscription(cls, json_entry, user=None):
if cls.woo is None:
cls.woo = get_woo_connection()
subscription_id = str(json_entry["id"])
......@@ -278,8 +280,7 @@ class Subscription:
if user is None:
try:
user_q = 'customers/' + str(json_entry['customer_id'])
user_r = cls.woo.get(user_q).json()
cls.woo = None
user_r = cls.woo_get(user_q).json()
# remove the domain part of the WC username
username = user_r['username'].split('@')[0]
user = User.objects.get(username=username)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment