diff --git a/civicfix/core/views.py b/civicfix/core/views.py index a91e134..e226760 100644 --- a/civicfix/core/views.py +++ b/civicfix/core/views.py @@ -1,9 +1,12 @@ -from django.shortcuts import render, redirect from django.contrib import messages from django.contrib.auth import authenticate, login from django.contrib.auth.decorators import login_required from django.contrib.auth.forms import AuthenticationForm -from .models import Issue, IssueCategory, User +from django.db.models import Exists, OuterRef +from django.http import JsonResponse +from django.shortcuts import render, redirect +from django.views.decorators.http import require_POST +from .models import Issue, IssueCategory, User, Vote from .forms import CitizenRegistrationForm, IssueForm def home(request): @@ -14,6 +17,9 @@ def home(request): # Get recently reported issues (last 3 issues) recent_issues = Issue.objects.all().order_by('-created_at')[:3] + for issue in recent_issues: + issue.user_has_voted = issue.has_user_voted(request.user) if request.user.is_authenticated else False + # Get municipal departments count (assuming you have a Department model) # If you don't have one yet, you can use a placeholder or create the model municipal_departments = 5 # Placeholder - replace with actual count when you have the model @@ -69,6 +75,39 @@ def report_issue(request): return render(request, 'core/report_issue.html', {'form': form}) +@login_required +def view_all_issues(request): + if not request.user.is_citizen: + messages.error(request, 'Access denied. Citizen role required.') + return redirect('home') + + # Efficiently annotate whether the current user has voted + user_vote_subq = Vote.objects.filter(user=request.user, issue_id=OuterRef('pk')) + issues = ( + Issue.objects + .select_related('category', 'reporter') + .annotate(user_has_voted=Exists(user_vote_subq)) + .order_by('-created_at') + ) + + categories = IssueCategory.objects.all() + + # Optional filters (status/category) if you wired the dropdowns + status = request.GET.get('status') or '' + category_id = request.GET.get('category') or '' + if status: + issues = issues.filter(status=status) + if category_id: + issues = issues.filter(category_id=category_id) + + return render(request, 'core/view_all_issues.html', { + 'issues': issues, + 'categories': categories, + 'selected_status': status, + 'selected_category': category_id, + }) + + def register(request): if request.method == 'POST': form = CitizenRegistrationForm(request.POST) @@ -98,4 +137,29 @@ def custom_login(request): messages.error(request, 'Invalid username or password.') else: form = AuthenticationForm() - return render(request, 'core/login.html', {'form': form}) \ No newline at end of file + return render(request, 'core/login.html', {'form': form}) + +@login_required +@require_POST +def vote_issue(request, issue_id): + try: + issue = Issue.objects.get(id=issue_id) + vote, created = Vote.objects.get_or_create(user=request.user, issue=issue) + + if not created: + # User already voted, so remove the vote (toggle) + vote.delete() + voted = False + else: + voted = True + + return JsonResponse({ + 'success': True, + 'voted': voted, + 'vote_count': issue.vote_count() + }) + + except Issue.DoesNotExist: + return JsonResponse({'success': False, 'error': 'Issue not found'}) + except Exception as e: + return JsonResponse({'success': False, 'error': str(e)}) \ No newline at end of file