Permalink
Browse files

Can create polls other than yes/no

  • Loading branch information...
1 parent deb29c9 commit 129bc629dc4c0722cd746c1d9a5ab860942753c0 @nicolasH nicolasH committed Dec 5, 2011
Showing with 25 additions and 22 deletions.
  1. +5 −4 poll/forms.py
  2. +18 −17 poll/models.py
  3. +2 −1 registration/views.py
View
@@ -17,12 +17,13 @@ class ReplyForm(forms.Form):
class NewPollForm(forms.Form): # pragma: no cover
TYPE_YES_NO = 'yn'
+ choices = [(choice['type'], choice['label']) for choice in Poll.TYPE_CHOICES.values()]
+ choices.append((TYPE_YES_NO, 'Yes/No Question'))
poll_type = forms.ChoiceField(
required=True,
- choices=(
- (TYPE_YES_NO, 'Yes/No Question'),
- ))
+ choices=tuple(choices)
+ )
response_type=forms.ChoiceField(choices=Poll.RESPONSE_TYPE_CHOICES,widget=RadioSelect,initial=Poll.RESPONSE_TYPE_ALL)
def updateTypes(self):
@@ -79,7 +80,7 @@ def clean(self):
# default manage to be replaced at run-time. There are many applications
# for that, such as filtering contacts by site_id (as is done in the
# authsites app, see github.com/daveycrockett/authsites).
- # This does, however, also make the polling app independent of authsites.
+ # This does, however, also make the polling app independent of authsites.
def __init__(self, data=None, **kwargs):
if data:
forms.ModelForm.__init__(self, data, **kwargs)
View
@@ -35,7 +35,7 @@
CONTAINS_PATTERN_TEMPLATE = '^.*\s*(%s)(\s|[^a-zA-Z]|$)'
-# This can be configurable from settings, but here's a default list of
+# This can be configurable from settings, but here's a default list of
# accepted yes keywords
#YES_WORDS = ['yes', 'yeah', 'yep', 'yay', 'y']
@@ -85,15 +85,15 @@ class Poll(models.Model):
Polls represent a simple-question, simple-response communication modality
via SMS. They can be thought of as a similar to a single datum in an XForm,
although for now the only data types available are yes/no, free-form text, and
- numeric response. Fairly simple idea, a poll is created, containing a question
+ numeric response. Fairly simple idea, a poll is created, containing a question
(the outgoing messages), a list of contacts (those to poll) and an expected
*type* of response. The poll can be edited, contact lists modified, etc. via
the web (the "user"), until it is eventually *started.* When a poll is started,
the outgoing question will be sent to all contacts, and any subsequent messages
coming in from the contacts associated with this poll (until they are polled again)
- will be parsed (or attempted to be parsed) by this poll, and bucketed into a
+ will be parsed (or attempted to be parsed) by this poll, and bucketed into a
particular category.
-
+
FIXME: contact groups, if implemented in core or contrib, should be used here,
instead of a many-to-many field
"""
@@ -238,14 +238,17 @@ def create_with_bulk(cls, name, type, question, default_response, contacts, user
# 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]])
- 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]])
- cursor.execute(raw_sql)
-
+ for c in localized_messages.get(language)[1]:
+ raw_sql = "insert into poll_poll_contacts (poll_id, contact_id) values (%d, %d)" % (poll.pk, c.pk)
+ print raw_sql
+ cursor.execute(raw_sql)
+
+ for m in localized_messages.get(language)[0]:
+ raw_sql = "insert into poll_poll_messages (poll_id, message_id) values (%d,%d)" % (poll.pk, m.pk)
+ print raw_sql
+ cursor.execute(raw_sql)
+
+ transaction.commit_unless_managed()
if 'django.contrib.sites' in settings.INSTALLED_APPS:
poll.sites.add(Site.objects.get_current())
return poll
@@ -408,7 +411,7 @@ def process_response(self, message):
else:
if db_message.connection.contact and db_message.connection.contact.language:
outgoing_message=gettext_db(language=db_message.connection.contact.language,field=outgoing_message)
-
+
return (resp, outgoing_message,)
def get_numeric_detailed_data(self):
@@ -543,7 +546,7 @@ def __unicode__(self):
class Category(models.Model):
"""
A category is a 'bucket' that an incoming poll response is placed into.
-
+
Categories have rules, which are regular expressions that a message must
satisfy to belong to a particular category (otherwise a response will have
None for its category). FIXME does this make sense, or should all polls
@@ -642,7 +645,7 @@ def __unicode__(self):
class Meta:
unique_together = ('field', 'language')
-
+
def gettext_db(field,language):
#if name exists in po file get it else look
if Translation.objects.filter(field=field,language=language).exists():
@@ -652,5 +655,3 @@ def gettext_db(field,language):
lang_str=ugettext(field)
deactivate()
return lang_str
-
-
View
@@ -54,7 +54,7 @@ def registration(req, pk=None):
location = line_list[4].strip()
except:
gender = age = location = ''
-
+
# we need this because of the groups extensions to contact and its custom save()
language = 'en-us' # default behavior for now
contact = Contact(name=name, phone=identity,
@@ -83,6 +83,7 @@ def registration(req, pk=None):
contact.phone = contact.phone.replace('+','').replace(' ','')
contact.language = 'en-us' #default behavior for now
contact.save()
+ print contact
backend, created = Backend.objects.get_or_create(name=DEFAULT_BACKEND_NAME)
connection = Connection.objects.get_or_create(backend=backend, contact=contact)[0]
connection.identity = contact.phone

0 comments on commit 129bc62

Please sign in to comment.