temp
This commit is contained in:
parent
a7964ce0ab
commit
3320ff93e3
5 changed files with 195 additions and 5 deletions
|
|
@ -611,10 +611,102 @@ class ApplicationApiFlowsTests(APITestCase):
|
|||
self.first_ad_view_reward_plan.refresh_from_db()
|
||||
self.assertEqual(self.first_ad_view_reward_plan.balance, 10000 - 800)
|
||||
|
||||
|
||||
# response = self.client.get(reverse('promotions:promotion-list', kwargs={"plan": self.first_ad_view_reward_plan.uuid}), event_create_data, HTTP_AUTHORIZATION=auth,
|
||||
# format='json')
|
||||
# print(response.json())
|
||||
|
||||
# response = self.client.post(reverse('promotions:promotion-create', kwargs={"plan": self.first_ad_view_reward_plan.uuid}), event_create_data, HTTP_AUTHORIZATION=auth, format='json')
|
||||
# print(response.json())
|
||||
|
||||
def test_application_promote_referral_reward_success(self):
|
||||
self.referral_reward_event_saver, _created = EventSaver.objects.get_or_create(
|
||||
title=self.referral_reward_label,
|
||||
event_label=self.referral_reward_label,
|
||||
save_once=True,
|
||||
)
|
||||
|
||||
self.referral_reward_plan, _created = Plan.objects.get_or_create(
|
||||
title=self.referral_reward_label,
|
||||
balance=10000,
|
||||
user=self.user_1,
|
||||
application=self.application,
|
||||
event_list=[self.referral_reward_label]
|
||||
)
|
||||
|
||||
self.referral_reward_recipient, _created = Recipient.objects.get_or_create(
|
||||
label=self.referral_reward_label,
|
||||
plan=self.referral_reward_plan,
|
||||
wallet_uuid=self.user_1_uuid,
|
||||
recipient_uuid_field="->event:referral",
|
||||
base_amount_field="1000",
|
||||
|
||||
)
|
||||
|
||||
auth = self._create_authorization_header(self.application_access_token.token)
|
||||
event_create_data = {
|
||||
'label': self.referral_reward_label,
|
||||
"data": {
|
||||
'user': self.user_1_uuid,
|
||||
'referral': self.user_2_uuid
|
||||
}
|
||||
}
|
||||
url = reverse('promotions-application:plan-promote',
|
||||
kwargs={"pk": self.referral_reward_plan.uuid, "user_uuid": self.user_1_uuid})
|
||||
response = self.client.post(url, event_create_data, HTTP_AUTHORIZATION=auth, format='json')
|
||||
print(response.json())
|
||||
self.referral_reward_plan.refresh_from_db()
|
||||
self.assertEqual(self.referral_reward_plan.balance, 10000 - 1000)
|
||||
self.assertEqual(response.json()['promotions'][0]['user'], str(self.user_2_uuid))
|
||||
|
||||
def test_application_get_promotion_plan_success(self):
|
||||
self.first_ad_view_reward_event_saver, _created = EventSaver.objects.get_or_create(
|
||||
title=self.first_ad_view_reward_label,
|
||||
event_label=self.first_ad_view_reward_label,
|
||||
save_once=True,
|
||||
)
|
||||
|
||||
self.first_ad_view_reward_plan, _created = Plan.objects.get_or_create(
|
||||
title=self.first_ad_view_reward_label,
|
||||
balance=10000,
|
||||
user=self.user_1,
|
||||
application=self.application,
|
||||
event_list=[self.first_ad_view_reward_label]
|
||||
)
|
||||
|
||||
self.first_ad_view_reward_recipient, _created = Recipient.objects.get_or_create(
|
||||
label=self.first_ad_view_reward_label,
|
||||
plan=self.first_ad_view_reward_plan,
|
||||
wallet_uuid=self.user_1_uuid,
|
||||
recipient_uuid_field="->event:user",
|
||||
base_amount_field="event:base_amount",
|
||||
|
||||
)
|
||||
|
||||
auth = self._create_authorization_header(self.application_access_token.token)
|
||||
event_create_data = {
|
||||
'label': self.first_ad_view_reward_label,
|
||||
"data": {
|
||||
'user': self.user_1_uuid,
|
||||
'base_amount': 800
|
||||
}
|
||||
}
|
||||
|
||||
url_promote = reverse('promotions-application:plan-promote',
|
||||
kwargs={"pk": self.first_ad_view_reward_plan.uuid, "user_uuid": self.user_1_uuid})
|
||||
response = self.client.post(url_promote, event_create_data, HTTP_AUTHORIZATION=auth, format='json')
|
||||
print(response.json())
|
||||
|
||||
self.first_ad_view_reward_plan.refresh_from_db()
|
||||
self.assertEqual(self.first_ad_view_reward_plan.balance, 10000 - 800)
|
||||
|
||||
|
||||
# url_get_promotion = reverse('promotions-application:plan-list',
|
||||
# kwargs={"pk": self.first_ad_view_reward_plan.uuid, "user_uuid": self.user_1_uuid})
|
||||
url_get_promotion = reverse('promotions-application:plan-list',
|
||||
kwargs={"user_uuid": self.user_1_uuid})
|
||||
|
||||
|
||||
response = self.client.get(url_get_promotion, HTTP_AUTHORIZATION=auth, format='json')
|
||||
print(response.json())
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -54,6 +54,7 @@ class ApplicationRouter(DefaultRouter):
|
|||
|
||||
router = DefaultRouter()
|
||||
|
||||
router.register('<user_uuid>/plan', views_application.ApplicationUserPlanViewSet, basename='plan')
|
||||
router.register('plan', views_application.ApplicationPlanViewSet, basename='plan')
|
||||
router.register('event', views_application.ApplicationEventViewSet, basename='event')
|
||||
|
||||
|
|
|
|||
|
|
@ -16,13 +16,13 @@ from .tasks import analyze_event_task
|
|||
from ..users.models import User
|
||||
|
||||
|
||||
class ApplicationPlanViewSet(
|
||||
class ApplicationUserPlanViewSet(
|
||||
mixins.RetrieveModelMixin,
|
||||
mixins.ListModelMixin,
|
||||
# mixins.CreateModelMixin,
|
||||
GenericViewSet
|
||||
):
|
||||
serializer_class = PromotionSerializer
|
||||
serializer_class = UserPlanSerializer
|
||||
|
||||
permission_classes = [TokenMatchesViewSetActions]
|
||||
required_action_scopes = {
|
||||
|
|
@ -33,6 +33,9 @@ class ApplicationPlanViewSet(
|
|||
|
||||
def get_queryset(self):
|
||||
application = get_application(self.request)
|
||||
# TODO: get user from accounts
|
||||
|
||||
user_uuid = self.kwargs.get('user_uuid')
|
||||
# return Promotion.objects.filter(application=application)
|
||||
return Plan.objects.all()
|
||||
|
||||
|
|
@ -61,7 +64,7 @@ class ApplicationPlanViewSet(
|
|||
|
||||
# TODO
|
||||
# try:
|
||||
user_uuid = self.kwargs.get('user_uuid')
|
||||
|
||||
event = event_saver.save_event(user=user_uuid, application=application, **serializer.validated_data)
|
||||
|
||||
# except Exception as e:
|
||||
|
|
@ -109,6 +112,24 @@ class ApplicationEventViewSet(
|
|||
analyze_event_task.delay(event.uuid)
|
||||
serializer.instance = event
|
||||
|
||||
class ApplicationPlanViewSet(
|
||||
GenericViewSet
|
||||
):
|
||||
serializer_class = PlanSerializer
|
||||
|
||||
permission_classes = [TokenMatchesViewSetActions]
|
||||
required_action_scopes = {
|
||||
"retrieve": [["promotions.application.plan:list-retrieve"]],
|
||||
"list": [["promotions.application.plan:list-retrieve"]],
|
||||
}
|
||||
|
||||
def get_queryset(self):
|
||||
application = get_application(self.request)
|
||||
# return Promotion.objects.filter(application=application)
|
||||
return Plan.objects.all()
|
||||
|
||||
|
||||
|
||||
|
||||
# NOT
|
||||
class UserPlanViewSet(mixins.RetrieveModelMixin,
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ urlpatterns = [
|
|||
path('', include('utils.urls')),
|
||||
path('oauth2/', include('oauth2_provider.urls', namespace='oauth2_provider')),
|
||||
path('promotions/', include('apps.promotions.urls_user', namespace='promotions')),
|
||||
path('api/v2/promotions/application/<user_uuid>/', include('apps.promotions.urls_application', namespace='promotions-application')),
|
||||
path('api/v2/promotions/application/', include('apps.promotions.urls_application', namespace='promotions-application')),
|
||||
]
|
||||
|
||||
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
||||
|
|
|
|||
76
utils/clients/accounts_client.py
Normal file
76
utils/clients/accounts_client.py
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
import time
|
||||
|
||||
import requests
|
||||
from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
|
||||
from gooyal_accounts_client import AuthenticatedClient
|
||||
from gooyal_accounts_client.models import PublicUser, Application
|
||||
from gooyal_accounts_client.api.users import users_api_users_retrieve
|
||||
from gooyal_accounts_client.api.oauth2 import oauth2_apis_applications_retrieve
|
||||
from gooyal_accounts_client.models import Account
|
||||
from gooyal_accounts_client.api.users import (users_api_users_details_retrieve)
|
||||
|
||||
|
||||
def login_as_client_credentials():
|
||||
access_token = cache.get('wallet_access_token')
|
||||
# expires_in = self.auth_data.get('expires_in')
|
||||
# created_at = self.auth_data.get('created_at')
|
||||
#
|
||||
# if access_token and expires_in > time.time() - created_at:
|
||||
# return self.auth_data
|
||||
if access_token:
|
||||
print('already in cache')
|
||||
return access_token
|
||||
|
||||
print('not in cache')
|
||||
|
||||
data = {
|
||||
"grant_type": 'client_credentials',
|
||||
"scope": settings.SCOPES,
|
||||
}
|
||||
auth = (settings.CLIENT_ID, settings.CLIENT_SECRET)
|
||||
|
||||
response = requests.post(f'{settings.BASE_OAUTH2_PROVIDER_PUBLIC_URL}/token/',
|
||||
data=data,
|
||||
auth=auth)
|
||||
|
||||
auth_data = response.json()
|
||||
|
||||
print(auth_data)
|
||||
|
||||
if 'access_token' in auth_data:
|
||||
auth_data['created_at'] = time.time()
|
||||
cache.set('wallet_access_token', auth_data, timeout=auth_data.get('expires_in')-5)
|
||||
|
||||
return auth_data
|
||||
|
||||
def get_client():
|
||||
access_token = login_as_client_credentials()['access_token']
|
||||
# TODO: url
|
||||
client = AuthenticatedClient(base_url="http://accounts:8000", token=access_token)
|
||||
return client
|
||||
|
||||
|
||||
def get_user_details(uuid):
|
||||
client = get_client()
|
||||
data: PublicUser = users_api_users_retrieve.sync(client=client, uuid=uuid)
|
||||
print(data)
|
||||
# or if you need more info (e.g. status_code)
|
||||
# response: Response[MyDataModel] = get_my_data_model.sync_detailed(client=client)
|
||||
|
||||
return data
|
||||
|
||||
def get_user_detailed_info(uuid):
|
||||
client = get_client()
|
||||
response: Account = users_api_users_details_retrieve.sync(uuid=uuid, client=client)
|
||||
print(response)
|
||||
|
||||
return response
|
||||
|
||||
def get_application_details(uuid):
|
||||
client = get_client()
|
||||
data: Application = oauth2_apis_applications_retrieve.sync(client=client, id=uuid)
|
||||
print(data)
|
||||
|
||||
return data
|
||||
Loading…
Add table
Reference in a new issue