Пример #1
0
def created(request):
    #if question exists and is not blank, create a new poll p
    try:
        q = request.POST['question']
    except (KeyError):
        #TODO: Tsk, tsk; return some html! Return to 'create' with error message and fields filled.
	return HttpResponse('You have to ask a question!')
    if (q == ''):
        #TODO: Tsk, tsk; return some html!
	return HttpResponse('You have to ask a question!')
    p = Poll(question=q, pub_date=timezone.now(), ballots=0)
    p.save()

    #while choice(c) exists and is not blank, add as a choice
    c = 0
    while (True):
        c += 1
        try:
            text = request.POST['choice'+str(c)]
        except (KeyError):
            break
        if (text == ''):
            continue
	p.choice_set.create(choice_text=text, votes=0)

    #redirect to detail page of your new poll
    return HttpResponseRedirect(reverse('approval_polls:detail', args=(p.id,)))
Пример #2
0
def add(request):
  choices = []

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

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

    c = 1
    name = 'choice1'

    while (name in request.POST):
      text = request.POST[name].strip()
      if (text): choices.append(text)
      c += 1
      name = 'choice{}'.format(c)

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

    p = Poll(question=question, pub_date=timezone.now())
    p.save()

    for choice in choices: p.choice_set.create(choice_text=choice)

    return HttpResponseRedirect(reverse('approval_polls:embed_instructions', args=(p.id,)))
Пример #3
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'}
                )

            c = 1
            name = 'choice1'

            while (name in request.POST):
                text = request.POST[name].strip()
                if (text):
                    choices.append(text)
                c += 1
                name = 'choice{}'.format(c)

            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']

            p = Poll(
                question=question,
                pub_date=timezone.now(),
                user=request.user,
                vtype=vtype
            )
            p.save()

            for choice in choices:
                p.choice_set.create(choice_text=choice)

            return HttpResponseRedirect(
                reverse('approval_polls:embed_instructions', args=(p.id,))
            )
Пример #4
0
def created(request):
    #if question exists and is not blank, create a new poll p
    error = False
    question = False
    choices = []

    # grab data from request
    # question
    try:
        question = request.POST['question']
    except (KeyError):
        error = True
    # while choice(c) exists and is not blank, add as a choice
    c = 0
    while (True):
        c += 1
        try:
            text = request.POST['choice'+str(c)]
        except (KeyError):
            break
        if (text == ''):
            continue
        choices.append(text)

    if (not question or error or len(choices) == 0):
        message = 'You need a question and at least one choice.'
        return render(request, 'approval_polls/error.html', {'error_message': message})

    p = Poll(question=question, pub_date=timezone.now(), ballots=0)
    p.save()

    for choice in choices:
        p.choice_set.create(choice_text=choice, votes=0)

    #redirect to detail page of your new poll
    #return HttpResponseRedirect(reverse('approval_polls:detail', args=(p.id,)))
    link = request.build_absolute_uri('/approval_polls/' + str(p.id))
    return render(request, 'approval_polls/embed_instructions.html', {'link': link})
Пример #5
0
def created(request):
    #if question exists and is not blank, create a new poll p
    error = False
    question = False
    choices = []

    # grab data from request
    # question
    try:
        question = request.POST['question']
    except (KeyError):
        error = True
    # while choice(c) exists and is not blank, add as a choice
    c = 0
    while (True):
        c += 1
        try:
            text = request.POST['choice' + str(c)]
        except (KeyError):
            break
        if (text == ''):
            continue
        choices.append(text)

    if (not question or error or len(choices) == 0):
        message = 'You need a question and at least one choice.'
        return render(request, 'approval_polls/error.html',
                      {'error_message': message})

    p = Poll(question=question, pub_date=timezone.now())
    p.save()

    for choice in choices:
        p.choice_set.create(choice_text=choice)

    link = request.build_absolute_uri('/approval_polls/' + str(p.id))
    return render(request, 'approval_polls/embed_instructions.html',
                  {'link': link})
Пример #6
0
def created(request):
    #if question exists and is not blank, create a new poll p
    q = request.POST['question']
    if (q == ''):
        #TODO: Tsk, tsk; return some html!
        return HttpResponse('You have to ask a question!')
    p = Poll(question=q, pub_date=timezone.now(), ballots=0)
    p.save()

    #while choice(i) exists and is not blank, add as a choice
    c = 1
    while (True):
        try:
            text = request.POST['choice' + str(c)]
        except (KeyError):
            break
        if (text == ''):
            continue
        p.choice_set.create(choice_text=text, votes=0)
        c += 1

    #redirect to detail page of your new poll
    return HttpResponseRedirect(reverse('approval_polls:detail',
                                        args=(p.id, )))
Пример #7
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, )))
Пример #8
0
    def post(self, request, *args, **kwargs):
        choices = []
        email_list = []
        choices_links = {}

        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'}
                )

            c = 1
            name = 'choice1'
            linkname = 'linkurl-choice1'

            while (name in request.POST):
                text = request.POST[name].strip()
                if (text):
                    choices.append(text)
                    if linkname in request.POST:
                        linktext = request.POST[linkname].strip()
                        if linktext:
                            choices_links[text] = linktext
                c += 1
                name = 'choice{}'.format(c)
                linkname = 'linkurl-choice{}'.format(c)

            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

            if vtype == '3':
                # Get all the email Ids to store in the DB.
                emails = request.POST['token-emails']
                for email in emails.split(','):
                    if (re.match("([^@|\s]+@[^@]+\.[^@|\s]+)", email.strip())):
                        email_list.append(email.strip())
                    email_list = list(set(email_list))

            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:
                if choice in choices_links:
                    p.choice_set.create(choice_text=choice, choice_link=choices_links[choice])
                else:
                    p.choice_set.create(choice_text=choice)

            # Add in the vote invitation info, if any.
            for email in email_list:
                vi = VoteInvitation(
                    email=email,
                    sent_date=timezone.now(),
                    poll=p,
                    key=VoteInvitation.generate_key(),
                )
                vi.save()
                vi.send_email()

            return HttpResponseRedirect(
                reverse('approval_polls:embed_instructions', args=(p.id,))
            )
Пример #9
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,))
            )
Пример #10
0
    def post(self, request, *args, **kwargs):
        choices = []
        email_list = []
        choices_links = {}

        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'})

            c = 1
            name = 'choice1'
            linkname = 'linkurl-choice1'

            while (name in request.POST):
                text = request.POST[name].strip()
                if (text):
                    choices.append(text)
                    if linkname in request.POST:
                        linktext = request.POST[linkname].strip()
                        if linktext:
                            choices_links[text] = linktext
                c += 1
                name = 'choice{}'.format(c)
                linkname = 'linkurl-choice{}'.format(c)

            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

            if vtype == '3':
                # Get all the email Ids to store in the DB.
                emails = request.POST['token-emails']
                for email in emails.split(','):
                    if (re.match("([^@|\s]+@[^@]+\.[^@|\s]+)", email.strip())):
                        email_list.append(email.strip())
                    email_list = list(set(email_list))

            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:
                if choice in choices_links:
                    p.choice_set.create(choice_text=choice,
                                        choice_link=choices_links[choice])
                else:
                    p.choice_set.create(choice_text=choice)

            # Add in the vote invitation info, if any.
            for email in email_list:
                vi = VoteInvitation(
                    email=email,
                    sent_date=timezone.now(),
                    poll=p,
                    key=VoteInvitation.generate_key(),
                )
                vi.save()
                vi.send_email()

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