2023-10-24 12:55:14 +00:00
|
|
|
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
|
2023-11-10 05:35:22 +00:00
|
|
|
from .forms import CommentForm, DownloadForm
|
2024-01-26 04:52:38 +00:00
|
|
|
from .emails import make_comment_email
|
2023-10-20 05:49:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def landing_page(request):
|
2023-10-24 12:55:14 +00:00
|
|
|
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)
|
2023-11-10 05:35:22 +00:00
|
|
|
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():
|
|
|
|
return redirect(url)
|
2023-10-24 12:55:14 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
'usethesource/download.html',
|
2023-11-10 05:35:22 +00:00
|
|
|
{'form': form, 'candidate': candidate, 'download_type': download_type},
|
2023-10-24 12:55:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@staff_member_required
|
|
|
|
def create_comment(request, slug):
|
|
|
|
candidate = get_object_or_404(Candidate, slug=slug)
|
|
|
|
if request.method == 'GET':
|
|
|
|
form = CommentForm()
|
|
|
|
else:
|
|
|
|
form = CommentForm(request.POST)
|
|
|
|
if form.is_valid():
|
2024-01-26 04:52:38 +00:00
|
|
|
comment = form.save(commit=False)
|
|
|
|
comment.candidate = candidate
|
|
|
|
comment.user = request.user
|
|
|
|
comment.save()
|
|
|
|
email = make_comment_email(comment)
|
|
|
|
email.send()
|
|
|
|
return redirect('usethesource:view_comment', comment_id=comment.id, show_add='true')
|
2023-10-24 12:55:14 +00:00
|
|
|
return render(request, 'usethesource/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.method == 'GET':
|
|
|
|
form = CommentForm(instance=comment)
|
|
|
|
else:
|
|
|
|
form = CommentForm(request.POST, instance=comment)
|
|
|
|
if form.is_valid():
|
2024-01-26 04:52:38 +00:00
|
|
|
comment = form.save()
|
|
|
|
return redirect('usethesource:view_comment', comment_id=comment.id, show_add='false')
|
2023-10-24 12:55:14 +00:00
|
|
|
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})
|
2023-10-20 05:49:17 +00:00
|
|
|
|
|
|
|
|
2023-10-24 12:55:14 +00:00
|
|
|
@staff_member_required
|
|
|
|
def delete_comment(request, comment_id, show_add):
|
|
|
|
show_add = show_add == 'true'
|
|
|
|
Comment.objects.filter(id=comment_id).delete()
|
|
|
|
return render(request, 'usethesource/comment_deleted.html', {'comment': None, 'add': show_add})
|
2023-10-20 05:49:17 +00:00
|
|
|
|
|
|
|
|
2023-10-24 12:55:14 +00:00
|
|
|
@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})
|