class OutingResource(ModelResource):
    user = fields.ForeignKey(UserResource, 'user')

    class Meta:
        queryset = Outing.objects.all()
        resource_name = 'outing'
        fields = ['id', 'name', 'description', 'status', 'beginning', 'ending', 'alert', 'latitude', 'longitude']
        allowed_methods = ['get']
        filtering = {
            'user': ALL_WITH_RELATIONS,
            'status': ['exact', 'gt', 'gte', 'lt', 'lte', 'range']
        }
        authentication = ApiKeyAuthentication()
        authorization = OutingAuthorization() class ProfileResource(ModelResource):
    user = fields.ForeignKey(UserResource, 'user')
    friends = fields.ToManyField('self', 'friends')

    class Meta:
        queryset = Profile.objects.all()
        resource_name = 'profile'
        fields = ['phone_number', 'language', 'timezone', 'friends']
        allowed_methods = ['get']
        authentication = ApiKeyAuthentication()
        authorization = ProfileAuthorization()

    def dehydrate(self, bundle):
        """ Hide private informations to friends """
        if not bundle.request.user == bundle.obj.user:
            del bundle.data['language']
            del bundle.data['timezone']
            del bundle.data['friends']

        return bundle class UserResource(ModelResource):
    profile = fields.ForeignKey('RandoAmisSecours.api.ProfileResource', 'profile')

    class Meta:
        queryset = User.objects.all()
        resource_name = 'user'
        fields = ['id', 'first_name', 'last_name', 'email']
        allowed_methods = ['get']
        filtering = {
            'id': ALL
        }
        authentication = ApiKeyAuthentication()
        authorization = UserAuthorization() class GPSPointResource(ModelResource):
    outing = fields.ForeignKey(OutingResource, 'outing')

    class Meta:
        queryset = GPSPoint.objects.all()
        resource_name = 'GPSPoint'
        fields = ['outing', 'date', 'latitude', 'longitude', 'precision']
        allowed_methods = ['get']
        authentication = ApiKeyAuthentication()
        authorization = GPSPointAuthorization() class LoginResource(ModelResource):
    class Meta:
        resource_name = 'login'
        fields = ['key']
        allowed_methods = ['get']
        include_resource_uri = False
        object_class = ApiKey
        authentication = BasicAuthentication()
        authorization = UserAuthorization()

    def obj_get_list(self, bundle, **kwargs):
        return [ApiKey.objects.get(user=bundle.request.user)]

    def dehydrate(self, bundle):
        bundle.data['user_id'] = bundle.request.user.id
        bundle.data['profile_id'] = bundle.request.user.profile.id
        return bundle class RASAuthenticationForm(AuthenticationForm):
    """
    Override the default AuthenticationForm in order to add HTML5 attributes.
    This is the only change done and needed
    """
    def __init__(self, *args, **kwargs):
        super(RASAuthenticationForm, self).__init__(*args, **kwargs)
        # Add HTML5 attributes
        self.fields['username'].widget.attrs['placeholder'] = _('Username')
        self.fields['username'].widget.attrs['autofocus'] = 'autofocus'
        self.fields['password'].widget.attrs['placeholder'] = _('Password')

        self.fields['username'].widget.attrs['class'] = 'form-control'
        self.fields['password'].widget.attrs['class'] = 'form-control' class RASPasswordChangeForm(PasswordChangeForm):
    """
    Override the default PasswordChangeForm in order to add HTML5 attributes.
    This is the only change done and needed
    """
    def __init__(self, *args, **kwargs):
        super(RASPasswordChangeForm, self).__init__(*args, **kwargs)
        # Add HTML5 attributes
        self.fields['old_password'].widget.attrs['placeholder'] = _('Old password')
        self.fields['old_password'].widget.attrs['autofocus'] = 'autofocus'
        self.fields['new_password1'].widget.attrs['placeholder'] = _('New password')
        self.fields['new_password2'].widget.attrs['placeholder'] = _('New password')

        self.fields['old_password'].widget.attrs['class'] = 'form-control'
        self.fields['new_password1'].widget.attrs['class'] = 'form-control'
        self.fields['new_password2'].widget.attrs['class'] = 'form-control' class RASPasswordResetForm(PasswordResetForm):
    """
    Override the default PasswordResetForm in order to add HTML5 attributes.
    This is the only change done and needed
    """
    def __init__(self, *args, **kwargs):
        super(RASPasswordResetForm, self).__init__(*args, **kwargs)
        # Add HTML5 attributes
        self.fields['email'].widget.attrs['placeholder'] = _('email')
        self.fields['email'].widget.attrs['autofocus'] = 'autofocus'
        self.fields['email'].widget.attrs['class'] = 'form-control' class RASSetPasswordForm(SetPasswordForm):
    """
    Override the default SetPasswordForm in order to add HTML5 attributes.
    This is the only change done and needed
    """
    def __init__(self, *args, **kwargs):
        super(RASSetPasswordForm, self).__init__(*args, **kwargs)
        # Add HTML5 attributes
        self.fields['new_password1'].widget.attrs['placeholder'] = _('New password')
        self.fields['new_password1'].widget.attrs['autofocus'] = 'autofocus'
        self.fields['new_password2'].widget.attrs['placeholder'] = _('New password')

        self.fields['new_password1'].widget.attrs['class'] = 'form-control'
        self.fields['new_password2'].widget.attrs['class'] = 'form-control' class RASAuthenticationForm(AuthenticationForm):
class RASPasswordChangeForm(PasswordChangeForm):
class RASPasswordResetForm(PasswordResetForm):
class RASSetPasswordForm(SetPasswordForm):
class RASUserCreationForm(UserCreationForm):
    class Meta:
class RASUserUpdateForm(ModelForm):
    class Meta:
class RASProfileUpdateForm(ModelForm):
    class Meta:
    def __init__(self, *args, **kwargs):
    def __init__(self, *args, **kwargs):
    def __init__(self, *args, **kwargs):
    def __init__(self, *args, **kwargs):
    def __init__(self, *args, **kwargs):
    def save(self, commit=True):
    def __init__(self, *args, **kwargs):
    def __init__(self, *args, **kwargs):
def register(request):
def register_confirm(request, user_id, user_hash):
def profile(request):
def update(request):
def password_change_done(request):
def password_reset_done(request):
def delete(request): def search(request):
def invite(request, user_id):
def accept(request, request_id):
def refuse(request, request_id):
def cancel(request, request_id):
def delete(request, user_id): def index(request):
def qa(request): def index(request): class OutingForm(ModelForm):
    class Meta:
    def __init__(self, *args, **kwargs):
    def clean(self):
def index(request):
def details(request, outing_id):
def details_trace(request, outing_id):
def create(request):
def update(request, outing_id):
def delete(request, outing_id):
def confirm(request, outing_id):
def finish(request, outing_id): def index(request):
def users(request):
def outings_late(request):
from django.conf.urls import include, url
from django.contrib.auth import views as r_auth
from django.core.urlresolvers import reverse_lazy
from tastypie.api import Api
from RandoAmisSecours.api import OutingResource, ProfileResource, UserResource, GPSPointResource, LoginResource
from RandoAmisSecours.views.account import RASAuthenticationForm, RASPasswordChangeForm, RASPasswordResetForm, RASSetPasswordForm
from RandoAmisSecours.views import account as r_account
from RandoAmisSecours.views import friends as r_friends
from RandoAmisSecours.views import help as r_help
from RandoAmisSecours.views import main as r_main
from RandoAmisSecours.views import outing as r_outing
from RandoAmisSecours.views import reporting as r_reporting
# -*- coding: utf-8 -*-
# vim: set ts=4

# Copyright 2013, 2014 Rémi Duraffort
# This file is part of RandoAmisSecours.
#
# RandoAmisSecours is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# RandoAmisSecours is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with RandoAmisSecours.  If not, see <http://www.gnu.org/licenses/>

from __future__ import unicode_literals





# API v1.0
api_1_0 = Api(api_name='1.0')
api_1_0.register(OutingResource())
api_1_0.register(ProfileResource())
api_1_0.register(UserResource())
api_1_0.register(GPSPointResource())
api_1_0.register(LoginResource())

urlpatterns = [
    # Main page
    url(r'^$', r_main.index, name='index'),

    # Help pages
    url(r'^help/$', r_help.index, name='help.index'),
    url(r'^help/qa/$', r_help.qa, name='help.qa'),

    # Authentication
    url(r'^accounts/login/$', r_auth.login, {'template_name': 'RandoAmisSecours/account/login.html', 'authentication_form': RASAuthenticationForm}, name='accounts.login'),
    url(r'^accounts/logout/$', r_auth.logout, {'template_name': 'RandoAmisSecours/account/logged_out.html'}, name='accounts.logout'),
    url(r'^accounts/password/change/$', r_auth.password_change, {'template_name': 'RandoAmisSecours/account/password_change.html', 'password_change_form': RASPasswordChangeForm, 'post_change_redirect': reverse_lazy('accounts.password_change_done')}, name='accounts.password_change'),
    url(r'^accounts/password/reset/$', r_auth.password_reset, {'template_name': 'RandoAmisSecours/account/password_reset.html', 'email_template_name': 'RandoAmisSecours/account/password_reset_email.txt', 'password_reset_form': RASPasswordResetForm, 'post_reset_redirect': reverse_lazy('accounts.password_reset_done')}, name='accounts.password_reset'),
    url(r'^accounts/password/reset/confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>.+)/$', r_auth.password_reset_confirm, {'template_name': 'RandoAmisSecours/account/password_reset_confirm.html', 'set_password_form': RASSetPasswordForm}, name='accounts.password_reset_confirm'),
    url(r'^accounts/password/reset/complete/$', r_auth.password_reset_complete, {'template_name': 'RandoAmisSecours/account/password_reset_complete.html'}, name='password_reset_complete'),


    url(r'^accounts/register/$', r_account.register, name='accounts.register'),
    url(r'^accounts/register/(?P<user_id>\d+)/confirm/(?P<user_hash>\w+)/$', r_account.register_confirm, name='accounts.register.confirm'),
    url(r'^accounts/profile/$', r_account.profile, name='accounts.profile'),
    url(r'^accounts/profile/update/$', r_account.update, name='accounts.profile.update'),
    url(r'^accounts/password/change/done/$', r_account.password_change_done, name='accounts.password_change_done'),
    url(r'^accounts/password/reset/done/$', r_account.password_reset_done, name='accounts.password_reset_done'),
    url(r'^accounts/delete/$', r_account.delete, name='accounts.delete'),

    # Friends
    url(r'^friends/search/$', r_friends.search, name='friends.search'),
    url(r'^friends/invite/(?P<user_id>\d+)/$', r_friends.invite, name='friends.invite'),
    url(r'^friends/accept/(?P<request_id>\d+)/$', r_friends.accept, name='friends.accept'),
    url(r'^friends/refuse/(?P<request_id>\d+)/$', r_friends.refuse, name='friends.refuse'),

    url(r'^friends/cancel/(?P<request_id>\d+)/$', r_friends.cancel, name='friends.cancel'),