From 71c40cb403783e97f1057e2fb3b5383fb3419bf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=AB=20Marschner?= Date: Sun, 15 Dec 2019 23:32:59 -0500 Subject: [PATCH] staff team attribute updated correctly --- backend/api/serializers.py | 2 +- backend/api/views.py | 47 ++++++++++++++------------------------ 2 files changed, 18 insertions(+), 31 deletions(-) diff --git a/backend/api/serializers.py b/backend/api/serializers.py index 6b93167d..bed06ecf 100644 --- a/backend/api/serializers.py +++ b/backend/api/serializers.py @@ -39,7 +39,7 @@ class TeamSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Team fields = ('url', 'id', 'league', 'name', 'avatar', 'users', 'wins', 'losses', 'draws', - 'bio', 'divisions', 'auto_accept_ranked', 'auto_accept_unranked', 'mu', 'sigma') + 'bio', 'divisions', 'auto_accept_ranked', 'auto_accept_unranked', 'mu', 'sigma', 'staff_team') read_only_fields = ('id',) def update(self, instance, validated_data): diff --git a/backend/api/views.py b/backend/api/views.py index 0a54286e..69d67df5 100644 --- a/backend/api/views.py +++ b/backend/api/views.py @@ -263,6 +263,14 @@ def create(self, request, league_id): team['name'] = request.data.get('name', None) team['users'] = [request.user.username] + try: + userObj = User.objects.get(username=request.user.username) + except User.DoesNotExist: + return Response({'message': 'Logged in user not found a valid user'}, status.HTTP_404_NOT_FOUND) + + if userObj.is_staff: + team['staff_team'] = True + serializer = self.get_serializer(data=team) if serializer.is_valid(): serializer.save() @@ -357,6 +365,15 @@ def join(self, request, league_id, pk=None): return Response({'message': 'Invalid team key'}, status.HTTP_400_BAD_REQUEST) if team.users.count() == 4: return Response({'message': 'Team has max number of users'}, status.HTTP_400_BAD_REQUEST) + + try: + userObj = User.objects.get(username=request.user.username) + except User.DoesNotExist: + return Response({'message': 'Logged in user not found a valid user'}, status.HTTP_404_NOT_FOUND) + + if userObj.is_staff: + team.staff_team = True + team.users.add(request.user.id) team.save() @@ -380,8 +397,6 @@ def leave(self, request, league_id, pk=None): serializer = self.get_serializer(team) return Response(serializer.data, status.HTTP_200_OK) - - class SubmissionViewSet(viewsets.GenericViewSet, mixins.CreateModelMixin, mixins.RetrieveModelMixin): @@ -458,34 +473,6 @@ def retrieve_file(self, request, team, league_id, pk=None): download_url = GCloudUploadDownload.signed_download_url(SUBMISSION_FILENAME(pk), GCLOUD_SUB_BUCKET) return Response({'download_url': download_url}, status.HTTP_200_OK) - - def signed_url(self, submission_id): - """ - returns a pre-signed url for uploading the submission with given id to google cloud - this URL can be used with a PUT request to upload data; no authentication needed. - """ - with tempfile.NamedTemporaryFile() as temp: - temp.write(settings.GOOGLE_APPLICATION_CREDENTIALS.encode('utf-8')) - temp.flush() - storage_client = storage.Client.from_service_account_json(temp.name) - bucket = storage_client.get_bucket(GCLOUD_BUCKET) - blob = bucket.blob(SUBMISSION_FILENAME(submission_id)) - return blob.create_resumable_upload_session() - - def signed_download_url(self, submisison_id): - """ - returns a pre-signed url for downloading the zip of the submission from - google cloud, this URL can be used with a GET request to dowload the file - with no additional authentication needed. - """ - with tempfile.NamedTemporaryFile() as temp: - temp.write(settings.GOOGLE_APPLICATION_CREDENTIALS.encode('utf-8')) - temp.flush() - storage_client = storage.Client.from_service_account_json(temp.name) - bucket = storage_client.get_bucket(GCLOUD_BUCKET) - blob = bucket.blob(SUBMISSION_FILENAME(submission_id)) - - @action(methods=['patch'], detail=True) def compilation_update(self, request, team, league_id, pk=None): is_admin = User.objects.all().get(username=request.user).is_superuser