get account stats from wargamming
This commit is contained in:
parent
af8eb4fa7b
commit
fb6159f65d
11 changed files with 181 additions and 123 deletions
|
@ -5,3 +5,4 @@ validators
|
|||
jsonschema
|
||||
Flask-JWT
|
||||
Flask-OpenID
|
||||
requests
|
||||
|
|
|
@ -2,7 +2,8 @@
|
|||
from flask import Flask, render_template, g, session
|
||||
from wotstats.database import db, migrate
|
||||
from wotstats.openid import oid
|
||||
from wotstats.views import pages_home
|
||||
from wotstats.views.home import pages_home
|
||||
from wotstats.views.account import pages_account
|
||||
|
||||
def init_app():
|
||||
app = Flask(__name__)
|
||||
|
@ -21,6 +22,7 @@ def init_app():
|
|||
# jwt = JWT(app, authenticate, identity)
|
||||
|
||||
app.register_blueprint(pages_home)
|
||||
app.register_blueprint(pages_account)
|
||||
|
||||
@app.before_request
|
||||
def lookup_current_user():
|
||||
|
|
11
wotstats/lib/__init__.py
Normal file
11
wotstats/lib/__init__.py
Normal file
|
@ -0,0 +1,11 @@
|
|||
import re
|
||||
|
||||
|
||||
def parse_wargaming_openid_url(url):
|
||||
"""
|
||||
>>> parse_wargaming_openid_url('https://ru.wargaming.net/id/69552613-CrazyPants1999/')
|
||||
('69552613', 'CrazyPants1999')
|
||||
|
||||
"""
|
||||
pattern = '^https?.*id\/([0-9]+)-(\w+)\/$'
|
||||
return re.findall(pattern, url)[0]
|
|
@ -4,6 +4,21 @@
|
|||
</head>
|
||||
<body>
|
||||
<h1>wotstats</h1>
|
||||
{% if session['openid'] %}
|
||||
<ul>
|
||||
{% if 'openid' in session %}
|
||||
<li>{{ session['openid'] }}</li>
|
||||
{% else %}
|
||||
<li>{{ session['email'] }}</li>
|
||||
{% endif %}
|
||||
<li><a href="{{ url_for('pages_account.index') }}">my statistics</a></li>
|
||||
<li><a href="{{ url_for('pages_home.logout') }}">logout</a></li>
|
||||
</ul>
|
||||
{% else %}
|
||||
<ul>
|
||||
<li><a href="{{ url_for('pages_home.login') }}">login</a></li>
|
||||
</ul>
|
||||
{% endif %}
|
||||
{% block content %}{% endblock %}
|
||||
</body>
|
||||
</html>
|
||||
|
|
5
wotstats/templates/pages/account/index.html
Normal file
5
wotstats/templates/pages/account/index.html
Normal file
|
@ -0,0 +1,5 @@
|
|||
{% extends 'layouts/main.html' %}
|
||||
|
||||
{% block content %}
|
||||
{{account_statistics}}
|
||||
{% endblock %}
|
5
wotstats/templates/pages/account/statistics.html
Normal file
5
wotstats/templates/pages/account/statistics.html
Normal file
|
@ -0,0 +1,5 @@
|
|||
{% extends 'layouts/main.html' %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
{% endblock %}
|
|
@ -1,7 +1,5 @@
|
|||
{% extends 'layouts/main.html' %}
|
||||
|
||||
{% block content %}
|
||||
<ul>
|
||||
<li><a href="{{ url_for('page_auth_step1') }}">auth</a></li>
|
||||
</ul>
|
||||
|
||||
{% endblock %}
|
||||
|
|
|
@ -1,19 +1,5 @@
|
|||
{% extends 'layouts/main.html' %}
|
||||
|
||||
{% block content %}
|
||||
{% if session['openid'] %}
|
||||
<ul>
|
||||
{% if 'openid' in session %}
|
||||
<li>{{ session['openid'] }}</li>
|
||||
{% else %}
|
||||
<li>{{ session['email'] }}</li>
|
||||
{% endif %}
|
||||
<li><a href="{{ url_for('pages_home.logout') }}">statistics</a></li>
|
||||
<li><a href="{{ url_for('pages_home.logout') }}">logout</a></li>
|
||||
</ul>
|
||||
{% else %}
|
||||
<ul>
|
||||
<li><a href="{{ url_for('pages_home.login') }}">login</a></li>
|
||||
</ul>
|
||||
{% endif %}
|
||||
|
||||
{% endblock %}
|
||||
|
|
|
@ -1,104 +0,0 @@
|
|||
import re
|
||||
from flask import (
|
||||
g, Blueprint, render_template, abort, current_app, redirect,
|
||||
redirect, request, url_for, session, flash
|
||||
)
|
||||
from jinja2 import TemplateNotFound
|
||||
from wotstats.openid import oid
|
||||
|
||||
from wotstats.database import db
|
||||
from wotstats.models import User
|
||||
|
||||
pages_home = Blueprint('pages_home', __name__, template_folder='templates')
|
||||
|
||||
# def show(page):
|
||||
# try:
|
||||
# return render_template('pages/%s.html' % page)
|
||||
# except TemplateNotFound:
|
||||
# abort(404)
|
||||
|
||||
def parse_wargaming_openid_url(url):
|
||||
"""
|
||||
>>> parse_wargaming_openid_url('https://ru.wargaming.net/id/69552613-CrazyPants1999/')
|
||||
('69552613', 'CrazyPants1999')
|
||||
|
||||
"""
|
||||
pattern = '^https?.*id\/([0-9]+)-(\w+)\/$'
|
||||
return re.findall(pattern, url)[0]
|
||||
|
||||
|
||||
@pages_home.route('/', defaults={'page': 'index'})
|
||||
@pages_home.route('/<page>')
|
||||
def index(page):
|
||||
z = session['openid']
|
||||
return render_template('pages/index.html')
|
||||
|
||||
|
||||
@pages_home.route('/auth.html')
|
||||
def auth_step1():
|
||||
return render_template('pages/auth_step1.html')
|
||||
|
||||
|
||||
@pages_home.route('/login', methods=['GET', 'POST'])
|
||||
@oid.loginhandler
|
||||
def login():
|
||||
if g.user is not None:
|
||||
return redirect(oid.get_next_url())
|
||||
if request.method == 'POST':
|
||||
openid = request.form.get('openid')
|
||||
if openid:
|
||||
return oid.try_login(
|
||||
openid,
|
||||
ask_for=['email', 'nickname'],
|
||||
ask_for_optional=['fullname'])
|
||||
return render_template(
|
||||
'pages/login.html',
|
||||
next=oid.get_next_url(),
|
||||
error=oid.fetch_error())
|
||||
|
||||
|
||||
@pages_home.route('/create-profile', methods=['GET', 'POST'])
|
||||
def create_profile():
|
||||
if g.user is not None or 'openid' not in session:
|
||||
return redirect(url_for('pages_home.index'))
|
||||
if request.method == 'POST':
|
||||
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')
|
||||
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())
|
||||
return render_template(
|
||||
'pages/create_profile.html',
|
||||
next=oid.get_next_url())
|
||||
|
||||
|
||||
@pages_home.route('/logout')
|
||||
def logout():
|
||||
session.pop('openid', None)
|
||||
flash(u'You were signed out')
|
||||
return redirect(oid.get_next_url())
|
||||
|
||||
@oid.after_login
|
||||
def create_or_login(resp):
|
||||
session['openid'] = resp.identity_url
|
||||
user = User.query.filter_by(openid=resp.identity_url).first()
|
||||
if user is not None:
|
||||
flash(u'Successfully signed in')
|
||||
g.user = user
|
||||
return redirect(oid.get_next_url())
|
||||
return redirect(url_for(
|
||||
'pages_home.create_profile',
|
||||
next=oid.get_next_url(),
|
||||
name=resp.fullname or resp.nickname,
|
||||
email=resp.email))
|
35
wotstats/views/account.py
Normal file
35
wotstats/views/account.py
Normal file
|
@ -0,0 +1,35 @@
|
|||
import requests
|
||||
from flask import (
|
||||
g, Blueprint, render_template, abort, current_app, redirect,
|
||||
redirect, request, url_for, session, flash
|
||||
)
|
||||
from jinja2 import TemplateNotFound
|
||||
from wotstats.openid import oid
|
||||
|
||||
from wotstats.database import db
|
||||
from wotstats.models import User
|
||||
from wotstats.lib import parse_wargaming_openid_url
|
||||
|
||||
pages_account = Blueprint('pages_account', __name__, url_prefix='/account', template_folder='templates')
|
||||
|
||||
def __get_player_personal_data():
|
||||
user_id = parse_wargaming_openid_url(session['openid'])[0]
|
||||
# current_app.config['WG_ID']
|
||||
__ = requests.get(
|
||||
'https://api.worldoftanks.ru/wot/account/info/?application_id={}&account_id={}'.format(
|
||||
current_app.config['WG_ID'], user_id
|
||||
)
|
||||
).json()
|
||||
return __.get('data', {}).get(user_id)
|
||||
|
||||
|
||||
@pages_account.route('/')
|
||||
def index():
|
||||
account_statistics = __get_player_personal_data()
|
||||
return render_template('pages/account/index.html', account_statistics=account_statistics)
|
||||
|
||||
|
||||
@pages_account.route('/statistics.html')
|
||||
def statistics():
|
||||
return render_template(
|
||||
'pages/account/statistics.html')
|
104
wotstats/views/home.py
Normal file
104
wotstats/views/home.py
Normal file
|
@ -0,0 +1,104 @@
|
|||
import re
|
||||
from flask import (
|
||||
g, Blueprint, render_template, abort, current_app, redirect,
|
||||
redirect, request, url_for, session, flash
|
||||
)
|
||||
from jinja2 import TemplateNotFound
|
||||
from wotstats.openid import oid
|
||||
|
||||
from wotstats.database import db
|
||||
from wotstats.models import User
|
||||
|
||||
pages_home = Blueprint('pages_home', __name__, template_folder='templates')
|
||||
|
||||
# def show(page):
|
||||
# try:
|
||||
# return render_template('pages/%s.html' % page)
|
||||
# except TemplateNotFound:
|
||||
# abort(404)
|
||||
|
||||
def parse_wargaming_openid_url(url):
|
||||
"""
|
||||
>>> parse_wargaming_openid_url('https://ru.wargaming.net/id/69552613-CrazyPants1999/')
|
||||
('69552613', 'CrazyPants1999')
|
||||
|
||||
"""
|
||||
pattern = '^https?.*id\/([0-9]+)-(\w+)\/$'
|
||||
return re.findall(pattern, url)[0]
|
||||
|
||||
|
||||
@pages_home.route('/', defaults={'page': 'index'})
|
||||
@pages_home.route('/<page>')
|
||||
def index(page):
|
||||
z = session['openid']
|
||||
return render_template('pages/index.html')
|
||||
|
||||
|
||||
@pages_home.route('/auth.html')
|
||||
def auth_step1():
|
||||
return render_template('pages/auth_step1.html')
|
||||
|
||||
|
||||
@pages_home.route('/login', methods=['GET', 'POST'])
|
||||
@oid.loginhandler
|
||||
def login():
|
||||
if g.user is not None:
|
||||
return redirect(oid.get_next_url())
|
||||
if request.method == 'POST':
|
||||
openid = request.form.get('openid')
|
||||
if openid:
|
||||
return oid.try_login(
|
||||
openid,
|
||||
ask_for=['email', 'nickname'],
|
||||
ask_for_optional=['fullname'])
|
||||
return render_template(
|
||||
'pages/login.html',
|
||||
next=oid.get_next_url(),
|
||||
error=oid.fetch_error())
|
||||
|
||||
|
||||
@pages_home.route('/create-profile', methods=['GET', 'POST'])
|
||||
def create_profile():
|
||||
if g.user is not None or 'openid' not in session:
|
||||
return redirect(url_for('pages_home.index'))
|
||||
if request.method == 'POST':
|
||||
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')
|
||||
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())
|
||||
return render_template(
|
||||
'pages/create_profile.html',
|
||||
next=oid.get_next_url())
|
||||
|
||||
|
||||
@pages_home.route('/logout')
|
||||
def logout():
|
||||
session.pop('openid', None)
|
||||
flash(u'You were signed out')
|
||||
return redirect(oid.get_next_url())
|
||||
|
||||
@oid.after_login
|
||||
def create_or_login(resp):
|
||||
session['openid'] = resp.identity_url
|
||||
user = User.query.filter_by(openid=resp.identity_url).first()
|
||||
if user is not None:
|
||||
flash(u'Successfully signed in')
|
||||
g.user = user
|
||||
return redirect(oid.get_next_url())
|
||||
return redirect(url_for(
|
||||
'pages_home.create_profile',
|
||||
next=oid.get_next_url(),
|
||||
name=resp.fullname or resp.nickname,
|
||||
email=resp.email))
|
Reference in a new issue