Skip to content

Commit

Permalink
Refactor of JSON structure in response of get_by_code
Browse files Browse the repository at this point in the history
  • Loading branch information
piemar committed Aug 28, 2024
1 parent fe7ab5f commit eef5f71
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 47 deletions.
15 changes: 8 additions & 7 deletions pola/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
}

DEFAULT_RESULT = {
'all_company_brands': [],
'product_id': None,
'code': None,
'name': None,
Expand Down Expand Up @@ -80,15 +79,11 @@ def get_result_from_code(code, multiple_company_supported=False, report_as_objec
if not product_company:
handle_unknown_company(code, report, result, multiple_company_supported)
elif multiple_company_supported:
handle_multiple_companies(code, companies, result, stats)
handle_multiple_companies(code, companies, result, stats, product_company)
else:
handle_companies_when_multiple_companies_are_not_supported(
code, companies, multiple_company_supported, result, stats
)
if product_company:
result['all_company_brands'] = [
serialize_brand(brand) for brand in Brand.objects.filter(company=product_company)
]
else:
result['name'] = 'Nieznany produkt'
result['altText'] = (
Expand Down Expand Up @@ -154,13 +149,19 @@ def append_brands_if_enabled(company, company_data):
company_data['description'] += f'Ten producent psoiada marki: {brand_list}.'


def handle_multiple_companies(code, companies, result, stats):
def add_brands(company_data, product_company):
if product_company:
company_data['brands'] = [serialize_brand(brand) for brand in Brand.objects.filter(company=product_company)]


def handle_multiple_companies(code, companies, result, stats, product_company):
companies_data = []

for company in companies:
company_data = serialize_company(company)
append_ru_by_warning_to_description(code, company_data)
append_brands_if_enabled(company, company_data)
add_brands(company_data, product_company)
stats['was_plScore'] = all(get_pl_score(c) for c in companies)
companies_data.append(company_data)
result['companies'] = companies_data
Expand Down
29 changes: 15 additions & 14 deletions pola/rpc_api/openapi-v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,6 @@ components:
type: object
additionalProperties: false
properties:
all_company_brands:
type: array
items:
type: object
additionalProperties: false
properties:
name:
type: string
logotype_url:
type: string
nullable: true
required:
- name
- logotype_url
altText:
type: string
nullable: true
Expand Down Expand Up @@ -250,6 +236,20 @@ components:
official_url:
type: string
nullable: true
brands:
type: array
items:
type: object
additionalProperties: false
properties:
name:
type: string
logotype_url:
type: string
nullable: true
required:
- name
- logotype_url
required:
- name
- plCapital
Expand All @@ -263,6 +263,7 @@ components:
- plScore
- plWorkers
- plWorkers_notes
- brands
product_id:
type: integer
nullable: true
Expand Down
2 changes: 0 additions & 2 deletions pola/rpc_api/tests/test_views_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ def test_should_return_200_when_polish_and_known_product(self):
self.maxDiff = None
self.assertEqual(
{
'all_company_brands': [],
"product_id": p.id,
"code": "5900049011829",
"name": c.official_name,
Expand Down Expand Up @@ -200,7 +199,6 @@ def test_should_return_200_when_one_comand_in_brand_and_product(self):
self.maxDiff = None
self.assertEqual(
{
'all_company_brands': [{'logotype_url': None, 'name': p.brand.common_name}],
'altText': None,
'card_type': 'type_white',
'code': '5900049011829',
Expand Down
16 changes: 6 additions & 10 deletions pola/rpc_api/tests/test_views_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def test_should_return_200_when_product_without_company(self):
self.maxDiff = None
self.assertEqual(
{
'all_company_brands': [],
'product_id': p.pk,
'code': '5900049011829',
'name': 'Tego produktu nie mamy jeszcze w bazie',
Expand Down Expand Up @@ -96,7 +95,6 @@ def test_should_return_200_when_polish_and_known_product(self):
self.maxDiff = None
self.assertEqual(
{
'all_company_brands': [],
"product_id": p.pk,
"code": "5900049011829",
"name": c.common_name,
Expand Down Expand Up @@ -127,6 +125,7 @@ def test_should_return_200_when_polish_and_known_product(self):
"friend_text": "To jest przyjaciel Poli",
"description": "TEST",
"sources": {"TEST": "BBBB"},
'brands': [],
}
],
"donate": {
Expand Down Expand Up @@ -165,18 +164,13 @@ def test_should_return_200_when_product_with_brand_and_image(self):
self.maxDiff = None
response_json = json.loads(response.content)

self.assertIn('pola-app-company-logotype', response_json['all_company_brands'][0]['logotype_url'])
del response_json['all_company_brands'][0]['logotype_url']
self.assertIn('pola-app-company-logotype', response_json['companies'][0]['brands'][0]['logotype_url'])
del response_json['companies'][0]['brands'][0]['logotype_url']

self.assertIn('ola-app-company-logotype', response_json['companies'][0]['logotype_url'])
del response_json['companies'][0]['logotype_url']
self.assertEqual(
{
'all_company_brands': [
{
'name': b.common_name,
}
],
'product_id': p.pk,
'code': '5900049011829',
'name': c.common_name,
Expand All @@ -201,6 +195,7 @@ def test_should_return_200_when_product_with_brand_and_image(self):
'friend_text': 'To jest przyjaciel Poli',
'description': 'TEST',
'sources': {'TEST': 'BBBB'},
'brands': [{'name': b.common_name}],
},
],
'report': {
Expand Down Expand Up @@ -258,7 +253,6 @@ def test_should_return_200_when_multiple_companies(self):

self.assertEqual(
{
'all_company_brands': [],
"product_id": p.id,
"code": "5900049011829",
"name": c1.official_name,
Expand Down Expand Up @@ -289,6 +283,7 @@ def test_should_return_200_when_multiple_companies(self):
"friend_text": "To jest przyjaciel Poli",
"description": "TEST",
"sources": {"TEST": "BBBB"},
'brands': [],
},
{
"name": c2.official_name,
Expand All @@ -309,6 +304,7 @@ def test_should_return_200_when_multiple_companies(self):
"friend_text": "To jest przyjaciel Poli",
"description": "TEST",
"sources": {"TEST": "BBBB"},
'brands': [],
},
],
"donate": {
Expand Down
14 changes: 0 additions & 14 deletions pola/tests/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def test_should_return_empty_message_on_invalid_code(self):
response = get_result_from_code("ABC")
expected_response = (
{
"all_company_brands": [],
"altText": (
"Pola rozpoznaje tylko kody kreskowe typu EAN8 i EAN13. "
"Zeskanowany przez Ciebie kod jest innego typu. Spróbuj "
Expand Down Expand Up @@ -70,7 +69,6 @@ def test_missing_company_and_590(self, mock_get_by_code):

expected_response = (
{
'all_company_brands': [],
"altText": (
"Każde skanowanie jest rejestrowane. Najczęściej skanowane firmy i produkty, "
"których nie mamy jeszcze w bazie, są weryfikowane w pierwszej kolejności. "
Expand Down Expand Up @@ -114,7 +112,6 @@ def test_missing_company_and_book(self, prefix):

expected_response = (
{
'all_company_brands': [],
"altText": (
'Zeskanowany kod jest kodem ISBN/ISSN/ISMN dotyczącym książki, '
'czasopisma lub albumu muzycznego. Wydawnictwa tego typu nie są '
Expand Down Expand Up @@ -157,7 +154,6 @@ def test_missing_company_and_russia(self, prefix, country):

expected_response = (
{
'all_company_brands': [],
"altText": (
'Ten produkt został wyprodukowany przez zagraniczną firmę, której '
f'miejscem rejestracji jest: {country}. \n'
Expand Down Expand Up @@ -213,7 +209,6 @@ def test_missing_company_and_wrong_country(self, prefix, country):

expected_response = (
{
'all_company_brands': [],
"altText": (
f'Ten produkt został wyprodukowany przez zagraniczną firmę, '
f'której miejscem rejestracji jest: {country}.'
Expand Down Expand Up @@ -255,7 +250,6 @@ def test_internal_code(self):

expected_response = (
{
'all_company_brands': [],
"altText": (
'Zeskanowany kod jest wewnętrznym kodem sieci handlowej. Pola nie '
'potrafi powiedzieć o nim nic więcej'
Expand Down Expand Up @@ -298,7 +292,6 @@ def test_code_with_one_company(self):

expected_response = (
{
'all_company_brands': [],
'altText': None,
'card_type': 'type_grey',
'code': TEST_EAN13,
Expand Down Expand Up @@ -360,7 +353,6 @@ def test_russian_code_with_one_company(self):

expected_response = (
{
'all_company_brands': [],
'altText': None,
'card_type': 'type_grey',
'code': '4621520000059',
Expand Down Expand Up @@ -411,10 +403,6 @@ def test_display_brand_when_enabled_on_company(self):

expected_response = (
{
'all_company_brands': [
{'logotype_url': None, 'name': 'brand-2'},
{'logotype_url': None, 'name': 'brand-1'},
],
'altText': None,
'card_type': 'type_grey',
'code': TEST_EAN13,
Expand Down Expand Up @@ -459,7 +447,6 @@ def test_code_with_multiple_company(self):
# TODO: Add support for multiple companies in this response
expected_response = (
{
'all_company_brands': [],
'altText': None,
'card_type': 'type_grey',
'code': TEST_EAN13,
Expand Down Expand Up @@ -505,7 +492,6 @@ def test_russian_code_with_multiple_company(self):
# TODO: Add support for multiple companies in this response
expected_response = (
{
'all_company_brands': [],
'altText': None,
'card_type': 'type_grey',
'code': '4621520000059',
Expand Down

0 comments on commit eef5f71

Please sign in to comment.