{{episode.name}}From
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):