forked from moscowpython/python.ru
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathviews.py
More file actions
23 lines (18 loc) · 723 Bytes
/
views.py
File metadata and controls
23 lines (18 loc) · 723 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from django.views.generic import View
from django.http import JsonResponse
from cards_api.models import Card
class CardsListJsonView(View):
DEFAULT_CARDS_AMOUNT = '100'
def get(self, request):
first_element = int(request.GET.get('from', '0'))
last_element = int(request.GET.get('to', self.DEFAULT_CARDS_AMOUNT))
cards = self.fetch_cards(first_element, last_element)
return JsonResponse({
'data': [c.to_dict() for c in cards],
'meta': {
'from': first_element,
'to': last_element
},
})
def fetch_cards(self, first_element, last_element):
return Card.objects.all()[first_element:last_element]