diff --git a/Discussions/forms.py b/Discussions/forms.py index b66a9dd..237908b 100644 --- a/Discussions/forms.py +++ b/Discussions/forms.py @@ -15,7 +15,7 @@ # along with this program. If not, see . from django import forms -from LandingPage.models import DiscussionBoard, DiscussionReply +from LandingPage.models import DiscussionBoard, DiscussionReply, Report class BoardForm(forms.ModelForm): body = forms.CharField(widget=forms.Textarea) @@ -35,3 +35,8 @@ class ReplyForm(forms.ModelForm): help_texts = { 'body': 'Enter your message here' } + +class ReportForm(forms.ModelForm): + class Meta(): + model = Report + fields = ('title','details',) diff --git a/Discussions/templates/board.html b/Discussions/templates/board.html index 7edb6d4..b4a85e5 100644 --- a/Discussions/templates/board.html +++ b/Discussions/templates/board.html @@ -40,6 +40,7 @@

Submitted {{board.timestamp}}

{{reply.body|markdown|safe}}
+
{% csrf_token %} diff --git a/Discussions/templates/report_reply.html b/Discussions/templates/report_reply.html new file mode 100644 index 0000000..42dc6c4 --- /dev/null +++ b/Discussions/templates/report_reply.html @@ -0,0 +1,36 @@ +{% extends "base.html" %} +{% block title %} + Report a Post - {{reply.board.title}} - {{show.name}} Discussions - Episodes.Community +{% endblock %} +{% block content %} +
+ +

Report a Post

+ {% if error %} +
{{error}}
+ {% endif %} + +
+
Posted by {{ reply.user.display_name }}
+
{{ reply.body }}
+ {% if reply.user.is_staff %} +
+ Warning +

This reply is made by a staff member. Unnecessary reporters will be banned.

+
+ {% endif %} +
+ {% include "form.html" %} +
+ +
+ +
+{% endblock %} diff --git a/Discussions/urls.py b/Discussions/urls.py index c138c66..1949177 100644 --- a/Discussions/urls.py +++ b/Discussions/urls.py @@ -22,6 +22,7 @@ urlpatterns = [ url(r'^$', views.Boards.as_view()), url(r'^vote/(?P\d+)/(?P[0-1])/?$', views.BoardVoteSubmit.as_view()), url(r'^board/new$', views.BoardForm), + url(r'^board/report/(?P\d{1,4})/?$', views.ReportForm), url(r'^board/reply/(?P\d{1,4})/?$', views.BoardReplyForm), url(r'^board/(?P\d{1,4})(-[\w-]+)?/?$', views.Board.as_view()), ] diff --git a/Discussions/views.py b/Discussions/views.py index f15f9b8..be30c8b 100644 --- a/Discussions/views.py +++ b/Discussions/views.py @@ -28,7 +28,7 @@ from django.utils.text import slugify from guardian.decorators import permission_required_or_403 -from LandingPage.models import Show, DiscussionBoard, DiscussionReply, DiscussionVote, Ban +from LandingPage.models import Show, DiscussionBoard, DiscussionReply, DiscussionVote, Ban, Report from . import forms import datetime @@ -282,3 +282,56 @@ class BoardVoteSubmit(LoginRequiredMixin, View): new_vote.save() return HttpResponseRedirect('/show/%s/discuss/board/%d-%s?findReply=%d'%(abbr, reply.board.pk, slugify(reply.board.title), reply.pk)) + +@login_required +def ReportForm(req, abbr, rid): + show = get_object_or_404(Show, abbr=abbr) + reply = get_object_or_404(DiscussionReply, pk=rid,board__show=show) + user = req.user + + form = forms.ReportForm() + + # Get bans for this user regarding this show + bans = Ban.objects.filter(Q(expiration__gte=datetime.datetime.now()) | Q(permanent=True), user=user, site_wide=True) + + if bans.count() > 0: + return HttpResponseForbidden('You are banned from the site and therefor not allowed to create reports.
Reason: %s'%(bans.first().reason)) + + # Request context + ctx = { + 'form': form, + 'show': show, + 'reply': reply + } + + url = '/show/%s/discuss/board/%d-%s?findReply=%d'%(abbr, reply.board.pk, slugify(reply.board.title), reply.pk) + + # Handle POST + if req.method == 'POST': + form = forms.ReportForm(req.POST) + ctx['form'] = form + + if form.is_valid(): + form_data = form.cleaned_data + + if not user.has_perm('LandingPage.can_moderate_board', show): + # Check if there have been many reports by this user within the last 12 hours + if Report.objects.filter(user=user,timestamp__gte=datetime.datetime.now() - datetime.timedelta(hours=12)).count() > 5: + ctx['error'] = 'You\'ve created too many reports recently!' + return render(req, "report_reply.html", ctx) + + if Report.objects.filter(url=url).count() > 1: + ctx['error'] = 'This reply has already been brought to our attention! Thank you for reporting.' + return render(req, "report_reply.html", ctx) + + # Save + new_report = form.save(commit=False) + new_report.reporter = user + new_report.url = url + new_report.save() + + return HttpResponseRedirect('/show/%s/discuss/board/%d-%s'%(abbr, reply.board.pk, slugify(reply.board.title))) + else: + ctx['error'] = 'Invalid fields!' + + return render(req, "report_reply.html", ctx) diff --git a/LandingPage/models.py b/LandingPage/models.py index 8d06e9c..1daf6f6 100644 --- a/LandingPage/models.py +++ b/LandingPage/models.py @@ -449,4 +449,4 @@ class DiscussionVote(TimestampedModel): help_text='If true, the vote is an upvote. Otherwise, it is a downvote. Neutral votes are not recorded' ) def __str__(self): - return "%s %s %s"%(self.user, '\U0001f592' if self.positive else '\U0001f44e', self.board.title) + return "%s %s reply %d"%(self.user, '\U0001f592' if self.positive else '\U0001f44e', self.reply.pk) diff --git a/Show/views.py b/Show/views.py index cbff335..052b558 100644 --- a/Show/views.py +++ b/Show/views.py @@ -26,7 +26,7 @@ from django.contrib.auth.mixins import LoginRequiredMixin from guardian.decorators import permission_required_or_403 -from LandingPage.models import User, Show, Season, Episode, Submission, SubmissionVote, Ban +from LandingPage.models import User, Show, Season, Episode, Submission, SubmissionVote, Ban, Report from . import forms @@ -390,6 +390,8 @@ def ReportSubmission(req, abbr, submission): 'submission': submission } + url = '/show/%s/episode/%d/%d?submission=%s'%(abbr, episode.season.number, episode.episode,submission.pk) + # Handle POST if req.method == 'POST': form = forms.ReportForm(req.POST) @@ -398,10 +400,20 @@ def ReportSubmission(req, abbr, submission): if form.is_valid(): form_data = form.cleaned_data + if not user.has_perm('LandingPage.can_moderate_show', show): + # Check if there have been many reports by this user within the last 12 hours + if Report.objects.filter(user=user,timestamp__gte=datetime.datetime.now() - datetime.timedelta(hours=12)).count() > 5: + ctx['error'] = 'You\'ve created too many reports recently!' + return render(req, "report_reply.html", ctx) + + if Report.objects.filter(url=url).count() > 1: + ctx['error'] = 'This submission has already been brought to our attention! Thank you for reporting.' + return render(req, "report_reply.html", ctx) + # Save new_report = form.save(commit=False) new_report.reporter = user - new_report.url = '/show/%s/episode/%d/%d?submission=%s'%(abbr, episode.season.number, episode.episode,submission.pk) + new_report.url = url new_report.save() return HttpResponseRedirect('/show/%s/episode/%d/%d'%(abbr, episode.season.number, episode.episode))