Exemplo n.º 1
0
def admin_signup_route():
    links = Links()
    links.tuq_on_pc = url_for('web.tuq_on_pc_route', _external=True)
    links.admin_reg = url_for('web.admin_signup_route', _external=True)
    links.index_url = url_for('web.index_page_route', _external=True)
    form = AdminRequestForm()
    if form.validate_on_submit():
        submission_info = {
            'username': form.username.data,
            'fullname': form.full_name.data,
            'address': form.address.data,
            'email': form.email.data,
            'nationality': form.nationality.data,
            'alias': form.display_name.data,
            'mobile': form.phone_number.data,
            'password': form.password.data
        }
        json_writer = MyJSONObjectWriter()
        json.dump(submission_info,
                  json_writer,
                  skipkeys=True,
                  indent=2,
                  separators=(',', ': '))
        data_string = json_writer.get_buffer()

        data_cache.hset('tuq:admin_requests', form.username.data, data_string)
        session['name'] = form.full_name.data
        return redirect(url_for('web.submission_made_route', _external=True))
    else:
        return render_template('admin_register.html', form=form, links=links)
Exemplo n.º 2
0
def admin_signup_route():
    form = AdminRequestForm()
    if form.validate_on_submit():
        submission_info = {'username': form.username.data, 'fullname': form.full_name.data,
                           'address': form.address.data, 'email': form.email.data,
                           'nationality': form.nationality.data, 'alias': form.display_name.data,
                           'mobile': form.phone_number.data, 'password': form.password.data }
        json_writer = MyJSONObjectWriter()
        json.dump(submission_info,json_writer, skipkeys=True, indent=2, separators=(',', ': '))
        data_string = json_writer.get_buffer()

        data_cache.hset('tuq:admin_requests', form.username.data, data_string)
        flash('Thank you for using Tuq services, {}'.format(form.full_name.data))
        return redirect(url_for('main.submission_made_route', _external=True))
    else:
        return render_template('admin_register.html', form=form)
Exemplo n.º 3
0
def get_result_route():
    all_results = db.session.query(ExamTaken).filter_by(
        participant_id=current_user.id).all()
    data = []
    for result in all_results:
        course_info = data_cache.hget(well_known_courses, result.course_id)
        course_info_obj = None
        if course_info is None:
            course_info = db.session.query(Course).filter_by(
                id=result.course_id).first()
            if course_info is None:  # still none?
                print('We have an issue with {}'.format(result.course_id))
                continue
            #  otherwise cache it
            owner = db.session.query(User).filter_by(
                id=result.course_owner).first()
            course_cache = {
                'name': course_info.name,
                'id': course_info.id,
                'owner': course_info.lecturer_in_charge,
                'owner_username': owner.username,
                'question': course_info.quiz_filename,
                'code': course_info.code,
                'solution': course_info.solution_filename,
                'icon': course_info.logo_location
            }
            print(course_cache)
            data_cache.hset(well_known_courses, result.course_id,
                            json.dumps(course_cache))
            course_info_obj = course_cache  # no need to recheck the data_cache, just use the data
        if course_info_obj is None:  # if it has not been loaded yet, use the data obtained
            course_info_obj = json.loads(course_info)
        data.append({
            'name': course_info_obj['name'],
            'score': result.score,
            'date': result.date_taken,
            'total': result.total_score,
            'code': course_info_obj['code'],
            'owner': course_info_obj['owner']
        })
    return success_response(data)
Exemplo n.º 4
0
def admin_add_course_route():
    try:
        data = request.get_json()
        if data is None:
            return error_response('Invalid data')

        print(data)

        course_name = data.get('name')
        course_code = data.get('course_code')
        personnel_in_charge = data.get('administrator_name')
        hearing_date = data.get('date_to_be_held')
        duration_in_minutes = data.get('duration')
        question = data.get('question')
        approach = data.get('approach')
        randomize_question = data.get('randomize')
        expires = data.get('expires_on')
        solution = data.get('answers')
        icon = data.get('icon')

        departments = data.get('departments')
        repository_name = data.get('repository_name')

        if course_code is None or course_name is None or personnel_in_charge is None\
                or hearing_date is None or duration_in_minutes is None or departments is None\
                or question is None or randomize_question is None or approach is None\
                or solution is None or icon is None:
            return error_response('Missing arguments')

        repositories = current_user.repositories
        repository_to_use = None
        for repo in repositories:
            if repo.repo_name == repository_name:
                repository_to_use = repo
                break

        if repository_to_use is None:
            return error_response('Repository does not exist')
        for course in repository_to_use.courses:
            if course_code == course.code:
                return error_response('Course with that code already exist')

        department_list = []
        try:
            for department_name in departments:
                if len(department_name.strip()) != 0:
                    department_list.append(Department(name=department_name))
        except AttributeError:
            return error_response('Expects a valid data in the departments')

        full_path, solution_fn = (None, None)
        try:
            filename = course_code.replace(' ', '_').replace('.',
                                                             '_') + '.json'
            dir_path = safe_makedir(UPLOAD_DIR, current_user.username,
                                    repository_name)
            full_path = safe_join(dir_path, filename)
            solution_fn = safe_join(dir_path, 'solutions_' + filename)

            with open(full_path, mode='wt') as out:
                json.dump(question,
                          out,
                          sort_keys=True,
                          indent=4,
                          separators=(',', ': '))
            with open(solution_fn, mode='wt') as out:
                json.dump(solution, out, indent=True, separators=(',', ': '))
        except ValueError:
            return error_response('Invalid JSON Document for question')
        course = Course(name=course_name,
                        code=course_code,
                        lecturer_in_charge=personnel_in_charge,
                        answers_approach=approach,
                        expires_on=expires,
                        date_to_be_held=date_from_string(hearing_date),
                        duration_in_minutes=int(duration_in_minutes),
                        departments=department_list,
                        quiz_filename=full_path,
                        solution_filename=solution_fn,
                        randomize_questions=randomize_question,
                        logo_location=icon)

        repository_to_use.courses.append(course)

        db.session.add(course)
        db.session.add(repository_to_use)
        db.session.add(current_user)

        db.session.commit()
        course_cache = {
            'name': course.name,
            'id': course.id,
            'owner': course.lecturer_in_charge,
            'code': course.code,
            'solution': course.solution_filename,
            'icon': course.logo_location,
            'owner_username': current_user.username,
            'question': course.quiz_filename
        }
        data_cache.hset(well_known_courses, course.id,
                        json.dumps(course_cache))
        return success_response('New course added successfully')
    except BadRequest:
        return error_response('Bad request')
    except Exception as e:
        print(e)
        return error_response('Could not add the course')
Exemplo n.º 5
0
def update_course(data, user, database_handle):
    if data is None:
        raise ValueError('Invalid data')
    course_name = data.get('name')
    course_code = data.get('course_code')
    personnel_in_charge = data.get('administrator_name')
    hearing_date = data.get('date_to_be_held')
    duration_in_minutes = data.get('duration')
    question = data.get('question')
    approach = data.get('approach')
    randomize_question = data.get('randomize')
    expires = data.get('expires_on')
    solution = data.get('answers')
    icon_location = data.get('icon')

    departments = data.get('departments')
    repository_name = data.get('repository_name')
    if course_code is None or course_name is None or personnel_in_charge is None \
            or hearing_date is None or duration_in_minutes is None or departments is None \
            or question is None or randomize_question is None or approach is None \
            or solution is None or icon_location is None:
        raise ValueError('Missing arguments')
    repositories = user.repositories
    repository_to_use = None
    for repo in repositories:
        if repo.repo_name == repository_name:
            repository_to_use = repo
            break

    if repository_to_use is None:
        raise ValueError('Repository does not exist')
    course_to_use = None
    for course in repository_to_use.courses:
        if course_code == course.code:
            course_to_use = course
            break
    if course_to_use is None:
        raise ValueError('Course does not exist')

    department_list = []
    try:
        for department_name in departments:
            department_list.append(Department(name=department_name))
    except AttributeError:
        raise ValueError('Expects a valid data in the departments')

    try:
        with open(course_to_use.quiz_filename, mode='wt') as out:
            json.dump(question,
                      out,
                      sort_keys=True,
                      indent=4,
                      separators=(',', ': '))
        with open(course_to_use.solution_filename, mode='wt') as out:
            json.dump(solution, out, indent=True, separators=(',', ': '))
    except ValueError:
        raise ValueError('Invalid JSON Document for question')
    course_to_use.name = course_name
    course_to_use.lecturer_in_charge = personnel_in_charge
    course_to_use.answers_approach = approach
    course_to_use.expires_on = expires
    course_to_use.date_to_be_held = date_from_string(hearing_date)
    course_to_use.duration_in_minutes = int(duration_in_minutes)
    course_to_use.departments = department_list
    course_to_use.randomize_questions = randomize_question
    course_to_use.logo_location = icon_location

    repository_to_use.courses.append(course_to_use)
    database_handle.session.add(course_to_use)
    db.session.commit()
    course_cache = {
        'name': course_to_use.name,
        'id': course_to_use.id,
        'owner': course_to_use.lecturer_in_charge,
        'icon': icon_location,
        'code': course_to_use.code,
        'solution': course_to_use.solution_filename,
        'question': course_to_use.quiz_filename
    }
    data_cache.hset(well_known_courses, course_to_use.id,
                    json.dumps(course_cache))