Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Add an API to return suggested product type choices #1708

Merged
merged 3 commits into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- Filter by extended fields [#1696](https://github.com/open-apparel-registry/open-apparel-registry/pull/1696)
- Add extended profile search APIs [#1697](https://github.com/open-apparel-registry/open-apparel-registry/pull/1697)
- Add an API to return suggested product type choices [#1708](https://github.com/open-apparel-registry/open-apparel-registry/pull/1708)

### Changed

Expand Down
16 changes: 15 additions & 1 deletion src/django/api/extended_fields.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re
from django.core import exceptions as core_exceptions
from api.models import Contributor, ExtendedField
from api.models import Contributor, ExtendedField, ProductType
from api.facility_type_processing_type import (
get_facility_and_processing_type,
ALL_PROCESSING_TYPES
Expand Down Expand Up @@ -228,3 +228,17 @@ def create_extendedfields_for_claim(claim):
else:
ExtendedField.objects.filter(facility_claim=claim,
field_name=extended_field).delete()


def get_product_types():
product_types = list(ProductType.objects.all()
.values_list('value', flat=True))
ef_values = (ExtendedField.objects.filter(field_name='product_type')
.values_list('value__raw_values', flat=True))
flat_ef_value_titles = [item.title() for sublist in
ef_values for item in sublist]
product_types.extend(flat_ef_value_titles)
# Converting to a set and back removes duplicates
product_types = list(set(product_types))
product_types.sort()
return product_types
28 changes: 2 additions & 26 deletions src/django/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from itertools import chain
from collections import defaultdict

from django.conf import settings
Expand Down Expand Up @@ -33,7 +32,6 @@
FacilityClaimReviewNote,
User,
Contributor,
ProductType,
Source,
ApiBlock,
FacilityActivityReport,
Expand All @@ -46,6 +44,7 @@
from api.helpers import (prefix_a_an,
get_single_contributor_field_values,
get_list_contributor_field_values)
from api.extended_fields import get_product_types
from api.facility_type_processing_type import (
ALL_FACILITY_TYPE_CHOICES,
ALL_PROCESSING_TYPE_CHOICES
Expand Down Expand Up @@ -1172,33 +1171,10 @@ def get_certification_choices(self, claim):
return FacilityClaim.CERTIFICATION_CHOICES

def get_product_type_choices(self, claim):
seeds = [
seed
for seed
in ProductType.objects.all().values_list('value', flat=True)
or []
]

new_values = FacilityClaim \
.objects \
.all() \
.values_list('facility_product_types', flat=True)

values = [
new_value
for new_value
in new_values if new_value is not None
]

# Using `chain` flattens nested lists
union_of_seeds_and_values = list(
set(chain.from_iterable(values)).union(seeds))
union_of_seeds_and_values.sort()

return [
(choice, choice)
for choice
in union_of_seeds_and_values
in get_product_types()
]

def get_production_type_choices(self, claim):
Expand Down
15 changes: 0 additions & 15 deletions src/django/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4600,21 +4600,6 @@ def test_product_and_production_options_are_serialized(self):
self.assertIsNotNone(data['production_type_choices'])
self.assertNotEqual([], data['production_type_choices'])

def test_product_and_production_values_from_claims_are_included(self):
self.claim.facility_product_types = ['A', 'B']
self.claim.facility_production_types = ['Blending', 'Bonding']
self.claim.save()
data = ApprovedFacilityClaimSerializer(self.claim).data

product_types = data['product_type_choices']
self.assertIn(('A', 'A'), product_types)
self.assertEqual(('A', 'A'), product_types[0])
self.assertIn(('B', 'B'), product_types)

production_types = data['production_type_choices']
self.assertIn(('blending', 'Blending'), production_types)
self.assertIn(('bonding', 'Bonding'), production_types)


class LogDownloadTests(APITestCase):
def setUp(self):
Expand Down
21 changes: 20 additions & 1 deletion src/django/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@
create_dissociate_match_change_reason)
from api.extended_fields import (create_extendedfields_for_single_item,
update_extendedfields_for_list_item,
create_extendedfields_for_claim)
create_extendedfields_for_claim,
get_product_types)
from api.facility_type_processing_type import (
FACILITY_PROCESSING_TYPES_VALUES)

Expand Down Expand Up @@ -620,6 +621,24 @@ def facility_processing_types(request):
return Response(FACILITY_PROCESSING_TYPES_VALUES)


@api_view(['GET'])
def product_types(request):
"""
Returns a list of suggested product types by combining standard types with
distinct values submitted by contributors.

## Sample Response

[
"Accessories",
"Belts",
"Caps"
]

"""
return Response(get_product_types())


@api_view(['GET'])
def current_tile_cache_key(request):
return Response(Facility.current_tile_cache_key())
Expand Down
4 changes: 3 additions & 1 deletion src/django/oar/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@
url(r'^api/workers-ranges/', views.number_of_workers_ranges,
name='number_of_workers_ranges'),
url(r'^api/facility-processing-types', views.facility_processing_types,
name='facility_processing_types')
name='facility_processing_types'),
url(r'^api/product-types', views.product_types,
name='prodcut_types'),
]

info_apis = [
Expand Down