Added limit parameter to VideoAPI search and improved documentation
This commit is contained in:
parent
bd28751013
commit
c4af1d1808
|
|
@ -1,7 +1,26 @@
|
||||||
# API
|
# VideoAPI
|
||||||
## VideoAPI
|
|
||||||
|
## Search
|
||||||
|
`GET \api?param=value`
|
||||||
|
|
||||||
|
### Parameters
|
||||||
|
| Param | Value |
|
||||||
|
|-|-|
|
||||||
|
| `q` | The search query string. |
|
||||||
|
| `limit` | A limit on the number of objects to be returned. Default is 6. |
|
||||||
|
|
||||||
|
### Response
|
||||||
|
```
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"name": "Video 1"
|
||||||
|
},
|
||||||
|
{...}
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
### Examples
|
||||||
|
> `GET /api?q=foo&limit=3` will return the first 3 videos with "foo" in their name.
|
||||||
|
|
||||||
|
|
||||||
| Method | URL | Action |
|
|
||||||
|-|-|-|
|
|
||||||
| `GET`| `/api` | Get all videos |
|
|
||||||
| `GET`| `/api?q=<query>` | Get all videos containing `<query>` |
|
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,11 @@ from core.serializers import VideoSerializer
|
||||||
@api_view(['GET'])
|
@api_view(['GET'])
|
||||||
def videoAPI(request):
|
def videoAPI(request):
|
||||||
q = request.GET.get('q', '')
|
q = request.GET.get('q', '')
|
||||||
videos = Video.objects.filter(Q(id__contains=q) | Q(name__contains=q))
|
l = request.GET.get('limit', '')
|
||||||
|
try:
|
||||||
|
l = int(l)
|
||||||
|
videos = Video.objects.filter(Q(id__contains=q) | Q(name__contains=q))[:l]
|
||||||
|
except:
|
||||||
|
videos = Video.objects.filter(Q(id__contains=q) | Q(name__contains=q))
|
||||||
serializer = VideoSerializer(videos, many=True)
|
serializer = VideoSerializer(videos, many=True)
|
||||||
return Response(serializer.data)
|
return Response(serializer.data)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user