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, 15, 25, 44), 'django.http.HttpResponse', 'HttpResponse', (), '', False, 'from django.http import HttpResponse\n'), ((31, 8, 31, 81), 'django.shortcuts.get_object_or_404', 'get_object_or_404', (), '', False, 'from django.shortcuts import get_object_or_404, redirect\n'), ((35, 11, 35, 41), 'django.shortcuts.render', 'render', ({(35, 18, 35, 25): 'request', (35, 27, 35, 40): '"""signup.html"""'}, {}), "(request, 'signup.html')", False, 'from django.shortcuts import render, redirect\n'), ((39, 11, 39, 39), 'django.shortcuts.render', 'render', ({(39, 18, 39, 25): 'request', (39, 27, 39, 38): '"""home.html"""'}, {}), "(request, 'home.html')", False, 'from django.shortcuts import render, redirect\n'), ((60, 11, 63, 6), 'django.shortcuts.render', 'render', ({(60, 18, 60, 25): 'request', (60, 27, 60, 42): '"""settings.html"""', (60, 44, 63, 5): "{'facebook_login': facebook_login, 'can_disconnect': can_disconnect}"}, {}), "(request, 'settings.html', {'facebook_login': facebook_login,\n    'can_disconnect': can_disconnect})", False, 'from django.shortcuts import render, redirect\n'), ((83, 11, 83, 59), 'django.shortcuts.render', 'render', ({(83, 18, 83, 25): 'request', (83, 27, 83, 42): '"""password.html"""', (83, 44, 83, 58): "{'form': form}"}, {}), "(request, 'password.html', {'form': form})", False, 'from django.shortcuts import render, redirect\n'), ((20, 8, 20, 26), 'app.methods.prepare_user', 'prepare_user', ({(20, 21, 20, 25): 'user'}, {}), '(user)', False, 'from app.methods import prepare_user\n'), ((21, 18, 21, 56), 'rest_framework.authtoken.models.Token.objects.get_or_create', 'Token.objects.get_or_create', (), '', False, 'from rest_framework.authtoken.models import Token\n'), ((76, 12, 76, 56), 'django.contrib.auth.update_session_auth_hash', 'update_session_auth_hash', ({(76, 37, 76, 44): 'request', (76, 46, 76, 55): 'form.user'}, {}), '(request, form.user)', False, 'from django.contrib.auth import update_session_auth_hash, login, authenticate\n'), ((77, 12, 77, 80), 'django.contrib.messages.success', 'messages.success', ({(77, 29, 77, 36): 'request', (77, 38, 77, 79): '"""Your password was successfully updated!"""'}, {}), "(request, 'Your password was successfully updated!')", False, 'from django.contrib import messages\n'), ((78, 19, 78, 39), 'django.shortcuts.redirect', 'redirect', ({(78, 28, 78, 38): '"""password"""'}, {}), "('password')", False, 'from django.shortcuts import get_object_or_404, redirect\n'), ((80, 12, 80, 70), 'django.contrib.messages.error', 'messages.error', ({(80, 27, 80, 34): 'request', (80, 36, 80, 69): '"""Please correct the error below."""'}, {}), "(request, 'Please correct the error below.')", False, 'from django.contrib import messages\n')]