# Path: securedpi_events/models.py
# class Event(models.Model):
#     """Define class for access events."""
# 
#     lock_id = models.CharField(max_length=20, blank=True)
#     action = models.CharField(max_length=30, default='unlock')
#     RFID = models.CharField(max_length=100, blank=True)
#     photo = models.ImageField(
#         upload_to='lock_photos',
#         blank=True,
#         null=True)
#     date_created = models.DateTimeField(auto_now_add=True)
#     serial = models.CharField(max_length=20)
#     status = models.CharField(max_length=20, default='failed')
#     mtype = models.CharField(max_length=20)
# 
#     def __str__(self):
#         """Show event."""
#         return 'Event for {}'.format(self.lock_id)
# 
# Path: securedpi_locks/models.py
# class Lock(models.Model):
#     """Define class for user profile."""
#     user = models.ForeignKey(
#         settings.AUTH_USER_MODEL,
#         related_name='locks',
#         on_delete=models.CASCADE
#     )
#     name = models.CharField(max_length=20)
#     location = models.CharField(max_length=50)
#     description = models.CharField(max_length=50, blank=True)
#     date_created = models.DateField(auto_now_add=True)
#     date_modified = models.DateField(auto_now=True)
#     serial = models.CharField(max_length=50, unique=True)
#     status = models.CharField(max_length=20, default='unlocked')
#     is_active = models.BooleanField(default=False)
#     facial_recognition = models.BooleanField(default=False)
#     RFID = models.CharField(max_length=100, blank=True)
# 
#     def __str__(self):
#         return 'Lock for {}'.format(self.user)
# 
# Path: securedpi_api/serializers.py
# class EventSerializer(serializers.ModelSerializer):
#     """Define class to serialize events."""
# 
#     class Meta:
#         model = Event
#         fields = ('pk', 'lock_id', 'photo', 'mtype', 'status',
#                   'serial', 'action', 'RFID', 'date_created')
# 
# class LockSerializer(serializers.ModelSerializer):
#     """Define class to serialize locks."""
# 
#     user = serializers.ReadOnlyField(source='user.username')
# 
#     class Meta:
#         model = Lock
#         fields = ('pk', 'user', 'name', 'location', 'description',
#                   'serial', 'status', 'is_active', 'facial_recognition',
#                   'date_created', 'date_modified', 'RFID')


from securedpi_events.models import Event
from securedpi_locks.models import Lock
from securedpi_api.serializers import EventSerializer, LockSerializer
from rest_framework.response import Response
from rest_framework import renderers
from rest_framework import viewsets
from rest_framework.decorators import detail_route
from django.shortcuts import get_object_or_404


class LockViewSet(viewsets.ModelViewSet):
    """
    This viewset automatically provides some actions.

    Provides 'list', 'create', 'retrieve', 'update' and 'destroy' actions.
    Additionally we also provide an extra 'highlight' action.
    """

    queryset = Lock.objects.all()
    serializer_class = LockSerializer

    def list(self, request):
        queryset = Lock.objects.filter(user=request.user)
        serializer = LockSerializer(queryset, many=True)
        return Response(serializer.data)

    def retrieve(self, request, pk=None):

# Path: securedpi_events/models.py
# class Event(models.Model):
#     """Define class for access events."""
# 
#     lock_id = models.CharField(max_length=20, blank=True)
#     action = models.CharField(max_length=30, default='unlock')
#     RFID = models.CharField(max_length=100, blank=True)
#     photo = models.ImageField(
#         upload_to='lock_photos',
#         blank=True,
#         null=True)
#     date_created = models.DateTimeField(auto_now_add=True)
#     serial = models.CharField(max_length=20)
#     status = models.CharField(max_length=20, default='failed')
#     mtype = models.CharField(max_length=20)
# 
#     def __str__(self):
#         """Show event."""
#         return 'Event for {}'.format(self.lock_id)
# 
# Path: securedpi_locks/models.py
# class Lock(models.Model):
#     """Define class for user profile."""
#     user = models.ForeignKey(
#         settings.AUTH_USER_MODEL,
#         related_name='locks',
#         on_delete=models.CASCADE
#     )
#     name = models.CharField(max_length=20)
#     location = models.CharField(max_length=50)
#     description = models.CharField(max_length=50, blank=True)
#     date_created = models.DateField(auto_now_add=True)
#     date_modified = models.DateField(auto_now=True)
#     serial = models.CharField(max_length=50, unique=True)
#     status = models.CharField(max_length=20, default='unlocked')
#     is_active = models.BooleanField(default=False)
#     facial_recognition = models.BooleanField(default=False)
#     RFID = models.CharField(max_length=100, blank=True)
# 
#     def __str__(self):
#         return 'Lock for {}'.format(self.user)
# 
# Path: securedpi_api/serializers.py
# class EventSerializer(serializers.ModelSerializer):
#     """Define class to serialize events."""
# 
#     class Meta:
#         model = Event
#         fields = ('pk', 'lock_id', 'photo', 'mtype', 'status',
#                   'serial', 'action', 'RFID', 'date_created')
# 
# class LockSerializer(serializers.ModelSerializer):
#     """Define class to serialize locks."""
# 
#     user = serializers.ReadOnlyField(source='user.username')
# 
#     class Meta:
#         model = Lock
#         fields = ('pk', 'user', 'name', 'location', 'description',
#                   'serial', 'status', 'is_active', 'facial_recognition',
#                   'date_created', 'date_modified', 'RFID')


# Path: securedpi_api/views.py
from securedpi_events.models import Event
from securedpi_locks.models import Lock
from securedpi_api.serializers import EventSerializer, LockSerializer
from rest_framework.response import Response
from rest_framework import renderers
from rest_framework import viewsets
from rest_framework.decorators import detail_route
from django.shortcuts import get_object_or_404



class LockViewSet(viewsets.ModelViewSet):
    """
    This viewset automatically provides some actions.

    Provides 'list', 'create', 'retrieve', 'update' and 'destroy' actions.
    Additionally we also provide an extra 'highlight' action.
    """

    queryset = Lock.objects.all()
    serializer_class = LockSerializer

    def list(self, request):
        queryset = Lock.objects.filter(user=request.user)
        serializer = LockSerializer(queryset, many=True)
        return Response(serializer.data)

    def retrieve(self, request, pk=None):

        queryset = Lock.objects.filter(user=request.user)