Official field in CategoryTag and categories in initial endpoint
This commit is contained in:
@@ -26,7 +26,7 @@ class ProductAdmin(admin.ModelAdmin):
|
|||||||
|
|
||||||
class CategoryTagAdmin(TagModelAdmin):
|
class CategoryTagAdmin(TagModelAdmin):
|
||||||
form = forms.CategoryTagForm
|
form = forms.CategoryTagForm
|
||||||
list_display = ('label', 'name')
|
list_display = ('label', 'name', 'official')
|
||||||
search_fields = ('label', 'name', 'slug')
|
search_fields = ('label', 'name', 'slug')
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ class CategoryTagForm(forms.ModelForm):
|
|||||||
parent_category = autocomplete.Select2ListCreateChoiceField(required=False, widget=autocomplete.Select2(url='category-tag-autocomplete', attrs={"data-tags": "true"}))
|
parent_category = autocomplete.Select2ListCreateChoiceField(required=False, widget=autocomplete.Select2(url='category-tag-autocomplete', attrs={"data-tags": "true"}))
|
||||||
class Meta:
|
class Meta:
|
||||||
model = CategoryTag
|
model = CategoryTag
|
||||||
fields = ('name', 'label', 'slug', 'parent_category')
|
fields = ('name', 'label', 'slug', 'parent_category', 'official')
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ class TreeTag(TagTreeModel):
|
|||||||
|
|
||||||
|
|
||||||
class CategoryTag(TagTreeModel):
|
class CategoryTag(TagTreeModel):
|
||||||
|
official = models.BooleanField('Oficial', default=False)
|
||||||
class TagMeta:
|
class TagMeta:
|
||||||
initial = ""
|
initial = ""
|
||||||
force_lowercase = False
|
force_lowercase = False
|
||||||
|
|||||||
@@ -120,6 +120,24 @@ def get_latest_companies(number):
|
|||||||
result = CompanySerializer(companies_qs, many=True).data
|
result = CompanySerializer(companies_qs, many=True).data
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def get_popular_categories(number):
|
||||||
|
categories_list = list(CategoryTag.objects.filter(level=1, official=True).values_list('name', flat=True))
|
||||||
|
counted_categories = []
|
||||||
|
for cat in categories_list:
|
||||||
|
count = 0
|
||||||
|
categories = get_category_and_descendants(cat)
|
||||||
|
for i in categories:
|
||||||
|
count += i.count
|
||||||
|
counted_categories.append({
|
||||||
|
"name": cat,
|
||||||
|
"image": None,
|
||||||
|
"count": count
|
||||||
|
})
|
||||||
|
popular = sorted(counted_categories, key=lambda d: d['count'], reverse=True)[:number]
|
||||||
|
return popular
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@api_view(['GET',]) # include allowed methods
|
@api_view(['GET',]) # include allowed methods
|
||||||
def initial(request):
|
def initial(request):
|
||||||
@@ -135,8 +153,14 @@ def initial(request):
|
|||||||
|
|
||||||
products = get_latest_products(12)
|
products = get_latest_products(12)
|
||||||
companies = get_latest_companies(12)
|
companies = get_latest_companies(12)
|
||||||
|
categories = get_popular_categories(12)
|
||||||
|
|
||||||
return Response(data={"cards": category_cards, "products": products, "companies": companies})
|
return Response(data={
|
||||||
|
"cards": category_cards,
|
||||||
|
"products": products,
|
||||||
|
"companies": companies,
|
||||||
|
"categories": categories
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
@api_view(['POST',])
|
@api_view(['POST',])
|
||||||
|
|||||||
Reference in New Issue
Block a user