# Path: assessment/serializers.py
# class ResultSerializer(serializers.ModelSerializer):
#     _uid = serializers.UUIDField(label='ID', read_only=True)
# 
#     class Meta:
#         model = Result
#         fields = (
#             '_uid',
#             'survey',
#             'user',
#         )
# 
#     def validate(self, attrs):
#         instance = Result(**attrs)
#         instance.clean()
#         return attrs
# 
# Path: assessment/models.py
# class Result(models.Model):
#     _uid = models.UUIDField(
#         primary_key=True,
#         editable=False,
#         default=uuid.uuid4,
#     )
# 
#     timestamp = models.DateTimeField(
#         editable=False,
#         default=datetime.datetime.now,
#     )
# 
#     survey = models.ForeignKey(
#         Survey,
#         related_name='results',
#         verbose_name=_("survey"),
#         on_delete=models.CASCADE,
#     )
# 
#     user = models.ForeignKey(
#         settings.AUTH_USER_MODEL,
#         related_name='results',
#         verbose_name=_("user"),
#         on_delete=models.CASCADE,
#     )
# 
#     class Meta:
#         app_label = 'assessment'
#         verbose_name = _('result')
#         verbose_name_plural = _('results')
#         unique_together = ('survey', 'user')
# 
#     def __str__(self):
#         return str(self.pk)
# 
#     def clean(self, *args, **kwargs):
#         if self.survey.is_private:
#             if self.user == self.survey.admin or self.user in self.survey.users.all():
#                 # The user is allowed to answer this survey.
#                 # sonarqube complains about how this is structured...
#                 pass
#             else:
#                 raise ValidationError("User attempted to answer private survey!")


import logging
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from assessment.serializers import ResultSerializer
from assessment.models import Result
def update_result(request, uuid):
    """
    """
    if request.method == 'PUT':
        response_data = {"details": "not implemented"}
        return Response(response_data, status=status.HTTP_200_OK)


@api_view(['GET', ])
def list_results(request):
    """
    Generate a list of all survey results for the authenticated user.
    If there is no authenticated user, return 401 UNAUTHORIZED.
    """
    if request.user.is_authenticated:
        results = Result.objects.filter(user=request.user)
        serializer = ResultSerializer(results, many=True)
        return Response(serializer.data, status.HTTP_200_OK)
    return Response({"details": "please log in"}, status.HTTP_401_UNAUTHORIZED)


@api_view(['DELETE', ])
def delete_result(request, uuid):
    """
    delete a single result if the request is from the result owner
    """

    if request.user.is_authenticated:
        try:
            result = Result.objects.get(pk=uuid, admin=request.user)

# Path: assessment/serializers.py
# class ResultSerializer(serializers.ModelSerializer):
#     _uid = serializers.UUIDField(label='ID', read_only=True)
# 
#     class Meta:
#         model = Result
#         fields = (
#             '_uid',
#             'survey',
#             'user',
#         )
# 
#     def validate(self, attrs):
#         instance = Result(**attrs)
#         instance.clean()
#         return attrs
# 
# Path: assessment/models.py
# class Result(models.Model):
#     _uid = models.UUIDField(
#         primary_key=True,
#         editable=False,
#         default=uuid.uuid4,
#     )
# 
#     timestamp = models.DateTimeField(
#         editable=False,
#         default=datetime.datetime.now,
#     )
# 
#     survey = models.ForeignKey(
#         Survey,
#         related_name='results',
#         verbose_name=_("survey"),
#         on_delete=models.CASCADE,
#     )
# 
#     user = models.ForeignKey(
#         settings.AUTH_USER_MODEL,
#         related_name='results',
#         verbose_name=_("user"),
#         on_delete=models.CASCADE,
#     )
# 
#     class Meta:
#         app_label = 'assessment'
#         verbose_name = _('result')
#         verbose_name_plural = _('results')
#         unique_together = ('survey', 'user')
# 
#     def __str__(self):
#         return str(self.pk)
# 
#     def clean(self, *args, **kwargs):
#         if self.survey.is_private:
#             if self.user == self.survey.admin or self.user in self.survey.users.all():
#                 # The user is allowed to answer this survey.
#                 # sonarqube complains about how this is structured...
#                 pass
#             else:
#                 raise ValidationError("User attempted to answer private survey!")


# Path: assessment/views/api/result.py
import logging
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from assessment.serializers import ResultSerializer
from assessment.models import Result

def update_result(request, uuid):
    """
    """
    if request.method == 'PUT':
        response_data = {"details": "not implemented"}
        return Response(response_data, status=status.HTTP_200_OK)


@api_view(['GET', ])
def list_results(request):
    """
    Generate a list of all survey results for the authenticated user.
    If there is no authenticated user, return 401 UNAUTHORIZED.
    """
    if request.user.is_authenticated:
        results = Result.objects.filter(user=request.user)
        serializer = ResultSerializer(results, many=True)
        return Response(serializer.data, status.HTTP_200_OK)
    return Response({"details": "please log in"}, status.HTTP_401_UNAUTHORIZED)


@api_view(['DELETE', ])
def delete_result(request, uuid):
    """
    delete a single result if the request is from the result owner
    """

    if request.user.is_authenticated:
        try:
            result = Result.objects.get(pk=uuid, admin=request.user)

        except (Result.DoesNotExist, ValueError):