from django.contrib.admin.views.decorators import staff_member_required from django.core.exceptions import PermissionDenied 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): candidate = get_object_or_404(Candidate, slug=slug) form = DownloadForm() if request.method == 'POST': form = DownloadForm(request.POST) url = candidate.source_url if download_type == 'source' else candidate.binary_url 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): candidate = get_object_or_404(Candidate, slug=slug) if request.method == 'GET': form = CommentForm(initial={'post_to_list': True}) else: form = CommentForm(request.POST) if form.is_valid(): 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') return render(request, 'usethesource/add_comment_form.html', {'form': form, 'candidate': candidate}) @staff_member_required def edit_comment(request, comment_id): comment = get_object_or_404(Comment, id=comment_id) if request.user != comment.user and not request.user.has_perm('usethesource.change_comment'): raise PermissionDenied if request.method == 'GET': form = CommentForm(instance=comment) else: form = CommentForm(request.POST, instance=comment) if form.is_valid(): comment = form.save() return redirect('usethesource:view_comment', comment_id=comment.id, show_add='false') return render(request, 'usethesource/edit_comment_form.html', {'form': form, 'comment': comment}) @staff_member_required def view_comment(request, comment_id, show_add): show_add = show_add == 'true' comment = get_object_or_404(Comment, id=comment_id) return render(request, 'usethesource/returned_comment.html', {'comment': comment, 'candidate': comment.candidate, 'add': show_add}) @staff_member_required def delete_comment(request, comment_id, show_add): comment = get_object_or_404(Comment, id=comment_id) if request.user != comment.user and not request.user.has_perm('usethesource.delete_comment'): raise PermissionDenied comment.delete() show_add = show_add == 'true' return render(request, 'usethesource/comment_deleted.html', {'comment': None, 'add': show_add}) @staff_member_required def add_button(request, slug): candidate = get_object_or_404(Candidate, slug=slug) return render(request, 'usethesource/add_comment_button_partial.html', {'candidate': candidate}) def ccirt_process(request): return render(request, 'usethesource/ccirt_process.html', {})