Beispiel #1
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()
Beispiel #2
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()
Beispiel #3
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)
Beispiel #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')
Beispiel #5
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)
Beispiel #6
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)
Beispiel #7
0
def show(qaire_id):
    qaires = Questionnaire.select().where(Questionnaire.public_id == qaire_id)
    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' : [], 'category_name': category.get().name}

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

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

    if not ret:
        ret = response_error('not_found', False)

    return json.dumps(ret)
Beispiel #8
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)
Beispiel #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)
Beispiel #10
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)
Beispiel #11
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')
Beispiel #12
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')
Beispiel #13
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')
Beispiel #14
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')
Beispiel #15
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)
Beispiel #16
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)
Beispiel #17
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')