update
This commit is contained in:
parent
76518a2354
commit
5694fe1676
8 changed files with 206 additions and 28 deletions
Binary file not shown.
50
manage.py
50
manage.py
|
@ -12,7 +12,55 @@ migrate = Migrate(app, db)
|
||||||
manager = Manager(app)
|
manager = Manager(app)
|
||||||
# Регистрируем команду, реализованную в виде потомка класса Command
|
# Регистрируем команду, реализованную в виде потомка класса Command
|
||||||
manager.add_command('db', MigrateCommand)
|
manager.add_command('db', MigrateCommand)
|
||||||
# managet.add_command('wot_harvers_all')
|
|
||||||
|
@manager.command
|
||||||
|
def wot_harvest_accounts():
|
||||||
|
"""Creates the admin user."""
|
||||||
|
import requests
|
||||||
|
from datetime import datetime
|
||||||
|
from sqlalchemy import func
|
||||||
|
from wotstats.models.wotaccounts import WotAccounts
|
||||||
|
from wotstats.models.wotaccountsstats import WotAccountsStats
|
||||||
|
|
||||||
|
app_id = app.config['WG_APPLICATION_ID']
|
||||||
|
url = 'https://api.worldoftanks.ru/wot/account/info/'
|
||||||
|
# Default account_id
|
||||||
|
account_id = 35
|
||||||
|
|
||||||
|
# TODO: get max account_id from wot_accounts
|
||||||
|
_account_id = db.session.query(func.max(WotAccounts.account_id)).scalar()
|
||||||
|
if _account_id:
|
||||||
|
account_id = _account_id
|
||||||
|
|
||||||
|
while True:
|
||||||
|
account_id += 1
|
||||||
|
__ = requests.get('{}?application_id={}&account_id={}'.format(url, app_id, account_id)).json()
|
||||||
|
if not __.get('data', {}).get("{}".format(account_id)):
|
||||||
|
print('account_id: {} SKIPPED'.format(account_id))
|
||||||
|
continue
|
||||||
|
# copy results
|
||||||
|
account_statistics = __.get('data', {}).get("{}".format(account_id)).get('statistics', {})
|
||||||
|
account_data = __['data']["{}".format(account_id)]
|
||||||
|
account_data.pop('statistics', None)
|
||||||
|
|
||||||
|
db.session.add(WotAccounts(account_id=account_id, nickname=account_data.get('nickname')))
|
||||||
|
db.session.commit()
|
||||||
|
db.session.flush()
|
||||||
|
|
||||||
|
ws = WotAccountsStats()
|
||||||
|
ws.account_id = account_id
|
||||||
|
ws.created_at = datetime.now()
|
||||||
|
ws.data = account_data
|
||||||
|
ws.statistics = account_statistics
|
||||||
|
db.session.add(ws)
|
||||||
|
db.session.commit()
|
||||||
|
db.session.flush()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
print('account_id: {} nickname: {} OK'.format(account_id, account_data.get('nickname')))
|
||||||
|
|
||||||
|
# break
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
manager.run()
|
manager.run()
|
||||||
|
|
36
migrations/versions/49d6ea45c818_.py
Normal file
36
migrations/versions/49d6ea45c818_.py
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
"""empty message
|
||||||
|
|
||||||
|
Revision ID: 49d6ea45c818
|
||||||
|
Revises: 3477657c864b
|
||||||
|
Create Date: 2017-09-17 18:59:29.455568
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.dialects.postgresql import JSONB
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '49d6ea45c818'
|
||||||
|
down_revision = '3477657c864b'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
op.create_unique_constraint(op.f('uq_wot_accounts_account_id'), 'wot_accounts', ['account_id'])
|
||||||
|
|
||||||
|
op.create_table(
|
||||||
|
'wot_accounts_stats',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('account_id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||||
|
sa.Column('data', JSONB, default='{}'),
|
||||||
|
sa.Column('statistics', JSONB, default='{}'),
|
||||||
|
sa.ForeignKeyConstraint(['account_id'], ['wot_accounts.account_id'], ),
|
||||||
|
sa.PrimaryKeyConstraint('id')
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
op.drop_table('wot_accounts_stats')
|
||||||
|
op.drop_constraint(op.f('uq_wot_accounts_account_id'), 'wot_accounts', type_='foreignkey')
|
|
@ -2,5 +2,8 @@
|
||||||
from .user import User
|
from .user import User
|
||||||
from .userwottokens import UserWotTokens
|
from .userwottokens import UserWotTokens
|
||||||
from .userwotdetails import UserWotDetails
|
from .userwotdetails import UserWotDetails
|
||||||
from .userwotstats import UserWotStats
|
# from .userwotstats import UserWotStats
|
||||||
from .userwotdata import UserWotData
|
from .userwotdata import UserWotData
|
||||||
|
|
||||||
|
from .wotaccounts import WotAccounts
|
||||||
|
from .wotaccountsstats import WotAccountsStats
|
||||||
|
|
19
wotstats/models/wotaccountsstats.py
Normal file
19
wotstats/models/wotaccountsstats.py
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
|
||||||
|
from sqlalchemy.dialects.postgresql import ARRAY, HSTORE, JSONB
|
||||||
|
from wotstats.database import db
|
||||||
|
|
||||||
|
|
||||||
|
class WotAccountsStats(db.Model):
|
||||||
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
|
account_id = db.Column(db.Integer, db.ForeignKey('wot_accounts.id'), nullable=False)
|
||||||
|
created_at = db.Column(db.DateTime, nullable=False)
|
||||||
|
# WG
|
||||||
|
data = db.Column(JSONB, nullable=False, default={})
|
||||||
|
statistics = db.Column(JSONB, nullable=False, default={})
|
||||||
|
|
||||||
|
# def __init__(self):
|
||||||
|
# pass
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return '<WotAccountsStats id={} account_id={} created_at={}>'.format(
|
||||||
|
self.id, self.user, self.created_at)
|
|
@ -2,31 +2,21 @@
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h2>My Statistics</h2>
|
<h2>My Statistics</h2>
|
||||||
<ul>
|
{% for x in account_statistics.statistics %}
|
||||||
{% for z in account_statistics %}
|
<h3>{{ x }}</h3>
|
||||||
<li>{{ z }}
|
{% if account_statistics.statistics[x] is mapping %}
|
||||||
{% if account_statistics[z] is mapping %}
|
<table class="mui-table mui-table--bordered">
|
||||||
<ul>
|
{% for c in account_statistics.statistics[x] %}
|
||||||
{% for x in account_statistics[z] %}
|
<tr>
|
||||||
<li>{{ x }}
|
<td>{{ c }}</td><td>{{ account_statistics.statistics[x][c] }}</td>
|
||||||
{% if account_statistics[z][x] is mapping %}
|
</tr>
|
||||||
<ul>
|
{% endfor %}
|
||||||
{% for c in account_statistics[z][x] %}
|
|
||||||
<li>{{ c }}: {{ account_statistics[z][x][c] }}</li>
|
|
||||||
{% endfor %}
|
|
||||||
</ul>
|
|
||||||
{% else %}
|
|
||||||
<ul>
|
|
||||||
<li>{{ x }}: {{ account_statistics[z][x] }}</li>
|
|
||||||
</ul>
|
|
||||||
{% endif %}
|
|
||||||
</li>
|
|
||||||
{% endfor %}
|
|
||||||
</ul>
|
</ul>
|
||||||
{% else %}
|
{% else %}
|
||||||
{{ account_statistics[z] }}
|
<ul>
|
||||||
{% endif %}
|
<li>{{ x }}: {{ account_statistics.statistics[x] }}</li>
|
||||||
</li>
|
</ul>
|
||||||
{% endfor %}
|
{% endif %}
|
||||||
</ul>
|
</table>
|
||||||
|
{% endfor %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
32
wotstats/templates/pages/account/index0.html
Normal file
32
wotstats/templates/pages/account/index0.html
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
{% extends 'layouts/main.html' %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<h2>My Statistics</h2>
|
||||||
|
<ul>
|
||||||
|
{% for z in account_statistics %}
|
||||||
|
<li>{{ z }}
|
||||||
|
{% if account_statistics[z] is mapping %}
|
||||||
|
<ul>
|
||||||
|
{% for x in account_statistics[z] %}
|
||||||
|
<li>{{ x }}
|
||||||
|
{% if account_statistics[z][x] is mapping %}
|
||||||
|
<ul>
|
||||||
|
{% for c in account_statistics[z][x] %}
|
||||||
|
<li>{{ c }}: {{ account_statistics[z][x][c] }}</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
{% else %}
|
||||||
|
<ul>
|
||||||
|
<li>{{ x }}: {{ account_statistics[z][x] }}</li>
|
||||||
|
</ul>
|
||||||
|
{% endif %}
|
||||||
|
</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
{% else %}
|
||||||
|
{{ account_statistics[z] }}
|
||||||
|
{% endif %}
|
||||||
|
</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
{% endblock %}
|
50
wotstats/views/directory.py
Normal file
50
wotstats/views/directory.py
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
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.log import log
|
||||||
|
from wotstats.database import db
|
||||||
|
from wotstats.models import User
|
||||||
|
from wotstats.lib import parse_wargaming_openid_url
|
||||||
|
|
||||||
|
pages_dir_accounts = Blueprint('pages_dir_accounts', __name__, url_prefix='/dir', template_folder='templates')
|
||||||
|
|
||||||
|
def __get_player_personal_data():
|
||||||
|
log.debug(session)
|
||||||
|
user_id = parse_wargaming_openid_url(session['openid'])[0]
|
||||||
|
|
||||||
|
__ = requests.get(
|
||||||
|
'https://api.worldoftanks.ru/wot/account/info/?application_id={}&account_id={}'.format(
|
||||||
|
current_app.config['WG_APPLICATION_ID'], user_id
|
||||||
|
)
|
||||||
|
).json()
|
||||||
|
return __.get('data', {}).get(user_id)
|
||||||
|
|
||||||
|
|
||||||
|
@pages_dir_account.route('/')
|
||||||
|
def index():
|
||||||
|
from wotstats.tasks import hello
|
||||||
|
# from wotstats.tasks import *
|
||||||
|
|
||||||
|
hello.delay()
|
||||||
|
|
||||||
|
if not g.user:
|
||||||
|
return redirect(url_for('pages_home.index'))
|
||||||
|
|
||||||
|
account_statistics = __get_player_personal_data()
|
||||||
|
return render_template(
|
||||||
|
'pages/account/index.html',
|
||||||
|
account_statistics=account_statistics)
|
||||||
|
|
||||||
|
|
||||||
|
@pages_dir_account.route('/statistics.html')
|
||||||
|
def statistics():
|
||||||
|
if not g.user:
|
||||||
|
return redirect(url_for('pages_home.index'))
|
||||||
|
|
||||||
|
return render_template(
|
||||||
|
'pages/account/statistics.html')
|
Reference in a new issue