updated
This commit is contained in:
+40
-4
@@ -4,10 +4,10 @@ from django.contrib.auth.decorators import login_required
|
||||
from django.contrib.auth.forms import AuthenticationForm
|
||||
from django.db.models import Exists, OuterRef
|
||||
from django.http import JsonResponse
|
||||
from django.shortcuts import render, redirect
|
||||
from django.shortcuts import render, redirect, get_object_or_404
|
||||
from django.views.decorators.http import require_POST
|
||||
from .models import Issue, IssueCategory, User, Vote
|
||||
from .forms import CitizenRegistrationForm, IssueForm
|
||||
from .models import Issue, IssueCategory, User, Vote, Comment
|
||||
from .forms import CitizenRegistrationForm, IssueForm, CommentForm
|
||||
|
||||
def home(request):
|
||||
total_issues = Issue.objects.count()
|
||||
@@ -162,4 +162,40 @@ def vote_issue(request, issue_id):
|
||||
except Issue.DoesNotExist:
|
||||
return JsonResponse({'success': False, 'error': 'Issue not found'})
|
||||
except Exception as e:
|
||||
return JsonResponse({'success': False, 'error': str(e)})
|
||||
return JsonResponse({'success': False, 'error': str(e)})
|
||||
|
||||
@login_required
|
||||
def add_comment(request, issue_id, parent_id=None):
|
||||
issue = get_object_or_404(Issue, id=issue_id)
|
||||
parent = None
|
||||
if parent_id:
|
||||
parent = get_object_or_404(Comment, id=parent_id)
|
||||
|
||||
if request.method == "POST":
|
||||
form = CommentForm(request.POST)
|
||||
if form.is_valid():
|
||||
comment = form.save(commit=False)
|
||||
comment.issue = issue
|
||||
comment.user = request.user
|
||||
comment.parent = parent
|
||||
comment.save()
|
||||
return redirect("issue_detail", issue_id=issue.id)
|
||||
else:
|
||||
form = CommentForm()
|
||||
|
||||
return render(request, "comments/add_comment.html", {"form": form, "issue": issue})
|
||||
|
||||
def issue_detail(request, pk):
|
||||
issue = get_object_or_404(Issue, pk=pk)
|
||||
comments = issue.comments.filter(parent__isnull=True) # top-level comments
|
||||
return render(request, "core/issue_detail.html", {"issue": issue, "comments": comments})
|
||||
|
||||
def add_comment(request, pk, parent_id=None):
|
||||
if request.method == "POST" and request.user.is_authenticated:
|
||||
issue = get_object_or_404(Issue, pk=pk)
|
||||
text = request.POST.get("text")
|
||||
content = request.POST.get("content")
|
||||
if content:
|
||||
parent = Comment.objects.get(pk=parent_id) if parent_id else None
|
||||
Comment.objects.create(issue=issue, user=request.user, content=content, parent=parent)
|
||||
return redirect("issue_detail", pk=pk)
|
||||
Reference in New Issue
Block a user