# 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)


from django.shortcuts import render
from django.views.generic.base import TemplateView
from securedpi_events.models import Event
from django.http import HttpResponseRedirect
from django.urls import reverse


class EventView(TemplateView):
    """Establish class for Event page view."""
    template_name = 'securedpi_events/events.html'

    def get_context_data(self, **kwargs):
        context = super(EventView, self).get_context_data(**kwargs)
        all_events = Event.objects.filter(lock_id=kwargs['pk']).order_by('-date_created')
        context['events'] = all_events
        context['lock_id'] = kwargs['pk']
        return context


def delete_old_events(request, **kwargs):
    """Delete 10 oldest events from the log."""

# 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_events/views.py
from django.shortcuts import render
from django.views.generic.base import TemplateView
from securedpi_events.models import Event
from django.http import HttpResponseRedirect
from django.urls import reverse



class EventView(TemplateView):
    """Establish class for Event page view."""
    template_name = 'securedpi_events/events.html'

    def get_context_data(self, **kwargs):
        context = super(EventView, self).get_context_data(**kwargs)
        all_events = Event.objects.filter(lock_id=kwargs['pk']).order_by('-date_created')
        context['events'] = all_events
        context['lock_id'] = kwargs['pk']
        return context


def delete_old_events(request, **kwargs):
    """Delete 10 oldest events from the log."""

    old_events = Event.objects.filter(lock_id=kwargs['pk']).order_by('date_created')[:10]