diff --git a/Discussions/templates/board.html b/Discussions/templates/board.html index b4a85e5..89cd053 100644 --- a/Discussions/templates/board.html +++ b/Discussions/templates/board.html @@ -3,24 +3,26 @@ {{board.title}} - {{show.name}} Discussions - Episodes.Community {% endblock %} {% load markdown %} +{% load guardian_tags %} {% block content %} +{% get_obj_perms request.user for show as "show_perms" %}
-

{{board.title}}

+

{% if board.locked %}{% endif %}{% if board.pinned %}{% endif %}{{board.title}}

{% if board.locked %}

This board is locked

{% else %} {% if user.is_authenticated %} -  Reply +  Reply {% else %}

Log in to reply

{% endif %} @@ -40,15 +42,15 @@

Submitted {{board.timestamp}}

{{reply.body|markdown|safe}}
- +
-
+ {% csrf_token %}
-
+ {% csrf_token %} @@ -112,5 +114,20 @@
{% endif %} + {% if user.is_authenticated %} +

Board Tools

+ + {% endif %}
{% endblock %} diff --git a/Discussions/templates/board_reply.html b/Discussions/templates/board_reply.html index 1e49e52..83b689d 100644 --- a/Discussions/templates/board_reply.html +++ b/Discussions/templates/board_reply.html @@ -6,9 +6,9 @@
diff --git a/Discussions/templates/boards.html b/Discussions/templates/boards.html index 4ad937b..b900d1b 100644 --- a/Discussions/templates/boards.html +++ b/Discussions/templates/boards.html @@ -6,7 +6,7 @@
@@ -14,7 +14,7 @@

Discuss {{show.name}} with your fellow community members!

{% if user.is_authenticated %} -  Create New Board +  Create New Board {% else %}

Log in to create boards

{% endif %} @@ -27,7 +27,7 @@
-

{{board.title}}

+

{% if board.locked %}{% endif %}{% if board.pinned %}{% endif %}{{board.title}}

Submitted {{board.timestamp}} by {% if board.user.is_staff %} diff --git a/Discussions/templates/boards_new.html b/Discussions/templates/boards_new.html index 04bfbeb..f489a15 100644 --- a/Discussions/templates/boards_new.html +++ b/Discussions/templates/boards_new.html @@ -6,8 +6,8 @@
diff --git a/Discussions/templates/report_reply.html b/Discussions/templates/report_reply.html index 42dc6c4..1a863ed 100644 --- a/Discussions/templates/report_reply.html +++ b/Discussions/templates/report_reply.html @@ -7,8 +7,8 @@ diff --git a/Discussions/urls.py b/Discussions/urls.py index 1949177..f11fab9 100644 --- a/Discussions/urls.py +++ b/Discussions/urls.py @@ -23,6 +23,9 @@ urlpatterns = [ 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/pin/(?P\d{1,4})/?$', views.BoardPin), + url(r'^board/delete/(?P\d{1,4})/?$', views.BoardDelete), + url(r'^board/lock/(?P\d{1,4})/?$', views.BoardLock), 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 be30c8b..e523363 100644 --- a/Discussions/views.py +++ b/Discussions/views.py @@ -34,12 +34,18 @@ from . import forms import datetime import re +# Append common values to context +def get_show_url(abbr): + return '/show/%s' % (abbr) + class Boards(TemplateView): template_name = "boards.html" def get_context_data(self, abbr, **kwargs): ctx = super().get_context_data() + ctx['showurl'] = get_show_url(abbr) + show = get_object_or_404(Show, abbr=abbr) page = self.request.GET.get('page', 1) @@ -56,7 +62,7 @@ class Boards(TemplateView): then=Max('replies__timestamp') ) ), - ).order_by('-recency') + ).order_by('-pinned','-recency') paginator = Paginator(boards_list, getattr(settings, "DISCUSSIONS_PER_PAGE", 26)) try: @@ -77,6 +83,8 @@ class Board(TemplateView): def get_context_data(self, abbr, bid, **kwargs): ctx = super().get_context_data() + ctx['showurl'] = get_show_url(abbr) + show = get_object_or_404(Show, abbr=abbr) board = get_object_or_404(DiscussionBoard, pk=bid) @@ -104,7 +112,7 @@ class Board(TemplateView): found = DiscussionReply.objects.filter(timestamp__lt=item.timestamp,board=board).count() page = int(found / perpage) + 1 index = int(found % perpage) + 1 - ctx['url'] = '/show/%s/discuss/board/%d-%s?page=%d#reply-%d'%(abbr, board.pk, slugify(board.title), page, index) + ctx['url'] = ctx['showurl'] + '/discuss/board/%d-%s?page=%d#reply-%d'%(board.pk, slugify(board.title), page, index) return ctx @@ -139,7 +147,8 @@ def BoardForm(req, abbr): # Request context ctx = { 'form': form, - 'show': show + 'show': show, + 'showurl': get_show_url(abbr) } # Get bans for this user regarding this show @@ -175,7 +184,7 @@ def BoardForm(req, abbr): new_post = DiscussionReply(user=user,board=new_board,body=form_data['body']) new_post.save() - return HttpResponseRedirect('/show/%s/discuss/board/%d-%s'%(abbr, new_board.pk, slugify(form_data['title']))) + return HttpResponseRedirect(ctx['showurl'] + '/discuss/board/%d-%s'%(new_board.pk, slugify(form_data['title']))) else: ctx['error'] = 'Invalid fields!' @@ -194,7 +203,8 @@ def BoardReplyForm(req, abbr, bid): ctx = { 'form': form, 'board': board, - 'show': show + 'show': show, + 'showurl': get_show_url(abbr) } # Get bans for this user regarding this show @@ -233,7 +243,7 @@ def BoardReplyForm(req, abbr, bid): new_reply.board = board new_reply.save() - return HttpResponseRedirect('/show/%s/discuss/board/%d-%s?findReply=%d'%(abbr, board.pk, slugify(board.title), new_reply.pk)) + return HttpResponseRedirect(ctx['showurl'] + '/discuss/board/%d-%s?findReply=%d'%(board.pk, slugify(board.title), new_reply.pk)) else: ctx['error'] = 'Invalid fields!' @@ -247,6 +257,7 @@ class BoardVoteSubmit(LoginRequiredMixin, View): pos_bool = int(positive) == 1 user = req.user + showurl = get_show_url(abbr) # Get the reply from the database reply = get_object_or_404(DiscussionReply, id=replyid) @@ -254,8 +265,8 @@ class BoardVoteSubmit(LoginRequiredMixin, View): # Prevent voting for own reply if reply.user == user: return HttpResponse('

Error

You cannot vote for your own reply.

' - 'Return to board

' - % (abbr, reply.board.pk, slugify(reply.board.title)), status=400) + 'Return to board

' + % (showurl, reply.board.pk, slugify(reply.board.title)), status=400) show = reply.board.show @@ -281,7 +292,7 @@ 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)) + return HttpResponseRedirect('%s/discuss/board/%d-%s?findReply=%d'%(showurl, reply.board.pk, slugify(reply.board.title), reply.pk)) @login_required def ReportForm(req, abbr, rid): @@ -301,10 +312,11 @@ def ReportForm(req, abbr, rid): ctx = { 'form': form, 'show': show, - 'reply': reply + 'reply': reply, + 'showurl': get_show_url(abbr) } - url = '/show/%s/discuss/board/%d-%s?findReply=%d'%(abbr, reply.board.pk, slugify(reply.board.title), reply.pk) + url = '%s/discuss/board/%d-%s?findReply=%d'%(ctx['showurl'], reply.board.pk, slugify(reply.board.title), reply.pk) # Handle POST if req.method == 'POST': @@ -330,8 +342,43 @@ def ReportForm(req, abbr, rid): new_report.url = url new_report.save() - return HttpResponseRedirect('/show/%s/discuss/board/%d-%s'%(abbr, reply.board.pk, slugify(reply.board.title))) + return HttpResponseRedirect('%s/discuss/board/%d-%s'%(ctx['showurl'], reply.board.pk, slugify(reply.board.title))) else: ctx['error'] = 'Invalid fields!' return render(req, "report_reply.html", ctx) + +@login_required +def BoardLock(req, abbr, bid): + user = req.user + board = get_object_or_404(DiscussionBoard, pk=bid) + showurl = get_show_url(abbr) + + if not user.has_perm('LandingPage.can_moderate_board', board.show) and not board.user == user: + return HttpResponse('

Error

You do not have permission to lock this show.

' + 'Return to board

' + % (showurl, board.pk, slugify(board.title)), status=400) + + lock = not board.locked + DiscussionBoard.objects.filter(pk=board.pk).update(locked=lock) + return HttpResponseRedirect('%s/discuss/board/%d-%s'%(showurl, board.pk, slugify(board.title))) + + +@permission_required_or_403('LandingPage.can_moderate_board', (Show, 'abbr', 'abbr'), accept_global_perms=True) +def BoardPin(req, abbr, bid): + board = get_object_or_404(DiscussionBoard, pk=bid) + showurl = get_show_url(abbr) + + pin = not board.pinned + + DiscussionBoard.objects.filter(pk=board.pk).update(pinned=pin) + return HttpResponseRedirect('%s/discuss/board/%d-%s'%(showurl, board.pk, slugify(board.title))) + +@permission_required_or_403('LandingPage.can_moderate_board', (Show, 'abbr', 'abbr'), accept_global_perms=True) +def BoardDelete(req, abbr, bid): + board = get_object_or_404(DiscussionBoard, pk=bid) + showurl = get_show_url(abbr) + + DiscussionBoard.objects.filter(pk=board.pk).delete() + + return HttpResponseRedirect('%s/discuss' % (showurl))