from django.contrib.admin.views.decorators import staff_member_required
from django.shortcuts import get_object_or_404, redirect, render
from .models import Candidate, Comment
from .forms import CommentForm, DownloadForm
from .emails import make_comment_email
def landing_page(request):
candidates = Candidate.objects.all()
return render(request, 'usethesource/landing_page.html', {'candidates': candidates})
def candidate_page(request, slug):
candidate = get_object_or_404(Candidate, slug=slug)
return render(request, 'usethesource/candidate.html', {'candidate': candidate, 'add': True})
def download_page(request, slug, download_type):
form = DownloadForm()
if request.method == 'POST':
form = DownloadForm(request.POST)
url = candidate.source_url if download_type == 'source' else candidate.binary_url
if form.is_valid():
if not candidate.show_download_disclaimer or form.is_valid():
return redirect(url)
return render(
request,
'usethesource/download.html',
{'form': form, 'candidate': candidate, 'download_type': download_type},
)
@staff_member_required
def create_comment(request, slug):
if request.method == 'GET':
form = CommentForm(initial={'post_to_list': True})
else:
form = CommentForm(request.POST)
comment = form.save(commit=False)
comment.candidate = candidate
comment.user = request.user
comment.save()
if 'post_to_list' in request.POST:
email = make_comment_email(comment)
email.send()
return redirect('usethesource:view_comment', comment_id=comment.id, show_add='true')