From e2069c33ee28067cc0eddb93436dea989fd7c2bc Mon Sep 17 00:00:00 2001 From: Ihsan Ullah Date: Wed, 5 Feb 2025 09:52:45 +0500 Subject: [PATCH 1/3] forum restrictions added --- src/apps/forums/views.py | 59 ++++++++++++++++-- src/templates/forums/base_forum.html | 89 ++++++++++++++++------------ 2 files changed, 106 insertions(+), 42 deletions(-) diff --git a/src/apps/forums/views.py b/src/apps/forums/views.py index 39f80ce3b..0d3a9a5fd 100644 --- a/src/apps/forums/views.py +++ b/src/apps/forums/views.py @@ -1,16 +1,18 @@ import datetime +from django.contrib import messages from django.contrib.auth import get_user_model from django.contrib.auth.decorators import login_required from django.contrib.auth.mixins import LoginRequiredMixin from django.core.exceptions import PermissionDenied from django.http import HttpResponseRedirect, Http404 -from django.shortcuts import get_object_or_404 +from django.shortcuts import get_object_or_404, redirect from django.utils.timezone import now from django.views.generic import DetailView, CreateView, DeleteView from .forms import PostForm, ThreadForm from .models import Forum, Thread, Post +from competitions.models import CompetitionParticipant User = get_user_model() @@ -45,9 +47,31 @@ class ForumDetailView(DetailView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) - context['thread_list_sorted'] = self.object.threads.order_by('pinned_date', '-date_created')\ - .select_related('forum', 'forum__competition', 'forum__competition__created_by', 'started_by')\ - .prefetch_related('forum__competition__collaborators', 'posts') + + user = self.request.user + forum = self.object + + # Default value (user is not a participant if not authenticated) + is_participant = False + + # Check if the user is a participant of this forum's competition + if user.is_authenticated: + is_participant = CompetitionParticipant.objects.filter( + competition=forum.competition, + user=user, + status=CompetitionParticipant.APPROVED + ).exists() + + context['is_participant'] = is_participant + + context['thread_list_sorted'] = self.object.threads.order_by( + 'pinned_date', '-date_created' + ).select_related( + 'forum', 'forum__competition', 'forum__competition__created_by', 'started_by' + ).prefetch_related( + 'forum__competition__collaborators', 'posts' + ) + return context @@ -66,6 +90,19 @@ class CreatePostView(ForumBaseMixin, RedirectToThreadMixin, LoginRequiredMixin, form_class = PostForm def form_valid(self, form): + + # Check if the user is a participant of the competition forum + is_participant = CompetitionParticipant.objects.filter( + competition=self.forum.competition, + user=self.request.user, + status=CompetitionParticipant.APPROVED + ).exists() + + if not is_participant: + messages.error(self.request, "You must be a participant of this competition to create a post.") + return redirect("forums:forum_thread_detail", forum_pk=self.forum.pk, thread_pk=self.thread.pk) + + # Create the post since the user is a participant self.post = form.save(commit=False) self.post.thread = self.thread self.post.posted_by = self.request.user @@ -106,6 +143,20 @@ class CreateThreadView(ForumBaseMixin, RedirectToThreadMixin, LoginRequiredMixin form_class = ThreadForm def form_valid(self, form): + + # Check if the user is a participant of the competition forum + is_participant = CompetitionParticipant.objects.filter( + competition=self.forum.competition, + user=self.request.user, + status=CompetitionParticipant.APPROVED + ).exists() + + if not is_participant: + messages.error(self.request, "You must be a participant of this competition to create a thread.") + return redirect("forums:forum_detail", forum_pk=self.forum.pk) + + # Create the thread since the user is a participant + self.thread = form.save(commit=False) self.thread = form.save(commit=False) self.thread.forum = self.forum self.thread.started_by = self.request.user diff --git a/src/templates/forums/base_forum.html b/src/templates/forums/base_forum.html index 779f533d6..209443643 100644 --- a/src/templates/forums/base_forum.html +++ b/src/templates/forums/base_forum.html @@ -9,50 +9,63 @@ {% block content %}
-
-
-

{{ forum.competition.title }} Forum

- - - - {% if thread or 'new_thread' in request.path %} - - - - {% endif %} + {% if user.is_authenticated %} + {% if is_participant %} +
+
+

{{ forum.competition.title }} Forum

+ + + + {% if thread or 'new_thread' in request.path %} + + + + {% endif %} - {% if not thread %} - - - - {% endif %} -
-
+ {% if not thread %} + + + + {% endif %} +
+
- {% if forum.competition.contact_email %} -
-
- Contact Email: - {{ forum.competition.contact_email }} + {% if forum.competition.contact_email %} +
+
+ Contact Email: + {{ forum.competition.contact_email }} -
-
- {% endif %} +
+
+ {% endif %} -
-
-
- {% block forum_content %} - {% endblock forum_content %} +
+
+
+ {% block forum_content %} + {% endblock forum_content %} +
+
+ {% else %} +
+

To participate in the forum, send a registration request to the competition.

-
+ {% endif %} + {% else %} +
+ Log In or + Sign Up to view this competition forum. +
+ {% endif %}