Skip to content

Commit 60da4fd

Browse files
committed
Update permissions and Swagger documentation
Fix #174 Changes to be committed: modified: api/views.py modified: search/apis.py modified: search/selectors.py
1 parent 50c6f15 commit 60da4fd

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

api/views.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,10 @@ class ApiObjectsSearch(APIView):
10531053
10541054
Shell
10551055
```shell
1056-
curl -X POST "http://localhost:8000/api/objects/search/" -H "accept: application/json" -H "Authorization: Token ${token}" -H "Content-Type: application/json" -d "{\"POST_api_objects_search\":[{\"type\": \"prefix\",\"search\": \"TEST\"}]}"
1056+
curl -X POST "http://localhost:8000/api/objects/search/" -H "accept:
1057+
application/json" -H "Authorization: Token ${token}" -H "Content-Type:
1058+
application/json" -d "{\"POST_api_objects_search\":
1059+
[{\"type\": \"prefix\",\"search\": \"TEST\"}]}"
10571060
```
10581061
10591062
JavaScript

search/apis.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@ class SearchObjectsAPI(APIView):
1818
-------------------
1919
2020
Endpoint for use of query string based search.
21+
Four parameters are defined by this API:
22+
1. contents: Search in the contents of the BCO
23+
2. prefix: BCO Prefix to search
24+
3. owner_user: Search by BCO owner
25+
4. object_id: BCO object_id to search for
26+
27+
Shell
28+
```shell
29+
curl -X GET "http://localhost:8000/api/objects/?contents=review&prefix=BCO&owner_user=bco_api_user&object_id=DRAFT" -H "accept: application/json"
30+
```
2131
"""
22-
23-
#TODO: multiple values in the URL will only return the last one.
24-
25-
authentication_classes = [CustomJSONWebTokenAuthentication]
26-
permission_classes = [AllowAny,]
2732

2833
auth = openapi.Parameter('test', openapi.IN_QUERY, description="test manual param", type=openapi.TYPE_BOOLEAN)
2934

@@ -39,12 +44,12 @@ class SearchObjectsAPI(APIView):
3944
description="BCO Prefix to search",
4045
type=openapi.TYPE_STRING
4146
),
42-
openapi.Parameter('owner',
47+
openapi.Parameter('owner_user',
4348
openapi.IN_QUERY,
4449
description="Search by BCO owner",
4550
type=openapi.TYPE_STRING
4651
),
47-
openapi.Parameter('bco_id',
52+
openapi.Parameter('object_id',
4853
openapi.IN_QUERY,
4954
description="BCO object_id to search for",
5055
type=openapi.TYPE_STRING
@@ -69,12 +74,15 @@ def get(self, request) -> Response:
6974
"state",
7075
]
7176

72-
search = self.request.GET
73-
print(request.user.username)
77+
search = dict(self.request.GET)
7478
result = controled_list(request.user)
7579
for query, value in search.items():
76-
filter = f'{query}__icontains'
77-
result = search_db(filter, value, result)
80+
for item in value:
81+
if query == 'owner_user':
82+
filter = f'{query}'
83+
else:
84+
filter = f'{query}__icontains'
85+
result = search_db(filter, item, result)
7886
search_result = chain(result.values(*return_values))
7987
return Response(status=status.HTTP_200_OK, data={search_result})
8088

search/selectors.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
# search/selectors.py
2+
3+
"""Search Selectors
4+
Set of selector functions to handle searching the BCODB
5+
"""
6+
27
from api.models import BCO
38
from django.db.models import QuerySet
49
from django.db.models.query import QuerySet
510
from django.contrib.auth.models import User
611
from guardian.shortcuts import get_objects_for_user
712
from itertools import chain
813
from api.scripts.utilities.UserUtils import UserUtils
14+
915
return_values = [
1016
"contents",
1117
"last_update",
@@ -20,22 +26,30 @@
2026

2127
def search_db(filter:str, value:str, result:QuerySet)-> QuerySet:
2228
"""Search DB
23-
Search the BCODB
29+
Takes a filter, a value, and a result query set and uses them to return
30+
a more refined query set.
2431
"""
32+
2533
new_result = result.filter(**{filter: value})
34+
print(len(result), ': ', len(new_result))
2635
return new_result
2736

28-
def controled_list(user: User):
37+
def controled_list(user: User) -> QuerySet:
38+
"""User Controlled List
39+
Takes a User object and returns a list of accessable BCOs based on their
40+
permissions.
41+
"""
42+
2943
prefix_list = []
3044
results_list = BCO.objects.none()
3145
raw_prefixes = UserUtils().prefix_perms_for_user(user_object=user)
3246
for prefix in raw_prefixes :
3347
pre = prefix.split("_")[1]
3448
if pre not in prefix_list and pre is not "prefix":
3549
prefix_list.append(pre)
36-
50+
3751
for prefix in prefix_list:
38-
if user.username == "AnonymousUser":
52+
if user.username == "AnonymousUser" or user.username == "":
3953
bco_list = BCO.objects.filter(prefix=prefix).values().exclude(state="DELETE").exclude(state="DRAFT")
4054
else:
4155
bco_list = BCO.objects.filter(prefix=prefix).values().exclude(state="DELETE")

0 commit comments

Comments
 (0)