From 5694fe1676c749dd42d62f2a63a901eaeaafc659 Mon Sep 17 00:00:00 2001 From: vanzhiganov Date: Mon, 18 Sep 2017 03:25:22 +0300 Subject: [PATCH] update --- celerybeat-schedule | Bin 16384 -> 20480 bytes manage.py | 50 ++++++++++++++++++- migrations/versions/49d6ea45c818_.py | 36 +++++++++++++ wotstats/models/__init__.py | 5 +- wotstats/models/wotaccountsstats.py | 19 +++++++ wotstats/templates/pages/account/index.html | 42 ++++++---------- wotstats/templates/pages/account/index0.html | 32 ++++++++++++ wotstats/views/directory.py | 50 +++++++++++++++++++ 8 files changed, 206 insertions(+), 28 deletions(-) create mode 100644 migrations/versions/49d6ea45c818_.py create mode 100644 wotstats/models/wotaccountsstats.py create mode 100644 wotstats/templates/pages/account/index0.html create mode 100644 wotstats/views/directory.py diff --git a/celerybeat-schedule b/celerybeat-schedule index 9a6d6a664a2045483721fe28f4dcf438ed509a0d..f4450368ccafd3d9b0c2079c738eec1abbd88d4a 100644 GIT binary patch delta 1201 zcmZvaO=}ZD7{_<*dt##)>7iJ(7j0^?o!OavYe;Qc#i~@L-gFe(Z5E2A)Hakos0n@o z<0`EvsCW`QSn?qR!9$ONpF}M6nRzCMEiTK<{AT|1e_nQ5EwlB}d^tXT+fdJ67)Nob z#(urNyU@1JkJdFMw{VeYu3u^|xy=hE;%^7%3C%w)r#a`P`Byp3^FH#VzE7^E*_cgp zwxYOcbT546D7dH%ST)Vt0di5Fw-|Cs^V@l3qWQ}-vaPv1gUmF)z9zY77~PG7D``PA zca-<`&*nPj$-@)PUOY0o$%YHKVQKU4*O95_hd0w+e{TGrkLsmL#yW&&1m>LFFpN&~ zTzh!9w1_kSQ=v0vy%;)9sfZ~Xx5WChd&yqqOs~y-C#mvB+02%;Wx)VrQuTKSqRE12 ztZge1B|#uX$Kb3Ue8@zkAlC1LQ;5fuDUZ&l>7V8xrK~6#+mG?c_n5pnsO&9EdhcYY zO-dprwFh&UgH6@_WFvTgDgs-l0H<8U%RbDm)bs@ClayzYq{Z4fEM(FGLHImE8;7YZ zdmt8Ay~|Qgro^TV5`uyN^X!8>Ad^{tH&2<2AYEV)E{k~8Ln*V_=PEacDKAaQ7*9o9 zbF>TfWJxMalfA;8I*f?TJF7~;7*$0NK_d9ds`C^oqKXz{R?VSD@k$%i_dU-AkSXJ0q%RGF#;%O9yQYv$9oj2l=kXml{vRfxD&!Or?|z aR^FUOQA0|g!BGISwxL|9;FoIe*Z%>E1ojL7 delta 417 zcmZozz}V2hI6+a+fdLHu07*_TYoe3d#0~>1Ek=mM8WRYu1LZduL-+ax={HtO9&rk0k6=|t46_zO= zStbj^$sc`lHZQh1%{cjj9Se_vnI=%XVI)w20YrhtWJdd($+3P@C!e)r=QS_}8J`wY W0#slGHs8X`6l{L3{p3wdMvMTi%2TBP diff --git a/manage.py b/manage.py index e8edddf..20a97bb 100644 --- a/manage.py +++ b/manage.py @@ -12,7 +12,55 @@ migrate = Migrate(app, db) manager = Manager(app) # Регистрируем команду, реализованную в виде потомка класса Command 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__': manager.run() diff --git a/migrations/versions/49d6ea45c818_.py b/migrations/versions/49d6ea45c818_.py new file mode 100644 index 0000000..cb459c2 --- /dev/null +++ b/migrations/versions/49d6ea45c818_.py @@ -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') diff --git a/wotstats/models/__init__.py b/wotstats/models/__init__.py index 35fc81d..b4aa3a4 100644 --- a/wotstats/models/__init__.py +++ b/wotstats/models/__init__.py @@ -2,5 +2,8 @@ from .user import User from .userwottokens import UserWotTokens from .userwotdetails import UserWotDetails -from .userwotstats import UserWotStats +# from .userwotstats import UserWotStats from .userwotdata import UserWotData + +from .wotaccounts import WotAccounts +from .wotaccountsstats import WotAccountsStats diff --git a/wotstats/models/wotaccountsstats.py b/wotstats/models/wotaccountsstats.py new file mode 100644 index 0000000..ef72fea --- /dev/null +++ b/wotstats/models/wotaccountsstats.py @@ -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 ''.format( + self.id, self.user, self.created_at) diff --git a/wotstats/templates/pages/account/index.html b/wotstats/templates/pages/account/index.html index dcc4682..4ab00d1 100644 --- a/wotstats/templates/pages/account/index.html +++ b/wotstats/templates/pages/account/index.html @@ -2,31 +2,21 @@ {% block content %}

My Statistics

-
    -{% for z in account_statistics %} -
  • {{ z }} - {% if account_statistics[z] is mapping %} -
      - {% for x in account_statistics[z] %} -
    • {{ x }} - {% if account_statistics[z][x] is mapping %} -
        - {% for c in account_statistics[z][x] %} -
      • {{ c }}: {{ account_statistics[z][x][c] }}
      • - {% endfor %} -
      - {% else %} -
        -
      • {{ x }}: {{ account_statistics[z][x] }}
      • -
      - {% endif %} -
    • - {% endfor %} + {% for x in account_statistics.statistics %} +

      {{ x }}

      + {% if account_statistics.statistics[x] is mapping %} + + {% for c in account_statistics.statistics[x] %} + + + + {% endfor %} - {% else %} - {{ account_statistics[z] }} - {% endif %} - -{% endfor %} - + {% else %} +
        +
      • {{ x }}: {{ account_statistics.statistics[x] }}
      • +
      + {% endif %} +
      {{ c }}{{ account_statistics.statistics[x][c] }}
      + {% endfor %} {% endblock %} diff --git a/wotstats/templates/pages/account/index0.html b/wotstats/templates/pages/account/index0.html new file mode 100644 index 0000000..dcc4682 --- /dev/null +++ b/wotstats/templates/pages/account/index0.html @@ -0,0 +1,32 @@ +{% extends 'layouts/main.html' %} + +{% block content %} +

      My Statistics

      +
        +{% for z in account_statistics %} +
      • {{ z }} + {% if account_statistics[z] is mapping %} +
          + {% for x in account_statistics[z] %} +
        • {{ x }} + {% if account_statistics[z][x] is mapping %} +
            + {% for c in account_statistics[z][x] %} +
          • {{ c }}: {{ account_statistics[z][x][c] }}
          • + {% endfor %} +
          + {% else %} +
            +
          • {{ x }}: {{ account_statistics[z][x] }}
          • +
          + {% endif %} +
        • + {% endfor %} +
        + {% else %} + {{ account_statistics[z] }} + {% endif %} +
      • +{% endfor %} +
      +{% endblock %} diff --git a/wotstats/views/directory.py b/wotstats/views/directory.py new file mode 100644 index 0000000..7551d58 --- /dev/null +++ b/wotstats/views/directory.py @@ -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')