This repository has been archived on 2021-03-10. You can view files and clone it, but cannot push or open issues or pull requests.
perktree/backend/perks/views.py

41 lines
1015 B
Python

import json
from os import listdir
from os.path import isfile, join
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import authentication, permissions
PERKS_DIR = '../static/perks'
def get_tree_list():
return sorted([file for file in listdir(PERKS_DIR) if isfile(join(PERKS_DIR, file))])
class ListTrees(APIView):
"""
View to list all perk trees
"""
authentication_classes = (authentication.TokenAuthentication,)
# permission_classes = (permissions.IsAuthenticated,)
def get(self, request, format=None):
return Response([tree[:-5] for tree in get_tree_list()])
class ListPerks(APIView):
"""
View to list all perks in a tree
"""
authentication_classes = (authentication.TokenAuthentication,)
# permission_classes = (permissions.IsAuthenticated,)
def get(self, request, tree, format=None):
print(request.user)
filename = get_tree_list()[tree]
with open(f'{PERKS_DIR}/{filename}', 'r') as f:
return Response(json.load(f))