Beispiel #1
0
    def post(self, request, *args, **kwargs):
        choices = []

        if 'question' not in request.POST:
            return render(request, 'approval_polls/create.html',
                          {'question_error': 'The question is missing'})
        else:
            question = request.POST['question'].strip()

            if not question:
                return render(request, 'approval_polls/create.html',
                              {'question_error': 'The question is missing'})

            for key in request.POST:
                # this could be done using String.startswith instead of re
                # but then it would be harder to avoid POST params
                # that aren't choices but happen to start with choice.
                # in case someone adds a "choiceType" option later.
                m = re.match("choice(\d+)", key)
                if m:
                    text = request.POST[key].strip()
                    if text == "":
                        continue
                    c = int(m.group(1))
                    linkname = 'linkurl-choice{}'.format(c)
                    if linkname in request.POST:
                        linktext = request.POST[linkname].strip()
                    else:
                        linktext = None
                    choices.append((c, text, linktext))

            choices.sort(key=lambda k: k[0])

            if not len(choices):
                return render(
                    request, 'approval_polls/create.html', {
                        'choice_error': 'At least one choice is required',
                        'question': question
                    })

            # The voting type to be used by the poll
            vtype = request.POST['radio-poll-type']

            if 'close-datetime' in request.POST:
                closedatetime = request.POST['close-datetime']
            else:
                closedatetime = ""

            if closedatetime:
                closedatetime = datetime.datetime.strptime(
                    closedatetime, '%Y/%m/%d %H:%M')
                current_datetime = timezone.localtime(timezone.now())
                current_tzinfo = current_datetime.tzinfo
                closedatetime = closedatetime.replace(tzinfo=current_tzinfo)
            else:
                closedatetime = None

            if 'show-close-date' in request.POST:
                show_close_date = True
            else:
                show_close_date = False

            if 'show-countdown' in request.POST:
                show_countdown = True
            else:
                show_countdown = False

            if 'show-write-in' in request.POST:
                show_write_in = True
            else:
                show_write_in = False

            if 'show-lead-color' in request.POST:
                show_lead_color = True
            else:
                show_lead_color = False

            if 'show-email-opt-in' in request.POST:
                show_email_opt_in = True
            else:
                show_email_opt_in = False

            if 'public-poll-visibility' in request.POST:
                is_private = False
            else:
                is_private = True

            p = Poll(
                question=question,
                pub_date=timezone.now(),
                user=request.user,
                vtype=vtype,
                close_date=closedatetime,
                show_close_date=show_close_date,
                show_countdown=show_countdown,
                show_write_in=show_write_in,
                show_lead_color=show_lead_color,
                show_email_opt_in=show_email_opt_in,
                is_private=is_private,
            )
            p.save()

            for choice in choices:
                p.choice_set.create(choice_text=choice[1],
                                    choice_link=choice[2])

            if len(str(request.POST['token-tags'])):
                p.add_tags(request.POST['token-tags'].split(','))
            if vtype == '3':
                p.send_vote_invitations(request.POST['token-emails'])

            return HttpResponseRedirect(
                reverse('approval_polls:embed_instructions', args=(p.id, )))
Beispiel #2
0
    def post(self, request, *args, **kwargs):
        choices = []

        if 'question' not in request.POST:
            return render(
                request,
                'approval_polls/create.html',
                {'question_error': 'The question is missing'}
            )
        else:
            question = request.POST['question'].strip()

            if not question:
                return render(
                    request,
                    'approval_polls/create.html',
                    {'question_error': 'The question is missing'}
                )

            for key in request.POST:
                # this could be done using String.startswith instead of re
                # but then it would be harder to avoid POST params
                # that aren't choices but happen to start with choice.
                # in case someone adds a "choiceType" option later.
                m = re.match("choice(\d+)", key)
                if m:
                    text = request.POST[key].strip()
                    if text == "":
                        continue
                    c = int(m.group(1))
                    linkname = 'linkurl-choice{}'.format(c)
                    if linkname in request.POST:
                        linktext = request.POST[linkname].strip()
                    else:
                        linktext = None
                    choices.append((c, text, linktext))

            choices.sort(key=lambda k: k[0])

            if not len(choices):
                return render(request, 'approval_polls/create.html', {
                    'choice_error': 'At least one choice is required',
                    'question': question
                })

            # The voting type to be used by the poll
            vtype = request.POST['radio-poll-type']

            if 'close-datetime' in request.POST:
                closedatetime = request.POST['close-datetime']
            else:
                closedatetime = ""

            if closedatetime:
                closedatetime = datetime.datetime.strptime(
                    closedatetime,
                    '%Y/%m/%d %H:%M'
                    )
                current_datetime = timezone.localtime(timezone.now())
                current_tzinfo = current_datetime.tzinfo
                closedatetime = closedatetime.replace(
                    tzinfo=current_tzinfo
                    )
            else:
                closedatetime = None

            if 'show-close-date' in request.POST:
                show_close_date = True
            else:
                show_close_date = False

            if 'show-countdown' in request.POST:
                show_countdown = True
            else:
                show_countdown = False

            if 'show-write-in' in request.POST:
                show_write_in = True
            else:
                show_write_in = False

            if 'show-lead-color' in request.POST:
                show_lead_color = True
            else:
                show_lead_color = False

            if 'public-poll-visibility' in request.POST:
                is_private = False
            else:
                is_private = True

            p = Poll(
                question=question,
                pub_date=timezone.now(),
                user=request.user,
                vtype=vtype,
                close_date=closedatetime,
                show_close_date=show_close_date,
                show_countdown=show_countdown,
                show_write_in=show_write_in,
                show_lead_color=show_lead_color,
                is_private=is_private,
            )
            p.save()

            for choice in choices:
                p.choice_set.create(choice_text=choice[1], choice_link=choice[2])

            if len(str(request.POST['token-tags'])):
                p.add_tags(request.POST['token-tags'].split(','))
            if vtype == '3':
                p.send_vote_invitations(request.POST['token-emails'])

            return HttpResponseRedirect(
                reverse('approval_polls:embed_instructions', args=(p.id,))
            )