Skip to content

Commit

Permalink
backend trang thông tin thể loại
Browse files Browse the repository at this point in the history
search và sort boardgame
  • Loading branch information
TTTThanh2812 committed Jun 16, 2023
1 parent e7c8f98 commit 0bbce3f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions Rent_boardgame/core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
path('', views.home, name='home'),
path('contact/', views.contact, name='contact'),
path('allCategories/', views.allCategories, name='allCategories'),
path('category/<int:category_mstl>/', views.category_view, name='category'),
path('signUp/', views.signUp, name='signUp'),
path('signIn/', auth_views.LoginView.as_view(template_name = 'core/signIn.html', authentication_form = SignInForm), name='signIn'),
]
51 changes: 51 additions & 0 deletions Rent_boardgame/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,57 @@ def allCategories(request):
'boardgame_ratings':boardgame_ratings,
})

def category_view(request, category_mstl):
category = Category.objects.get(mstl=category_mstl)
boardgames = BoardGame.objects.filter(category=category)
boardgame_ratings = []
for boardgame in boardgames:
total_comments = Review.objects.filter(boardgame=boardgame).exclude(comment='').count()
if Rating.objects.filter(boardgame=boardgame).exclude(stars=None).aggregate(models.Avg('stars'))['stars__avg'] != None:
average_stars = int(Rating.objects.filter(boardgame=boardgame).exclude(stars=None).aggregate(models.Avg('stars'))['stars__avg'])
else:
average_stars = 0
draw_average_stars = range(0,average_stars)
draw_non_stars = range(0,5 - average_stars)
boardgame_ratings.append({'boardgame': boardgame,
'total_comments': total_comments,
'average_stars': average_stars,
'draw_average_stars':draw_average_stars,
'draw_non_stars': draw_non_stars,
})

#Search form
search_query = request.GET.get('search')
if search_query:
boardgames = boardgames.filter(name__icontains=search_query)

# Sort options
sort_option = request.GET.get('sort')
if sort_option == 'category':
boardgames = boardgames.order_by('category')
elif sort_option == 'people':
boardgames = boardgames.order_by('people')
elif sort_option == 'age':
boardgames = boardgames.order_by('age_rating')
elif sort_option == 'time':
boardgames = boardgames.order_by('play_time')
elif sort_option == 'author':
boardgames = boardgames.order_by('author')
elif sort_option == 'producer':
boardgames = boardgames.order_by('producer')
elif sort_option == 'year':
boardgames = boardgames.order_by('publication_year')
elif sort_option == 'price':
boardgames = boardgames.order_by('price')
elif sort_option == 'rating':
boardgames = boardgames.order_by('-average_rating')

return render(request, 'core/category.html', {
'category': category,
'boardgames': boardgames,
'boardgame_ratings':boardgame_ratings,
})

def signUp(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
Expand Down

0 comments on commit 0bbce3f

Please sign in to comment.