本文整理匯總了Python中rapidsms_httprouter.models.Message.mass_text方法的典型用法代碼示例。如果您正苦於以下問題:Python Message.mass_text方法的具體用法?Python Message.mass_text怎麽用?Python Message.mass_text使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類rapidsms_httprouter.models.Message
的用法示例。
在下文中一共展示了Message.mass_text方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: sendSMS
# 需要導入模塊: from rapidsms_httprouter.models import Message [as 別名]
# 或者: from rapidsms_httprouter.models.Message import mass_text [as 別名]
def sendSMS(request, facility_pk=0):
groups = Group.objects.filter(name__in=['FHD', 'HC',
'Incharge', 'Records Assistant', 'VHT']).order_by('name')
facility = HealthFacility.objects.get(pk=facility_pk)
if request.method == "GET":
return render_to_response("cvs/facility/partials/facility_sendsms.html",
{'groups': groups, 'facility': facility},
context_instance=RequestContext(request)
)
else:
msg = request.POST['msg']
grp = request.POST['group']
if not grp:
json = simplejson.dumps({'error': "role is required"})
return HttpResponse(json, mimetype='application/json')
if not msg:
json = simplejson.dumps({'error': "message is required"})
return HttpResponse(json, mimetype='application/json')
reporters = HealthProvider.objects.filter(facility=request.POST['facility'],
groups__in=[grp])
recipient_count = reporters.count()
conns = Connection.objects.filter(contact__in=reporters)
Message.mass_text(msg, conns, status='Q', batch_status='Q')
json = simplejson.dumps({'msg': "sent to %s recipients" % recipient_count, 'error': ""})
return HttpResponse(json, mimetype='application/json')
示例2: create_poll
# 需要導入模塊: from rapidsms_httprouter.models import Message [as 別名]
# 或者: from rapidsms_httprouter.models.Message import mass_text [as 別名]
def create_poll(name, type, question, default_response, contacts, user, start_immediately=False):
localized_messages = {}
bad_conns = Blacklist.objects.values_list('connection__pk', flat=True).distinct()
contacts = contacts.exclude(connection__in=bad_conns)
poll = Poll.objects.create(name=name, type=type, question=question, default_response=default_response, user=user)
for language in dict(settings.LANGUAGES).keys():
if language == "en":
"""default to English for contacts with no language preference"""
localized_contacts = contacts.filter(language__in=["en", ''])
else:
localized_contacts = contacts.filter(language=language)
if localized_contacts:
if start_immediately:
messages = Message.mass_text(gettext_db(field=question, language=language),
Connection.objects.filter(contact__in=localized_contacts).distinct(),
status='Q', batch_status='Q')
else:
messages = Message.mass_text(gettext_db(field=question, language=language),
Connection.objects.filter(contact__in=localized_contacts).distinct(),
status='L', batch_status='L')
poll.messages.add(*messages.values_list('pk', flat=True))
if 'django.contrib.sites' in settings.INSTALLED_APPS:
poll.sites.add(Site.objects.get_current())
if start_immediately:
poll.start_date = datetime.datetime.now()
poll.save()
return poll
示例3: perform
# 需要導入模塊: from rapidsms_httprouter.models import Message [as 別名]
# 或者: from rapidsms_httprouter.models.Message import mass_text [as 別名]
def perform(self, request, results):
if results is None or len(results) == 0:
return ('A message must have one or more recipients!', 'error')
if request.user and request.user.has_perm('contact.can_message'):
connections = \
list(Connection.objects.filter(contact__in=results).distinct())
text = self.cleaned_data.get('text', "")
text = text.replace('%', u'\u0025')
messages = Message.mass_text(text, connections)
MassText.bulk.bulk_insert(send_pre_save=False,
user=request.user,
text=text,
contacts=list(results))
masstexts = MassText.bulk.bulk_insert_commit(send_post_save=False, autoclobber=True)
masstext = masstexts[0]
if settings.SITE_ID:
masstext.sites.add(Site.objects.get_current())
return ('Message successfully sent to %d numbers' % len(connections), 'success',)
else:
return ("You don't have permission to send messages!", 'error',)
示例4: start
# 需要導入模塊: from rapidsms_httprouter.models import Message [as 別名]
# 或者: from rapidsms_httprouter.models.Message import mass_text [as 別名]
def start(self):
"""
This starts the poll: outgoing messages are sent to all the contacts
registered with this poll, and the start date is updated accordingly.
All incoming messages from these users will be considered as
potentially a response to this poll.
"""
if self.start_date:
return
contacts = self.contacts
localized_messages = {}
for language in dict(settings.LANGUAGES).keys():
if language == "en":
"""default to English for contacts with no language preference"""
localized_contacts = contacts.filter(language__in=["en", ''])
else:
localized_contacts = contacts.filter(language=language)
if localized_contacts.exists():
messages = Message.mass_text(gettext_db(field=self.question, language=language), Connection.objects.filter(contact__in=localized_contacts).distinct(), status='Q', batch_status='Q')
#localized_messages[language] = [messages, localized_contacts]
self.messages.add(*messages.values_list('pk',flat=True))
self.start_date = datetime.datetime.now()
self.save()
poll_started.send(sender=self)
示例5: perform
# 需要導入模塊: from rapidsms_httprouter.models import Message [as 別名]
# 或者: from rapidsms_httprouter.models.Message import mass_text [as 別名]
def perform(self, request, results):
if type(results).__name__ != 'QuerySet':
results = Contact.objects.filter(pk__in=request.REQUEST.get('results', ""))
if results is None or len(results) == 0:
return 'A message must have one or more recipients!', 'error'
if request.user and request.user.has_perm('contact.can_message'):
if type(results[0]).__name__ == 'Reporters':
con_ids = \
[r.default_connection.split(',')[1] if len(r.default_connection.split(',')) > 1 else 0 for r in
results]
connections = list(Connection.objects.filter(pk__in=con_ids).distinct())
contacts = list(Contact.objects.filter(pk__in=results.values_list('id', flat=True)))
print contacts
else:
connections = \
list(Connection.objects.filter(contact__pk__in=results.values_list('id', flat=True)).distinct())
contacts = list(results)
text = self.cleaned_data.get('text', "")
text = text.replace('%', u'\u0025')
messages = Message.mass_text(text, connections)
MassText.bulk.bulk_insert(send_pre_save=False,
user=request.user,
text=text,
contacts=contacts)
masstexts = MassText.bulk.bulk_insert_commit(send_post_save=False, autoclobber=True)
masstext = masstexts[0]
if settings.SITE_ID:
masstext.sites.add(Site.objects.get_current())
return 'Message successfully sent to %d numbers' % len(connections), 'success',
else:
return "You don't have permission to send messages!", 'error',
示例6: perform
# 需要導入模塊: from rapidsms_httprouter.models import Message [as 別名]
# 或者: from rapidsms_httprouter.models.Message import mass_text [as 別名]
def perform(self, request, results):
if results is None or len(results) == 0:
return ('A message must have one or more recipients!', 'error')
if request.user and request.user.has_perm('contact.can_message'):
blacklists = Blacklist.objects.values_list('connection')
connections = \
Connection.objects.filter(contact__in=results).exclude(pk__in=blacklists).distinct()
text = self.cleaned_data.get('text', "")
text = text.replace('%', u'\u0025')
if not self.cleaned_data['text_luo'] == '':
(translation, created) = \
Translation.objects.get_or_create(language='ach',
field=self.cleaned_data['text'],
value=self.cleaned_data['text_luo'])
messages = Message.mass_text(text, connections)
contacts=Contact.objects.filter(pk__in=results)
MassText.bulk.bulk_insert(send_pre_save=False,
user=request.user,
text=text,
contacts=list(contacts))
masstexts = MassText.bulk.bulk_insert_commit(send_post_save=False, autoclobber=True)
masstext = masstexts[0]
return ('Message successfully sent to %d numbers' % len(connections), 'success',)
else:
return ("You don't have permission to send messages!", 'error',)
示例7: start
# 需要導入模塊: from rapidsms_httprouter.models import Message [as 別名]
# 或者: from rapidsms_httprouter.models.Message import mass_text [as 別名]
def start(self):
"""
This starts the poll: outgoing messages are sent to all the contacts
registered with this poll, and the start date is updated accordingly.
All incoming messages from these users will be considered as
potentially a response to this poll.
"""
if self.start_date:
return
contacts = self.contacts
localized_messages = {}
for language in dict(settings.LANGUAGES).keys():
if language == "en":
"""default to English for contacts with no language preference"""
localized_contacts = contacts.filter(language__in=["en", ''])
else:
localized_contacts = contacts.filter(language=language)
if localized_contacts.exists():
messages = Message.mass_text(gettext_db(field=self.question, language=language), Connection.objects.filter(contact__in=localized_contacts).distinct(), status='Q', batch_status='Q')
localized_messages[language] = [messages, localized_contacts]
# This is the fastest (pretty much only) was to get messages M2M into the
# DB fast enough at scale
cursor = connection.cursor()
for language in localized_messages.keys():
raw_sql = "insert into poll_poll_messages (poll_id, message_id) values %s" % ','.join(\
["(%d, %d)" % (self.pk, m.pk) for m in localized_messages.get(language)[0].iterator()])
cursor.execute(raw_sql)
self.start_date = datetime.datetime.now()
self.save()
poll_started.send(sender=self)
示例8: perform
# 需要導入模塊: from rapidsms_httprouter.models import Message [as 別名]
# 或者: from rapidsms_httprouter.models.Message import mass_text [as 別名]
def perform(self, request, results):
if results is None or len(results) == 0:
return ("A message must have one or more recipients!", "error")
if request.user and request.user.has_perm("contact.can_message"):
if type(results[0]).__name__ == "Reporters":
con_ids = [
r.default_connection.split(",")[1] if len(r.default_connection.split(",")) > 1 else 0
for r in results
]
connections = list(Connection.objects.filter(pk__in=con_ids).distinct())
contacts = list(Contact.objects.filter(pk__in=results.values_list("id", flat=True)))
else:
connections = list(
Connection.objects.filter(contact__pk__in=results.values_list("id", flat=True)).distinct()
)
contacts = list(results)
text = self.cleaned_data.get("text", "")
text = text.replace("%", u"\u0025")
messages = Message.mass_text(text, connections)
MassText.bulk.bulk_insert(send_pre_save=False, user=request.user, text=text, contacts=contacts)
masstexts = MassText.bulk.bulk_insert_commit(send_post_save=False, autoclobber=True)
masstext = masstexts[0]
if settings.SITE_ID:
masstext.sites.add(Site.objects.get_current())
return ("Message successfully sent to %d numbers" % len(connections), "success")
else:
return ("You don't have permission to send messages!", "error")
示例9: mass_text
# 需要導入模塊: from rapidsms_httprouter.models import Message [as 別名]
# 或者: from rapidsms_httprouter.models.Message import mass_text [as 別名]
def mass_text(self):
# get one scriptprogress since they are all supposed to be on the same step
if self.exists():
prog = self[0]
else:
return False
if prog.step.poll:
text = prog.step.poll.question
elif prog.step.email:
text = prog.step.email
else:
text = prog.step.message
for language in dict(settings.LANGUAGES).keys():
if language == "en":
"""default to English for contacts with no language preference"""
localized_progs = self.filter(Q(language__in=["en", '']) | Q(language=None))
else:
localized_progs = self.filter(language=language)
if localized_progs.exists():
localized_conns = localized_progs.values_list('connection', flat=True)
messages = Message.mass_text(gettext_db(field=text, language=language),
Connection.objects.filter(pk__in=localized_conns).distinct(), status='P')
return True
示例10: add_to_poll
# 需要導入模塊: from rapidsms_httprouter.models import Message [as 別名]
# 或者: from rapidsms_httprouter.models.Message import mass_text [as 別名]
def add_to_poll(poll, contacts):
localized_messages = {}
bad_conns = Blacklist.objects.values_list('connection__pk', flat=True).distinct()
contacts = contacts.exclude(connection__in=bad_conns)
for language in dict(settings.LANGUAGES).keys():
if language == "en":
"""default to English for contacts with no language preference"""
localized_contacts = contacts.filter(language__in=["en", ''])
else:
localized_contacts = contacts.filter(language=language)
if localized_contacts:
messages = Message.mass_text(gettext_db(field=poll.question, language=language),
Connection.objects.filter(contact__in=localized_contacts).distinct(),
status='Q', batch_status='Q')
localized_messages[language] = [messages, localized_contacts]
# This is the fastest (pretty much only) was to get contacts and messages M2M into the
# DB fast enough at scale
# cursor = connection.cursor()
# for language in localized_messages.keys():
# raw_sql = "insert into poll_poll_contacts (poll_id, contact_id) values %s" % ','.join(\
# ["(%d, %d)" % (poll.pk, c.pk) for c in localized_messages.get(language)[1].iterator()])
# cursor.execute(raw_sql)
#
# raw_sql = "insert into poll_poll_messages (poll_id, message_id) values %s" % ','.join(\
# ["(%d, %d)" % (poll.pk, m.pk) for m in localized_messages.get(language)[0].iterator()])
# cursor.execute(raw_sql)
return poll
示例11: perform
# 需要導入模塊: from rapidsms_httprouter.models import Message [as 別名]
# 或者: from rapidsms_httprouter.models.Message import mass_text [as 別名]
def perform(self, request, results):
from poll.models import gettext_db
if results is None or len(results) == 0:
return ('A message must have one or more recipients!', 'error')
if request.user and request.user.has_perm('contact.can_message'):
blacklists = Blacklist.objects.values_list('connection')
#TODO: Revise for proper internationalization
languages = ["fr"]
text_fr = self.cleaned_data.get('text_fr', "")
text_fr = text_fr.replace('%', u'\u0025')
if not self.cleaned_data['text_en'] == '':
languages.append('en')
(translation, created) = \
Translation.objects.get_or_create(language='en',
field=self.cleaned_data['text_fr'],
value=self.cleaned_data['text_en'])
if not self.cleaned_data['text_ki'] == '':
languages.append('ki')
(translation, created) = \
Translation.objects.get_or_create(language='ki',
field=self.cleaned_data['text_fr'],
value=self.cleaned_data['text_ki'])
#Everyone gets a message in their language. This behavior may not be ideal
#since one may wish to send out a non translated message to everyone regardless of language
#TODO: allow sending of non translated message to everyone using a flag
total_connections = []
for language in languages:
connections = Connection.objects.filter(contact__in=results.filter(language=language)).exclude(pk__in=blacklists).distinct()
messages = Message.mass_text(gettext_db(field=text_fr, language=language), connections)
contacts = Contact.objects.filter(pk__in=results)
total_connections.extend(connections)
#Bulk wont work because of a ManyToMany relationship to Contact on MassText
#Django API does not allow bulk_create() to work with relation to multiple tables
#TODO: Find work around
# bulk_list = [
# MassText(user=request.user),
# MassText(text=text_fr),
# MassText(contacts=list(contacts))
# ]
# masstext = MassText.objects.bulk_create(bulk_list)
#The bulk_insert manager needs to be updated
#TODO: investigate changes made in new Django that are not compatible with BulkInsertManager()
# MassText.bulk.bulk_insert(send_pre_save=False,
# user=request.user,
# text=text_fr,
# contacts=list(contacts))
# masstexts = MassText.bulk.bulk_insert_commit(send_post_save=False, autoclobber=True)
# masstext = masstexts[0]
return ('Message successfully sent to %d numbers' % len(total_connections), 'success',)
else:
return ("You don't have permission to send messages!", 'error',)
示例12: test_can_send_mass_text_with_batch_name
# 需要導入模塊: from rapidsms_httprouter.models import Message [as 別名]
# 或者: from rapidsms_httprouter.models.Message import mass_text [as 別名]
def test_can_send_mass_text_with_batch_name(self):
messages_sent = Message.mass_text("MassTestTest-MESSAGE", [self.connection_1, self.connection_2],
batch_name="FOO")
message_1 = Message.objects.get(pk=messages_sent[0].pk)
message_2 = Message.objects.get(pk=messages_sent[1].pk)
self.assertEqual(message_1.batch.name, "FOO")
self.assertEqual(message_2.batch.name, "FOO")
示例13: testAddBulk
# 需要導入模塊: from rapidsms_httprouter.models import Message [as 別名]
# 或者: from rapidsms_httprouter.models.Message import mass_text [as 別名]
def testAddBulk(self):
connection2 = Connection.objects.create(backend=self.backend, identity='8675309')
connection3 = Connection.objects.create(backend=self.backend, identity='8675310')
connection4 = Connection.objects.create(backend=self.backend, identity='8675311')
# test that mass texting works with a single number
msgs = Message.mass_text('Jenny I got your number', [self.connection])
self.assertEquals(msgs.count(), 1)
self.assertEquals(msgs[0].text, 'Jenny I got your number')
# test no connections are re-created
self.assertEquals(msgs[0].connection.pk, self.connection.pk)
msgs = Message.mass_text('Jenny dont change your number',
[self.connection, connection2, connection3, connection4], status='L')
self.assertEquals(str(msgs.values_list('status', flat=True).distinct()[0]), 'L')
self.assertEquals(msgs.count(), 4)
# test duplicate connections don't create duplicate messages
msgs = Message.mass_text('Turbo King is the greatest!', [self.connection, self.connection])
self.assertEquals(msgs.count(), 1)
示例14: create_poll
# 需要導入模塊: from rapidsms_httprouter.models import Message [as 別名]
# 或者: from rapidsms_httprouter.models.Message import mass_text [as 別名]
def create_poll(name, type, question, default_response, contacts, user,start_immediately=False):
localized_messages = {}
bad_conns = Blacklist.objects.values_list('connection__pk', flat=True).distinct()
contacts=contacts.exclude(connection__in=bad_conns)
for language in dict(settings.LANGUAGES).keys():
if language == "en":
"""default to English for contacts with no language preference"""
localized_contacts = contacts.filter(language__in=["en", ''])
else:
localized_contacts = contacts.filter(language=language)
if localized_contacts.exists():
if start_immediately:
messages = Message.mass_text(gettext_db(field=question, language=language), Connection.objects.filter(contact__in=localized_contacts).distinct(), status='Q', batch_status='Q')
else:
messages = Message.mass_text(gettext_db(field=question, language=language), Connection.objects.filter(contact__in=localized_contacts).distinct(), status='L', batch_status='L')
localized_messages[language] = [messages, localized_contacts]
poll = Poll.objects.create(name=name, type=type, question=question, default_response=default_response, user=user)
# This is the fastest (pretty much only) was to get contacts and messages M2M into the
# DB fast enough at scale
cursor = connection.cursor()
for language in localized_messages.keys():
raw_sql = "insert into poll_poll_contacts (poll_id, contact_id) values %s" % ','.join(\
["(%d, %d)" % (poll.pk, c.pk) for c in localized_messages.get(language)[1].iterator()])
cursor.execute(raw_sql)
raw_sql = "insert into poll_poll_messages (poll_id, message_id) values %s" % ','.join(\
["(%d, %d)" % (poll.pk, m.pk) for m in localized_messages.get(language)[0].iterator()])
cursor.execute(raw_sql)
if 'django.contrib.sites' in settings.INSTALLED_APPS:
poll.sites.add(Site.objects.get_current())
if start_immediately:
poll.start_date = datetime.datetime.now()
poll.save()
return poll
示例15: handle
# 需要導入模塊: from rapidsms_httprouter.models import Message [as 別名]
# 或者: from rapidsms_httprouter.models.Message import mass_text [as 別名]
def handle(self, message):
# We fall to this app when xform fails to match message
# Also added here is a special chat group to share messages
# between members belonging to the same health facility
groups = []
mentions = []
for token in message.text.split():
if token.startswith("#"):
groups.append(token[1:])
if token.startswith("@"):
mentions.append(token[1:])
groups = [i.lower() for i in groups]
mentions = [i.lower() for i in mentions]
if 'chat' in groups or 'chat' in mentions:
sender = HealthProvider.objects.filter(connection=message.connection)
recipients = []
if sender:
sender = sender[0]
facility = sender.facility
if facility:
recipients = HealthProvider.objects.filter(
facility=sender.facility).exclude(connection__identity=None)
recipients = recipients.exclude(connection=sender.default_connection)
text = "{0}: {1}".format(sender.default_connection.identity, message.text)
sender_text = "sent to {0} members of {1}".format(
len(recipients), sender.facility)
conns = Connection.objects.filter(contact__in=recipients)
if conns:
Message.mass_text(text, conns, status='Q', batch_status='Q')
message.respond(sender_text)
return True
if message.connection.contact and not ScriptProgress.objects.filter(connection=message.connection).exists():
if message.connection.contact.healthproviderbase:
message.respond("Thank you for your message. We have forwarded to your DHT for follow-up. If this was meant to be a weekly report, please check and resend.")
return True
return False