# 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}

[((682, 714), 'apps.core.helpers.render_to', 'render_to', (['"""accounts/login.html"""'], {}), "('accounts/login.html')\n", (691, 714), False, 'from apps.core.helpers import render_to, ajax_response, get_object_or_None\n'), ((1011, 1034), 'apps.core.helpers.render_to', 'render_to', (['"""index.html"""'], {}), "('index.html')\n", (1020, 1034), False, 'from apps.core.helpers import render_to, ajax_response, get_object_or_None\n'), ((1098, 1132), 'apps.core.helpers.render_to', 'render_to', (['"""accounts/profile.html"""'], {}), "('accounts/profile.html')\n", (1107, 1132), False, 'from apps.core.helpers import render_to, ajax_response, get_object_or_None\n'), ((1499, 1523), 'apps.core.decorators.lock', 'lock', (['"""REGISTER_ALLOWED"""'], {}), "('REGISTER_ALLOWED')\n", (1503, 1523), False, 'from apps.core.decorators import lock, login_required_json\n'), ((1525, 1560), 'apps.core.helpers.render_to', 'render_to', (['"""accounts/register.html"""'], {}), "('accounts/register.html')\n", (1534, 1560), False, 'from apps.core.helpers import render_to, ajax_response, get_object_or_None\n'), ((1931, 1964), 'apps.core.helpers.render_to', 'render_to', (['"""accounts/invite.html"""'], {}), "('accounts/invite.html')\n", (1940, 1964), False, 'from apps.core.helpers import render_to, ajax_response, get_object_or_None\n'), ((2927, 2950), 'apps.accounts.decorators.check_invite', 'check_invite', ([], {'sid': '"""sid"""'}), "(sid='sid')\n", (2939, 2950), False, 'from apps.accounts.decorators import check_invite\n'), ((2952, 2994), 'apps.core.helpers.render_to', 'render_to', (['"""accounts/invite_register.html"""'], {}), "('accounts/invite_register.html')\n", (2961, 2994), False, 'from apps.core.helpers import render_to, ajax_response, get_object_or_None\n'), ((746, 777), 'apps.accounts.forms.LoginForm', 'LoginForm', (['(request.POST or None)'], {}), '(request.POST or None)\n', (755, 777), False, 'from apps.accounts.forms import LoginForm, AccountRegisterForm, SendInviteForm, InviteRegisterForm\n'), ((1060, 1080), 'django.contrib.auth.logout', 'auth.logout', (['request'], {}), '(request)\n', (1071, 1080), False, 'from django.contrib import auth\n'), ((1595, 1636), 'apps.accounts.forms.AccountRegisterForm', 'AccountRegisterForm', (['(request.POST or None)'], {}), '(request.POST or None)\n', (1614, 1636), False, 'from apps.accounts.forms import LoginForm, AccountRegisterForm, SendInviteForm, InviteRegisterForm\n'), ((1997, 2050), 'apps.accounts.forms.SendInviteForm', 'SendInviteForm', (['(request.POST or None)'], {'request': 'request'}), '(request.POST or None, request=request)\n', (2011, 2050), False, 'from apps.accounts.forms import LoginForm, AccountRegisterForm, SendInviteForm, InviteRegisterForm\n'), ((3043, 3078), 'apps.core.helpers.get_object_or_None', 'get_object_or_None', (['Invite'], {'sid': 'sid'}), '(Invite, sid=sid)\n', (3061, 3078), False, 'from apps.core.helpers import render_to, ajax_response, get_object_or_None\n'), ((3149, 3189), 'apps.accounts.forms.InviteRegisterForm', 'InviteRegisterForm', (['(request.POST or None)'], {}), '(request.POST or None)\n', (3167, 3189), False, 'from apps.accounts.forms import LoginForm, AccountRegisterForm, SendInviteForm, InviteRegisterForm\n'), ((896, 921), 'django.contrib.auth.login', 'auth.login', (['request', 'user'], {}), '(request, user)\n', (906, 921), False, 'from django.contrib import auth\n'), ((2382, 2437), 'django.core.urlresolvers.reverse', 'reverse', (['"""accounts:invite-register"""'], {'args': '(invite.sid,)'}), "('accounts:invite-register', args=(invite.sid,))\n", (2389, 2437), False, 'from django.core.urlresolvers import reverse\n'), ((2547, 2590), 'django.utils.translation.ugettext_lazy', '_', (['"""You have been invited to b3ban service"""'], {}), "('You have been invited to b3ban service')\n", (2548, 2590), True, 'from django.utils.translation import ugettext_lazy as _\n')]