from django.shortcuts import render

from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import AdminPasswordChangeForm, PasswordChangeForm, UserCreationForm
from django.contrib.auth import update_session_auth_hash, login, authenticate
from django.contrib import messages
from django.shortcuts import render, redirect

from social_django.models import UserSocialAuth
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect
from rest_framework.authtoken.models import Token

from app.methods import prepare_user


def get_token(request):
    if request.user:
        user = request.user
        prepare_user(user)
        token,_ = Token.objects.get_or_create(user=user)
        url = "travel://?token=" + token.key + '&id=' + str(user.id)
    else:
        url = "travel://error"
    response = HttpResponse(url, status=302)
    response['Location'] = url
    return response

@login_required
def get_facebook_token(request):
    q = get_object_or_404(UserSocialAuth, user=request.user, provider='facebook')
    return HttpResponse(str(q.extra_data))

def signup(request):
    return render(request, 'signup.html')

@login_required
def home(request):
    return render(request, 'home.html')

@login_required
def settings(request):
    user = request.user

    try:
        github_login = user.social_auth.get(provider='github')
    except UserSocialAuth.DoesNotExist:
        github_login = None
    try:
        twitter_login = user.social_auth.get(provider='twitter')
    except UserSocialAuth.DoesNotExist:
        twitter_login = None
    try:
        facebook_login = user.social_auth.get(provider='facebook')
    except UserSocialAuth.DoesNotExist:
        facebook_login = None

    can_disconnect = (user.social_auth.count() > 1 or user.has_usable_password())

    return render(request, 'settings.html', {
        'facebook_login': facebook_login,
        'can_disconnect': can_disconnect
    })

@login_required
def password(request):
    if request.user.has_usable_password():
        PasswordForm = PasswordChangeForm
    else:
        PasswordForm = AdminPasswordChangeForm

    if request.method == 'POST':
        form = PasswordForm(request.user, request.POST)
        if form.is_valid():
            form.save()
            update_session_auth_hash(request, form.user)
            messages.success(request, 'Your password was successfully updated!')
            return redirect('password')
        else:
            messages.error(request, 'Please correct the error below.')
    else:
        form = PasswordForm(request.user)
    return render(request, 'password.html', {'form': form})

[(25, 'django.http.HttpResponse', 'HttpResponse', (['url'], {'status': '(302)'}), False, 'from django.http import HttpResponse\n'), (31, 'django.shortcuts.get_object_or_404', 'get_object_or_404', (['UserSocialAuth'], {'user': 'request.user', 'provider': '"""facebook"""'}), False, 'from django.shortcuts import get_object_or_404, redirect\n'), (35, 'django.shortcuts.render', 'render', (['request', '"""signup.html"""'], {}), False, 'from django.shortcuts import render, redirect\n'), (39, 'django.shortcuts.render', 'render', (['request', '"""home.html"""'], {}), False, 'from django.shortcuts import render, redirect\n'), (60, 'django.shortcuts.render', 'render', (['request', '"""settings.html"""', "{'facebook_login': facebook_login, 'can_disconnect': can_disconnect}"], {}), False, 'from django.shortcuts import render, redirect\n'), (83, 'django.shortcuts.render', 'render', (['request', '"""password.html"""', "{'form': form}"], {}), False, 'from django.shortcuts import render, redirect\n'), (20, 'app.methods.prepare_user', 'prepare_user', (['user'], {}), False, 'from app.methods import prepare_user\n'), (21, 'rest_framework.authtoken.models.Token.objects.get_or_create', 'Token.objects.get_or_create', ([], {'user': 'user'}), False, 'from rest_framework.authtoken.models import Token\n'), (76, 'django.contrib.auth.update_session_auth_hash', 'update_session_auth_hash', (['request', 'form.user'], {}), False, 'from django.contrib.auth import update_session_auth_hash, login, authenticate\n'), (77, 'django.contrib.messages.success', 'messages.success', (['request', '"""Your password was successfully updated!"""'], {}), False, 'from django.contrib import messages\n'), (78, 'django.shortcuts.redirect', 'redirect', (['"""password"""'], {}), False, 'from django.shortcuts import get_object_or_404, redirect\n'), (80, 'django.contrib.messages.error', 'messages.error', (['request', '"""Please correct the error below."""'], {}), False, 'from django.contrib import messages\n')]