# Path: RandoAmisSecours/models.py
# 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)
# 
# 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())
# 
# Path: RandoAmisSecours/utils.py
# def send_localized_mail(user, subject, template_name, ctx):
#     with Localize(user.profile.language,
#                   user.profile.timezone):
#         send_mail_help(user, subject, template_name, ctx)


from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm, PasswordChangeForm, PasswordResetForm, SetPasswordForm
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.forms import ModelForm
from django.forms.widgets import HiddenInput
from django.shortcuts import get_object_or_404, render
from django.utils import translation
from django.utils.translation import ugettext as _
from RandoAmisSecours.models import Profile, FriendRequest
from RandoAmisSecours.utils import send_localized_mail
            return render(request, 'RandoAmisSecours/account/register_end.html')
        else:
            messages.error(request, _("Some information are missing or mistyped"))
    else:
        user_form = RASUserCreationForm()

    return render(request, 'RandoAmisSecours/account/register.html',
                  {'user_form': user_form})


def register_confirm(request, user_id, user_hash):
    """
    Check that the User and the Hash are correct before activating the User
    """
    user = get_object_or_404(User, pk=user_id, profile__hash_id=user_hash)
    user.is_active = True
    user.save()

    return render(request, 'RandoAmisSecours/account/confirm.html',
                  {'user': user})


@login_required
def profile(request):
    # Force the user to provide language and timezone
    if not request.user.profile.language or request.user.profile.timezone == 'UTC':
        messages.error(request, _("You should update your timezone. Without it R.A.S. will not work as expected."))
        return HttpResponseRedirect(reverse('accounts.profile.update'))

    friend_requests = FriendRequest.objects.filter(to=request.user)

# Path: RandoAmisSecours/models.py
# 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)
# 
# 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())
# 
# Path: RandoAmisSecours/utils.py
# def send_localized_mail(user, subject, template_name, ctx):
#     with Localize(user.profile.language,
#                   user.profile.timezone):
#         send_mail_help(user, subject, template_name, ctx)


# Path: RandoAmisSecours/views/account.py
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm, PasswordChangeForm, PasswordResetForm, SetPasswordForm
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.forms import ModelForm
from django.forms.widgets import HiddenInput
from django.shortcuts import get_object_or_404, render
from django.utils import translation
from django.utils.translation import ugettext as _
from RandoAmisSecours.models import Profile, FriendRequest
from RandoAmisSecours.utils import send_localized_mail

            return render(request, 'RandoAmisSecours/account/register_end.html')
        else:
            messages.error(request, _("Some information are missing or mistyped"))
    else:
        user_form = RASUserCreationForm()

    return render(request, 'RandoAmisSecours/account/register.html',
                  {'user_form': user_form})


def register_confirm(request, user_id, user_hash):
    """
    Check that the User and the Hash are correct before activating the User
    """
    user = get_object_or_404(User, pk=user_id, profile__hash_id=user_hash)
    user.is_active = True
    user.save()

    return render(request, 'RandoAmisSecours/account/confirm.html',
                  {'user': user})


@login_required
def profile(request):
    # Force the user to provide language and timezone
    if not request.user.profile.language or request.user.profile.timezone == 'UTC':
        messages.error(request, _("You should update your timezone. Without it R.A.S. will not work as expected."))
        return HttpResponseRedirect(reverse('accounts.profile.update'))

    friend_requests = FriendRequest.objects.filter(to=request.user)

    friend_requests_sent = FriendRequest.objects.filter(user=request.user)