diff --git a/src/apps/api/views/competitions.py b/src/apps/api/views/competitions.py index 4dc0e4b30..206eedccf 100644 --- a/src/apps/api/views/competitions.py +++ b/src/apps/api/views/competitions.py @@ -508,9 +508,26 @@ def perform_update(self, serializer): class PhaseViewSet(ModelViewSet): - queryset = Phase.objects.all() serializer_class = PhaseSerializer + def get_queryset(self): + qs = Phase.objects.all() + return qs + + def list(self, request, *args, **kwargs): + # Check if it's a direct request to /api/phases/ + # i.e without a pk + direct_request = 'pk' not in kwargs or kwargs['pk'] == 'list' + + if direct_request: + # return empty response in direct request + return Response([], status=status.HTTP_200_OK) + + # Otherwise, allow other functions to use the list functionality as usual + queryset = self.get_queryset() + serializer = self.get_serializer(queryset, many=True) + return Response(serializer.data) + # TODO! Security, who can access/delete/etc this? @action(detail=True, methods=('POST',), url_name='manually_migrate') @@ -619,7 +636,6 @@ def get_leaderboard(self, request, pk): # put detailed results in its submission for k, v in submissions_keys.items(): response['submissions'][v]['detailed_results'] = submission_detailed_results[k] - print(f"\n{response['submissions']}\n") for task in query['tasks']: # This can be used to rendered variable columns on each task @@ -643,12 +659,41 @@ class CompetitionParticipantViewSet(ModelViewSet): search_fields = ('user__username', 'user__email',) def get_queryset(self): - qs = super().get_queryset() - user = self.request.user - if not user.is_superuser: - qs = qs.filter(competition__in=user.competitions.all() | user.collaborations.all()) - qs = qs.select_related('user').order_by('user__username') - return qs + + # a boolean set to true if the request is considered valid + # i.e. it is either GET request with `competition`` + # or patch request with `status` + # or post request with `message` + is_valid_request = False + + if self.request.method == "PATCH": + # PATCH request is considered valid if it has `status` + if 'status' in self.request.data: + is_valid_request = True + + if self.request.method == "POST": + # POST request is considered valid if it has `message` + if 'message' in self.request.data: + is_valid_request = True + + if self.request.method == "GET": + # GET request is considered valid if it has `competition`` + # if there is no competition then it si called from /api/participants/ + # URL which is not considered valid + if 'competition' in self.request.GET: + is_valid_request = True + + if is_valid_request: + # API to act normally i.e return participants + qs = super().get_queryset() + user = self.request.user + if not user.is_superuser: + qs = qs.filter(competition__in=user.competitions.all() | user.collaborations.all()) + qs = qs.select_related('user').order_by('user__username') + return qs + else: + # API will work but will return empty participants list + return CompetitionParticipant.objects.none() def update(self, request, *args, **kwargs): if request.method == 'PATCH': diff --git a/src/apps/forums/views.py b/src/apps/forums/views.py index 9bc13dc78..6525255f3 100644 --- a/src/apps/forums/views.py +++ b/src/apps/forums/views.py @@ -85,7 +85,6 @@ def delete(self, request, *args, **kwargs): self.object = self.get_object() if self.object.thread.forum.competition.created_by == request.user or \ - request.user in self.object.thread.forum.competition.admins.all() or \ self.object.posted_by == request.user: # If there are more posts in the thread, leave it around, otherwise delete it @@ -129,7 +128,6 @@ def delete(self, request, *args, **kwargs): self.object = self.get_object() if self.object.forum.competition.created_by == request.user or \ - request.user in self.object.forum.competition.admins.all() or \ self.object.started_by == request.user: success_url = self.object.forum.get_absolute_url() @@ -161,7 +159,7 @@ def pin_thread(request, thread_pk): except Thread.DoesNotExist: raise Http404() - if thread.forum.competition.created_by == request.user or request.user in thread.forum.competition.admins.all(): + if thread.forum.competition.created_by == request.user or thread.started_by == request.user: # Toggle pinned date on/off thread.pinned_date = now() if thread.pinned_date is None else None thread.save() diff --git a/src/apps/profiles/backends.py b/src/apps/profiles/backends.py index dd5cdd502..082354685 100644 --- a/src/apps/profiles/backends.py +++ b/src/apps/profiles/backends.py @@ -10,7 +10,10 @@ def authenticate(self, request, username=None, password=None, **kwargs): try: user = User.objects.get(email=username) if user.check_password(password): - return user + if user.is_active: + return user + else: + return None else: return None except ObjectDoesNotExist: diff --git a/src/apps/profiles/models.py b/src/apps/profiles/models.py index a6b92e944..7c18e21bf 100644 --- a/src/apps/profiles/models.py +++ b/src/apps/profiles/models.py @@ -89,7 +89,7 @@ def get_full_name(self): return self.name def __str__(self): - return f'{self.username} | {self.email}' + return self.username @property def slug_url(self): diff --git a/src/static/css/forums.css b/src/static/css/forums.css index 6e7651998..40eccedb5 100644 --- a/src/static/css/forums.css +++ b/src/static/css/forums.css @@ -15,16 +15,6 @@ .forum-panel .thread_title { border-bottom: 1px solid #CCC; } - -.pin-button, .remove-button { - opacity: 0.5; - color: darkred; -} -.pin-button, .remove-button:hover { - opacity: 1; - cursor: pointer; -} - .pinned-thread-icon { opacity: 0.5; color: darkred; diff --git a/src/static/riot/competitions/detail/_tabs.tag b/src/static/riot/competitions/detail/_tabs.tag index 57ceacb71..09647588c 100644 --- a/src/static/riot/competitions/detail/_tabs.tag +++ b/src/static/riot/competitions/detail/_tabs.tag @@ -69,7 +69,7 @@
| Posts | - {% if forum.competition.creator == request.user %} -- - | - {% endif %} ++ Actions + | {% for thread in thread_list_sorted %} -|||
|---|---|---|---|---|---|
| {% if thread.pinned_date %} - + {% endif %} - {{ thread.title }} + + {{ thread.title }} | {{ thread.started_by }} | {{ thread.date_created|date:"M d, Y" }} | {{ thread.last_post_date|timesince }} | {# date:"g:iA M d" #}{{ thread.posts.count }} | - {% if forum.competition.creator == request.user or request.user in forum.competition.admins.all %} + {% if thread.started_by == request.user %}- - + + {% if not thread.pinned_date %} + + {% endif %} | {% endif %}