# Path: RandoAmisSecours/models.py
# class FriendRequest(models.Model):
#     class Meta:
#         app_label = 'RandoAmisSecours'
# 
#     user = models.ForeignKey(User, related_name='+')
#     to = models.ForeignKey(User, related_name='+')
#     creation = models.DateTimeField(auto_now=True)
# 
#     def __str__(self):
#         return "%s => %s" % (self.user.get_full_name(), self.to.get_full_name())
# 
# 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
# 
# class Profile(models.Model):
#     class Meta:
#         app_label = 'RandoAmisSecours'
# 
#     user = models.OneToOneField(User)
#     friends = models.ManyToManyField('self', blank=True)
#     phone_number = models.CharField(max_length=30, blank=True, null=True)
#     provider = models.CharField(max_length=30, blank=True, null=True, choices=PROVIDERS)
#     provider_data = models.TextField(blank=True, null=True)
#     hash_id = models.CharField(unique=True, max_length=30, default=random_hash)
#     language = models.CharField(max_length=4, blank=True, null=True, choices=LANGUAGES)
#     timezone = models.CharField(max_length=40, choices=[(tz, tz) for tz in pytz.all_timezones], default='UTC')
# 
#     def clean(self):
#         if self.provider_data:
#             try:
#                 json.loads(self.provider_data)
#             except Exception:
#                 raise ValidationError({'provider_data': [ugettext('This should be JSON serialized')]})
# 
#         if self.timezone == 'UTC':
#             raise ValidationError({'timezone': [ugettext('UTC is not valid timezone')]})
# 
#     def __str__(self):
#         return "%s" % (self.user)
# 
# Path: RandoAmisSecours/models.py
# CONFIRMED = 1
# 
# DRAFT = 0
# 
# FINISHED = 4


from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.test.client import Client
from django.utils.timezone import datetime, utc
from tastypie.test import ResourceTestCase
from RandoAmisSecours.models import FriendRequest, Outing, Profile
from RandoAmisSecours.models import CONFIRMED, DRAFT, FINISHED
        self.user1 = User.objects.create_user('alpha',
                                              'alpha@example.com',
                                              '12789azertyuiop')
        self.user1.first_name = 'Alpha'
        self.user1.last_name = 'Tester'
        self.user1.save()
        self.user1.profile = Profile.objects.create(user=self.user1, timezone='Europe/Paris', language='fr')
        self.client.login(username='alpha', password='12789azertyuiop')

        self.user2 = User.objects.create_user('Beta',
                                              'beta@example.org',
                                              'ertyfjnbfvfceqsryuj')
        self.user2.first_name = 'Beta'
        self.user2.last_name = 'Testing'
        self.user2.save()
        self.user2.profile = Profile.objects.create(user=self.user2, timezone='Europe/London', language='en')

        self.user3 = User.objects.create_user('Gamma',
                                              'gamma@example.net',
                                              'gzgvaryurvyyjchrvyhubtr')
        self.user3.first_name = 'Gamma'
        self.user3.last_name = 'Ray'
        self.user3.save()
        self.user3.profile = Profile.objects.create(user=self.user3, timezone='America/Chicago', language='en')

        # user1 and user2 are friends
        self.user1.profile.friends.add(self.user2.profile)

        date = datetime(2011, 2, 15, 2, 0).replace(tzinfo=utc)
        # 3 outings for user1, and one for each user 2 and user3

# Path: RandoAmisSecours/models.py
# class FriendRequest(models.Model):
#     class Meta:
#         app_label = 'RandoAmisSecours'
# 
#     user = models.ForeignKey(User, related_name='+')
#     to = models.ForeignKey(User, related_name='+')
#     creation = models.DateTimeField(auto_now=True)
# 
#     def __str__(self):
#         return "%s => %s" % (self.user.get_full_name(), self.to.get_full_name())
# 
# 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
# 
# class Profile(models.Model):
#     class Meta:
#         app_label = 'RandoAmisSecours'
# 
#     user = models.OneToOneField(User)
#     friends = models.ManyToManyField('self', blank=True)
#     phone_number = models.CharField(max_length=30, blank=True, null=True)
#     provider = models.CharField(max_length=30, blank=True, null=True, choices=PROVIDERS)
#     provider_data = models.TextField(blank=True, null=True)
#     hash_id = models.CharField(unique=True, max_length=30, default=random_hash)
#     language = models.CharField(max_length=4, blank=True, null=True, choices=LANGUAGES)
#     timezone = models.CharField(max_length=40, choices=[(tz, tz) for tz in pytz.all_timezones], default='UTC')
# 
#     def clean(self):
#         if self.provider_data:
#             try:
#                 json.loads(self.provider_data)
#             except Exception:
#                 raise ValidationError({'provider_data': [ugettext('This should be JSON serialized')]})
# 
#         if self.timezone == 'UTC':
#             raise ValidationError({'timezone': [ugettext('UTC is not valid timezone')]})
# 
#     def __str__(self):
#         return "%s" % (self.user)
# 
# Path: RandoAmisSecours/models.py
# CONFIRMED = 1
# 
# DRAFT = 0
# 
# FINISHED = 4


# Path: RandoAmisSecours/tests.py
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.test.client import Client
from django.utils.timezone import datetime, utc
from tastypie.test import ResourceTestCase
from RandoAmisSecours.models import FriendRequest, Outing, Profile
from RandoAmisSecours.models import CONFIRMED, DRAFT, FINISHED

        self.user1 = User.objects.create_user('alpha',
                                              'alpha@example.com',
                                              '12789azertyuiop')
        self.user1.first_name = 'Alpha'
        self.user1.last_name = 'Tester'
        self.user1.save()
        self.user1.profile = Profile.objects.create(user=self.user1, timezone='Europe/Paris', language='fr')
        self.client.login(username='alpha', password='12789azertyuiop')

        self.user2 = User.objects.create_user('Beta',
                                              'beta@example.org',
                                              'ertyfjnbfvfceqsryuj')
        self.user2.first_name = 'Beta'
        self.user2.last_name = 'Testing'
        self.user2.save()
        self.user2.profile = Profile.objects.create(user=self.user2, timezone='Europe/London', language='en')

        self.user3 = User.objects.create_user('Gamma',
                                              'gamma@example.net',
                                              'gzgvaryurvyyjchrvyhubtr')
        self.user3.first_name = 'Gamma'
        self.user3.last_name = 'Ray'
        self.user3.save()
        self.user3.profile = Profile.objects.create(user=self.user3, timezone='America/Chicago', language='en')

        # user1 and user2 are friends
        self.user1.profile.friends.add(self.user2.profile)

        date = datetime(2011, 2, 15, 2, 0).replace(tzinfo=utc)
        # 3 outings for user1, and one for each user 2 and user3

        self.outing1 = Outing.objects.create(user=self.user1, beginning=date,