# 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
# 
# DRAFT = 0
# 
# CONFIRMED = 1
# 
# FINISHED = 4


from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.db.models import Q
from django.forms import ModelForm
from django.http import Http404, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.utils.translation import ugettext as _
from RandoAmisSecours.models import Outing, DRAFT, CONFIRMED, FINISHED
def index(request):
    # List all outings owned by the user and his friends
    user_outings = Outing.objects.filter(user=request.user)
    user_outings_confirmed = user_outings.filter(status=CONFIRMED)
    user_outings_draft = user_outings.filter(status=DRAFT)
    user_outings_finished = user_outings.filter(status=FINISHED)

    friends_outings = Outing.objects.filter(user__profile__in=request.user.profile.friends.all()).select_related()
    friends_outings_confirmed = friends_outings.filter(status=CONFIRMED)
    friends_outings_draft = friends_outings.filter(status=DRAFT)
    friends_outings_finished = friends_outings.filter(status=FINISHED)

    return render(request, 'RandoAmisSecours/outing/index.html',
                  {'user_outings_confirmed': user_outings_confirmed,
                   'user_outings_draft': user_outings_draft,
                   'user_outings_finished': user_outings_finished,
                   'friends_outings_confirmed': friends_outings_confirmed,
                   'friends_outings_draft': friends_outings_draft,
                   'friends_outings_finished': friends_outings_finished})


@login_required
def details(request, outing_id):
    # Return 404 if the outing does not belong to the user or his friends
    outing = get_object_or_404(Outing, Q(user=request.user) | Q(user__profile__in=request.user.profile.friends.all()), pk=outing_id)

    return render(request, 'RandoAmisSecours/outing/details.html',
                  {'outing': outing,
                   'FINISHED': FINISHED,
                   'CONFIRMED': CONFIRMED,

# 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
# 
# DRAFT = 0
# 
# CONFIRMED = 1
# 
# FINISHED = 4


# Path: RandoAmisSecours/views/outing.py
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.db.models import Q
from django.forms import ModelForm
from django.http import Http404, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.utils.translation import ugettext as _
from RandoAmisSecours.models import Outing, DRAFT, CONFIRMED, FINISHED

def index(request):
    # List all outings owned by the user and his friends
    user_outings = Outing.objects.filter(user=request.user)
    user_outings_confirmed = user_outings.filter(status=CONFIRMED)
    user_outings_draft = user_outings.filter(status=DRAFT)
    user_outings_finished = user_outings.filter(status=FINISHED)

    friends_outings = Outing.objects.filter(user__profile__in=request.user.profile.friends.all()).select_related()
    friends_outings_confirmed = friends_outings.filter(status=CONFIRMED)
    friends_outings_draft = friends_outings.filter(status=DRAFT)
    friends_outings_finished = friends_outings.filter(status=FINISHED)

    return render(request, 'RandoAmisSecours/outing/index.html',
                  {'user_outings_confirmed': user_outings_confirmed,
                   'user_outings_draft': user_outings_draft,
                   'user_outings_finished': user_outings_finished,
                   'friends_outings_confirmed': friends_outings_confirmed,
                   'friends_outings_draft': friends_outings_draft,
                   'friends_outings_finished': friends_outings_finished})


@login_required
def details(request, outing_id):
    # Return 404 if the outing does not belong to the user or his friends
    outing = get_object_or_404(Outing, Q(user=request.user) | Q(user__profile__in=request.user.profile.friends.all()), pk=outing_id)

    return render(request, 'RandoAmisSecours/outing/details.html',
                  {'outing': outing,
                   'FINISHED': FINISHED,
                   'CONFIRMED': CONFIRMED,

                   'DRAFT': DRAFT})