def widget_marketplace_view(request, marketplace_id): """ Display the five most purchased products of the marketplace if they are available. :return: A JSON object with two keys, `balance` (the balance of the marketplace) and `suggested_products`, a list of serialized `Product` objects. """ marketplace = Marketplace.objects.get(pk=marketplace_id) suggested_products = ( marketplace.products.annotate( number_of_purchases=Count( "transactions", filter=Q(transactions__buyer=request.user) ) ) .order_by("-number_of_purchases") .exclude(number_left=0)[0:5] .all() ) return Response( { "balance": BalanceView.get_balance_in_json(request.user, marketplace), "suggested_products": [ ProductShortSerializer().to_representation(product) for product in suggested_products ], } )
def widget_balance_view(request): """ :return: A JSON object with one key, `balances`, which is a list of objects `{"balance", "marketplace", "user"}`. """ return Response({ "balances": [ BalanceView.get_balance_in_json(request.user, marketplace) for marketplace in Marketplace.objects.filter(enabled=True).all() ] })
# Library. router.register(r"library", LibraryViewSet) router.register(r"loans", LoansViewSet) router.register(r"loanables", LoanableViewSet) # Marketplace. router.register(r"marketplace", MarketplaceViewSet) router.register(r"products", ProductViewSet) router.register(r"transactions", TransactionViewSet) router.register(r"fundings", FundingViewSet) urlpatterns += [ path("associations/<association_pk>/image", set_association_logo), path("marketplace/balance/", widget_balance_view, name="balance-list"), path( "marketplace/<slug:marketplace_id>/balance/", BalanceView.as_view(), name="balance-list-marketplace", ), path( "marketplace/<slug:marketplace_id>/balance/<slug:user_id>/", BalanceView.as_view(), name="balance-detail", ), ] # Pages. router.register(r"pages", PageViewSet) # Roles. router.register(r"roles", RoleViewSet)
from rest_framework import routers from associations.views import AssociationViewSet, PageViewSet, NewsViewSet, GroupViewSet, MarketplaceViewSet, \ ProductViewSet, OrderViewSet, LibraryViewSet, FundingViewSet, BalanceView from authentication.views import UserViewSet, JWTSetCookiesView, CheckCredentials, LogoutView from chat.views import ChatMessageViewSet router = routers.DefaultRouter() router.register(r'users', UserViewSet) router.register(r'associations', AssociationViewSet) router.register(r'pages', PageViewSet) router.register(r'news', NewsViewSet) router.register(r'groups', GroupViewSet) router.register(r'marketplace', MarketplaceViewSet) router.register(r'products', ProductViewSet) router.register(r'orders', OrderViewSet) router.register(r'library', LibraryViewSet) router.register(r'funding', FundingViewSet) router.register( r'chat', ChatMessageViewSet ) # Adds classic REST endpoint + retrieve_up_to + retrieve_from urlpatterns = [ path('auth/', JWTSetCookiesView.as_view(), name='token_obtain_pair'), path('auth/check/', CheckCredentials.as_view(), name='check'), url( r'^marketplace/(?P<marketplace_id>[^/.]+)/balance/(?P<user_id>[^/.]*)$', BalanceView.as_view()), path('auth/logout/', LogoutView.as_view(), name='logout'), ] + router.urls