diff --git a/LandingPage/static/css/style.css b/LandingPage/static/css/style.css index 5c3ff2b..e421c70 100644 --- a/LandingPage/static/css/style.css +++ b/LandingPage/static/css/style.css @@ -13,6 +13,29 @@ body { text-decoration: none; color: #000; } +input[type="submit"] { + font-size: 120%; +} +label { + width: 200px; + display: block; +} +input[type=text], input:not([type=submit]) { + padding: 5px; + font-size: 120%; + width: 280px; + border-radius: 5px; + border: 1px solid #b3b3b3; + background-color: #ffffff; + box-shadow: inset 2px 2px 5px #ddd; +} +.helptext { + display: block; + margin-bottom: 10px; + font-style: italic; + font-size: 80%; + cursor: help; +} .unibar { padding: 20px; border-bottom: 1px solid #ddd; @@ -169,6 +192,13 @@ a.episode .submission_cnt { .submission-list .submission.buried { opacity: 0.3; } +.message.error { + width: fit-content; + padding: 10px; + background-color: #ffcaca; + border: 1px solid #b10000; + margin: 5px 0; +} @media all and (max-width: 800px) { .logo { font-size: 5vw !important; diff --git a/Show/forms.py b/Show/forms.py new file mode 100644 index 0000000..163cff9 --- /dev/null +++ b/Show/forms.py @@ -0,0 +1,11 @@ +from django import forms +from LandingPage.models import Submission + +class SubmissionForm(forms.ModelForm): + class Meta(): + model = Submission + fields = ('url','tags',) + help_texts = { + 'url': 'URL to your episode', + 'tags': 'Describe your link. Comma-separated list of keywords' + } diff --git a/Show/templates/episode.html b/Show/templates/episode.html index 70a5109..9a28c22 100644 --- a/Show/templates/episode.html +++ b/Show/templates/episode.html @@ -18,7 +18,10 @@
- << Show Index +  Show Index + {% if request.session.user_id %} +  Submit New Link + {% endif %}

Watch {{episode.name}} From

{% for sbm in submissions %} diff --git a/Show/templates/submit.html b/Show/templates/submit.html new file mode 100644 index 0000000..d34a925 --- /dev/null +++ b/Show/templates/submit.html @@ -0,0 +1,34 @@ +{% extends "base.html" %} +{% block title %} + Episode "{{episode.name}}" - S{{episode.season.number}}E{{episode.episode}} - {{show.name}} - Episodes.Community +{% endblock %} +{% block content %} +
+ + +
+
+  Show Index + Back to Episode +

Submit a link

+ {% if error %} +
{{error}}
+ {% endif %} +
+ {% csrf_token %} + {{ form }} + +
+
+
+{% endblock %} diff --git a/Show/urls.py b/Show/urls.py index 2817b0e..10095fa 100644 --- a/Show/urls.py +++ b/Show/urls.py @@ -21,5 +21,6 @@ from . import views urlpatterns = [ url(r'^$', views.IndexView.as_view()), url(r'^episode/(?P\d{1,4})/(?P\d{1,4})(-[\w-]+)?/?$', views.EpisodeView.as_view()), + url(r'^episode/(?P\d{1,4})/(?P\d{1,4})(-[\w-]+)?/submit$', views.SubmissionForm), url(r'^vote/(?P\d+)/(?P[0-1])/?$', views.SubmissionVoteSubmit.as_view()) ] diff --git a/Show/views.py b/Show/views.py index e6e7a02..9e91303 100644 --- a/Show/views.py +++ b/Show/views.py @@ -1,4 +1,4 @@ -from django.shortcuts import render +from django.template import RequestContext from django.shortcuts import render from django.views import View from django.views.generic.base import TemplateView @@ -14,6 +14,10 @@ from LandingPage.models import Season from LandingPage.models import Episode from LandingPage.models import Submission, SubmissionVote +from . import forms + +import datetime + # Create your views here. # Index page of a show @@ -50,7 +54,7 @@ class EpisodeView(TemplateView): show = Show.objects.filter(abbr=abbreviation).first() # Get episode by season and episode number - episode = Episode.objects.filter(show=show,episode=episode).first() + episode = Episode.objects.filter(show=show,season__number=season,episode=episode).first() # 404's if not show: @@ -80,6 +84,64 @@ class EpisodeView(TemplateView): return ctx +# Submission form GET and POST +def SubmissionForm(req, abbreviation, season, episode): + show = Show.objects.get(abbr=abbreviation) + episode = Episode.objects.filter(show=show,season__number=season,episode=episode).first() + + # Check for login status + if not 'user_id' in req.session: + return HttpResponse('

Error

You need to be logged in to submit. Please log in

', status=400) + + user = User.objects.get(user_id=req.session['user_id']) + + # 404's + if not show: + raise Http404("Show does not exist") + + if not episode: + raise Http404("Episode does not exist") + + form = forms.SubmissionForm() + + # Request context + ctx = { + 'form': form, + 'show': show, + 'episode': episode + } + + # Handle POST + if req.method == 'POST': + form = forms.SubmissionForm(req.POST) + ctx['form'] = form + + if form.is_valid(): + form_data = form.cleaned_data + + # Check if the URL has already been submitted + if Submission.objects.filter(episode=episode,url=form_data['url']).count() > 0: + ctx['error'] = 'This URL has already been submitted!' + return render(req, "submit.html", ctx) + + # Check if there has been a submission by this user for this episode within the last 24 hours + if Submission.objects.filter(user=user,episode=episode,timestamp__gte=datetime.date.today() - datetime.timedelta(hours=24)).count() > 0: + ctx['error'] = 'You can only submit one link for an episode in 24 hours!' + return render(req, "submit.html", ctx) + + # Have to do this because you can't add fields to a form + # If you know a better way of doing this, be my guest + new_submission = form.save(commit=False) + new_submission.user = user + new_submission.episode = episode + new_submission.save() + + return HttpResponseRedirect('/show/%s/episode/%d/%d'%(abbreviation, episode.season.number, episode.episode)) + else: + ctx['error'] = 'Invalid fields!' + + return render(req, "submit.html", ctx) + # Vote request # /show/{{abbr}}/vote/{{submission id}}/{{positive == 1}} class SubmissionVoteSubmit(View):