Select active myskillbox product
This commit is contained in:
parent
3c89a8b256
commit
5c8c836a2b
|
|
@ -1,11 +1,12 @@
|
|||
from datetime import datetime, timedelta
|
||||
from django.utils.dateparse import parse_datetime
|
||||
from datetime import timedelta
|
||||
|
||||
from django.conf import settings
|
||||
import logging
|
||||
import requests
|
||||
|
||||
from oauth.oauth_client import oauth
|
||||
from users.licenses import MYSKILLBOX_LICENSES
|
||||
from users.licenses import MYSKILLBOX_LICENSES, is_myskillbox_product, TEACHER_KEY
|
||||
from users.models import License
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
|
@ -63,12 +64,71 @@ class HepClient:
|
|||
return response.json()['data']
|
||||
|
||||
def fetch_eorders(self, request=None, token=None):
|
||||
self._has_credentials(request, token)
|
||||
data = {
|
||||
'filters[product_type]': 'eLehrmittel',
|
||||
}
|
||||
response = self._call('api/partners/users/orders/search', request, token, data=data)
|
||||
return response.json()['data']
|
||||
|
||||
# self._has_credentials(request, token)
|
||||
# data = {
|
||||
# 'filters[product_type]': 'eLehrmittel',
|
||||
# }
|
||||
# response = self._call('api/partners/users/orders/search', request, token, data=data)
|
||||
# return response.json()['data']
|
||||
|
||||
return [
|
||||
{
|
||||
"id": 5,
|
||||
"status": "paid",
|
||||
"order_total": 10000,
|
||||
"created_at": "2021-05-10T20:52:16.000000Z",
|
||||
"billing_address": {
|
||||
"id": 20,
|
||||
"salutation": "male",
|
||||
"first_name": "Hans",
|
||||
"last_name": "Meier",
|
||||
"company": "Lustig GmbH",
|
||||
"street": "Sulgenrain 12890",
|
||||
"city": "Bern",
|
||||
"post_code": "3007",
|
||||
"country": "Schweiz"
|
||||
},
|
||||
"delivery_address": {
|
||||
"id": 19,
|
||||
"salutation": "male",
|
||||
"first_name": "Hans",
|
||||
"last_name": "Muster",
|
||||
"company": "Muster AG",
|
||||
"street": "Ruderweg 24",
|
||||
"city": "Bern",
|
||||
"post_code": "3000",
|
||||
"country": "Schweiz"
|
||||
},
|
||||
"entries": [
|
||||
{
|
||||
"id": 3433,
|
||||
"uri": "\/products\/myskillbox-lernende",
|
||||
"url": None,
|
||||
"title": "mySkillbox für Lernende ",
|
||||
"subtitle": "Lizenz gültig für 4 Jahre",
|
||||
"isbn": "978-3-0355-1397-4",
|
||||
"slug": "myskillbox-lernende",
|
||||
"product_type": "eLehrmittel",
|
||||
"product_form": "",
|
||||
"cover": "https:\/\/hep-verlag.fra1.digitaloceanspaces.com\/staging\/products\/2921\/978-3-0355-1397-4.jpg",
|
||||
"price": 100,
|
||||
"price_total": 100,
|
||||
"amount": 1,
|
||||
"authors": []
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
def active_myskillbox_product_for_customer(self, request=None, token=None):
|
||||
eorders = self.fetch_eorders(request=request, token=token)
|
||||
myskillbox_products = self._extract_myskillbox_products(eorders)
|
||||
|
||||
if len(myskillbox_products) == 0:
|
||||
return None
|
||||
else:
|
||||
return self._get_active_product(myskillbox_products)
|
||||
|
||||
def _has_credentials(self, request, token):
|
||||
if request is None and token is None:
|
||||
|
|
@ -76,8 +136,8 @@ class HepClient:
|
|||
|
||||
def customers_search(self, admin_token, email):
|
||||
response = self._call('/rest/V1/customers/search?searchCriteria[filterGroups][0][filters][0][field]='
|
||||
f'email&searchCriteria[filterGroups][0][filters][0][value]={email}',
|
||||
additional_headers={'authorization': f'Bearer {admin_token}'})
|
||||
f'email&searchCriteria[filterGroups][0][filters][0][value]={email}',
|
||||
additional_headers={'authorization': f'Bearer {admin_token}'})
|
||||
|
||||
json_data = response.json()
|
||||
if len(json_data['items']) > 0:
|
||||
|
|
@ -91,7 +151,7 @@ class HepClient:
|
|||
|
||||
def _customer_orders(self, admin_token, customer_id):
|
||||
url = ('/rest/V1/orders/?searchCriteria[filterGroups][0][filters][0]['
|
||||
f'field]=customer_id&searchCriteria[filterGroups][0][filters][0][value]={customer_id}')
|
||||
f'field]=customer_id&searchCriteria[filterGroups][0][filters][0][value]={customer_id}')
|
||||
|
||||
response = self._call(url, additional_headers={'authorization': 'Bearer {}'.format(admin_token)})
|
||||
return response.json()
|
||||
|
|
@ -117,44 +177,39 @@ class HepClient:
|
|||
else:
|
||||
return self._get_relevant_product(products)
|
||||
|
||||
def _extract_myskillbox_products(self, orders):
|
||||
def _extract_myskillbox_products(self, eorders):
|
||||
products = []
|
||||
|
||||
for order_item in orders['items']:
|
||||
for eorder in eorders:
|
||||
|
||||
status = ''
|
||||
if 'status' in order_item:
|
||||
status = order_item['status']
|
||||
if 'status' in eorder:
|
||||
status = eorder['status']
|
||||
|
||||
for item in order_item['items']:
|
||||
|
||||
order_id = -1
|
||||
if 'order_id' in item:
|
||||
order_id = item['order_id']
|
||||
|
||||
if item['sku'] in list(MYSKILLBOX_LICENSES.keys()):
|
||||
for entry in eorder['entries']:
|
||||
if is_myskillbox_product(entry['isbn']):
|
||||
product = {
|
||||
'raw': item,
|
||||
'activated': self._get_item_activation(order_item),
|
||||
'raw': entry,
|
||||
'activated': self._get_item_activation(eorder),
|
||||
'status': status,
|
||||
'order_id': order_id,
|
||||
'license': MYSKILLBOX_LICENSES[item['sku']],
|
||||
'isbn': item['sku']
|
||||
'order_id': entry['id'],
|
||||
'license': MYSKILLBOX_LICENSES[entry['isbn']],
|
||||
'isbn': entry['isbn']
|
||||
}
|
||||
|
||||
products.append(product)
|
||||
|
||||
return products
|
||||
|
||||
def _get_item_activation(self, item):
|
||||
if 'created_at' in item:
|
||||
return datetime.strptime(item['created_at'], '%Y-%m-%d %H:%M:%S')
|
||||
def _get_item_activation(self, eorder):
|
||||
if 'created_at' in eorder:
|
||||
return parse_datetime(eorder['created_at'])
|
||||
|
||||
def _get_relevant_product(self, products):
|
||||
def _get_active_product(self, products):
|
||||
|
||||
def filter_valid_products(product):
|
||||
|
||||
if product['status'] != 'complete':
|
||||
if product['status'] != 'paid':
|
||||
return False
|
||||
|
||||
expiry_delta = product['activated'] + timedelta(product['license']['duration'])
|
||||
|
|
@ -187,4 +242,3 @@ class HepClient:
|
|||
return active_products[0]
|
||||
|
||||
return teacher_edition
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,48 @@
|
|||
[
|
||||
{
|
||||
"id": 5,
|
||||
"status": "paid",
|
||||
"order_total": 10000,
|
||||
"created_at": "2021-05-10T20:52:16.000000Z",
|
||||
"billing_address": {
|
||||
"id": 20,
|
||||
"salutation": "male",
|
||||
"first_name": "Hans",
|
||||
"last_name": "Meier",
|
||||
"company": "Lustig GmbH",
|
||||
"street": "Sulgenrain 12890",
|
||||
"city": "Bern",
|
||||
"post_code": "3007",
|
||||
"country": "Schweiz"
|
||||
},
|
||||
"delivery_address": {
|
||||
"id": 19,
|
||||
"salutation": "male",
|
||||
"first_name": "Hans",
|
||||
"last_name": "Muster",
|
||||
"company": "Muster AG",
|
||||
"street": "Ruderweg 24",
|
||||
"city": "Bern",
|
||||
"post_code": "3000",
|
||||
"country": "Schweiz"
|
||||
},
|
||||
"entries": [
|
||||
{
|
||||
"id": 3433,
|
||||
"uri": "\/products\/myskillbox-lernende",
|
||||
"url": null,
|
||||
"title": "mySkillbox für Lernende ",
|
||||
"subtitle": "Lizenz gültig für 4 Jahre",
|
||||
"isbn": "978-3-0355-1397-4",
|
||||
"slug": "myskillbox-lernende",
|
||||
"product_type": "eLehrmittel",
|
||||
"product_form": "",
|
||||
"cover": "https:\/\/hep-verlag.fra1.digitaloceanspaces.com\/staging\/products\/2921\/978-3-0355-1397-4.jpg",
|
||||
"price": 100,
|
||||
"price_total": 100,
|
||||
"amount": 1,
|
||||
"authors": []
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
|
@ -4,6 +4,7 @@ from datetime import datetime, timedelta
|
|||
from django.test import TestCase
|
||||
from oauth.hep_client import HepClient
|
||||
from users.licenses import MYSKILLBOX_LICENSES
|
||||
from users.models import License
|
||||
|
||||
ISBNS = list(MYSKILLBOX_LICENSES.keys())
|
||||
|
||||
|
|
@ -26,25 +27,25 @@ class HepClientTestCases(TestCase):
|
|||
'isbn': TEACHER_ISBN,
|
||||
'raw': {},
|
||||
'activated': self.now - timedelta(2*TEACHER_LICENSE['duration']),
|
||||
'status': 'complete'
|
||||
'status': 'paid'
|
||||
},
|
||||
{
|
||||
'license': TEACHER_LICENSE,
|
||||
'isbn': TEACHER_ISBN,
|
||||
'raw': {},
|
||||
'activated': self.now - timedelta(3 * TEACHER_LICENSE['duration']),
|
||||
'status': 'complete'
|
||||
'status': 'paid'
|
||||
},
|
||||
{
|
||||
'license': TEACHER_LICENSE,
|
||||
'isbn': TEACHER_ISBN,
|
||||
'raw': {},
|
||||
'activated': self.now - timedelta(4 * TEACHER_LICENSE['duration']),
|
||||
'status': 'complete'
|
||||
'status': 'paid'
|
||||
}
|
||||
]
|
||||
|
||||
relevant_product = self.hep_client._get_relevant_product(products)
|
||||
relevant_product = self.hep_client._get_active_product(products)
|
||||
self.assertIsNone(relevant_product)
|
||||
|
||||
def test_has_no_not_completed_product(self):
|
||||
|
|
@ -58,7 +59,7 @@ class HepClientTestCases(TestCase):
|
|||
}
|
||||
]
|
||||
|
||||
relevant_product = self.hep_client._get_relevant_product(products)
|
||||
relevant_product = self.hep_client._get_active_product(products)
|
||||
self.assertIsNone(relevant_product)
|
||||
|
||||
def test_has_valid_product(self):
|
||||
|
|
@ -92,7 +93,7 @@ class HepClientTestCases(TestCase):
|
|||
}
|
||||
]
|
||||
|
||||
relevant_product = self.hep_client._get_relevant_product(products)
|
||||
relevant_product = self.hep_client._get_active_product(products)
|
||||
self.assertEqual(relevant_product['raw']['id'], 0)
|
||||
|
||||
def test_has_multiple_valid_teacher_products_but_only_one_active(self):
|
||||
|
|
@ -126,7 +127,7 @@ class HepClientTestCases(TestCase):
|
|||
}
|
||||
]
|
||||
|
||||
relevant_product = self.hep_client._get_relevant_product(products)
|
||||
relevant_product = self.hep_client._get_active_product(products)
|
||||
self.assertEqual(relevant_product['raw']['id'], 0)
|
||||
|
||||
def test_has_valid_student_and_teacher_edition(self):
|
||||
|
|
@ -158,12 +159,12 @@ class HepClientTestCases(TestCase):
|
|||
|
||||
expiry_date = self.now + timedelta(3)
|
||||
|
||||
is_active = HepClient.is_product_active(expiry_date, TEACHER_ISBN)
|
||||
is_active = License.is_product_active(expiry_date, TEACHER_ISBN)
|
||||
self.assertTrue(is_active)
|
||||
|
||||
def test_product_is_not_active(self):
|
||||
|
||||
expiry_date = self.now - timedelta(3 * TEACHER_LICENSE['duration'])
|
||||
|
||||
is_active = HepClient.is_product_active(expiry_date, TEACHER_ISBN)
|
||||
is_active = License.is_product_active(expiry_date, TEACHER_ISBN)
|
||||
self.assertFalse(is_active)
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ def authorize(request):
|
|||
|
||||
OAuth2Token.objects.update_or_create_token(token, user)
|
||||
|
||||
hep_client.fetch_eorders(token=token)
|
||||
some = hep_client.active_myskillbox_product_for_customer(token=token)
|
||||
if status_msg:
|
||||
return redirect(f'/login-success?state={status_msg}')
|
||||
# return status_msg
|
||||
|
|
|
|||
|
|
@ -28,3 +28,8 @@ MYSKILLBOX_LICENSES = {
|
|||
'name': 'Teacher 1 year'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def is_myskillbox_product(isbn):
|
||||
valid_isbns = list(MYSKILLBOX_LICENSES.keys())
|
||||
return isbn in valid_isbns
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ from django.contrib.auth.models import AbstractUser, Permission
|
|||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.db import models
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.utils import timezone
|
||||
|
||||
from users.licenses import MYSKILLBOX_LICENSES
|
||||
from users.managers import RoleManager, UserRoleManager, UserManager, LicenseManager
|
||||
|
|
@ -312,7 +313,7 @@ class License(models.Model):
|
|||
|
||||
@staticmethod
|
||||
def is_product_active(expiry_date, isbn):
|
||||
now = datetime.now()
|
||||
now = timezone.now()
|
||||
|
||||
return expiry_date >= now >= expiry_date - timedelta(days=MYSKILLBOX_LICENSES[isbn]['duration'])
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue