Ejemplo n.º 1
0
def new_poll(req):
    log.info("[new_poll] TRANSACTION START")
    if req.method == 'POST':
        log.info("[new-poll] - request recieved to create a poll")
        form = NewPollForm(req.POST, request=req)
        groups_form = GroupsFilter(req.POST, request=req)
        form.updateTypes()
        if form.is_valid() and groups_form.is_valid():
            # create our XForm
            is_urgent = form.cleaned_data['is_urgent']
            question = form.cleaned_data['question_en']
            default_response = form.cleaned_data['default_response_en']
            districts = form.cleaned_data['districts']
            excluded_groups = groups_form.cleaned_data['group_list']
            if hasattr(Contact, 'groups'):
                groups = form.cleaned_data['groups']

            log.info("[new-poll] - finding all contacts for this poll...")
            if len(districts):
                contacts = Contact.objects.filter(reporting_location__in=districts).filter(groups__in=groups).exclude(
                    groups__in=excluded_groups)
            else:
                contacts = Contact.objects.filter(groups__in=groups).exclude(groups__in=excluded_groups)

            log.info("[new-poll] - found all contacts ok.")

            log.info("[new-poll] - setting up translations...")
            name = form.cleaned_data['name']
            p_type = form.cleaned_data['type']
            response_type = form.cleaned_data['response_type']
            if not form.cleaned_data['default_response_luo'] == '' \
                and not form.cleaned_data['default_response_en'] == '':
                (translation, created) = \
                    Translation.objects.get_or_create(language='ach',
                                                      field=form.cleaned_data['default_response_en']
                    )
                translation.value = form.cleaned_data['default_response_luo']
                translation.save()
            if not form.cleaned_data['default_response_kdj'] == '' \
                and not form.cleaned_data['default_response_en'] == '':
                (translation, created) = \
                    Translation.objects.get_or_create(language='kdj',
                                                      field=form.cleaned_data['default_response_en']
                    )
                translation.value = form.cleaned_data['default_response_kdj']
                translation.save()
            if not form.cleaned_data['question_luo'] == '':
                (translation, created) = \
                    Translation.objects.get_or_create(language='ach',
                                                      field=form.cleaned_data['question_en']
                    )
                translation.value = form.cleaned_data['question_luo']
                translation.save()
            if not form.cleaned_data['question_kdj'] == '':
                (translation, created) = \
                    Translation.objects.get_or_create(language='kdj',
                                                      field=form.cleaned_data['question_en']
                    )
                translation.value = form.cleaned_data['question_kdj']
                translation.save()
            log.info("[new-poll] - translations ok.")

            poll_type = (Poll.TYPE_TEXT if p_type
                                           == NewPollForm.TYPE_YES_NO else p_type)

            poll = Poll.create_with_bulk(
                name,
                poll_type,
                question,
                default_response,
                contacts,
                req.user,
                is_urgent=is_urgent)

            if p_type == NewPollForm.TYPE_YES_NO:
                log.info("[new-poll] - is Y/N poll so adding categories...")
                poll.add_yesno_categories()
                log.info("[new-poll] - categories added ok.")

            if settings.SITE_ID:
                log.info("[new-poll] - SITE_ID is set, so adding the site to the poll")
                poll.sites.add(Site.objects.get_current())
                log.info("[new-poll] - site added ok")

            log.info("[new-poll] - poll created ok.")
            log.info("[new_poll] TRANSACTION COMMIT")
            return redirect(reverse('ureport.views.view_poll', args=[poll.pk]))

    else:
        form = NewPollForm(request=req)
        groups_form = GroupsFilter(request=req)
        form.updateTypes()

        log.info("[new_poll] TRANSACTION COMMIT")
    return render_to_response('ureport/new_poll.html', {'form': form, 'groups_form': groups_form},
                                  context_instance=RequestContext(req))
Ejemplo n.º 2
0
def new_poll(req):
    log.info("[new_poll] TRANSACTION START")
    if req.method == 'POST':
        log.info("[new-poll] - request recieved to create a poll")
        form = NewPollForm(req.POST, request=req)
        groups_form = GroupsFilter(req.POST, request=req)
        #         form.updateTypes() #Added to form directly already no need to add them again
        if form.is_valid() and groups_form.is_valid():
            # create our XForm
            question = form.cleaned_data['question_fr']
            default_response = form.cleaned_data['default_response_fr']
            provinces = form.cleaned_data['provinces']
            excluded_groups = groups_form.cleaned_data['group_list']
            if hasattr(Contact, 'groups'):
                groups = form.cleaned_data['groups']

            log.info("[new-poll] - finding all contacts for this poll...")
            if len(provinces):
                contacts = Contact.objects.filter(
                    reporting_location__in=provinces).filter(
                        groups__in=groups).exclude(groups__in=excluded_groups)
            else:
                if -1 in groups or '-1' in groups:
                    contacts = Contact.objects.all().exclude(
                        groups__in=excluded_groups)
                else:
                    contacts = Contact.objects.filter(
                        groups__in=groups).exclude(groups__in=excluded_groups)

            log.info("[new-poll] - found all contacts ok.")

            log.info("[new-poll] - setting up translations...")
            name = form.cleaned_data['name']
            p_type = form.cleaned_data['type']
            response_type = form.cleaned_data['response_type']
            if not form.cleaned_data['default_response_en'] == '' \
                and not form.cleaned_data['default_response_fr'] == '':
                (translation, created) = \
                    Translation.objects.get_or_create(language='en',
                                                      field=form.cleaned_data['default_response_fr'],
                                                      value=form.cleaned_data['default_response_en'])
            if not form.cleaned_data['default_response_ki'] == '' \
                and not form.cleaned_data['default_response_fr'] == '':
                (translation, created) = \
                    Translation.objects.get_or_create(language='ki',
                                                      field=form.cleaned_data['default_response_fr'],
                                                      value=form.cleaned_data['default_response_ki'])

            if not form.cleaned_data['question_en'] == '':
                (translation, created) = \
                    Translation.objects.get_or_create(language='en',
                                                      field=form.cleaned_data['question_fr'],
                                                      value=form.cleaned_data['question_en'])

            if not form.cleaned_data['question_ki'] == '':
                (translation, created) = \
                    Translation.objects.get_or_create(language='ki',
                                                      field=form.cleaned_data['question_fr'],
                                                      value=form.cleaned_data['question_ki'])

            log.info("[new-poll] - translations ok.")

            poll_type = (Poll.TYPE_TEXT
                         if p_type == NewPollForm.TYPE_YES_NO else p_type)

            poll = Poll.create_with_bulk(name, poll_type, question,
                                         default_response, contacts, req.user)

            if p_type == NewPollForm.TYPE_YES_NO:
                log.info("[new-poll] - is Y/N poll so adding categories...")
                poll.add_yesno_categories()
                log.info("[new-poll] - categories added ok.")

            if settings.SITE_ID:
                log.info(
                    "[new-poll] - SITE_ID is set, so adding the site to the poll"
                )
                poll.sites.add(Site.objects.get_current())
                log.info("[new-poll] - site added ok")

            log.info("[new-poll] - poll created ok.")
            log.info("[new_poll] TRANSACTION COMMIT")
            return redirect(reverse('ureport.views.view_poll', args=[poll.pk]))

    else:
        form = NewPollForm(request=req)
        groups_form = GroupsFilter(request=req)


#         form.updateTypes() #Added to form directly already no need to add them again

    log.info("[new_poll] TRANSACTION COMMIT")
    return render_to_response('ureport/new_poll.html', {
        'form': form,
        'groups_form': groups_form
    },
                              context_instance=RequestContext(req))
Ejemplo n.º 3
0
def new_poll(req):
    if req.method == 'POST':
        form = NewPollForm(req.POST)
        groups_form = GroupsFilter(req.POST)
        form.updateTypes()
        if form.is_valid() and groups_form.is_valid():
            # create our XForm
            question = form.cleaned_data['question_en']
            default_response = form.cleaned_data['default_response_en']
            districts = form.cleaned_data['districts']
            excluded_groups = groups_form.cleaned_data['group_list']
            if hasattr(Contact, 'groups'):
                groups = form.cleaned_data['groups']

            if len(districts):
                contacts = Contact.objects.filter(reporting_location__in=districts).filter(groups__in=groups).exclude(
                    groups__in=excluded_groups)
            else:
                contacts = Contact.objects.filter(groups__in=groups).exclude(groups__in=excluded_groups)

            name = form.cleaned_data['name']
            p_type = form.cleaned_data['type']
            response_type = form.cleaned_data['response_type']
            if not form.cleaned_data['default_response_luo'] == '' \
                and not form.cleaned_data['default_response_en'] == '':
                (translation, created) = \
                    Translation.objects.get_or_create(language='ach',
                                                      field=form.cleaned_data['default_response_en'],
                                                      value=form.cleaned_data['default_response_luo'])
            if not form.cleaned_data['default_response_kdj'] == '' \
                and not form.cleaned_data['default_response_en'] == '':
                (translation, created) = \
                    Translation.objects.get_or_create(language='kdj',
                                                      field=form.cleaned_data['default_response_en'],
                                                      value=form.cleaned_data['default_response_kdj'])

            if not form.cleaned_data['question_luo'] == '':
                (translation, created) = \
                    Translation.objects.get_or_create(language='ach',
                                                      field=form.cleaned_data['question_en'],
                                                      value=form.cleaned_data['question_luo'])

            if not form.cleaned_data['question_kdj'] == '':
                (translation, created) = \
                    Translation.objects.get_or_create(language='kdj',
                                                      field=form.cleaned_data['question_en'],
                                                      value=form.cleaned_data['question_kdj'])

            poll_type = (Poll.TYPE_TEXT if p_type
                                           == NewPollForm.TYPE_YES_NO else p_type)

            poll = Poll.create_with_bulk( \
                name,
                poll_type,
                question,
                default_response,
                contacts,
                req.user)

            if p_type == NewPollForm.TYPE_YES_NO:
                poll.add_yesno_categories()

            if settings.SITE_ID:
                poll.sites.add(Site.objects.get_current())

            return redirect(reverse('ureport.views.view_poll', args=[poll.pk]))

    else:
        form = NewPollForm()
        groups_form = GroupsFilter()
        form.updateTypes()

    return render_to_response('ureport/new_poll.html', {'form': form, 'groups_form': groups_form},
                              context_instance=RequestContext(req))