Django 6 + DRF resource server against the Gooyal accounts OAuth2 service, matching the Winsoo ecosystem's conventions. Covers locations, stores, catalog, cart, checkout/orders (with the multi-store-cart split and the status stepper), reviews, and notifications, plus a demo-data seed command. Payment integration (wallet debits, online gateway, seller payouts) is intentionally left out here — see feature/payment. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
import logging
|
|
import time
|
|
|
|
import requests
|
|
from django.conf import settings
|
|
from django.core.cache import cache
|
|
|
|
from utils.clients.gooyal_accounts_client import AuthenticatedClient
|
|
from utils.clients.gooyal_accounts_client.models import Account
|
|
from utils.clients.gooyal_accounts_client.api.users import users_api_users_details_retrieve
|
|
|
|
logger = logging.getLogger('winofy.utils.accounts_client')
|
|
|
|
|
|
def login_as_client_credentials():
|
|
access_token = cache.get('winofy_accounts_access_token')
|
|
if access_token:
|
|
return access_token
|
|
|
|
data = {
|
|
"grant_type": 'client_credentials',
|
|
"scope": settings.OAUTH2_PROVIDER_SCOPES,
|
|
}
|
|
auth = (settings.OAUTH2_PROVIDER_CLIENT_ID, settings.OAUTH2_PROVIDER_CLIENT_SECRET)
|
|
|
|
response = requests.post(
|
|
f'{settings.OAUTH2_PROVIDER_BASE_PUBLIC_URL}/token/',
|
|
data=data,
|
|
auth=auth,
|
|
timeout=10,
|
|
)
|
|
auth_data = response.json()
|
|
|
|
if 'access_token' in auth_data:
|
|
auth_data['created_at'] = time.time()
|
|
cache.set('winofy_accounts_access_token', auth_data, timeout=auth_data.get('expires_in') - 5)
|
|
|
|
return auth_data
|
|
|
|
|
|
def get_client():
|
|
auth_data = login_as_client_credentials()
|
|
if 'access_token' not in auth_data:
|
|
raise Exception(f"OAuth2 token error: {auth_data.get('error', 'unknown')}")
|
|
return AuthenticatedClient(base_url=settings.OAUTH2_PROVIDER_BASE_PUBLIC_URL, token=auth_data['access_token'])
|
|
|
|
|
|
def get_user_detailed_info(uuid):
|
|
client = get_client()
|
|
response: Account = users_api_users_details_retrieve.sync(uuid=uuid, client=client)
|
|
return response
|