def handle(self, *args, **options): now = datetime.datetime.now() try: body = Template.objects.filter(type__id=10)[0] except: print u"Создайте шаблон SMS сообщения" for acc in Account.objects.filter(ballance__lte=settings.SENDSMS_IF_BALLANCE_AMOUNT, ballance__gte=settings.SENDSMS_NOT_SEND_IF_BALANCE_LESS): if not acc.phone_m: continue if Message.objects.filter(account=acc, created__gte=datetime.datetime.now()-datetime.timedelta(days=settings.SENDSMS_SEND_EVERY_N_DAY)).count()>0: continue acc.ballance = '%.2f' % acc.ballance item = Message() item.account = acc item.backend = settings.SENDSMS_DEFAULT_BACKEND item.to = acc.phone_m item.body = body.body item.save() item.send()
def handle(self, *args, **options): now = datetime.datetime.now() try: body = Template.objects.filter(type__id=10)[0] except: print u"Создайте шаблон SMS сообщения" for acc in Account.objects.filter( ballance__lte=settings.SENDSMS_IF_BALLANCE_AMOUNT, ballance__gte=settings.SENDSMS_NOT_SEND_IF_BALANCE_LESS): if not acc.phone_m: continue if Message.objects.filter( account=acc, created__gte=datetime.datetime.now() - datetime.timedelta( days=settings.SENDSMS_SEND_EVERY_N_DAY)).count() > 0: continue acc.ballance = '%.2f' % acc.ballance item = Message() item.account = acc item.backend = settings.SENDSMS_DEFAULT_BACKEND item.to = acc.phone_m item.body = body.body item.save() item.send()
def handle(self, *args, **options): items = NotificationsSettings.objects.all() notifications = {} now = datetime.datetime.now() for n in items: for t in n.tariffs.all(): notifications[t.id] = n accounts = Account.objects.extra(select={'tarif_id': 'get_tarif(billservice_account.id)'}) print 'Accounts fetched' for account in accounts: notification = notifications.get(account.tarif_id) print account if not notification: continue an = AccountNotification.objects.filter(account = account, notificationsettings=notification) if an: an = an[0] else: an = AccountNotification() an.account = account an.notificationsettings = notification print account, an if notification.balance_notifications: if an.ballance_notification_count>=notification.balance_notifications_limit and (account.ballance+account.credit)>notification.balance_edge: an.ballance_notification_count = 0 an.ballance_notification_last_date = None an.save() continue if an.ballance_notification_count>=notification.balance_notifications_limit or (an.ballance_notification_last_date and an.ballance_notification_last_date<(now-datetime.timedelta(days=notification.balance_notifications_each))): """ if notifications count reached and nothing changed """ continue if (account.ballance+account.credit)<=notification.balance_edge: an.ballance_notification_count+=1 an.ballance_notification_last_date = now if account.disable_notifications: item = Message() item.account = account item.backend = notification.backend if notification.notification_type=='SMS': item.to = account.phone_m else: item.to = account.email t = Template(notification.balance_notifications_template) c = Context({"account": account}) item.body = t.render(c) item.publish_date = now item.save() item.send() an.save() if notification.payment_notifications: if not an.payment_notification_last_date: """ If have no payment notifications - skip """ an.payment_notification_last_date = now an.save() continue for item in Transaction.objects.filter(account = account, created__gte=an.payment_notification_last_date): if account.disable_notifications: item = Message() item.account = account item.backend = notification.backend if notification.notification_type=='SMS': item.to = account.phone_m else: item.to = account.email t = Template(notification.payment_notifications_template) c = Context({"account": account, 'transaction': item}) item.body = t.render(c) item.publish_date = now item.save() an.payment_notification_last_date = now item.send() an.save()
def handle(self, *args, **options): now = datetime.datetime.now() items = NotificationsSettings.objects.all() notifications = {} now = datetime.datetime.now() for n in items: for t in n.tariffs: notifications[t.id] = n accounts = Account.objects.extra( select={'tarif_id': 'get_tarif(billservice_account.id)'}) print 'Accounts fetched' for account in accounts: notification = notifications.get(account.tarif_id) print account if not notification: continue an = AccountNotification.objects.filter( account=account, notificationsettings=notification) if an: an = an[0] else: an = AccountNotification() an.account = account an.notificationsettings = notification print account, an if notification.balance_notifications: if an.ballance_notification_count >= notification.balance_notifications_limit and ( account.ballance + account.credit) > notification.balance_edge: an.ballance_notification_count = 0 an.ballance_notification_last_date = None an.save() continue if an.ballance_notification_count >= notification.balance_notifications_limit or ( now - an.ballance_notification_last_date ) < an.balance_notifications_each: """ if notifications count reached and nothing changed """ continue if (account.ballance + account.credit) <= notification.balance_edge: an.ballance_notification_count += 1 an.ballance_notification_last_date = now item = Message() item.account = account item.backend = notification.backend if notification.notification_type == 'SMS': item.to = account.phone_m else: item.to = account.email t = Template(notification.balance_notifications_template) c = Context({"account": account}) item.body = t.render(c) item.publish_date = now item.save() item.send() an.save() if notification.payment_notifications: if not an.payment_notification_last_date: """ If have no payment notifications - skip """ an.payment_notification_last_date = now an.save() continue for item in Transaction.objects.filter( account=account, datetime__gte=an.payment_notification_last_date): item = Message() item.account = account item.backend = notification.backend if notification.notification_type == 'SMS': item.to = account.phone_m else: item.to = account.email t = Template(notification.payment_notifications_template) c = Context({"account": account, 'transaction': item}) item.body = t.render(c) item.publish_date = now item.save() an.payment_notification_last_date = now an.save() item.send()