From e59c03539de04b44e3986b18298b08233586f193 Mon Sep 17 00:00:00 2001 From: Evert Date: Fri, 2 Mar 2018 15:46:25 +0200 Subject: [PATCH] Add better show url formation method --- Discussions/templates/board.html | 30 ++++++++--------- Discussions/templates/board_reply.html | 6 ++-- Discussions/templates/boards.html | 6 ++-- Discussions/templates/boards_new.html | 4 +-- Discussions/templates/report_reply.html | 6 ++-- Discussions/views.py | 35 ++++++++------------ EpisodesCommunity/settings.py | 4 +-- LandingPage/models.py | 12 ++++++- LandingPage/templates/landing_page.html | 2 +- LandingPage/templates/shows.html | 2 +- LandingPage/views.py | 12 +------ Show/templates/create_ban.html | 2 +- Show/templates/episode.html | 20 ++++++------ Show/templates/episode_add.html | 2 +- Show/templates/report.html | 4 +-- Show/templates/season_add.html | 2 +- Show/templates/show.html | 4 +-- Show/templates/submit.html | 4 +-- Show/templates/submit_mod.html | 4 +-- Show/views.py | 43 ++++++++++--------------- options_example.ini | 4 +++ 21 files changed, 97 insertions(+), 111 deletions(-) diff --git a/Discussions/templates/board.html b/Discussions/templates/board.html index 53eebf4..bf032e5 100644 --- a/Discussions/templates/board.html +++ b/Discussions/templates/board.html @@ -9,8 +9,8 @@
@@ -22,7 +22,7 @@

This board is locked

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

Log in to reply

{% endif %} @@ -38,8 +38,8 @@ ID: {{reply.pk}} {% if "can_moderate_board" in show_perms or board.user == user %}
{{reply.body}}
- Restore - Ban + Restore + Ban {% endif %}
{% else %} @@ -54,20 +54,20 @@
{% if user.is_authenticated %} {% if "can_moderate_board" in show_perms or board.user == user %} - Delete Content - Ban + Delete Content + Ban {% else %} - + {% endif %} {% endif %}
-
+ {% csrf_token %}
-
+ {% csrf_token %} @@ -136,14 +136,14 @@

Board Tools

{% endif %} diff --git a/Discussions/templates/board_reply.html b/Discussions/templates/board_reply.html index 83b689d..10c5fbb 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 b900d1b..dabc7e3 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 @@
-

{% if board.locked %}{% endif %}{% if board.pinned %}{% endif %}{{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 f489a15..db51c55 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 1a863ed..c15564d 100644 --- a/Discussions/templates/report_reply.html +++ b/Discussions/templates/report_reply.html @@ -6,9 +6,9 @@
diff --git a/Discussions/views.py b/Discussions/views.py index 6fc1b45..32fb37f 100644 --- a/Discussions/views.py +++ b/Discussions/views.py @@ -29,7 +29,6 @@ from django.utils.text import slugify from guardian.decorators import permission_required_or_403 from LandingPage.models import Show, DiscussionBoard, DiscussionReply, DiscussionVote, Ban, Report -from LandingPage.views import get_show_url from . import forms import datetime @@ -41,7 +40,6 @@ class Boards(TemplateView): 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) @@ -80,8 +78,6 @@ 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) @@ -109,7 +105,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'] = ctx['showurl'] + '/discuss/board/%d-%s?page=%d#reply-%d'%(board.pk, slugify(board.title), page, index) + ctx['url'] = show.url() + '/discuss/board/%d-%s?page=%d#reply-%d'%(board.pk, slugify(board.title), page, index) return ctx @@ -181,7 +177,7 @@ def BoardForm(req, abbr): new_post = DiscussionReply(user=user,board=new_board,body=form_data['body']) new_post.save() - return HttpResponseRedirect(ctx['showurl'] + '/discuss/board/%d-%s'%(new_board.pk, slugify(form_data['title']))) + return HttpResponseRedirect(show.url() + '/discuss/board/%d-%s'%(new_board.pk, slugify(form_data['title']))) else: ctx['error'] = 'Invalid fields!' @@ -200,8 +196,7 @@ def BoardReplyForm(req, abbr, bid): ctx = { 'form': form, 'board': board, - 'show': show, - 'showurl': get_show_url(abbr) + 'show': show } # Get bans for this user regarding this show @@ -240,7 +235,7 @@ def BoardReplyForm(req, abbr, bid): new_reply.board = board new_reply.save() - return HttpResponseRedirect(ctx['showurl'] + '/discuss/board/%d-%s?findReply=%d'%(board.pk, slugify(board.title), new_reply.pk)) + return HttpResponseRedirect(show.url() + '/discuss/board/%d-%s?findReply=%d'%(board.pk, slugify(board.title), new_reply.pk)) else: ctx['error'] = 'Invalid fields!' @@ -254,10 +249,10 @@ 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) + showurl = reply.board.show.url() # Prevent voting for own reply if reply.user == user: @@ -309,11 +304,10 @@ def ReportForm(req, abbr, rid): ctx = { 'form': form, 'show': show, - 'reply': reply, - 'showurl': get_show_url(abbr) + 'reply': reply } - url = '%s/discuss/board/%d-%s?findReply=%d'%(ctx['showurl'], reply.board.pk, slugify(reply.board.title), reply.pk) + url = '%s/discuss/board/%d-%s?findReply=%d'%(show.url(), reply.board.pk, slugify(reply.board.title), reply.pk) # Handle POST if req.method == 'POST': @@ -339,7 +333,7 @@ def ReportForm(req, abbr, rid): new_report.url = url new_report.save() - return HttpResponseRedirect('%s/discuss/board/%d-%s'%(ctx['showurl'], reply.board.pk, slugify(reply.board.title))) + return HttpResponseRedirect('%s/discuss/board/%d-%s'%(show.url(), reply.board.pk, slugify(reply.board.title))) else: ctx['error'] = 'Invalid fields!' @@ -349,27 +343,25 @@ def ReportForm(req, abbr, rid): 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) + % (board.show.url(), 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))) + return HttpResponseRedirect('%s/discuss/board/%d-%s'%(board.show.url(), 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))) + return HttpResponseRedirect('%s/discuss/board/%d-%s'%(board.show.url(), 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): @@ -378,15 +370,14 @@ def BoardDelete(req, abbr, bid): DiscussionBoard.objects.filter(pk=board.pk).delete() - return HttpResponseRedirect('%s/discuss' % (showurl)) + return HttpResponseRedirect('%s/discuss' % (board.show.url())) @permission_required_or_403('LandingPage.can_moderate_board', (Show, 'abbr', 'abbr'), accept_global_perms=True) def BoardDeleteReply(req, abbr, rid): reply = get_object_or_404(DiscussionReply, pk=rid) - showurl = get_show_url(abbr) delete = not reply.deleted DiscussionReply.objects.filter(pk=reply.pk).update(deleted=delete) - return HttpResponseRedirect('%s/discuss/board/%d-%s'%(showurl, reply.board.pk, slugify(reply.board.title))) + return HttpResponseRedirect('%s/discuss/board/%d-%s'%(reply.show.url(), reply.board.pk, slugify(reply.board.title))) diff --git a/EpisodesCommunity/settings.py b/EpisodesCommunity/settings.py index e646da2..9620082 100644 --- a/EpisodesCommunity/settings.py +++ b/EpisodesCommunity/settings.py @@ -176,7 +176,7 @@ DISCUSSIONS_PER_PAGE = 26 DISCUSSIONS_REPLIES_PER_PAGE = 10 # Domain of this app -DOMAIN='localhost' +DOMAIN=options.get('domain') # Use subdomains for each show -DOMAIN_SUBDOMAIN_SHOWS=False +DOMAIN_SUBDOMAIN_SHOWS=options.get('use_subdomain_paths') == 'true' diff --git a/LandingPage/models.py b/LandingPage/models.py index 1daf6f6..78f4e76 100644 --- a/LandingPage/models.py +++ b/LandingPage/models.py @@ -15,6 +15,7 @@ # along with this program. If not, see . from django.db import models +from django.conf import settings from django.contrib.auth.models import AbstractUser from django.core.files.storage import FileSystemStorage from django.conf import settings @@ -95,6 +96,15 @@ class Show(TimestampedModel): ('can_moderate_board', 'Can delete and edit boards and replies of this show'), ) + def url(self): + use_sdms = getattr(settings, "DOMAIN_SUBDOMAIN_SHOWS", False) + domain = getattr(settings, "DOMAIN", 'localhost') + + if use_sdms: + return domain.format(sub=self.abbr + '.',path='') + + return '/show/%s' % (self.abbr) + def __str__(self): return '%s [%s]'%(self.name,self.abbr) @@ -253,7 +263,7 @@ class Season(models.Model): help_text="The artwork associated with the season. Should display the name of the show in a movie-poster esque format. Aspect ration should be about 2:3", verbose_name="Artwork", blank=True - ) + ) def __str__(self): return self.show.name + " S%d"%self.number diff --git a/LandingPage/templates/landing_page.html b/LandingPage/templates/landing_page.html index 933935e..16bf0c9 100644 --- a/LandingPage/templates/landing_page.html +++ b/LandingPage/templates/landing_page.html @@ -16,7 +16,7 @@ {% if not recent %} Nothing to show {% endif %}
{% for show in recent %} - + {{show.name}} diff --git a/LandingPage/templates/shows.html b/LandingPage/templates/shows.html index 969774e..6880c90 100644 --- a/LandingPage/templates/shows.html +++ b/LandingPage/templates/shows.html @@ -5,7 +5,7 @@
{% for show in shows %} {% if forloop.counter|divisibleby:3 %}
diff --git a/LandingPage/views.py b/LandingPage/views.py index cf848ce..d0a36e2 100644 --- a/LandingPage/views.py +++ b/LandingPage/views.py @@ -21,7 +21,7 @@ from django.contrib.auth import login as auth_login, authenticate from django.conf import settings from django.http import HttpResponse from django.http import HttpResponseRedirect -from django.db.models import Max +from django.db.models import Max, F from django.contrib.auth.views import logout import requests import hashlib @@ -31,16 +31,6 @@ from .models import Show from .models import Submission from .models import DiscussionBoard -# Get a show's URL by its abbreviation -def get_show_url(abbr): - use_sdms = getattr(settings, "DOMAIN_SUBDOMAIN_SHOWS", False) - domain = getattr(settings, "DOMAIN", 'localhost') - - if use_sdms: - return '%s.%s' % (abbr, domain) - - return '/show/%s' % (abbr) - # Redirect url should point to this view class LoginRedirect(View): def get(self, req): diff --git a/Show/templates/create_ban.html b/Show/templates/create_ban.html index e798897..57981b2 100644 --- a/Show/templates/create_ban.html +++ b/Show/templates/create_ban.html @@ -7,7 +7,7 @@
diff --git a/Show/templates/episode.html b/Show/templates/episode.html index 5a94280..9273541 100644 --- a/Show/templates/episode.html +++ b/Show/templates/episode.html @@ -46,7 +46,7 @@
@@ -73,13 +73,13 @@
- + {% csrf_token %} -
+ {% csrf_token %}
@@ -111,19 +111,19 @@
{% if user.is_authenticated %} {% if "can_moderate_show" in show_perms %} -  Add New Link +  Add New Link {% else %} -  Submit New Link +  Submit New Link {% endif %} {% else %} Log in to submit a link {% endif %}
-

Discuss {{episode.name}} on the discussion boards!

+

Discuss {{episode.name}} on the discussion boards!

{% if "can_moderate_show" in show_perms %}
- +
{% endif %}
{% endfor %} {% if "can_moderate_show" in show_perms %} {% endif %}
diff --git a/Show/templates/submit.html b/Show/templates/submit.html index 30634e1..2f3dc18 100644 --- a/Show/templates/submit.html +++ b/Show/templates/submit.html @@ -33,8 +33,8 @@
diff --git a/Show/templates/submit_mod.html b/Show/templates/submit_mod.html index cc4b8e1..5c054bd 100644 --- a/Show/templates/submit_mod.html +++ b/Show/templates/submit_mod.html @@ -35,8 +35,8 @@ {% get_obj_perms request.user for show as "show_perms" %} diff --git a/Show/views.py b/Show/views.py index 8c1a966..ab093f4 100644 --- a/Show/views.py +++ b/Show/views.py @@ -27,7 +27,6 @@ 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, Report -from LandingPage.views import get_show_url from . import forms @@ -51,7 +50,6 @@ class IndexView(TemplateView): # Add fields to context ctx['show'] = show ctx['seasons'] = seasons - ctx['showurl'] = get_show_url(abbr) return ctx @@ -61,7 +59,6 @@ class EpisodeView(TemplateView): def get_context_data(self, abbr, season, episode, **kwargs): ctx = super().get_context_data() - ctx['showurl'] = get_show_url(abbr) highlight = self.request.GET.get('submission', None) if not highlight == None: @@ -88,11 +85,11 @@ class EpisodeView(TemplateView): raise Http404('No Episode matches the given query.') episode_number = int(epobj.episode) - ctx['url'] = '%s/episode/%d/%d'%(ctx['showurl'], season_number, episode_number) + ctx['url'] = '%s/episode/%d/%d'%(show.url(), season_number, episode_number) elif episode_number > int(lastep.episode): season_number += 1 episode_number = 1 - ctx['url'] = '%s/episode/%d/%d'%(ctx['showurl'], season_number, episode_number) + ctx['url'] = '%s/episode/%d/%d'%(show.url(), season_number, episode_number) if 'url' in ctx: return ctx @@ -132,11 +129,10 @@ class EpisodeView(TemplateView): def EpisodeFindSubmission(req, abbr, submission): show = get_object_or_404(Show, abbr=abbr) submission = int(submission) - showurl = get_show_url(abbr) episode = get_object_or_404(Episode, submissions__id=submission) - return HttpResponseRedirect('%s/episode/%d/%d?submission=%d'%(showurl, episode.season.number, episode.episode, submission)) + return HttpResponseRedirect('%s/episode/%d/%d?submission=%d'%(show.url(), episode.season.number, episode.episode, submission)) # Submission form GET and POST @login_required @@ -151,8 +147,7 @@ def SubmissionForm(req, abbr, season, episode): ctx = { 'form': form, 'show': show, - 'episode': episode, - 'showurl': get_show_url(abbr) + 'episode': episode } # Get bans for this user regarding this show @@ -185,7 +180,7 @@ def SubmissionForm(req, abbr, season, episode): new_submission.episode = episode new_submission.save() - return HttpResponseRedirect('%s/episode/%d/%d'%(ctx['showurl'], episode.season.number, episode.episode)) + return HttpResponseRedirect('%s/episode/%d/%d'%(show.url(), episode.season.number, episode.episode)) else: ctx['error'] = 'Invalid fields!' @@ -205,19 +200,18 @@ def SubmissionModForm(req, abbr, submission): ctx = { 'form': form, 'show': show, - 'episode': episode, - 'showurl': get_show_url(abbr) + 'episode': episode } # Handle POST if req.method == 'POST': if 'delete' in req.POST: submission.delete() - return HttpResponseRedirect('%s/episode/%d/%d'%(ctx['showurl'], episode.season.number, episode.episode)) + return HttpResponseRedirect('%s/episode/%d/%d'%(show.url(), episode.season.number, episode.episode)) if 'delete_ban' in req.POST: submission.delete() - return HttpResponseRedirect('%s/create_ban?user=%s'%(ctx['showurl'],submission.user.username)) + return HttpResponseRedirect('%s/create_ban?user=%s'%(show.url(),submission.user.username)) form = forms.SubmissionFormAdmin(req.POST, instance=submission) ctx['form'] = form @@ -226,7 +220,7 @@ def SubmissionModForm(req, abbr, submission): form_data = form.cleaned_data form.save() - return HttpResponseRedirect('%s/episode/%d/%d'%(ctx['showurl'], episode.season.number, episode.episode)) + return HttpResponseRedirect('%s/episode/%d/%d'%(show.url(), episode.season.number, episode.episode)) else: ctx['error'] = 'Invalid fields!' @@ -243,8 +237,7 @@ def SeasonSubmitForm(req, abbr): # Request context ctx = { 'form': form, - 'show': show, - 'showurl': get_show_url(abbr) + 'show': show } # Handle POST @@ -264,7 +257,7 @@ def SeasonSubmitForm(req, abbr): new_season.show = show new_season.save() - return HttpResponseRedirect(ctx['showurl']) + return HttpResponseRedirect(show.url()) else: ctx['error'] = 'Invalid fields!' @@ -283,8 +276,7 @@ def EpisodeSubmitForm(req, abbr, season): ctx = { 'form': form, 'season': season, - 'show': show, - 'showurl': get_show_url(abbr) + 'show': show } # Handle POST @@ -305,7 +297,7 @@ def EpisodeSubmitForm(req, abbr, season): new_episode.season = season new_episode.save() - return HttpResponseRedirect(ctx['showurl']) + return HttpResponseRedirect(show.url()) else: ctx['error'] = 'Invalid fields!' @@ -319,7 +311,6 @@ class SubmissionVoteSubmit(LoginRequiredMixin, View): pos_bool = int(positive) == 1 user = req.user - showurl = get_show_url(abbr) # Get the submission from the database submission = get_object_or_404(Submission, id=subid) @@ -352,7 +343,7 @@ class SubmissionVoteSubmit(LoginRequiredMixin, View): ) new_vote.save() - return HttpResponseRedirect('%s/episode/%d/%d'%(showurl, submission.episode.season.number, submission.episode.episode)) + return HttpResponseRedirect('%s/episode/%d/%d'%(show.url(), submission.episode.season.number, submission.episode.episode)) # Episode form GET and POST @permission_required_or_403('LandingPage.can_create_show_ban', (Show, 'abbr', 'abbr'), accept_global_perms=True) @@ -407,7 +398,7 @@ def BanFromShowForm(req, abbr): if 'delete' in req.POST: Submission.objects.filter(episode__show=show,user=banTarget).delete() - return HttpResponseRedirect(ctx['showurl']) + return HttpResponseRedirect(show.url()) else: ctx['error'] = 'Invalid fields!' @@ -437,7 +428,7 @@ def ReportSubmission(req, abbr, submission): 'showurl': get_show_url(abbr) } - url = '%s/episode/%d/%d?submission=%s'%(ctx['showurl'], episode.season.number, episode.episode,submission.pk) + url = '%s/episode/%d/%d?submission=%s'%(show.url(), episode.season.number, episode.episode, submission.pk) # Handle POST if req.method == 'POST': @@ -463,7 +454,7 @@ def ReportSubmission(req, abbr, submission): new_report.url = url new_report.save() - return HttpResponseRedirect('%s/episode/%d/%d'%(ctx['showurl'], episode.season.number, episode.episode)) + return HttpResponseRedirect('%s/episode/%d/%d'%(show.url(), episode.season.number, episode.episode)) else: ctx['error'] = 'Invalid fields!' diff --git a/options_example.ini b/options_example.ini index 516fc94..48c0668 100644 --- a/options_example.ini +++ b/options_example.ini @@ -9,6 +9,10 @@ secret_key=5up3r s3cr3t k3y #For configuration details database=sqlite:///database.sqlite3 +#Domain of this website to use in show path manufacturing +domain=http://{sub}localhost:8000{path} +use_subdomain_paths=false + [OAuth] #The root of the oauth endpoint you are using for oauth settings token_endpoint=https://icynet.eu/oauth/