def ussd_poll(sender, **kwargs): connection = sender.connection if not sender.connection.contact: connection.contact = Contact.objects.create(name='Anonymous User') try: serial = sender.navigations.order_by('date')[1].response.rsplit("_")[0] connection.contact.reporting_location = EquatelLocation.objects.get(serial=serial).location connection.contact.save() except EquatelLocation.DoesNotExist: pass connection.save() equatel, created = Group.objects.get_or_create(name="equatel") connection.contact.groups.add(equatel) if sender.navigations.filter(screen__slug='weekly_poll').exists(): field = XFormField.objects.get(name="latest_poll") nav = sender.navigations.filter(screen__slug='weekly_poll').latest('date') poll = Poll.objects.get(pk=int(field.command.rsplit('_')[1])) if poll.categories.filter(name__in=["yes", "no"]): yes = poll.categories.get(name="yes") no = poll.categories.get(name='no') cats = {'1': ['yes', yes], '2': ['no', no]} msg = Message.objects.create(connection=connection, text=cats[nav.response][0], direction="I") resp = Response.objects.create(poll=poll, message=msg, contact=connection.contact, date=nav.date) resp.categories.add(ResponseCategory.objects.create(response=resp, category=cats[nav.response][1])) #update results update_poll_results() if sender.navigations.filter(screen__slug='send_report'): Message.objects.create(connection=connection, text=sender.navigations.filter(screen__slug='send_report').latest('date').response, direction="I")
def update_latest_poll(sender, **kwargs): poll = kwargs['instance'] if poll.categories.filter(name__in=['yes', 'no']): try: xf = XFormField.objects.get(name='latest_poll', command="poll_" + str(poll.pk)) xf.question = poll.question xf.command = "poll_" + str(poll.pk) xf.save() stub_screen = StubScreen.objects.get(slug='question_response') if poll.default_response: stub_screen.text = poll.default_response stub_screen.save() else: stub_screen.text = "Thanks For Your Response." stub_screen.save() update_poll_results() except (XFormField.DoesNotExist, StubScreen.DoesNotExist): pass try: Menu.tree.rebuild() except: pass