def sendsms(request): if request.method == 'POST': print request.POST form = SendSmsForm(request.POST) if not (request.user.account.has_perm('billservice.add_news')): messages.error(request, _(u'У вас нет прав на создание новостей'), extra_tags='alert-danger') return {} if form.is_valid(): accounts = form.cleaned_data.get('accounts') publish_date = form.cleaned_data.get('publish_date') body = form.cleaned_data.get('body') backend = form.cleaned_data.get('backend') for acc in accounts: if not acc.phone_m: continue acc.ballance = '%.2f' % acc.ballance item = Message() item.account = acc item.backend = backend item.to = acc.phone_m item.body = body item.publish_date = publish_date item.save() log('CREATE', request.user, item) return {'form': form, 'status': True} else: return {'form': form, 'status': False} else: if not (request.user.account.has_perm('billservice.add_news')): messages.error(request, _(u'У вас нет прав на создание новостей'), extra_tags='alert-danger') return {} m_form = AccountManagementForm(request.GET) form = None if m_form.is_valid(): form = SendSmsForm(initial={ 'accounts': m_form.cleaned_data.get('accounts', []) }) # An unbound form return {'form': form, 'status': False}
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 sendsms(request): if request.method == 'POST': print request.POST form = SendSmsForm(request.POST) if not (request.user.account.has_perm('billservice.add_news')): messages.error(request, _(u'У вас нет прав на создание новостей'), extra_tags='alert-danger') return {} if form.is_valid(): accounts = form.cleaned_data.get('accounts') publish_date = form.cleaned_data.get('publish_date') body = form.cleaned_data.get('body') backend = form.cleaned_data.get('backend') for acc in accounts: if not acc.phone_m: continue acc.ballance = '%.2f' % acc.ballance item = Message() item.account = acc item.backend = backend item.to = acc.phone_m item.body = body item.publish_date = publish_date item.save() log('CREATE', request.user, item) return {'form':form, 'status': True} else: return {'form':form, 'status': False} else: if not (request.user.account.has_perm('billservice.add_news')): messages.error(request, _(u'У вас нет прав на создание новостей'), extra_tags='alert-danger') return {} m_form = AccountManagementForm(request.GET) form = None if m_form.is_valid(): form = SendSmsForm(initial={'accounts': m_form.cleaned_data.get('accounts', [])}) # An unbound form return { 'form':form, 'status': False}
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()