Esempio n. 1
0
File: misc.py Progetto: vojto/riddle
def show_questions(qaire_id):
    user = auth.get_logged_in_user()
    qaires = Questionnaire.select().join(Category).where(Questionnaire.public_id == qaire_id).where(Category.teacher == user)

    ret = {}

    for qaire in qaires:
        category = Category.select().join(Questionnaire).where(Questionnaire.id == qaire.id)
        questions = Question.select().join(Questionnaire).where(Questionnaire.id == qaire.id)

        catname = ''

        for cat in category:
            catname = cat.name
            break

        ret = {'id': qaire.id, 'public_id': qaire.public_id, 'name': qaire.name, 'category': catname, 'questions' : []}

        for qion in questions:
            qtype = qtype2str(qion.typ)
            ret['questions'].append({'id': qion.id, 'type': qion.typ, 'description': qion.description, 'presented': qion.presented})

            if qtype == 'single' or qtype == 'multi':
                ret['questions'][-1]['options'] = []
                options = Option.select().join(Question).where(Question.id == qion.id)

                for opt in options:
                    ret['questions'][-1]['options'].append({'id': opt.id, 'text': opt.text})

    return json.dumps(ret)
Esempio n. 2
0
def new_questionnaire():
    user = auth.get_logged_in_user()

    name = request.form['name']
    category_id = request.form['category_id']
    public_id = request.form.get('public_id')

    if not public_id:
        public_id = public_id_from_name(name)

    ret = {}

    cats = Category.select().where(Category.teacher == user).where(
        Category.id == category_id)

    category = None

    for cat in cats:
        category = cat
        break

    if not category:
        return response_error('category_not_found')

    questionnaire = Questionnaire.create(name=name,
                                         public_id=public_id,
                                         category=category)
    if not questionnaire:
        return response_error('already_exists')

    ret = response_success(False)
    ret['public_id'] = public_id
    ret['id'] = questionnaire.id
    return json.dumps(ret)
Esempio n. 3
0
def new_question():
    user = auth.get_logged_in_user()

    description = request.form['description']
    typ = request.form['type']
    presented = request.form.get('presented', False)
    public_id = request.form['public_id']
    options = request.form.getlist('options[]')

    try:
        # Find questionnaire
        qaire = Questionnaire.select().join(Category).where(
            Category.teacher == user).where(
                Questionnaire.public_id == public_id).get()

        # Create question
        if qtype2str(typ) is None:
            return response_error('unknown_question_type')
        question = Question.create(description=description,
                                   typ=typ,
                                   presented=presented,
                                   questionnaire=qaire)

        # Create option
        for option_text in options:
            option = Option.create(question=question, text=option_text)

        ret = response_success(False)
        ret['question_id'] = question.id
        return json.dumps(ret)

    except Questionnaire.DoesNotExist:
        return response_error('public_id_not_found')
Esempio n. 4
0
def edit_questionnaire():
    user = auth.get_logged_in_user()

    questionnaire_id = request.form['id']
    name = request.form.get('name')
    category_id = request.form.get('category_id')
    public_id = request.form.get('public_id')

    qaires = Questionnaire.select().join(Category).where(Category.teacher == user).where(Questionnaire.id == questionnaire_id)

    for qaire in qaires:
        if category_id:
            categs = Category.select().where(Category.teacher == user).where(Category.id == category_id)
            for categ in categs:
                qaire.category = categ
                break
            else:
                return response_error('category_not_found')

        if public_id:
            qaires2 = Questionnaire.select().where(Questionnaire.public_id == public_id)
            for qaire2 in qaires2:
                return response_error('public_id_already_exists')

            qaire.public_id = public_id

        if name:
            qaire.name = name

        qaire.save()

        return response_success()

    return response_error('questionnaire_not_found')
Esempio n. 5
0
def new_question():
    user = auth.get_logged_in_user()

    description = request.form['description']
    typ = request.form['type']
    presented = request.form.get('presented', False)
    public_id = request.form['public_id']
    options = request.form.getlist('options[]')

    try:
        # Find questionnaire
        qaire = Questionnaire.select().join(Category).where(Category.teacher == user).where(Questionnaire.public_id == public_id).get()

        # Create question
        if qtype2str(typ) is None:
            return response_error('unknown_question_type')
        question = Question.create(description=description, typ=typ, presented=presented, questionnaire=qaire)

        # Create option
        for option_text in options:
            option = Option.create(question=question, text=option_text)

        ret = response_success(False)
        ret['question_id'] = question.id
        return json.dumps(ret)

    except Questionnaire.DoesNotExist:
        return response_error('public_id_not_found')
Esempio n. 6
0
def new_questionnaire():
    user = auth.get_logged_in_user()

    name = request.form['name']
    category_id = request.form['category_id']
    public_id = request.form.get('public_id')

    if not public_id:
        public_id = public_id_from_name(name)

    ret = {}

    cats = Category.select().where(Category.teacher == user).where(Category.id == category_id)

    category = None

    for cat in cats:
        category = cat
        break

    if not category:
        return response_error('category_not_found')

    questionnaire = Questionnaire.create(name=name, public_id=public_id, category=category)
    if not questionnaire:
        return response_error('already_exists')

    ret = response_success(False)
    ret['public_id'] = public_id
    ret['id'] = questionnaire.id
    return json.dumps(ret)
Esempio n. 7
0
def results_texts():
    user = auth.get_logged_in_user()
    question_id = request.form['question_id']

    qions = Question.select().join(Questionnaire).join(Category).where(
        Category.teacher == user).where(Question.id == question_id)

    for qion in qions:
        strtype = qtype2str(qion.typ)

        ret = {'question_type': strtype, 'question_answers': []}

        answers = Answer.select(Answer, Student).where(Answer.question == qion)

        for answer in answers:
            if answer.text:
                ret['question_answers'].append({
                    'text': answer.text,
                    'student': answer.student.name,
                    'id': answer.id
                })

        return json.dumps(ret)

    return response_error('question_not_found')
Esempio n. 8
0
def results_options():
    user = auth.get_logged_in_user()
    question_id = request.form['question_id']

    qions = Question.select().join(Questionnaire).join(Category).where(
        Category.teacher == user).where(Question.id == question_id)
    for qion in qions:
        strtype = qtype2str(qion.typ)

        ret = {'question_type': strtype, 'question_answers': []}

        if strtype == 'single' or strtype == 'multi':
            opts = Option.select().where(
                Option.question == qion).annotate(Answer)

            for opt in opts:
                ret['question_answers'].append({
                    'option_id': opt.id,
                    'option_text': opt.text,
                    'answers': opt.count
                })
        else:
            return response_error('wrong_question_type')

        return json.dumps(ret)

    return response_error('question_not_found')
Esempio n. 9
0
def show_categories():
    user = auth.get_logged_in_user()
    cats = Category.select().where(Category.teacher == user)
    ret = []
    for c in cats:
        ret.append({'name': c.name, 'id': c.id})

    return json.dumps(ret)
Esempio n. 10
0
def show_categories():
    user = auth.get_logged_in_user()
    cats = Category.select().where(Category.teacher == user)
    ret = []
    for c in cats:
        ret.append({'name': c.name, 'id': c.id})

    return json.dumps(ret)
Esempio n. 11
0
def add_category():
    user = auth.get_logged_in_user()
    name = request.form['name']
    cats = Category.select().where(Category.teacher == user).where(Category.name == name);

    for c in cats:
        return response_error('already_exists')

    Category.insert(name=name, teacher=user).execute()

    return response_success()
Esempio n. 12
0
def remove_option():
    user = auth.get_logged_in_user()
    option_id = request.form['option_id']

    opts = Option.select().join(Question).join(Questionnaire).join(Category).where(Category.teacher == user).where(Option.id == option_id)

    for opt in opts:
        opt.delete_instance(recursive=True, delete_nullable=True)
        return response_success()

    return response_error('option_not_found')
Esempio n. 13
0
def remove_question():
    user = auth.get_logged_in_user()
    question_id = request.form['id']

    qions = Question.select().join(Questionnaire).join(Category).where(Category.teacher == user).where(Question.id == question_id)

    for qion in qions:
        qion.delete_instance(recursive=True, delete_nullable=True)
        return response_success()

    return response_error('question_not_found')
Esempio n. 14
0
File: misc.py Progetto: vojto/riddle
def show_qaires():
    user = auth.get_logged_in_user()
    cats = Category.select().join(Teacher).where(Teacher.id == user.id)
    ret = []
    for c in cats:
        ret.append({'id': c.id, 'category': c.name, 'questionnaires': []})
        qaires = Questionnaire.select().join(Category).where(Category.id == c.id)
        for q in qaires:
            ret[-1]['questionnaires'].append({'id': q.id, 'name': q.name, 'public_id': q.public_id})

    return json.dumps(ret)
Esempio n. 15
0
def add_category():
    user = auth.get_logged_in_user()
    name = request.form['name']
    cats = Category.select().where(Category.teacher == user).where(
        Category.name == name)

    for c in cats:
        return response_error('already_exists')

    Category.insert(name=name, teacher=user).execute()

    return response_success()
Esempio n. 16
0
def remove_question():
    user = auth.get_logged_in_user()
    question_id = request.form['id']

    qions = Question.select().join(Questionnaire).join(Category).where(
        Category.teacher == user).where(Question.id == question_id)

    for qion in qions:
        qion.delete_instance(recursive=True, delete_nullable=True)
        return response_success()

    return response_error('question_not_found')
Esempio n. 17
0
File: misc.py Progetto: vojto/riddle
def remove_comment():
    user = auth.get_logged_in_user()

    comment_id = request.form['id']

    comments = Comment.select().join(Questionnaire).join(Category).where(Category.teacher == user).where(Comment.id == comment_id)

    for comment in comments:
        comment.delete_instance(recursive=True, delete_nullable=True)
        return response_success()

    return response_error('comment_not_found')
Esempio n. 18
0
def remove_category():
    user = auth.get_logged_in_user()
    category_id = request.form['id']
    name = request.form['name']

    cats = Category.select().where(Category.teacher == user).where(Category.id == category_id);

    for cat in cats:
        cat.delete_instance(recursive=True, delete_nullable=True)
        return response_success()

    return response_error('category_not_found')
Esempio n. 19
0
def remove_comment():
    user = auth.get_logged_in_user()

    comment_id = request.form['id']

    comments = Comment.select().join(Questionnaire).join(Category).where(
        Category.teacher == user).where(Comment.id == comment_id)

    for comment in comments:
        comment.delete_instance(recursive=True, delete_nullable=True)
        return response_success()

    return response_error('comment_not_found')
Esempio n. 20
0
def remove_category():
    user = auth.get_logged_in_user()
    category_id = request.form['id']
    name = request.form['name']

    cats = Category.select().where(Category.teacher == user).where(
        Category.id == category_id)

    for cat in cats:
        cat.delete_instance(recursive=True, delete_nullable=True)
        return response_success()

    return response_error('category_not_found')
Esempio n. 21
0
def edit_option():
    user = auth.get_logged_in_user()
    option_id = request.form['option_id']
    text = request.form['text']

    opts = Option.select().join(Question).join(Questionnaire).join(Category).where(Category.teacher == user).where(Option.id == option_id)

    for opt in opts:
        opt.text = text
        opt.save()
        return response_success()

    return response_error('option_not_found')
Esempio n. 22
0
def edit_category():
    user = auth.get_logged_in_user()
    category_id = request.form['id']
    name = request.form['name']

    cats = Category.select().where(Category.teacher == user).where(Category.id == category_id);

    for cat in cats:
        cat.name = name
        cat.save()
        return response_success()

    return response_error('category_not_found')
Esempio n. 23
0
def edit_category():
    user = auth.get_logged_in_user()
    category_id = request.form['id']
    name = request.form['name']

    cats = Category.select().where(Category.teacher == user).where(
        Category.id == category_id)

    for cat in cats:
        cat.name = name
        cat.save()
        return response_success()

    return response_error('category_not_found')
Esempio n. 24
0
def edit_question():
    user = auth.get_logged_in_user()

    question_id = request.form['id']
    description = request.form.get('description')
    typ = request.form.get('type')
    presented = request.form.get('presented')
    public_id = request.form.get('public_id')

    ret = {}

    qions = Question.select().join(Questionnaire).join(Category).where(
        Category.teacher == user).where(Question.id == question_id)

    for qion in qions:
        if description is not None:
            qion.description = description

        if typ:
            if not qtype2str(typ):
                return response_error('unknown_question_type')

            qion.typ = typ

        if presented is not None:
            qion.presented = presented

        if public_id:
            qaires = Questionnaire.select().join(Category).where(
                Category.teacher == user).where(
                    Questionnaire.public_id == public_id)
            for qaire in qaires:
                qion.public_id = public_id
                break
            else:
                return response_error('public_id_not_found')

        qion.save()

        if typ and qtype2str(qion.typ) == 'text':
            # Delete options when changing type to 'text'.
            opts = Option.select().where(Option.question == qion)
            for opt in opts:
                opt.delete_instance(recursive=True, delete_nullable=True)

        return response_success()

    return response_error('question_not_found')
Esempio n. 25
0
def show_questions(qaire_id):
    user = auth.get_logged_in_user()
    qaires = Questionnaire.select().join(Category).where(
        Questionnaire.public_id == qaire_id).where(Category.teacher == user)

    ret = {}

    for qaire in qaires:
        category = Category.select().join(Questionnaire).where(
            Questionnaire.id == qaire.id)
        questions = Question.select().join(Questionnaire).where(
            Questionnaire.id == qaire.id)

        catname = ''

        for cat in category:
            catname = cat.name
            break

        ret = {
            'id': qaire.id,
            'public_id': qaire.public_id,
            'name': qaire.name,
            'category': catname,
            'questions': []
        }

        for qion in questions:
            qtype = qtype2str(qion.typ)
            ret['questions'].append({
                'id': qion.id,
                'type': qion.typ,
                'description': qion.description,
                'presented': qion.presented
            })

            if qtype == 'single' or qtype == 'multi':
                ret['questions'][-1]['options'] = []
                options = Option.select().join(Question).where(
                    Question.id == qion.id)

                for opt in options:
                    ret['questions'][-1]['options'].append({
                        'id': opt.id,
                        'text': opt.text
                    })

    return json.dumps(ret)
Esempio n. 26
0
def show_qaires():
    user = auth.get_logged_in_user()
    cats = Category.select().join(Teacher).where(Teacher.id == user.id)
    ret = []
    for c in cats:
        ret.append({'id': c.id, 'category': c.name, 'questionnaires': []})
        qaires = Questionnaire.select().join(Category).where(
            Category.id == c.id)
        for q in qaires:
            ret[-1]['questionnaires'].append({
                'id': q.id,
                'name': q.name,
                'public_id': q.public_id
            })

    return json.dumps(ret)
Esempio n. 27
0
def edit_question():
    user = auth.get_logged_in_user()

    question_id = request.form['id']
    description = request.form.get('description')
    typ = request.form.get('type')
    presented = request.form.get('presented')
    public_id = request.form.get('public_id')

    ret = {}

    qions = Question.select().join(Questionnaire).join(Category).where(Category.teacher == user).where(Question.id == question_id)

    for qion in qions:
        if description is not None:
            qion.description = description

        if typ:
            if not qtype2str(typ):
                return response_error('unknown_question_type')

            qion.typ = typ

        if presented is not None:
            qion.presented = presented

        if public_id:
            qaires = Questionnaire.select().join(Category).where(Category.teacher == user).where(Questionnaire.public_id == public_id)
            for qaire in qaires:
                qion.public_id = public_id
                break
            else:
                return response_error('public_id_not_found')


        qion.save()

        if typ and qtype2str(qion.typ) == 'text':
            # Delete options when changing type to 'text'.
            opts = Option.select().where(Option.question == qion)
            for opt in opts:
                opt.delete_instance(recursive=True, delete_nullable=True)

        return response_success()


    return response_error('question_not_found')
Esempio n. 28
0
def new_option():
    user = auth.get_logged_in_user()
    question_id = request.form['question_id']
    text = request.form['text']

    qions = Question.select().join(Questionnaire).join(Category).where(Category.teacher == user).where(Question.id == question_id)

    for qion in qions:
        typ = qtype2str(qion.typ)
        if typ == 'single' or typ == 'multi':
            option = Option.create(text=text, question=qion)
            ret = response_success(False)
            ret['option_id'] = option.id
            return json.dumps(ret)
        else:
            return response_error('options_not_supported')

    return response_error('question_not_found')
Esempio n. 29
0
File: misc.py Progetto: vojto/riddle
def results_texts():
    user = auth.get_logged_in_user()
    question_id = request.form['question_id']

    qions = Question.select().join(Questionnaire).join(Category).where(Category.teacher == user).where(Question.id == question_id)

    for qion in qions:
        strtype = qtype2str(qion.typ)

        ret = {'question_type': strtype, 'question_answers': []}

        answers = Answer.select(Answer, Student).where(Answer.question == qion)

        for answer in answers:
            if answer.text:
                ret['question_answers'].append({'text': answer.text, 'student': answer.student.name, 'id': answer.id})


        return json.dumps(ret)

    return response_error('question_not_found')
Esempio n. 30
0
File: misc.py Progetto: vojto/riddle
def results_options():
    user = auth.get_logged_in_user()
    question_id = request.form['question_id']

    qions = Question.select().join(Questionnaire).join(Category).where(Category.teacher == user).where(Question.id == question_id)
    for qion in qions:
        strtype = qtype2str(qion.typ)

        ret = {'question_type': strtype, 'question_answers': []}

        if strtype == 'single' or strtype == 'multi':
            opts = Option.select().where(Option.question == qion).annotate(Answer)

            for opt in opts:
                ret['question_answers'].append({'option_id': opt.id, 'option_text': opt.text, 'answers': opt.count})
        else:
            return response_error('wrong_question_type')

        return json.dumps(ret)

    return response_error('question_not_found')
Esempio n. 31
0
def edit_questionnaire():
    user = auth.get_logged_in_user()

    questionnaire_id = request.form['id']
    name = request.form.get('name')
    category_id = request.form.get('category_id')
    public_id = request.form.get('public_id')

    qaires = Questionnaire.select().join(Category).where(
        Category.teacher == user).where(Questionnaire.id == questionnaire_id)

    for qaire in qaires:
        if category_id:
            categs = Category.select().where(Category.teacher == user).where(
                Category.id == category_id)
            for categ in categs:
                qaire.category = categ
                break
            else:
                return response_error('category_not_found')

        if public_id:
            qaires2 = Questionnaire.select().where(
                Questionnaire.public_id == public_id)
            for qaire2 in qaires2:
                return response_error('public_id_already_exists')

            qaire.public_id = public_id

        if name:
            qaire.name = name

        qaire.save()

        return response_success()

    return response_error('questionnaire_not_found')
Esempio n. 32
0
def set_teacher_settings():
    user = auth.get_logged_in_user()

    fullname = request.form.get('fullname')
    old_password = request.form.get('old_password')
    new_password = request.form.get('new_password')
    email = request.form.get('email')

    if fullname:
        user.fullname = fullname

    if email:
        user.email = email

    if old_password and new_password:
        u = auth.authenticate(user.username, old_password)
        if not u:
            return response_error('wrong_old_password')

        user.set_password(new_password)

    user.save()

    return response_success()
Esempio n. 33
0
def logout():
    auth.logout_user(auth.get_logged_in_user())

    return response_success()
Esempio n. 34
0
def get_teacher_settings():
    user = auth.get_logged_in_user()

    return json.dumps({'username': user.username, 'fullname': user.fullname, 'email': user.email})