Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions src/apps/forums/views.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -26,16 +28,31 @@ def dispatch(self, *args, **kwargs):
self.forum = get_object_or_404(Forum, pk=self.kwargs['forum_pk'])
if 'thread_pk' in self.kwargs:
self.thread = get_object_or_404(Thread, pk=self.kwargs['thread_pk'])

# Determine if the user is a participant and store it as an instance variable
self.is_participant = self.is_user_participant(self.request.user, self.forum)

return super().dispatch(*args, **kwargs)

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['forum'] = self.forum
context['thread'] = self.thread if hasattr(self, 'thread') else None
context['is_participant'] = self.is_participant
return context

def is_user_participant(self, user, forum):
is_participant = False
if user.is_authenticated:
is_participant = CompetitionParticipant.objects.filter(
competition=forum.competition,
user=user,
status=CompetitionParticipant.APPROVED
).exists()
return is_participant


class ForumDetailView(DetailView):
class ForumDetailView(ForumBaseMixin, DetailView):
"""
Shows the details of a particular Forum.
"""
Expand All @@ -45,9 +62,15 @@ 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')

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


Expand All @@ -66,6 +89,12 @@ class CreatePostView(ForumBaseMixin, RedirectToThreadMixin, LoginRequiredMixin,
form_class = PostForm

def form_valid(self, form):

if not self.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
Expand Down Expand Up @@ -106,6 +135,13 @@ class CreateThreadView(ForumBaseMixin, RedirectToThreadMixin, LoginRequiredMixin
form_class = ThreadForm

def form_valid(self, form):

if not self.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
Expand Down
107 changes: 70 additions & 37 deletions src/templates/forums/base_forum.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,83 @@

{% block content %}
<div class="ui container">
<div class="row">
<div class="col-lg-12">
<h1>{{ forum.competition.title }} Forum</h1>
<a href="{% url 'competitions:detail' pk=forum.competition.pk %}">
<button class="ui button">
Go back to competition
</button>
</a>
{% if thread or 'new_thread' in request.path %}
<a href="{% url 'forums:forum_detail' forum_pk=forum.pk %}">
<button class="ui button">
Back to thread list
</button>
</a>
{% if user.is_authenticated %}
{% if is_participant %}
<div class="row">
<div class="col-lg-12">
<h1>{{ forum.competition.title }} Forum</h1>
<a href="{% url 'competitions:detail' pk=forum.competition.pk %}">
<button class="ui button">
Go back to competition
</button>
</a>
{% if thread or 'new_thread' in request.path %}
<a href="{% url 'forums:forum_detail' forum_pk=forum.pk %}">
<button class="ui button">
Back to thread list
</button>
</a>
{% endif %}

{% if not thread %}
<a href="{% url 'forums:forum_new_thread' forum_pk=forum.pk %}">
<button class="ui button primary">
Start a new topic
</button>
</a>
{% endif %}
</div>
</div>

{% if forum.competition.contact_email %}
<div class="row">
<div class="forum-contact">
<span>Contact Email: </span>
<span class="forum-contact-email">{{ forum.competition.contact_email }}</span>

</div>
</div>
{% endif %}

{% if not thread %}
<a href="{% url 'forums:forum_new_thread' forum_pk=forum.pk %}">
<button class="ui button primary">
Start a new topic
<div class="row">
<div class="col-lg-12">
<div class="panel forum-panel">
{% block forum_content %}
{% endblock forum_content %}
</div>
</div>
</div>
{% else %}
<div class="row">
<div class="col-lg-12">
<h1>{{ forum.competition.title }} Forum</h1>
<a href="{% url 'competitions:detail' pk=forum.competition.pk %}">
<button class="ui button">
Go back to competition
</button>
</a>
</div>
</div>
<div class="ui yellow message">
<p>To participate in the forum, send a registration request to the competition.</p>
</div>
{% endif %}
{% else %}
<div class="row">
<div class="col-lg-12">
<h1>{{ forum.competition.title }} Forum</h1>
<a href="{% url 'competitions:detail' pk=forum.competition.pk %}">
<button class="ui button">
Go back to competition
</button>
</a>
{% endif %}
</div>
</div>
</div>

{% if forum.competition.contact_email %}
<div class="row">
<div class="forum-contact">
<span>Contact Email: </span>
<span class="forum-contact-email">{{ forum.competition.contact_email }}</span>

<div class="ui yellow message">
<a href="{% url 'accounts:login' %}">Log In</a> or
<a href="{% url 'accounts:signup' %}" target="_blank">Sign Up</a> to view this competition forum.
</div>
</div>
{% endif %}

<div class="row">
<div class="col-lg-12">
<div class="panel forum-panel">
{% block forum_content %}
{% endblock forum_content %}
</div>
</div>
</div>
</div>
<style>
.ui.horizontal.divider {
Expand Down