# Path: RandoAmisSecours/models.py
# class Outing(models.Model):
#     class Meta:
#         app_label = 'RandoAmisSecours'
#         ordering = ['beginning', 'ending', 'alert', 'name']
# 
#     user = models.ForeignKey(User)
# 
#     # Visible information
#     name = models.CharField(max_length=200)
#     description = models.TextField()
#     status = models.IntegerField(choices=OUTING_STATUS, default=DRAFT)
# 
#     # Time frame: (begin, end, alert)
#     beginning = models.DateTimeField()
#     ending = models.DateTimeField()
#     alert = models.DateTimeField()
# 
#     # Position on the map
#     latitude = models.FloatField()
#     longitude = models.FloatField()
# 
#     def __str__(self):
#         return "%s: %s" % (self.user.get_full_name(), self.name)
# 
#     def getPercents(self):
#         current_time = datetime.utcnow().replace(tzinfo=utc)
#         # now < begin < end < alert
#         if current_time < self.beginning:
#             return (0, 0, 0)
#         # begin < end < alert < now
#         elif self.alert < current_time:
#             return (0, 0, 100)
#         # begin < now < end < alert
#         elif current_time < self.ending:
#             return (((current_time - self.beginning).total_seconds()) / float((self.alert - self.beginning).total_seconds()) * 100, 0, 0)
#         # begin < end < now < alert
#         else:
#             assert(current_time < self.alert)
#             return (((self.ending - self.beginning).total_seconds()) / float((self.alert - self.beginning).total_seconds()) * 100,
#                     ((current_time - self.ending).total_seconds()) / float((self.alert - self.beginning).total_seconds()) * 100,
#                     0)
# 
#     def is_running(self):
#         """ Return True if beginning <= now < end """
#         now = datetime.utcnow().replace(tzinfo=utc)
#         return self.beginning <= now and now < self.ending
# 
#     def is_late(self):
#         """ Return True if end <= now < alert """
#         now = datetime.utcnow().replace(tzinfo=utc)
#         return self.ending <= now and now < self.alert
# 
#     def is_alerting(self):
#         """ Return True if alert <= now """
#         now = datetime.utcnow().replace(tzinfo=utc)
#         return self.alert <= now
# 
#     is_running.boolean = True
#     is_late.boolean = True
#     is_alerting.boolean = True
# 
# CONFIRMED = 1


from django.conf import settings
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth.models import User
from django.contrib.sessions.models import Session
from django.shortcuts import render
from django.utils.timezone import datetime, utc
from RandoAmisSecours.models import Outing, CONFIRMED
        if days_delta <= 365:
            last_logins[365 - days_delta] += 1

    # Active sessions
    all_sessions = Session.objects.all()
    sessions_list = [0] * 366
    for session in all_sessions:
        end = (now - session.expire_date).days
        begin = int(end + settings.SESSION_COOKIE_AGE / 86400)

        # If begin after today (error)
        if begin < 0:
            continue
        # Crop to 365
        if end <= 0:
            end = 0

        for day in range(end, begin + 1):
            if day <= 365:
                sessions_list[365 - day] += 1

    return render(request, 'RandoAmisSecours/reporting/users.html',
                  {'joining_dates': joining_dates,
                   'last_logins': last_logins,
                   'sessions': sessions_list})


@staff_member_required
def outings_late(request):
    now = datetime.utcnow().replace(tzinfo=utc)

# Path: RandoAmisSecours/models.py
# class Outing(models.Model):
#     class Meta:
#         app_label = 'RandoAmisSecours'
#         ordering = ['beginning', 'ending', 'alert', 'name']
# 
#     user = models.ForeignKey(User)
# 
#     # Visible information
#     name = models.CharField(max_length=200)
#     description = models.TextField()
#     status = models.IntegerField(choices=OUTING_STATUS, default=DRAFT)
# 
#     # Time frame: (begin, end, alert)
#     beginning = models.DateTimeField()
#     ending = models.DateTimeField()
#     alert = models.DateTimeField()
# 
#     # Position on the map
#     latitude = models.FloatField()
#     longitude = models.FloatField()
# 
#     def __str__(self):
#         return "%s: %s" % (self.user.get_full_name(), self.name)
# 
#     def getPercents(self):
#         current_time = datetime.utcnow().replace(tzinfo=utc)
#         # now < begin < end < alert
#         if current_time < self.beginning:
#             return (0, 0, 0)
#         # begin < end < alert < now
#         elif self.alert < current_time:
#             return (0, 0, 100)
#         # begin < now < end < alert
#         elif current_time < self.ending:
#             return (((current_time - self.beginning).total_seconds()) / float((self.alert - self.beginning).total_seconds()) * 100, 0, 0)
#         # begin < end < now < alert
#         else:
#             assert(current_time < self.alert)
#             return (((self.ending - self.beginning).total_seconds()) / float((self.alert - self.beginning).total_seconds()) * 100,
#                     ((current_time - self.ending).total_seconds()) / float((self.alert - self.beginning).total_seconds()) * 100,
#                     0)
# 
#     def is_running(self):
#         """ Return True if beginning <= now < end """
#         now = datetime.utcnow().replace(tzinfo=utc)
#         return self.beginning <= now and now < self.ending
# 
#     def is_late(self):
#         """ Return True if end <= now < alert """
#         now = datetime.utcnow().replace(tzinfo=utc)
#         return self.ending <= now and now < self.alert
# 
#     def is_alerting(self):
#         """ Return True if alert <= now """
#         now = datetime.utcnow().replace(tzinfo=utc)
#         return self.alert <= now
# 
#     is_running.boolean = True
#     is_late.boolean = True
#     is_alerting.boolean = True
# 
# CONFIRMED = 1


# Path: RandoAmisSecours/views/reporting.py
from django.conf import settings
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth.models import User
from django.contrib.sessions.models import Session
from django.shortcuts import render
from django.utils.timezone import datetime, utc
from RandoAmisSecours.models import Outing, CONFIRMED

        if days_delta <= 365:
            last_logins[365 - days_delta] += 1

    # Active sessions
    all_sessions = Session.objects.all()
    sessions_list = [0] * 366
    for session in all_sessions:
        end = (now - session.expire_date).days
        begin = int(end + settings.SESSION_COOKIE_AGE / 86400)

        # If begin after today (error)
        if begin < 0:
            continue
        # Crop to 365
        if end <= 0:
            end = 0

        for day in range(end, begin + 1):
            if day <= 365:
                sessions_list[365 - day] += 1

    return render(request, 'RandoAmisSecours/reporting/users.html',
                  {'joining_dates': joining_dates,
                   'last_logins': last_logins,
                   'sessions': sessions_list})


@staff_member_required
def outings_late(request):
    now = datetime.utcnow().replace(tzinfo=utc)

    late_outings = Outing.objects.filter(status=CONFIRMED, ending__lt=now)