Advertisement
maurobaraldi

Gemalto

Sep 23rd, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import csv
  4. import os
  5. import sys
  6. from datetime import datetime, timedelta
  7. from sqlalchemy import and_
  8. from flask.ext.script import Manager
  9.  
  10.  
  11. TML_BASE_PATH = os.path.abspath(os.path.join(os.path.abspath(__file__),
  12. '..', '..', '..', '..'))
  13. sys.path.append(TML_BASE_PATH)
  14. from tml import app
  15. from tml.models.country_package import ColombiaSms
  16. from tml.colombia.constants import CARRIER, CHANNELS, LARGE_ACCOUNT
  17. from tml.colombia.models import UserColombia as UC
  18.  
  19. manager = Manager(app)
  20.  
  21. CONTENT_PROVIDER_ID = 'Titans Group'
  22. KEYWORD = 'BASICOSAT'
  23. PROVITER = 'TITANS_GROUP'
  24. OPERATOR = 'CLARO_CO'
  25.  
  26.  
  27. def today():
  28. return datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
  29.  
  30.  
  31. def yesterday():
  32. return today() - timedelta(days=1)
  33.  
  34.  
  35. def this_month():
  36. return today().replace(day=1)
  37.  
  38.  
  39. def users():
  40. return UC.query.filter_by(carrier=CARRIER, channel=CHANNELS['SAT_PUSH'])
  41.  
  42.  
  43. def _active_users(status=True):
  44. return users().filter_by(active=status)
  45.  
  46.  
  47. def subscribed_users():
  48. return active_users().filter(UC.subscription < yesterday())
  49.  
  50.  
  51. def active_users():
  52. actives = _active_users(True)
  53. return actives.filter(
  54. and_(UC.last_charge > this_month(),
  55. UC.last_charge < today()))
  56.  
  57.  
  58. def charged_messages():
  59. return users().filter(UC.last_charge == yesterday()).count()
  60.  
  61.  
  62. def new_subscriptions():
  63. return _active_users().filter(UC.subscription == yesterday()).count()
  64.  
  65.  
  66. def unsuscriptions():
  67. return _active_users().filter(UC.cancelled == yesterday())
  68.  
  69.  
  70. @manager.command
  71. def daily_subscriptions_report():
  72. line = {
  73. 'Date': datetime.now(),
  74. 'service_ID': 'Ideas Idiomas',
  75. 'billing_code': ColombiaSms.billing_code,
  76. 'navigation_code': LARGE_ACCOUNT,
  77. 'subscribed_users': subscribed_users(),
  78. 'active_users': _active_users(),
  79. 'charged_messages': charged_messages(),
  80. 'new_subscriptions': new_subscriptions(),
  81. 'Unsuscriptions': unsuscriptions(),
  82. 'service_price': ColombiaSms.price,
  83. 'content_provider_ID': CONTENT_PROVIDER_ID,
  84. 'Keyword': KEYWORD
  85. }
  86.  
  87. file_name = '%s-%s-REPORTE_TOTAL-SUS-%s.csv' % (PROVITER,
  88. OPERATOR,
  89. _report_date().isoformat())
  90. with open(file_name, 'w') as f:
  91. w = csv.DictWriter(f, line.keys())
  92. w.writeheader()
  93. w.writerow(line)
  94.  
  95.  
  96. if __name__ == '__main__':
  97. manager.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement