2017-09-21 12:05:21 +03:00
|
|
|
# coding: utf-8
|
|
|
|
|
2017-08-27 04:45:13 +03:00
|
|
|
import re
|
2017-08-28 10:22:38 +03:00
|
|
|
import requests
|
2017-08-27 04:45:13 +03:00
|
|
|
from flask import (
|
2017-09-21 12:05:21 +03:00
|
|
|
g, Blueprint, render_template, abort, current_app, redirect, redirect,
|
|
|
|
request, url_for, session, flash
|
2017-08-27 04:45:13 +03:00
|
|
|
)
|
|
|
|
from jinja2 import TemplateNotFound
|
|
|
|
|
2017-09-21 12:05:21 +03:00
|
|
|
from wotstats.openid import oid
|
2017-08-27 04:45:13 +03:00
|
|
|
from wotstats.database import db
|
2017-09-03 18:46:19 +03:00
|
|
|
from wotstats.models import *
|
2017-10-23 02:41:54 +03:00
|
|
|
from wotstats.lib import parse_wargaming_openid_url
|
2017-09-22 00:20:22 +03:00
|
|
|
# from wotstats.tasks import get_stats
|
2017-08-27 04:45:13 +03:00
|
|
|
|
|
|
|
pages_home = Blueprint('pages_home', __name__, template_folder='templates')
|
|
|
|
|
2017-09-21 13:16:04 +03:00
|
|
|
# TODO: add it after login
|
|
|
|
# get_stats.delay()
|
2017-08-27 04:45:13 +03:00
|
|
|
|
|
|
|
@pages_home.route('/', defaults={'page': 'index'})
|
|
|
|
@pages_home.route('/<page>')
|
|
|
|
def index(page):
|
|
|
|
return render_template('pages/index.html')
|
|
|
|
|
|
|
|
|
2017-09-21 12:05:21 +03:00
|
|
|
@pages_home.route('/login', methods=['GET'])
|
2017-08-27 04:45:13 +03:00
|
|
|
@oid.loginhandler
|
|
|
|
def login():
|
2017-08-28 10:22:38 +03:00
|
|
|
print request.args
|
2017-09-21 12:05:21 +03:00
|
|
|
|
2017-08-27 04:45:13 +03:00
|
|
|
if g.user is not None:
|
|
|
|
return redirect(oid.get_next_url())
|
2017-09-21 12:05:21 +03:00
|
|
|
|
2017-08-27 04:45:13 +03:00
|
|
|
return render_template(
|
|
|
|
'pages/login.html',
|
|
|
|
next=oid.get_next_url(),
|
|
|
|
error=oid.fetch_error())
|
|
|
|
|
|
|
|
|
2017-09-21 12:05:21 +03:00
|
|
|
@pages_home.route('/login', methods=['POST'])
|
|
|
|
@oid.loginhandler
|
|
|
|
def login_post():
|
|
|
|
print request.form
|
|
|
|
print request.args
|
2017-09-21 13:16:04 +03:00
|
|
|
|
2017-09-21 12:05:21 +03:00
|
|
|
if g.user is not None:
|
|
|
|
return redirect(oid.get_next_url())
|
|
|
|
|
|
|
|
openid = request.form.get('openid')
|
2017-09-21 13:16:04 +03:00
|
|
|
if not openid:
|
|
|
|
return redirect(url_for('home.login'))
|
2017-09-21 12:05:21 +03:00
|
|
|
|
2017-09-21 13:16:04 +03:00
|
|
|
return oid.try_login(
|
|
|
|
openid, ask_for=['email', 'nickname'], ask_for_optional=['fullname'])
|
2017-09-21 12:05:21 +03:00
|
|
|
|
2017-09-21 13:16:04 +03:00
|
|
|
|
|
|
|
@pages_home.route('/create-profile', methods=['GET'])
|
2017-08-27 04:45:13 +03:00
|
|
|
def create_profile():
|
|
|
|
if g.user is not None or 'openid' not in session:
|
|
|
|
return redirect(url_for('pages_home.index'))
|
|
|
|
|
2017-09-21 13:16:04 +03:00
|
|
|
return render_template('pages/create_profile.html', next=oid.get_next_url())
|
|
|
|
|
|
|
|
|
|
|
|
@pages_home.route('/create-profile', methods=['POST'])
|
2017-09-22 00:20:22 +03:00
|
|
|
def create_profile_post():
|
2017-09-21 13:16:04 +03:00
|
|
|
if g.user is not None or 'openid' not in session:
|
|
|
|
return redirect(url_for('pages_home.index'))
|
|
|
|
|
|
|
|
name = request.form['name']
|
|
|
|
email = request.form['email']
|
|
|
|
if not name:
|
|
|
|
flash(u'Error: you have to provide a name')
|
|
|
|
elif '@' not in email:
|
|
|
|
flash(u'Error: you have to enter a valid email address')
|
|
|
|
return redirect(url_for('home.create_profile'))
|
|
|
|
else:
|
|
|
|
flash(u'Profile successfully created')
|
|
|
|
u = User(email)
|
|
|
|
u.name = name
|
|
|
|
u.openid = session['openid']
|
|
|
|
u.password = ''
|
|
|
|
|
|
|
|
db.session.add(u)
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(oid.get_next_url())
|
2017-08-27 04:45:13 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pages_home.route('/logout')
|
|
|
|
def logout():
|
|
|
|
session.pop('openid', None)
|
2017-08-28 10:22:38 +03:00
|
|
|
session.pop('token', None)
|
2017-09-03 18:46:19 +03:00
|
|
|
session.pop('user', None)
|
2017-08-28 10:22:38 +03:00
|
|
|
# flash(u'You were signed out')
|
2017-08-27 04:45:13 +03:00
|
|
|
return redirect(oid.get_next_url())
|
|
|
|
|
2017-08-28 10:22:38 +03:00
|
|
|
|
|
|
|
@pages_home.route('/token')
|
|
|
|
def token():
|
|
|
|
print request.args
|
|
|
|
print request.form
|
|
|
|
|
2017-09-03 18:46:19 +03:00
|
|
|
if 'openid' not in session or 'user' in session:
|
2017-08-28 10:22:38 +03:00
|
|
|
return redirect(url_for('pages_home.index'))
|
|
|
|
# ImmutableMultiDict([('status', u'ok'), ('access_token', u'a4d0a13df7c733102fbf6cd650794c6d047e91aa'), ('nickname', u'CrazyPants1999'), ('account_id', u'69552613'), ('', u'1505047809')])
|
|
|
|
if request.args.get('status') == 'ok' and request.args.get('access_token'):
|
2017-09-03 18:46:19 +03:00
|
|
|
token = UserWotTokens.query.filter_by(user=session['user'])
|
|
|
|
access_token = request.args.get('access_token')
|
|
|
|
expires_at = request.args.get('expires_at')
|
|
|
|
|
|
|
|
if token.count() == 0:
|
|
|
|
t = UserWotTokens()
|
|
|
|
t.user = session['user']
|
|
|
|
t.access_token = access_token
|
|
|
|
t.expires_at = expires_at
|
|
|
|
db.session.add(t)
|
|
|
|
db.session.commit()
|
|
|
|
else:
|
|
|
|
token.access_token = access_token
|
|
|
|
token.expires_at = expires_at
|
|
|
|
db.session.commit()
|
|
|
|
|
2017-08-28 10:22:38 +03:00
|
|
|
session['token'] = {
|
2017-09-03 18:46:19 +03:00
|
|
|
'access_token': access_token,
|
|
|
|
'expires_at': expires_at
|
2017-08-28 10:22:38 +03:00
|
|
|
}
|
|
|
|
return redirect(oid.get_next_url())
|
|
|
|
|
2017-09-21 13:16:04 +03:00
|
|
|
url = current_app.config['WG_TOKEN_URL']
|
|
|
|
payload = {
|
|
|
|
'application_id': current_app.config['WG_APPLICATION_ID'],
|
|
|
|
'redirect_uri': current_app.config['WG_REDIRECT_URL'],
|
|
|
|
'nofollow': 1,
|
|
|
|
}
|
|
|
|
response = requests.get(url, params=payload).json()
|
2017-08-28 10:22:38 +03:00
|
|
|
|
|
|
|
if response.get('status') == 'ok':
|
|
|
|
return redirect(response.get('data', {}).get('location'))
|
|
|
|
return redirect(oid.get_next_url())
|
|
|
|
|
|
|
|
|
2017-08-27 04:45:13 +03:00
|
|
|
@oid.after_login
|
|
|
|
def create_or_login(resp):
|
2017-10-23 02:41:54 +03:00
|
|
|
# resp
|
|
|
|
# 'aim', 'blog', 'country', 'date_of_birth', 'email', 'extensions', 'fullname', 'gender', 'icq', 'identity_url', 'image', 'jabber', 'language', 'month_of_birth', 'msn', 'nickname', 'phone', 'postcode', 'skype', 'timezone', 'website', 'yahoo', 'year_of_birth'
|
|
|
|
|
2017-08-27 04:45:13 +03:00
|
|
|
session['openid'] = resp.identity_url
|
2017-08-28 10:22:38 +03:00
|
|
|
session['token'] = None
|
2017-09-03 18:46:19 +03:00
|
|
|
session['user'] = None
|
2017-08-28 10:22:38 +03:00
|
|
|
|
2017-08-27 04:45:13 +03:00
|
|
|
user = User.query.filter_by(openid=resp.identity_url).first()
|
2017-08-28 10:22:38 +03:00
|
|
|
|
2017-09-03 18:46:19 +03:00
|
|
|
if not user:
|
|
|
|
return redirect(url_for(
|
|
|
|
'pages_home.create_profile',
|
|
|
|
next=oid.get_next_url(),
|
|
|
|
name=resp.fullname or resp.nickname,
|
|
|
|
email=resp.email))
|
2017-08-28 10:22:38 +03:00
|
|
|
|
2017-10-23 02:41:54 +03:00
|
|
|
wot_account_id, wot_account_nickname = parse_wargaming_openid_url(resp.identity_url)
|
|
|
|
|
|
|
|
user_wot_account = WotAccounts.query.filter_by(account_id=wot_account_id).first()
|
|
|
|
if not user_wot_account:
|
|
|
|
x = WotAccounts(account_id=wot_account_id, nickname=wot_account_nickname)
|
|
|
|
x.user = user.id
|
|
|
|
db.session.add(x)
|
|
|
|
db.session.commit()
|
|
|
|
else:
|
|
|
|
if user_wot_account.user != user.id:
|
|
|
|
user_wot_account.user = user.id
|
|
|
|
db.session.commit()
|
|
|
|
|
2017-09-03 18:46:19 +03:00
|
|
|
session['user'] = user.id
|
|
|
|
|
|
|
|
# flash(u'Successfully signed in')
|
|
|
|
g.user = user
|
|
|
|
if not session['token']:
|
|
|
|
return redirect(url_for('pages_home.token'))
|
|
|
|
return redirect(oid.get_next_url())
|