forked from monzo/response
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurls.py
More file actions
61 lines (46 loc) · 2.25 KB
/
Copy pathurls.py
File metadata and controls
61 lines (46 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from django.conf.urls import url, include
from rest_framework import routers, viewsets, pagination
from rest_framework.decorators import action
from core.models.incident import Incident
from core.models.action import Action
from core.models.user_external import ExternalUser
from datetime import datetime
from calendar import monthrange
from core.serializers import *
class ExternalUserViewSet(viewsets.ModelViewSet):
# ViewSets define the view behavior.
queryset = ExternalUser.objects.all()
serializer_class = ExternalUserSerializer
class ActionViewSet(viewsets.ModelViewSet):
# ViewSets define the view behavior.
queryset = Action.objects.all()
serializer_class = ActionSerializer
# Will return the incidents of the current month
# Can pass ?start=2019-05-28&end=2019-06-03 to change range
class IncidentViewSet(viewsets.ModelViewSet):
# ViewSets define the view behavior.
serializer_class = IncidentSerializer
pagination_class = None # Remove pagination
def get_queryset(self):
# Same query is used to get single items so we check if pk is passed
# incident/2/ if we use the filter below we would have to have correct time range
if 'pk' in self.kwargs:
return Incident.objects.filter(pk=self.kwargs['pk'])
today = datetime.today()
first_day_of_current_month = datetime(today.year, today.month, 1)
days_in_month = monthrange(today.year, today.month)[1]
last_day_of_current_month = datetime(today.year, today.month, days_in_month)
start = self.request.GET.get('start', first_day_of_current_month)
end = self.request.GET.get('end', last_day_of_current_month)
return Incident.objects.filter(start_time__gte=start, start_time__lte=end)
# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r'incidents', IncidentViewSet, base_name='Incidents')
router.register(r'actions', ActionViewSet)
router.register(r'ExternalUser', ExternalUserViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
url(r'^', include(router.urls)),
#url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]