File size: 1,619 Bytes
823a3a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from django.views.generic import FormView, DetailView, ListView
from django.shortcuts import redirect
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.exceptions import PermissionDenied
from .forms import CheckInForm
from .models import CheckIn
from .services import AnalysisService

class CheckInCreateView(LoginRequiredMixin, FormView):
    template_name = 'backend/checkin_form.html'
    form_class = CheckInForm

    def form_valid(self, form):
        service = AnalysisService()
        # Match the dictionary structure the Analyzer expects
        responses_dict = {'narration': form.cleaned_data['narration']}
        
        checkin = service.perform_full_checkin(
            user=self.request.user, 
            responses_dict=responses_dict
        )
        return redirect('checkin-detail', pk=checkin.pk)

class CheckInDetailView(LoginRequiredMixin, DetailView):
    model = CheckIn
    template_name = 'backend/checkin_detail.html'
    context_object_name = 'checkin'

    def get_object(self, queryset=None):
        """Ensure users can only access their own records."""
        obj = super().get_object(queryset)
        if obj.user != self.request.user and not self.request.user.is_staff:
            raise PermissionDenied("You do not have permission to view this assessment.")
        return obj

class HistoryListView(LoginRequiredMixin, ListView):
    model = CheckIn
    template_name = 'backend/history.html'
    
    def get_queryset(self):
        return CheckIn.objects.filter(user=self.request.user).order_by('-created_at')