diff --git a/celerybeat-schedule b/celerybeat-schedule index 9a6d6a6..f445036 100644 Binary files a/celerybeat-schedule and b/celerybeat-schedule differ 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

-