accounts client
This commit is contained in:
parent
4f8c409637
commit
fe5fc3fadc
3 changed files with 49 additions and 6 deletions
|
|
@ -9,6 +9,9 @@ from django.utils.translation import gettext_lazy as _
|
||||||
from rest_framework.exceptions import APIException, ValidationError
|
from rest_framework.exceptions import APIException, ValidationError
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from oauth2_provider import settings as oauth2_settings
|
from oauth2_provider import settings as oauth2_settings
|
||||||
|
|
||||||
|
from gooyal_accounts_client.models import PublicUser
|
||||||
|
from utils.accounts_client import get_user_details, get_application_details
|
||||||
from .constans import StateChoices, TypeChoices
|
from .constans import StateChoices, TypeChoices
|
||||||
from apps.users.models import User
|
from apps.users.models import User
|
||||||
from ..gooyal_oauth2.models import Application
|
from ..gooyal_oauth2.models import Application
|
||||||
|
|
@ -208,8 +211,42 @@ class Transaction(models.Model):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def update_details(self, force_update=False):
|
def update_details(self, force_update=False):
|
||||||
# detail = {'payer_name', 'payee_name', 'application_name', 'payer_avatar_url', 'payee_avatar_url'}
|
# detail = {'payer_name', 'payee_name', 'application_name', 'payer_avatar_url', 'payee_avatar_url', 'application_avatar_url'}
|
||||||
pass
|
is_updated = False
|
||||||
|
if not self.details.get('payer_name'):
|
||||||
|
if self.payer_type == TypeChoices.USER.value:
|
||||||
|
payer_details: PublicUser = get_user_details(self.payer_id)
|
||||||
|
self.details['payer_name'] = payer_details.first_name + payer_details.last_name
|
||||||
|
self.details['payer_avatar_url'] = payer_details.avatar
|
||||||
|
else:
|
||||||
|
payer_details = get_application_details(self.payer_id)
|
||||||
|
self.details['payer_name'] = payer_details.name
|
||||||
|
#TODO: get avatar from details
|
||||||
|
|
||||||
|
is_updated = True
|
||||||
|
|
||||||
|
if not self.details.get('payee_name'):
|
||||||
|
if self.payee_type == TypeChoices.USER.value:
|
||||||
|
payee_details: PublicUser = get_user_details(self.payee_id)
|
||||||
|
self.details['payee_name'] = payee_details.first_name + payee_details.last_name
|
||||||
|
self.details['payee_avatar_url'] = payee_details.avatar
|
||||||
|
else:
|
||||||
|
payee_details = get_application_details(self.payee_id)
|
||||||
|
self.details['payee_name'] = payee_details.name
|
||||||
|
#TODO: get avatar from details
|
||||||
|
|
||||||
|
is_updated = True
|
||||||
|
|
||||||
|
if not self.details.get('application_name'):
|
||||||
|
application_details = get_application_details(self.application_id)
|
||||||
|
self.details['application_name'] = application_details.name
|
||||||
|
# TODO: get avatar from details
|
||||||
|
|
||||||
|
is_updated = True
|
||||||
|
|
||||||
|
if is_updated:
|
||||||
|
self.save()
|
||||||
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.get_payer()} -> {self.get_payee()} ({self.uuid}): {self.amount} - {self.get_state_display()}"
|
return f"{self.get_payer()} -> {self.get_payee()} ({self.uuid}): {self.amount} - {self.get_state_display()}"
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,10 @@ class BaseTransactionSerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
read_only_fields = ['uuid', 'updated_at', 'state', 'created_at', 'details', 'role']
|
read_only_fields = ['uuid', 'updated_at', 'state', 'created_at', 'details', 'role']
|
||||||
|
|
||||||
|
def to_representation(self, instance):
|
||||||
|
instance.update_details()
|
||||||
|
return super().to_representation(instance)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class UserTransactionSerializer(BaseTransactionSerializer):
|
class UserTransactionSerializer(BaseTransactionSerializer):
|
||||||
|
|
|
||||||
|
|
@ -41,13 +41,14 @@ def login_as_client_credentials():
|
||||||
|
|
||||||
return auth_data
|
return auth_data
|
||||||
|
|
||||||
|
def get_client():
|
||||||
access_token = login_as_client_credentials()['access_token']
|
access_token = login_as_client_credentials()['access_token']
|
||||||
|
client = AuthenticatedClient(base_url="https://accounts.gooyal.com", token=access_token)
|
||||||
client = AuthenticatedClient(base_url="https://accounts.gooyal.com", token=access_token)
|
return client
|
||||||
|
|
||||||
|
|
||||||
def get_user_details(uuid):
|
def get_user_details(uuid):
|
||||||
|
client = get_client()
|
||||||
data: PublicUser = users_api_users_retrieve.sync(client=client, uuid=uuid)
|
data: PublicUser = users_api_users_retrieve.sync(client=client, uuid=uuid)
|
||||||
print(data)
|
print(data)
|
||||||
# or if you need more info (e.g. status_code)
|
# or if you need more info (e.g. status_code)
|
||||||
|
|
@ -56,6 +57,7 @@ def get_user_details(uuid):
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def get_application_details(uuid):
|
def get_application_details(uuid):
|
||||||
|
client = get_client()
|
||||||
data: Application = oauth2_apis_applications_retrieve.sync(client=client, id=uuid)
|
data: Application = oauth2_apis_applications_retrieve.sync(client=client, id=uuid)
|
||||||
print(data)
|
print(data)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue