# Create your views here.
# -*- coding: utf-8 -*-
from apps.core.helpers import render_to, ajax_response, get_object_or_None
from apps.core.decorators import lock, login_required_json
from apps.accounts.models import Invite
from apps.accounts.decorators import check_invite
from apps.accounts.forms import (
    LoginForm, AccountRegisterForm, SendInviteForm, InviteRegisterForm
)

from django.core.mail import send_mail
from django.core.urlresolvers import reverse
from django.contrib import auth
from django.contrib.auth.decorators import login_required
from django.conf import settings
from django.db import transaction
from django.utils.translation import ugettext_lazy as _


@render_to('accounts/login.html')
def login(request):
    form = LoginForm(request.POST or None)
    if request.method == 'POST':
        if form.is_valid():
            user = form.cleaned_data['user']
            auth.login(request, user)
            return {'redirect': 'core:index'}
    return {
        'form': form
    }


@render_to('index.html')
def logout(request):
    auth.logout(request)
    return {}


@render_to('accounts/profile.html')
def profile(request):
    return {}


@login_required_json
@ajax_response
def generate_new_api_key(request):
    if request.method == 'POST':
        request.user.api_key.key = request.user.api_key.generate_key()
        request.user.api_key.save()
        key = request.user.api_key.key
        return {'success': True, 'key': key}
    return {'success': False}


@lock("REGISTER_ALLOWED")
@render_to('accounts/register.html')
def register(request):
    form = AccountRegisterForm(request.POST or None)
    if request.method == "POST":
        if form.is_valid():
            user = form.save(commit=False)
            user.set_password(form.cleaned_data['password'])
            user.save()
            return {'redirect': 'core:index'}
    return {
        'form': form
    }


@login_required
@render_to('accounts/invite.html')
def invite(request):
    form = SendInviteForm(request.POST or None, request=request)
    if request.method == 'POST':
        if form.is_valid():
            form.save(commit=False)
            invite = form.instance
            email = form.cleaned_data['email']
            msg = settings.INVITE_MESSAGE % {
                'user': request.user.username,
                'link': "http://b3ban.blacklibrary.ru%s" % reverse('accounts:invite-register', args=(invite.sid, ))
            }
            #no mail send, no money :)
            send_mail(
                subject=unicode(_('You have been invited to b3ban service')),
                message=unicode(msg),
                from_email=settings.EMAIL_FROM,
                recipient_list=[email]
            )
            invite.save()
            return {'redirect': 'accounts:invite-success'}
    return {
        'form': form
    }


#@check for possibility to register
@transaction.commit_on_success
@check_invite(sid='sid')
@render_to('accounts/invite_register.html')
def invite_register(request, sid):
    invite = get_object_or_None(Invite, sid=sid)
    if not invite:
        return {'redirect': 'core:ufo'}
    form = InviteRegisterForm(request.POST or None)
    if request.method == 'POST':
        if form.is_valid():
            invite.is_verified = True
            invite.save()
            user = form.save(commit=False)
            user.email = invite.email
            user.set_password(form.cleaned_data['password'])
            user.save()
            return {'redirect': 'accounts:invite-register-success'}
    return {'form': form, 'sid': sid}

[((20, 1, 20, 33), 'apps.core.helpers.render_to', 'render_to', ({(20, 11, 20, 32): '"""accounts/login.html"""'}, {}), "('accounts/login.html')", False, 'from apps.core.helpers import render_to, ajax_response, get_object_or_None\n'), ((33, 1, 33, 24), 'apps.core.helpers.render_to', 'render_to', ({(33, 11, 33, 23): '"""index.html"""'}, {}), "('index.html')", False, 'from apps.core.helpers import render_to, ajax_response, get_object_or_None\n'), ((39, 1, 39, 35), 'apps.core.helpers.render_to', 'render_to', ({(39, 11, 39, 34): '"""accounts/profile.html"""'}, {}), "('accounts/profile.html')", False, 'from apps.core.helpers import render_to, ajax_response, get_object_or_None\n'), ((55, 1, 55, 25), 'apps.core.decorators.lock', 'lock', ({(55, 6, 55, 24): '"""REGISTER_ALLOWED"""'}, {}), "('REGISTER_ALLOWED')", False, 'from apps.core.decorators import lock, login_required_json\n'), ((56, 1, 56, 36), 'apps.core.helpers.render_to', 'render_to', ({(56, 11, 56, 35): '"""accounts/register.html"""'}, {}), "('accounts/register.html')", False, 'from apps.core.helpers import render_to, ajax_response, get_object_or_None\n'), ((71, 1, 71, 34), 'apps.core.helpers.render_to', 'render_to', ({(71, 11, 71, 33): '"""accounts/invite.html"""'}, {}), "('accounts/invite.html')", False, 'from apps.core.helpers import render_to, ajax_response, get_object_or_None\n'), ((99, 1, 99, 24), 'apps.accounts.decorators.check_invite', 'check_invite', (), '', False, 'from apps.accounts.decorators import check_invite\n'), ((100, 1, 100, 43), 'apps.core.helpers.render_to', 'render_to', ({(100, 11, 100, 42): '"""accounts/invite_register.html"""'}, {}), "('accounts/invite_register.html')", False, 'from apps.core.helpers import render_to, ajax_response, get_object_or_None\n'), ((22, 11, 22, 42), 'apps.accounts.forms.LoginForm', 'LoginForm', ({(22, 21, 22, 41): 'request.POST or None'}, {}), '(request.POST or None)', False, 'from apps.accounts.forms import LoginForm, AccountRegisterForm, SendInviteForm, InviteRegisterForm\n'), ((35, 4, 35, 24), 'django.contrib.auth.logout', 'auth.logout', ({(35, 16, 35, 23): 'request'}, {}), '(request)', False, 'from django.contrib import auth\n'), ((58, 11, 58, 52), 'apps.accounts.forms.AccountRegisterForm', 'AccountRegisterForm', ({(58, 31, 58, 51): 'request.POST or None'}, {}), '(request.POST or None)', False, 'from apps.accounts.forms import LoginForm, AccountRegisterForm, SendInviteForm, InviteRegisterForm\n'), ((73, 11, 73, 64), 'apps.accounts.forms.SendInviteForm', 'SendInviteForm', (), '', False, 'from apps.accounts.forms import LoginForm, AccountRegisterForm, SendInviteForm, InviteRegisterForm\n'), ((102, 13, 102, 48), 'apps.core.helpers.get_object_or_None', 'get_object_or_None', (), '', False, 'from apps.core.helpers import render_to, ajax_response, get_object_or_None\n'), ((105, 11, 105, 51), 'apps.accounts.forms.InviteRegisterForm', 'InviteRegisterForm', ({(105, 30, 105, 50): 'request.POST or None'}, {}), '(request.POST or None)', False, 'from apps.accounts.forms import LoginForm, AccountRegisterForm, SendInviteForm, InviteRegisterForm\n'), ((26, 12, 26, 37), 'django.contrib.auth.login', 'auth.login', ({(26, 23, 26, 30): 'request', (26, 32, 26, 36): 'user'}, {}), '(request, user)', False, 'from django.contrib import auth\n'), ((81, 59, 81, 115), 'django.core.urlresolvers.reverse', 'reverse', (), '', False, 'from django.core.urlresolvers import reverse\n'), ((85, 32, 85, 75), 'django.utils.translation.ugettext_lazy', '_', ({(85, 34, 85, 74): '"""You have been invited to b3ban service"""'}, {}), "('You have been invited to b3ban service')", True, 'from django.utils.translation import ugettext_lazy as _\n')]