Beispiel #1
0
def get_stateSurvey_or_error(id_survey,user,ip = None):
    stateSurvey, status = StateSurvey.getStateSurvey(id_survey,user,ip)
    if status == StateSurvey.NO_ERROR:
        return stateSurvey
    else:
        if status == StateSurvey.ERROR_EXCEEDED:
            raise ErrorExceeded
        if status == StateSurvey.ERROR_TIMED_OUT:
            raise ErrorTimedOut
        if status == StateSurvey.ERROR_END_DATE_OUT:
            raise ErrorEndDateOut
        if status == StateSurvey.ERROR_NO_SURVEY:
            return abort(404)
        return abort(500)    
Beispiel #2
0
def generate_answers_fake(id_survey, number=6):
    '''responding to the survey(id_survey) randomly 
    '''
    import random
    import forgery_py
    from app.models import Survey, Consent, Section, Answer, User, StateSurvey
    from app.models import Question, QuestionChoice, QuestionText
    from app.models import QuestionYN ,QuestionLikertScale
    
    def generateUserFake_1(count=100):
        from sqlalchemy.exc import IntegrityError
        base=len(User.query.all())+1
        for i in range(count):
            j=i+base
            u = models.User(email="user"+str(j)+"gmail.com",
                     nickname="user"+str(j))
            db.session.add(u)
        try:
            db.session.commit()
        except IntegrityError:
            db.session.rollback()
        return base

    def answer_question(user,q, time):
        l=[0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8,8.5,9,9.5,10]
        l2=[0,2,4,6,8,10,12,14,16,18,20]
        if q.decision=="decision_one_v1":
            i=random.choice(l)
            answer=models.Answer(answerNumeric=i,user=user,question=q)
            answer.answerText=(i)
        elif q.decision=="decision_one_v2":
            i=random.choice(l)
            answer=models.Answer(answerNumeric=i,user=user,question=q)
            answer.answerText=(i)
        elif q.decision=="decision_two":
            i=random.choice(l)
            answer=models.Answer(answerNumeric=i,user=user,question=q)
            answer.answerText=(i)
        elif q.decision=="decision_three":
            i=random.choice(l)
            answer=models.Answer(answerNumeric=i,user=user,question=q)
            answer.answerText=(i)
        elif q.decision=="decision_four":
            i=random.choice(l2)
            answer=models.Answer(answerNumeric=i,user=user,question=q)
            answer.answerText=(i)
        elif q.decision=="decision_six":
            i=random.choice(l2)
            answer=models.Answer(answerNumeric=i,user=user,question=q)
            answer.answerText=(i)
        elif q.decision=="decision_five":
            i=random.randrange(0,2)
            answer=models.Answer(answerNumeric=i,user=user,question=q)
            answer.answerText = q.choices[answer.answerNumeric]
        elif isinstance (q, QuestionYN):
            answer = Answer (answerYN =random.randrange(0,2)==1, user= user, question = q)
            answer.answerText = str(answer.answerYN)
        elif isinstance (q, QuestionText):
            if q.isExpectedAnswer():
                if random.choice([0,1,1])==1:
                # 66% of know the answer
                    answer = Answer (answerText =q.expectedAnswer.lower(), user= user, question = q)
                    answer.numberAttempt= random.randrange(1, q.maxNumberAttempt+1)
                else:
                    answer = Answer (answerText =forgery_py.forgery.lorem_ipsum.word(), user= user, question = q)
                    answer.numberAttempt= q.maxNumberAttempt+1
            elif q.isNumber:
                answer = Answer (answerNumeric =random.randrange(0,100), user= user, question = q)
                answer.answerText = answer.answerNumeric
            else:
                answer = Answer (answerText =forgery_py.forgery.lorem_ipsum.word(), user= user, question = q)
        elif isinstance (q,QuestionChoice):
            if q.is_range:
                answer = Answer (answerNumeric =random.randrange(q.range_min,q.range_max), user= user, question = q)
                answer.answerText = answer.answerNumeric
            else:
                answer = Answer (answerNumeric =random.randrange(0,len(q.choices)), user= user, question = q)
                answer.answerText = q.choices[answer.answerNumeric]
        elif isinstance (q,QuestionLikertScale):
            answer = Answer (answerNumeric =random.randrange(q.minLikert,q.maxLikert+1), user= user, question = q)
            answer.answerText = answer.answerNumeric
        else:
            print "tipo de pregunta invalido"
            quit()
        t = random.randrange(900, 6500)
        time = time + t
        answer.globalTime = time
        answer.differentialTime = t
        db.session.add(answer)
        return time


    base = generateUserFake_1(number)
    print "a tope"
    for i in range(base,number+base):
        user = User.query.get(i);
        ss, error = StateSurvey.getStateSurvey(id_survey,user,forgery_py.forgery.internet.ip_v4())
        if error != StateSurvey.NO_ERROR:
            raise "error en fechas o en algo"
        ss.accept_consent()
        while not ss.is_finished():
            section = ss.nextSection()
            questions = section.questions
            time = 0
            for question in questions:
                time = answer_question(user,question, time)
            db.session.commit()
            time = time + random.randrange(100, 500)
            ss.finishedSection(time)
        print "finish", i