Пример #1
0
def dummy(count):
    words = [
        'why','do','federer','nadal','elena','deepak','rudra','go','live','love','startup','india',
        'near', 'far', 'about','give', 'take', 'bird', 'lion', 'window','door', 'try', 'yoda', 'prestige',
        'star', 'sun', 'father', 'mother'
    ]

    ans = [
        'maybe', 'yes', 'no', 'never', 'do', 'lie', 'cheat', 'steal',
        'run', 'whatever', 'see', 'eat', 'why', 'not'
    ]

    topics = Topic.objects.all()

    for i in xrange(count):
        q = Question()
        q.question_text = ' '.join(random.sample(words, random.randint(8,15)))
        q.topic = topics[random.randint(0,len(topics)-1)]
        q.user = random.sample(User.objects.all(), 1)[0]
        q.save()

        choice_count = random.randint(2,6)
        for j in xrange(choice_count):
            ch = Choice()
            ch.choice_text = ' '.join(random.sample(ans, random.randint(2,4)))
            ch.question = q
            ch.save()
Пример #2
0
 def post(self, request):
     data = request.POST
     question_id = data.get('question')
     try:
         question = Question.objects.get(pk=question_id)
     except Question.DoesNotExist as e:
         return HttpResponseNotFound(e)
     return_data = {
         'question': question_id,
         'question_type': question.type,
         'question_text': question.question_text
     }
     ch = []
     if question:
         for possible_answer in data.getlist('possible_answer'):
             if possible_answer:
                 choice = Choice(question=question, value=possible_answer)
                 choice.save()
                 ch.append({
                     'value': choice.value,
                     'id': choice.id,
                 })
         return_data['choices'] = ch
     else:
         return JsonResponse({'error': 'error'}, safe=False, status=400)
     if request.is_ajax():
         return JsonResponse(return_data, safe=False)
     else:
         return HttpResponse('')
Пример #3
0
def create_a_poll(request,survey_id):
    # create a poll in a survey 
    # get questions from input 
    # redirect to craetPollPage
    
    
    logging.debug(request.POST)
    question = request.POST['Question0']
    choice_list = request.POST.getlist('Field1')
    s = get_object_or_404(Survey,pk = survey_id)
    # check if user correct
    if not s.author == request.user.username:
        return 
    
    p = Poll(survey=s, question = question)
    p.save()
    # get choice from form input
    logging.debug(choice_list)
    for c in choice_list:
        if c:
            logging.debug(c)
            choice = Choice(poll= p,  choice = c, votes=0)
            choice.save()
    
     
    return HttpResponseRedirect('createPollPage')
Пример #4
0
def choice_add(request):
    question = Question.objects.get(id=request.session['current_question'])
    newChoice = Choice()
    newChoice.choice_text = request.POST["choice_text"]
    question.choice_set.add(newChoice)
    newChoice.save()
    question.save()
    return redirect('admin-choice-add-view')
Пример #5
0
def api_poll_create(request):
    api_result = {"api": "poll_create", "status": "success"}
    try:
        token = request.POST["token"]
        user = get_user_from_token(token)
        if not user:
            api_result["status"] = "failure"
            api_result["error"] = "user not found"
        else:
            question = request.POST["question"]
            choice_text = request.POST["choices"]
            group_id = request.POST["group_id"]
            if group_id == "0":
                group = None
            else:
                group = Group.objects.get(pk=group_id)

            choices = set(choice_text.split("##"))
            if "" in choices:
                choices.remove("")

            try:
                topic = Topic.objects.get(name=request.POST["topic"])
            except:
                topic = Topic.objects.get(name="others")

            if len(question) == 0 or len(choices) < 2:
                raise ValueError("invalid poll arguments")

            q = Question()
            q.question_text = question.strip()
            q.user = user
            q.topic_id = topic.id
            q.group = group
            # q.pub_date = datetime.now(pytz.timezone("Asia/Calcutta"))

            q.save()

            for choice in choices:
                c = Choice()
                c.choice_text = choice
                c.question = q
                c.votes = 0
                c.save()
                print c.id

            print q.id
            api_result["question_id"] = q.id

    except Exception as e:
        api_result["status"] = "failure"
        api_result["error"] = e.message

    return JsonResponse(api_result)
Пример #6
0
def create(request):
    topics = Topic.objects.all()
    context = {}
    context["topics"] = topics
    if request.method == "POST":
        print "inside"
        print request.POST
        try:
            question = request.POST["question"]
            print "question", question
            choices = set(request.POST["choices"].split("##"))
            if "" in choices:
                choices.remove("")
            print "choices", choices
            topic_id = request.POST["topic_id"]
            print "topic_id", topic_id
            if len(question) == 0 or len(choices) < 2:
                raise ValueError("invalid poll arguments")

            q = Question()
            q.question_text = question.strip()
            q.user = request.user
            q.topic_id = topic_id
            # q.pub_date = datetime.now(pytz.timezone("Asia/Calcutta"))

            q.save()

            for choice in choices:
                c = Choice()
                c.choice_text = choice
                c.question = q
                c.votes = 0
                c.save()
                print c.id

            print q.id
            data = {}
            data["question_id"] = q.id
            return JsonResponse(data)

        except Exception as e:
            print e.errno
            print e.strerror

            context["error"] = "Please enter valid question and at least two choices"
            return render(request, "polls/v1_create.html", context)

    else:
        return render(request, "polls/v1_create.html", context)
Пример #7
0
def add(request):
    if not request.user.id:
        return redirect('/accounts/login?flag=1')



    if request.POST:
        poll = Poll(question=request.POST.get('question'))
        poll.save()
        for ch in request.POST.getlist('choice'):
            choice = Choice(poll=poll, choice=ch)
            choice.save()
        poll_list = Poll.objects.all()
        return render_to_response('index.html',dict(poll_list=poll_list, msg_success='Successfully submitted poll and choices'))
    else:
        return render(request,'add.html',{})
Пример #8
0
def create_a_poll(request, survey_id):
    # create a poll in a survey
    # get questions from input
    # redirect to craetPollPage

    logging.debug(request.POST)
    question = request.POST['Question0']
    choice_list = request.POST.getlist('Field1')
    s = get_object_or_404(Survey, pk=survey_id)
    # check if user correct
    if not s.author == request.user.username:
        return

    p = Poll(survey=s, question=question)
    p.save()
    # get choice from form input
    logging.debug(choice_list)
    for c in choice_list:
        if c:
            logging.debug(c)
            choice = Choice(poll=p, choice=c, votes=0)
            choice.save()

    return HttpResponseRedirect('createPollPage')
Пример #9
0
    u.password = "******"
    u.save()

    p1 = Poll()
    p1.name = "Testpoll 1"
    p1.number = "+46766862842"
    p1.allow_duplicate_answers = True
    p1.allow_multiple_choices = True
    p1.owner = u
    p1.save()

    c1 = Choice()
    c1.poll = p1
    c1.name = "01"
    c1.description = "Bidrag nr. 1 - Bä bä vita lamm"
    c1.save()

    c2 = Choice()
    c2.poll = p1
    c2.name = "02"
    c2.description = "Bidrag nr. 2 - Blinka lilla stjärna"
    c2.save()

    c3 = Choice()
    c3.poll = p1
    c3.name = "03"
    c3.description = "Bidrag nr. 3 - Björnen sover"
    c3.save()

    c4 = Choice()
    c4.poll = p1