-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlimit.py
More file actions
47 lines (39 loc) · 1.57 KB
/
Copy pathlimit.py
File metadata and controls
47 lines (39 loc) · 1.57 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
"""
Limit views.
"""
import re
from django.db.models import Q
from rest_framework.viewsets import ReadOnlyModelViewSet
from django.shortcuts import get_object_or_404
from api import models, serializers
class LimitSpecViewSet(ReadOnlyModelViewSet):
"""A viewset for interacting with Limit objects."""
model = models.limit.LimitSpec
serializer_class = serializers.LimitSpecSerializer
def get_queryset(self, **kwargs):
q = Q(disabled=False)
keywords = self.request.query_params.get('keywords', '').strip()
if keywords:
q &= Q(
keywords__contains=[keyword.lower() for keyword in re.split(r"\W+", keywords)])
return self.model.objects.filter(q)
class LimitPlanViewSet(ReadOnlyModelViewSet):
"""A viewset for interacting with Limit objects."""
lookup_field = 'id'
lookup_value_regex = r'[-.\w]+'
model = models.limit.LimitPlan
serializer_class = serializers.LimitPlanSerializer
def get_object(self):
return get_object_or_404(self.model, id=self.kwargs["id"])
def get_queryset(self, **kwargs):
q = Q(disabled=False)
spec_id = self.request.query_params.get('spec-id', '')
if spec_id:
q &= Q(spec_id=spec_id)
cpu_match = re.search("^[0-9]+", self.request.query_params.get('cpu', ''))
if cpu_match:
q &= Q(cpu=cpu_match.group())
memory_match = re.search("^[0-9]+", self.request.query_params.get('memory', ''))
if memory_match:
q &= Q(memory=memory_match.group())
return self.model.objects.filter(q)