# Path: wh_habitica/forms.py
# class AuthForm(forms.ModelForm):
#     """
#     Form to enter and validate the Habitica authentication credentials.
#     """
# 
#     AUTH_ERROR = _('Could not authenticate to Habitica. Please check the User '
#                    'ID and the API Token.')
#     HABITICA_ERROR = _('Something went wrong while loading Habitica user data.'
#                        ' Please try again or contact the admin.')
# 
#     class Meta:
#         model = Habitica
#         fields = ['user_id', 'api_token']
# 
#     def clean(self):
#         cleaned_data = super(AuthForm, self).clean()
# 
#         api = HabiticaApi(cleaned_data['user_id'], cleaned_data['api_token'])
#         user_details = api.get_user_details()
# 
#         # Validate authentication
#         if not user_details or cleaned_data['user_id'] != user_details[default.JSON_ID]:
#             raise forms.ValidationError(self.AUTH_ERROR)
# 
#         # Get optional user details
#         try:
#             self.instance.name = user_details[default.JSON_NAME]
#             self.instance.email = user_details[default.JSON_EMAIL]
#         except (ValueError, KeyError):
#             logger.exception('Could not get user details: %s', str(user_details))
# 
#         return cleaned_data
# 
# Path: wh_habitica/decorators.py
# def has_habitica(view_func):
#     """
#     Checks if the current user is connected to Habitica.
#     If not, the user is redirected to the Habitica connect page.
#     """
# 
#     def _wrapped_view_func(request, *args, **kwargs):
#         user = request.user
#         if not hasattr(user, 'habitica'):
#             return redirect('habitica:index')
#         if not user.habitica.api_token:
#             return redirect('habitica:index')
#         if not user.habitica.user_id:
#             return redirect('habitica:index')
#         return view_func(request, *args, **kwargs)
#     return _wrapped_view_func
# 
# Path: wunderlist/decorators.py
# def has_wunderlist(view_func):
#     """
#     Checks if the current user is connected to Wunderlist.
#     If not, the user is redirected to the Wunderlist connect page.
#     """
# 
#     def _wrapped_view_func(request, *args, **kwargs):
#         user = request.user
#         if not hasattr(user, 'wunderlist'):
#             return redirect('index')
#         if not user.wunderlist.api_token:
#             return redirect('index')
#         if not user.wunderlist.user_id:
#             return redirect('index')
#         if not user.wunderlist.email:
#             return redirect('index')
#         return view_func(request, *args, **kwargs)
#     return _wrapped_view_func


from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.utils.translation import ugettext_lazy as _
from django.contrib import messages
from .forms import AuthForm
from .decorators import has_habitica
from wunderlist.decorators import has_wunderlist



@login_required
@has_wunderlist
def auth(request):
    """
    Authenticates the user to Habitica.
    """

    user = request.user

    if hasattr(user, 'habitica'):
        return redirect('index')

    form = AuthForm()
    if request.method == 'POST':

# Path: wh_habitica/forms.py
# class AuthForm(forms.ModelForm):
#     """
#     Form to enter and validate the Habitica authentication credentials.
#     """
# 
#     AUTH_ERROR = _('Could not authenticate to Habitica. Please check the User '
#                    'ID and the API Token.')
#     HABITICA_ERROR = _('Something went wrong while loading Habitica user data.'
#                        ' Please try again or contact the admin.')
# 
#     class Meta:
#         model = Habitica
#         fields = ['user_id', 'api_token']
# 
#     def clean(self):
#         cleaned_data = super(AuthForm, self).clean()
# 
#         api = HabiticaApi(cleaned_data['user_id'], cleaned_data['api_token'])
#         user_details = api.get_user_details()
# 
#         # Validate authentication
#         if not user_details or cleaned_data['user_id'] != user_details[default.JSON_ID]:
#             raise forms.ValidationError(self.AUTH_ERROR)
# 
#         # Get optional user details
#         try:
#             self.instance.name = user_details[default.JSON_NAME]
#             self.instance.email = user_details[default.JSON_EMAIL]
#         except (ValueError, KeyError):
#             logger.exception('Could not get user details: %s', str(user_details))
# 
#         return cleaned_data
# 
# Path: wh_habitica/decorators.py
# def has_habitica(view_func):
#     """
#     Checks if the current user is connected to Habitica.
#     If not, the user is redirected to the Habitica connect page.
#     """
# 
#     def _wrapped_view_func(request, *args, **kwargs):
#         user = request.user
#         if not hasattr(user, 'habitica'):
#             return redirect('habitica:index')
#         if not user.habitica.api_token:
#             return redirect('habitica:index')
#         if not user.habitica.user_id:
#             return redirect('habitica:index')
#         return view_func(request, *args, **kwargs)
#     return _wrapped_view_func
# 
# Path: wunderlist/decorators.py
# def has_wunderlist(view_func):
#     """
#     Checks if the current user is connected to Wunderlist.
#     If not, the user is redirected to the Wunderlist connect page.
#     """
# 
#     def _wrapped_view_func(request, *args, **kwargs):
#         user = request.user
#         if not hasattr(user, 'wunderlist'):
#             return redirect('index')
#         if not user.wunderlist.api_token:
#             return redirect('index')
#         if not user.wunderlist.user_id:
#             return redirect('index')
#         if not user.wunderlist.email:
#             return redirect('index')
#         return view_func(request, *args, **kwargs)
#     return _wrapped_view_func


# Path: wh_habitica/views.py
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.utils.translation import ugettext_lazy as _
from django.contrib import messages
from .forms import AuthForm
from .decorators import has_habitica
from wunderlist.decorators import has_wunderlist




@login_required
@has_wunderlist
def auth(request):
    """
    Authenticates the user to Habitica.
    """

    user = request.user

    if hasattr(user, 'habitica'):
        return redirect('index')

    form = AuthForm()
    if request.method == 'POST':

        form = AuthForm(request.POST)