Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import csv
- import os
- import sys
- from datetime import datetime, timedelta
- from sqlalchemy import and_
- from flask.ext.script import Manager
- TML_BASE_PATH = os.path.abspath(os.path.join(os.path.abspath(__file__),
- '..', '..', '..', '..'))
- sys.path.append(TML_BASE_PATH)
- from tml import app
- from tml.models.country_package import ColombiaSms
- from tml.colombia.constants import CARRIER, CHANNELS, LARGE_ACCOUNT
- from tml.colombia.models import UserColombia as UC
- manager = Manager(app)
- CONTENT_PROVIDER_ID = 'Titans Group'
- KEYWORD = 'BASICOSAT'
- PROVITER = 'TITANS_GROUP'
- OPERATOR = 'CLARO_CO'
- def today():
- return datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
- def yesterday():
- return today() - timedelta(days=1)
- def this_month():
- return today().replace(day=1)
- def users():
- return UC.query.filter_by(carrier=CARRIER, channel=CHANNELS['SAT_PUSH'])
- def _active_users(status=True):
- return users().filter_by(active=status)
- def subscribed_users():
- return active_users().filter(UC.subscription < yesterday())
- def active_users():
- actives = _active_users(True)
- return actives.filter(
- and_(UC.last_charge > this_month(),
- UC.last_charge < today()))
- def charged_messages():
- return users().filter(UC.last_charge == yesterday()).count()
- def new_subscriptions():
- return _active_users().filter(UC.subscription == yesterday()).count()
- def unsuscriptions():
- return _active_users().filter(UC.cancelled == yesterday())
- @manager.command
- def daily_subscriptions_report():
- line = {
- 'Date': datetime.now(),
- 'service_ID': 'Ideas Idiomas',
- 'billing_code': ColombiaSms.billing_code,
- 'navigation_code': LARGE_ACCOUNT,
- 'subscribed_users': subscribed_users(),
- 'active_users': _active_users(),
- 'charged_messages': charged_messages(),
- 'new_subscriptions': new_subscriptions(),
- 'Unsuscriptions': unsuscriptions(),
- 'service_price': ColombiaSms.price,
- 'content_provider_ID': CONTENT_PROVIDER_ID,
- 'Keyword': KEYWORD
- }
- file_name = '%s-%s-REPORTE_TOTAL-SUS-%s.csv' % (PROVITER,
- OPERATOR,
- _report_date().isoformat())
- with open(file_name, 'w') as f:
- w = csv.DictWriter(f, line.keys())
- w.writeheader()
- w.writerow(line)
- if __name__ == '__main__':
- manager.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement