Exemplo n.º 1
0
def group_get(id):
    group = Group.query.get(id)
    if group:
        if group.creator == g.user:
            return json.dumps(group.as_dict())
        else:
            flash('You are not authorised to view this group')
            return json.dumps({'error': 'unauthorised'})
    else:
        flash('This group does not exist')
        return json.dumps({'error': 'not found'})
Exemplo n.º 2
0
def group_get(id):
    group = Group.query.get(id)
    if group:
        if group.creator == g.user:
            return json.dumps(group.as_dict())
        else:
            flash('You are not authorised to view this group')
            return json.dumps({'error': 'unauthorised'})
    else:
        flash('This group does not exist')
        return json.dumps({'error': 'not found'})
Exemplo n.º 3
0
def scheme_get(id):
    scheme = SchemeOfWork.query.get(id)
    if scheme:
        if scheme.creator == g.user:
            return json.dumps(scheme.as_dict())
        else:
            flash('You are not authorised to view this scheme of work')
            return json.dumps({'error': 'unauthorised'})
    else:
        flash('This scheme of work does not exist')
        return json.dumps({'error': 'not found'})
Exemplo n.º 4
0
def scheme_get(id):
    scheme = SchemeOfWork.query.get(id)
    if scheme:
        if scheme.creator == g.user:
            return json.dumps(scheme.as_dict())
        else:
            flash('You are not authorised to view this scheme of work')
            return json.dumps({'error': 'unauthorised'})
    else:
        flash('This scheme of work does not exist')
        return json.dumps({'error': 'not found'})
Exemplo n.º 5
0
def objective_assess(profile_id, objective_id):
    objective = Objective.query.get(
        objective_id
    )  # DJG - may restrict search to just some set of visisble objectives
    if not objective:
        flash("This objective does not exist")
        return redirect(url_for('.objectives', id=g.user.id))
    profile = User.query.get(profile_id)
    if not profile:
        flash("This user does not exist")
        return redirect(url_for('.objectives', id=g.user.id))
    elif not profile.permission(g.user):
        flash(
            "You do not have permission to view this user's learning objectives"
        )
        return redirect(url_for('.objectives', id=g.user.id))
    else:
        userobjective = UserObjective.FindOrCreate(profile_id, g.user.id,
                                                   objective_id)
        userobjective.assess()
        # import pdb; pdb.set_trace()
        return json.dumps({
            'assessed_display_class':
            userobjective.assessed_display_class(),
            'assessed':
            userobjective.completed
        })
Exemplo n.º 6
0
def send_message(service_layer=_service_layer):

    form = service_layer.messages.populate_message_form(g.user, g.user.subject_id)
    result = {}
    result['savedsuccess'] = False
    if form.validate():
        #import pdb; pdb.set_trace()
        recommended_material = Module.query.get(form.recommended_material.data)
        if recommended_material:
            if form.message_type.data == "Individual":
                recipient = User.user_by_email(form.message_to.data)
                if recipient:
                    message = Message(
                        from_user=g.user,
                        to_user=recipient,
                        subject=form.message_subject.data,
                        body=form.message_body.data,
                        request_access=form.request_access.data,
                        recommended_material_id=recommended_material.id
                    )
                    db.session.add(message)
                    db.session.commit()
                    result['savedsuccess'] = True
                else:
                    result["message_to_unfound"] = form.message_to.data
            elif form.message_type.data == "Group":
                group = Group.query.filter_by(name=form.message_to.data, creator=g.user).one()
                if group:
                    group.message(
                        subject=form.message_subject.data,
                        body=form.message_body.data,
                        request_access=form.request_access.data,
                        recommended_material=recommended_material
                    )
                    result['savedsuccess'] = True
                else:
                    result["message_to"] = "Group of recipients not found"
            else:
                result["message_type"] = "Message type not recognised"
        else:
            result["recommended_material"] = "Module not found"

        return json.dumps(result)
    else:
        form.errors['savedsuccess'] = False
        return json.dumps(form.errors)
Exemplo n.º 7
0
def send_message():
    form = forms.SendMessage()
    result = {}
    result['savedsuccess'] = False
    #import pdb; pdb.set_trace()
    if form.validate():
        #print 'message form submitted'
        #import pdb; pdb.set_trace()
        recommended_material = Module.query.get(form.recommended_material.data)
        if recommended_material:
            if form.message_type.data == "Individual":
                recipient = User.user_by_email(form.message_to.data)
                if recipient:
                    message = Message(
                        from_user=g.user,
                        to_user=recipient,
                        subject=form.message_subject.data,
                        body=form.message_body.data,
                        request_access=form.request_access.data,
                        recommended_material_id=recommended_material.id)
                    db.session.add(message)
                    db.session.commit()
                    result['savedsuccess'] = True
                else:
                    result["message_to_unfound"] = form.message_to.data
            elif form.message_type.data == "Group":
                group = Group.query.filter_by(name=form.message_to.data,
                                              creator=g.user).one()
                if group:
                    group.message(subject=form.message_subject.data,
                                  body=form.message_body.data,
                                  request_access=form.request_access.data,
                                  recommended_material=recommended_material)
                    result['savedsuccess'] = True
                else:
                    result["message_to"] = "Group of recipients not found"
            else:
                result["message_type"] = "Message type not recognised"
        else:
            result["recommended_material"] = "Module not found"

        return json.dumps(result)
    else:
        form.errors['savedsuccess'] = False
        return json.dumps(form.errors)
Exemplo n.º 8
0
def deny_access(request_id):
    result = {}
    request_from = User.query.get(request_id)
    if request_from:
        result['savedsuccess'] = g.user.deny_access(request_from)
    else:
        flash("Tutor not found")
        result['savedsuccess'] = False
    return json.dumps(result)
Exemplo n.º 9
0
def deny_access(request_id):
    result = {}
    request_from = User.query.get(request_id)
    if request_from:
        result['savedsuccess'] = g.user.deny_access(request_from)
    else:
        flash("Tutor not found")
        result['savedsuccess'] = False
    return json.dumps(result)
Exemplo n.º 10
0
def scheme_save():
    form = forms.EditScheme()
    form.edit_scheme_objectives.choices = [
        (i, i) for i in form.edit_scheme_objectives.data
    ]  #DJG - could put some actual validation here

    if form.validate():
        result = {}
        result['savedsuccess'] = False
        id = int(form.edit_scheme_id.data)
        #import pdb; pdb.set_trace()
        scheme_objectives = [
            Objective.query.filter_by(name=o).one()
            for o in form.edit_scheme_objectives.data
        ]
        if id > 0:
            scheme = SchemeOfWork.query.get(id)
            if scheme:
                if scheme.creator == g.user:
                    scheme.name = form.edit_scheme_name.data
                    scheme.objectives = scheme_objectives
                    db.session.add(scheme)
                    db.session.commit()
                    result['savedsuccess'] = True
                    flash('Scheme saved as ' + scheme.name)
                else:
                    flash('You are not authorised to edit this scheme of work')
                    result['error'] = "unauthorised"
            else:
                flash('This scheme of work does not exist')
                result['error'] = "not found"
        else:
            scheme = SchemeOfWork(name=form.edit_scheme_name.data,
                                  creator=g.user,
                                  objectives=scheme_objectives)
            db.session.add(scheme)
            db.session.commit()
            result['savedsuccess'] = True
            flash('New scheme of work saved as ' + scheme.name)
        return json.dumps(result)
    else:
        form.errors['savedsuccess'] = False
        return json.dumps(form.errors)
Exemplo n.º 11
0
def group_save():
    form = forms.EditGroup()
    form.edit_group_members.choices = [
        (i, i) for i in form.edit_group_members.data
    ]  #DJG - could put some actual validation here

    if form.validate():
        result = {}
        result['savedsuccess'] = False
        id = int(form.edit_group_id.data)
        #import pdb; pdb.set_trace()
        group_members = [
            User.user_by_email(e) for e in form.edit_group_members.data
        ]
        if id > 0:
            group = Group.query.get(id)
            if group:
                if group.creator == g.user:
                    group.name = form.edit_group_name.data
                    group.members = group_members
                    db.session.add(group)
                    db.session.commit()
                    result['savedsuccess'] = True
                    flash('Group saved as ' + group.name)
                else:
                    flash('You are not authorised to edit this group')
                    result['error'] = "unauthorised"
            else:
                flash('This group does not exist')
                result['error'] = "not found"
        else:
            group = Group(name=form.edit_group_name.data,
                          creator=g.user,
                          members=group_members)
            db.session.add(group)
            db.session.commit()
            result['savedsuccess'] = True
            flash('New Group saved as ' + group.name)
        return json.dumps(result)
    else:
        form.errors['savedsuccess'] = False
        return json.dumps(form.errors)
Exemplo n.º 12
0
def select_question(id=0):
    result = {}
    result['savedsuccess'] = False
    question = Question.query.get(id)
    if question:
        result['selected_class'] = g.user.toggle_select_question(question)
        result['savedsuccess'] = True
        return json.dumps(result)
    else:
        flash('This question does not exist')
        return redirect(url_for('.questions'))
Exemplo n.º 13
0
def select_question(id=0):
    result = {}
    result['savedsuccess'] = False
    question = Question.query.get(id)
    if question:
        result['selected_class'] = g.user.toggle_select_question(question)
        result['savedsuccess'] = True
        return json.dumps(result)
    else:
        flash('This question does not exist')
        return redirect(url_for('.questions'))
Exemplo n.º 14
0
def group_save():
    form = forms.EditGroup()
    form.edit_group_members.choices = [(i, i) for i in
                                       form.edit_group_members.data]  #DJG - could put some actual validation here

    if form.validate():
        result = {}
        result['savedsuccess'] = False
        id = int(form.edit_group_id.data)
        #import pdb; pdb.set_trace()
        group_members = [User.user_by_email(e) for e in form.edit_group_members.data]
        if id > 0:
            group = Group.query.get(id)
            if group:
                if group.creator == g.user:
                    group.name = form.edit_group_name.data
                    group.members = group_members
                    db.session.add(group)
                    db.session.commit()
                    result['savedsuccess'] = True
                    flash('Group saved as ' + group.name)
                else:
                    flash('You are not authorised to edit this group')
                    result['error'] = "unauthorised"
            else:
                flash('This group does not exist')
                result['error'] = "not found"
        else:
            group = Group(
                name=form.edit_group_name.data,
                creator=g.user,
                members=group_members
            )
            db.session.add(group)
            db.session.commit()
            result['savedsuccess'] = True
            flash('New Group saved as ' + group.name)
        return json.dumps(result)
    else:
        form.errors['savedsuccess'] = False
        return json.dumps(form.errors)
Exemplo n.º 15
0
def scheme_save():
    form = forms.EditScheme()
    form.edit_scheme_objectives.choices = [(i, i) for i in
                                           form.edit_scheme_objectives.data]  #DJG - could put some actual validation here

    if form.validate():
        result = {}
        result['savedsuccess'] = False
        id = int(form.edit_scheme_id.data)
        #import pdb; pdb.set_trace()
        scheme_objectives = [Objective.query.filter_by(name=o).one() for o in form.edit_scheme_objectives.data]
        if id > 0:
            scheme = SchemeOfWork.query.get(id)
            if scheme:
                if scheme.creator == g.user:
                    scheme.name = form.edit_scheme_name.data
                    scheme.objectives = scheme_objectives
                    db.session.add(scheme)
                    db.session.commit()
                    result['savedsuccess'] = True
                    flash('Scheme saved as ' + scheme.name)
                else:
                    flash('You are not authorised to edit this scheme of work')
                    result['error'] = "unauthorised"
            else:
                flash('This scheme of work does not exist')
                result['error'] = "not found"
        else:
            scheme = SchemeOfWork(
                name=form.edit_scheme_name.data,
                creator=g.user,
                objectives=scheme_objectives
            )
            db.session.add(scheme)
            db.session.commit()
            result['savedsuccess'] = True
            flash('New scheme of work saved as ' + scheme.name)
        return json.dumps(result)
    else:
        form.errors['savedsuccess'] = False
        return json.dumps(form.errors)
Exemplo n.º 16
0
def index():
    title = "CourseMe"
    if g.user.is_authenticated():
        modules = g.user.visible_modules().all()
    else:
        modules = Module.LiveModules().all()

    catalogue = [mod.as_dict() for mod in modules]
    # DJG - confused over best way to do this http://stackoverflow.com/questions/1958219/convert-sqlalchemy-row-object-to-python-dict
    return render_template('index.html',
                           title=title,
                           modules=modules,
                           # DJG - temporary; unless there is a way to pass datatables a list of objects
                           catalogue=json.dumps(catalogue))
Exemplo n.º 17
0
def questions_print():
    # return app.send_static_file('print_questions.html')
    title = "CourseMe - Questions"
    if g.user.is_authenticated():
        questions = g.user.questions_selected
        catalogue = [question.as_dict(g.user) for question in questions]  #DJG - confused over best way to do this http://stackoverflow.com/questions/1958219/convert-sqlalchemy-row-object-to-python-dict
    else:
        questions =[]
        catalogue = []
    return render_template('questions_print.html',
                           title=title,
                           questions=questions,
                           catalogue=json.dumps(catalogue)
    )
Exemplo n.º 18
0
def delete_module(id):
    result = {'savedsuccess': False}
    module = Module.query.get(id)

    if module:
        if module.author == g.user:
            module.delete()
            result['savedsuccess'] = True
        else:
            flash("You are not authorised to delete this " + module.material_type)
    else:
        flash('No Module identified with id ' + id)

    return json.dumps(result)
Exemplo n.º 19
0
def index():
    title = "CourseMe"
    if g.user.is_authenticated():
        modules = g.user.visible_modules().all()
    else:
        modules = Module.LiveModules().all()

    catalogue = [mod.as_dict() for mod in modules]
    # DJG - confused over best way to do this http://stackoverflow.com/questions/1958219/convert-sqlalchemy-row-object-to-python-dict
    return render_template(
        'index.html',
        title=title,
        modules=modules,
        # DJG - temporary; unless there is a way to pass datatables a list of objects
        catalogue=json.dumps(catalogue))
Exemplo n.º 20
0
def delete_module(id):
    result = {'savedsuccess': False}
    module = Module.query.get(id)

    if module:
        if module.author == g.user:
            module.delete()
            result['savedsuccess'] = True
        else:
            flash("You are not authorised to delete this " +
                  module.material_type)
    else:
        flash('No Module identified with id ' + id)

    return json.dumps(result)
Exemplo n.º 21
0
def questions_print():
    # return app.send_static_file('print_questions.html')
    title = "CourseMe - Questions"
    if g.user.is_authenticated():
        questions = g.user.questions_selected
        catalogue = [
            question.as_dict(g.user) for question in questions
        ]  #DJG - confused over best way to do this http://stackoverflow.com/questions/1958219/convert-sqlalchemy-row-object-to-python-dict
    else:
        questions = []
        catalogue = []
    return render_template('questions_print.html',
                           title=title,
                           questions=questions,
                           catalogue=json.dumps(catalogue))
Exemplo n.º 22
0
def scheme_delete(id):
    result = {}
    result['savedsuccess'] = False
    scheme = SchemeOfWork.query.get(id)
    if scheme:
        if scheme.creator == g.user:
            db.session.delete(scheme)
            db.session.commit()
            result['savedsuccess'] = True
        else:
            flash('You are not authorised to delete this scheme of work')
            result['error'] = "unauthorised"
    else:
        flash('This scheme of work does not exist')
        result['error'] = "not found"
    return json.dumps(result)
Exemplo n.º 23
0
def scheme_delete(id):
    result = {}
    result['savedsuccess'] = False
    scheme = SchemeOfWork.query.get(id)
    if scheme:
        if scheme.creator == g.user:
            db.session.delete(scheme)
            db.session.commit()
            result['savedsuccess'] = True
        else:
            flash('You are not authorised to delete this scheme of work')
            result['error'] = "unauthorised"
    else:
        flash('This scheme of work does not exist')
        result['error'] = "not found"
    return json.dumps(result)
Exemplo n.º 24
0
def delete_question(id=0):
    result = {}
    result['savedsuccess'] = False
    question = Question.query.get(id)
    if question:
        if question.author == g.user:
            db.session.delete(question)
            db.session.commit()
            result['savedsuccess'] = True
            return json.dumps(result)
        else:
            flash('You are not authorised to delete this question')
            return redirect(url_for('.questions'))
    else:
        flash('This question does not exist')
        return redirect(url_for('.questions'))
Exemplo n.º 25
0
def questions():
    title = "CourseMe - Questions"
    if g.user.is_authenticated():
        questions = g.user.visible_questions().all()
        catalogue = [question.as_dict(g.user) for question in
                     questions]  #DJG - confused over best way to do this http://stackoverflow.com/questions/1958219/convert-sqlalchemy-row-object-to-python-dict
    else:
        questions = Question.query.all()
        catalogue = [question.as_dict() for question in
                     questions]  #DJG - confused over best way to do this http://stackoverflow.com/questions/1958219/convert-sqlalchemy-row-object-to-python-dict

    return render_template('questions.html',
                           title=title,
                           questions=questions,
                           catalogue=json.dumps(catalogue)
    )
Exemplo n.º 26
0
def delete_question(id=0):
    result = {}
    result['savedsuccess'] = False
    question = Question.query.get(id)
    if question:
        if question.author == g.user:
            db.session.delete(question)
            db.session.commit()
            result['savedsuccess'] = True
            return json.dumps(result)
        else:
            flash('You are not authorised to delete this question')
            return redirect(url_for('.questions'))
    else:
        flash('This question does not exist')
        return redirect(url_for('.questions'))
Exemplo n.º 27
0
def group_delete(id):
    result = {}
    result['savedsuccess'] = False
    group = Group.query.get(id)
    if group:
        if group.creator == g.user:
            db.session.delete(group)
            db.session.commit()
            result['savedsuccess'] = True
        else:
            flash('You are not authorised to delete this group')
            result['error'] = "unauthorised"
    else:
        flash('This group does not exist')
        result['error'] = "not found"
    return json.dumps(result)
Exemplo n.º 28
0
def group_delete(id):
    result = {}
    result['savedsuccess'] = False
    group = Group.query.get(id)
    if group:
        if group.creator == g.user:
            db.session.delete(group)
            db.session.commit()
            result['savedsuccess'] = True
        else:
            flash('You are not authorised to delete this group')
            result['error'] = "unauthorised"
    else:
        flash('This group does not exist')
        result['error'] = "not found"
    return json.dumps(result)
Exemplo n.º 29
0
def questions():
    title = "CourseMe - Questions"
    if g.user.is_authenticated():
        questions = g.user.visible_questions().all()
        catalogue = [
            question.as_dict(g.user) for question in questions
        ]  #DJG - confused over best way to do this http://stackoverflow.com/questions/1958219/convert-sqlalchemy-row-object-to-python-dict
    else:
        questions = Question.query.all()
        catalogue = [
            question.as_dict() for question in questions
        ]  #DJG - confused over best way to do this http://stackoverflow.com/questions/1958219/convert-sqlalchemy-row-object-to-python-dict

    return render_template('questions.html',
                           title=title,
                           questions=questions,
                           catalogue=json.dumps(catalogue))
Exemplo n.º 30
0
def course_enroll(course_id):
    result = {}
    result['savedsuccess'] = False
    course = Module.query.get(course_id)

    if course:
        if course.material_type == "Course":
            usermodule = UserModule.FindOrCreate(g.user.id, course.id)
            usermodule.enrolled = not usermodule.enrolled
            db.session.commit()
            result['savedsuccess'] = True
            result['enrolled'] = usermodule.enrolled
        else:
            flash("Cannot enroll to " + course.material_type)
    else:
        flash('No Course identified with id ' + course_id)

    return json.dumps(result)
Exemplo n.º 31
0
def course_enroll(course_id):
    result = {}
    result['savedsuccess'] = False
    course = Module.query.get(course_id)

    if course:
        if course.material_type == "Course":
            usermodule = UserModule.FindOrCreate(g.user.id, course.id)
            usermodule.enrolled = not usermodule.enrolled
            db.session.commit()
            result['savedsuccess'] = True
            result['enrolled'] = usermodule.enrolled
        else:
            flash("Cannot enroll to " + course.material_type)
    else:
        flash('No Course identified with id ' + course_id)

    return json.dumps(result)
Exemplo n.º 32
0
def add_module_to_course(module_id, course_id):
    result = {'savedsuccess': False}
    if not g.user.subject:
        flash("You need to select what subject you are interested in")
        return redirect(url_for('.index'))

    if course_id == 0:
        course = Module.CreateModule(name="New Course",
                                     author=g.user,
                                     subject=g.user.subject,
                                     material_type="Course")
    else:
        course = Module.query.get(course_id)
    module = Module.query.get(module_id)
    #import pdb; pdb.set_trace()        #DJG - remove
    if module:
        if module.material_type == "Course":
            flash('You cannot embed a course within another course')
        else:
            if course:
                if course.author_id != g.user.id:
                    flash('You are not authorised to edit this course')
                elif course.material_type != "Course":
                    flash('You cannot add modules to a ' +
                          course.material_type)
                elif course.subject_id != module.subject_id:
                    flash('You cannot add a ' + module.subject.name +
                          ' module to a ' + course.subject.name + ' course')
                else:
                    course_modules = course.modules.all()
                    course_modules.append(module)
                    course.modules = course_modules
                    course.last_updated = datetime.utcnow()
                    db.session.commit()
                    result['savedsuccess'] = True
            else:
                flash('No Course identified with id ' + course_id)
    else:
        flash('No Module identified with id ' + module_id)

    return json.dumps(result)
Exemplo n.º 33
0
def add_module_to_course(module_id, course_id):
    result = {'savedsuccess': False}
    if not g.user.subject:
        flash("You need to select what subject you are interested in")
        return redirect(url_for('.index'))

    if course_id == 0:
        course = Module.CreateModule(
            name="New Course",
            author=g.user,
            subject=g.user.subject,
            material_type="Course")
    else:
        course = Module.query.get(course_id)
    module = Module.query.get(module_id)
    #import pdb; pdb.set_trace()        #DJG - remove
    if module:
        if module.material_type == "Course":
            flash('You cannot embed a course within another course')
        else:
            if course:
                if course.author_id != g.user.id:
                    flash('You are not authorised to edit this course')
                elif course.material_type != "Course":
                    flash('You cannot add modules to a ' + course.material_type)
                elif course.subject_id != module.subject_id:
                    flash('You cannot add a ' + module.subject.name + ' module to a ' + course.subject.name + ' course')
                else:
                    course_modules = course.modules.all()
                    course_modules.append(module)
                    course.modules = course_modules
                    course.last_updated = datetime.utcnow()
                    db.session.commit()
                    result['savedsuccess'] = True
            else:
                flash('No Course identified with id ' + course_id)
    else:
        flash('No Module identified with id ' + module_id)

    return json.dumps(result)
Exemplo n.º 34
0
def restrict_modules_viewed(user_id, institution_id):
    result = {}
    result['savedsuccess'] = False
    if user_id == g.user.id:
        if institution_id == 0:
            g.user.view_institution_only_id = 0
            db.session.add(g.user)
            db.session.commit()
            result['savedsuccess'] = True
        else:
            institution = Institution.query.get(institution_id)
            if institution:
                g.user.view_institution_only = institution
                db.session.add(g.user)
                db.session.commit()
                result['savedsuccess'] = True
            else:
                flash('Institution not found with id ' + institution_id)
                result["institution_error"] = True
    else:
        flash('You are not logged in as this user')
        result["user_error"] = True

    return json.dumps(result)
Exemplo n.º 35
0
def restrict_modules_viewed(user_id, institution_id):
    result = {}
    result['savedsuccess'] = False
    if user_id == g.user.id:
        if institution_id == 0:
            g.user.view_institution_only_id = 0
            db.session.add(g.user)
            db.session.commit()
            result['savedsuccess'] = True
        else:
            institution = Institution.query.get(institution_id)
            if institution:
                g.user.view_institution_only = institution
                db.session.add(g.user)
                db.session.commit()
                result['savedsuccess'] = True
            else:
                flash('Institution not found with id ' + institution_id)
                result["institution_error"] = True
    else:
        flash('You are not logged in as this user')
        result["user_error"] = True

    return json.dumps(result)
Exemplo n.º 36
0
def objective_get():
    objective = Objective.query.get(request.args.get("objective_id"))
    return json.dumps(objective.as_dict(), sort_keys=True)
Exemplo n.º 37
0
def objective_assess(profile_id, objective_id, service_layer=_service_layer):
    return json.dumps(service_layer.objectives.assess(objective_id, profile_id, g.user.id, g.user))
Exemplo n.º 38
0
def editmodule(id=0, service_layer=_service_layer):
    title = 'CourseMe - Edit Module'
    moduleform = forms.EditModule()
    objectiveform = forms.EditObjective(topic_choices=Topic.TopicChoices(g.user))
    module_objectives = []
    module = None
    if not g.user.subject:
        flash("You need to select what subject you are interested in")
        return redirect(url_for('.index'))

    form_header = "Create new " + g.user.subject.name + " module:"
    if id > 0:
        module = Module.query.get(id)
        if module:
            if module.author_id != g.user.id:
                flash('You are not authorised to edit this module')
                return redirect(url_for('.module', id=id))
            elif request.method == 'GET':
                g.user.subject = module.subject
                db.session.add(g.user)
                db.session.commit()
            return redirect(url_for('.index'))
        else:
            flash('There is no such module to edit')

    if request.method == 'GET':
        objectives = service_layer.objectives.objectives_for_selection(g.user, g.user.subject_id).all()
        objectives.sort(key=operator.methodcaller(
            "score"))  #DJG - isn't there a way of doing this within the order_by of the query

        return render_template('editmodule.html',
                               title=title,
                               form_header=form_header,
                               edit_id=id,
                               objectives=objectives,
                               module=module,
                               module_objectives=module_objectives,
                               edit_module_form=moduleform,
                               objectiveform=objectiveform)

    if request.method == 'POST':
        #import pdb; pdb.set_trace()
        moduleform.module_objectives.choices = [(i, i) for i in moduleform.module_objectives.data]
        #Both material upload types are required in the moduleform definition so need to remove the redundant field now to prevent validation errors

        #if material_source == 'upload':           #DJG - need a way to define the global list of sources so value means the same thing as database definitions 
        #    del moduleform.material_youtube
        #elif material_source == 'youtube':
        #    del moduleform.material_upload              
        #if moduleform.material_type.data == 'course':
        #    del moduleform.material_upload
        #    del moduleform.material_youtube
        #import pdb; pdb.set_trace()
        if moduleform.validate():
            objectives = []
            course_modules = []
            result = {}
            result['savedsuccess'] = False
            proceed = False
            material_type = module.material_type if id > 0 else moduleform.material_type.data
            material_source = ""
            material_path = ""
            if material_type != "Course":
                select_list = moduleform.module_objectives.data
                objectives = service_layer.objectives.available_to(g.user, select_list)
                undefined_objectives = list(set(select_list) - set(obj.name for obj in
                                                                   objectives))
                                # DJG - Need to trap undefined objectives and return savedsucess as json if failed

                if undefined_objectives:  #DJG - code repeat of above, how to avoid this
                    is_are = 'is not already defined as an objetive' if len(
                        undefined_objectives) == 1 else 'are not already defined as objetives'
                    result['objectives'] = ["'" + "', '".join(undefined_objectives) + "' " + is_are]
                    return json.dumps(result)

                material_source = moduleform.material_source.data
                if module:
                    if module.material_source == material_source and material_source == "upload":
                        material_path = module.material_path

                if material_source == 'upload' and 'material' in request.files:  #DJG - Does flask-uploads automatically check against the allowed extention types and make the filename safe? Believe so.
                    material_path = lectures.save(request.files[
                        'material'])  #This saves the file and returns its name (including the folder)

                elif material_source == 'youtube' and moduleform.material_youtube.data:
                    material_path = moduleform.material_youtube.data
                    if not "?rel=0" in material_path:
                        material_path = material_path + "?rel=0"  #DJG - Add this text string on to stop youtube videos showing followon videos directly in the iframe


                if material_path:
                    proceed = True
                    result['material'] = [material_path]
                else:
                    result['material'] = ["No content provided"]
            else:  #DJG - method below can be improved now?!...
                unicode_list = request.form[
                    "course_modules"]  #DJG - the data sent by the ajax request has the list converted into a unicode text string with commas
                material_type = module.material_type
                python_list = filter(None, unicode_list.split(','))
                if python_list: course_modules = [Module.query.get(mod_id) for mod_id in
                                                  python_list]  #DJG - need some validation here to make sure modules exist and are not themselves courses etc.
                proceed = True

            if proceed:
                if module:
                    module.name = moduleform.name.data
                    module.description = moduleform.description.data
                    module.notes = moduleform.notes.data
                    module.last_updated = datetime.utcnow()
                    module.material_source = material_source
                    module.material_path = material_path
                    module.objectives = objectives
                    module.modules = []  #DJG - need this to make the order of modules editable - or else need an association object in sqlalchamy to capture order as extra data of the many to many relationship
                    db.session.commit()
                    module.modules = course_modules
                    module.subtitles = moduleform.subtitles.data
                    module.easy_language = moduleform.easy_language.data
                    module.extension = moduleform.extension.data
                    module.for_teachers = moduleform.for_teachers.data
                    db.session.add(module)
                    db.session.commit()
                else:
                    module = Module.CreateModule(
                        name=moduleform.name.data,
                        description=moduleform.description.data,
                        notes=moduleform.notes.data,
                        author=g.user,
                        material_type=material_type,
                        material_source=material_source,
                        material_path=material_path,
                        subject=g.user.subject,
                        objectives=objectives,
                        subtitles=moduleform.subtitles.data,
                        easy_language=moduleform.easy_language.data,
                        extension=moduleform.extension.data,
                        for_teachers=moduleform.for_teachers.data
                    )

                result['savedsuccess'] = True
                result['module_id'] = module.id
                flash(material_type + " saved as " + module.name)

            return json.dumps(result)

        else:
            moduleform.errors['savedsuccess'] = False
            return json.dumps(moduleform.errors)
Exemplo n.º 39
0
def _ajax_success(status_code=200, **data):
    """Successfuly complete an AJAX request"""
    result = {'success': True, 'data': data}
    return json.dumps(result), status_code
Exemplo n.º 40
0
def editmodule(id=0):
    title = 'CourseMe - Edit Module'
    moduleform = forms.EditModule(
    )  #DJG - need the arguement because using validate not validate_on_submit?
    #import pdb; pdb.set_trace()            #DJG - remove
    objectiveform = forms.EditObjective(
        topic_choices=Topic.TopicChoices(g.user))
    module_objectives = []
    module = None
    if not g.user.subject:
        flash("You need to select what subject you are interested in")
        return redirect(url_for('.index'))

    form_header = "Create new " + g.user.subject.name + " module:"
    if id > 0:
        module = Module.query.get(id)
        if module:
            if module.author_id != g.user.id:
                flash('You are not authorised to edit this module')
                return redirect(url_for('.module', id=id))
            elif request.method == 'GET':
                g.user.subject = module.subject
                db.session.add(g.user)
                db.session.commit()
            return redirect(url_for('.index'))
        else:
            flash('There is no such module to edit')

    if request.method == 'GET':
        objectives = g.user.visible_objectives().all()
        objectives.sort(
            key=operator.methodcaller("score")
        )  #DJG - isn't there a way of doing this within the order_by of the query

        return render_template('editmodule.html',
                               title=title,
                               form_header=form_header,
                               edit_id=id,
                               objectives=objectives,
                               module=module,
                               module_objectives=module_objectives,
                               edit_module_form=moduleform,
                               objectiveform=objectiveform)

    if request.method == 'POST':
        #import pdb; pdb.set_trace()
        moduleform.module_objectives.choices = [
            (i, i) for i in moduleform.module_objectives.data
        ]
        #Both material upload types are required in the moduleform definition so need to remove the redundant field now to prevent validation errors

        #if material_source == 'upload':           #DJG - need a way to define the global list of sources so value means the same thing as database definitions
        #    del moduleform.material_youtube
        #elif material_source == 'youtube':
        #    del moduleform.material_upload
        #if moduleform.material_type.data == 'course':
        #    del moduleform.material_upload
        #    del moduleform.material_youtube
        #import pdb; pdb.set_trace()
        if moduleform.validate():
            objectives = []
            course_modules = []
            result = {}
            result['savedsuccess'] = False
            proceed = False
            material_type = module.material_type if id > 0 else moduleform.material_type.data
            material_source = ""
            material_path = ""
            if material_type != "Course":
                select_list = moduleform.module_objectives.data
                if select_list:
                    objectives = g.user.visible_objectives(
                    ).filter(Objective.name.in_(select_list)).all(
                    )  #DJG - avoiding using the in_ operation when the list is empty as this is an inefficiency

                undefined_objectives = list(
                    set(select_list) - set(obj.name for obj in objectives)
                )  #DJG - Need to trap undefined objectives and return savedsucess as json if failed

                if undefined_objectives:  #DJG - code repeat of above, how to avoid this
                    is_are = 'is not already defined as an objetive' if len(
                        undefined_objectives
                    ) == 1 else 'are not already defined as objetives'
                    result['objectives'] = [
                        "'" + "', '".join(undefined_objectives) + "' " + is_are
                    ]
                    return json.dumps(result)

                material_source = moduleform.material_source.data
                if module:
                    if module.material_source == material_source and material_source == "upload":
                        material_path = module.material_path

                if material_source == 'upload' and 'material' in request.files:  #DJG - Does flask-uploads automatically check against the allowed extention types and make the filename safe? Believe so.
                    material_path = lectures.save(
                        request.files['material']
                    )  #This saves the file and returns its name (including the folder)

                elif material_source == 'youtube' and moduleform.material_youtube.data:
                    material_path = moduleform.material_youtube.data
                    if not "?rel=0" in material_path:
                        material_path = material_path + "?rel=0"  #DJG - Add this text string on to stop youtube videos showing followon videos directly in the iframe

                if material_path:
                    proceed = True
                    result['material'] = [material_path]
                else:
                    result['material'] = ["No content provided"]
            else:  #DJG - method below can be improved now?!...
                unicode_list = request.form[
                    "course_modules"]  #DJG - the data sent by the ajax request has the list converted into a unicode text string with commas
                material_type = module.material_type
                python_list = filter(None, unicode_list.split(','))
                if python_list:
                    course_modules = [
                        Module.query.get(mod_id) for mod_id in python_list
                    ]  #DJG - need some validation here to make sure modules exist and are not themselves courses etc.
                proceed = True

            if proceed:
                if module:
                    module.name = moduleform.name.data
                    module.description = moduleform.description.data
                    module.notes = moduleform.notes.data
                    module.last_updated = datetime.utcnow()
                    module.material_source = material_source
                    module.material_path = material_path
                    module.objectives = objectives
                    module.modules = [
                    ]  #DJG - need this to make the order of modules editable - or else need an association object in sqlalchamy to capture order as extra data of the many to many relationship
                    db.session.commit()
                    module.modules = course_modules
                    module.subtitles = moduleform.subtitles.data
                    module.easy_language = moduleform.easy_language.data
                    module.extension = moduleform.extension.data
                    module.for_teachers = moduleform.for_teachers.data
                    db.session.add(module)
                    db.session.commit()
                else:
                    module = Module.CreateModule(
                        name=moduleform.name.data,
                        description=moduleform.description.data,
                        notes=moduleform.notes.data,
                        author=g.user,
                        material_type=material_type,
                        material_source=material_source,
                        material_path=material_path,
                        subject=g.user.subject,
                        objectives=objectives,
                        subtitles=moduleform.subtitles.data,
                        easy_language=moduleform.easy_language.data,
                        extension=moduleform.extension.data,
                        for_teachers=moduleform.for_teachers.data)

                result['savedsuccess'] = True
                result['module_id'] = module.id
                flash(material_type + " saved as " + module.name)

            return json.dumps(result)

        else:
            moduleform.errors['savedsuccess'] = False
            return json.dumps(moduleform.errors)
Exemplo n.º 41
0
 def test_datetime_serialisation(self):
     dt = datetime(2015, 1, 11, 10, 30, 13, 23)
     obj = {"time": dt}
     result = json.loads(json.dumps(obj))
     self.assertEqual(result["time"], dt.isoformat())
Exemplo n.º 42
0
def _ajax_failure(status_code=400, **errors):
    """Complete an AJAX request with listed errors"""
    assert status_code >= 400, "Error status code must be >= 400"
    result = {'success': False, 'errors': errors}
    return json.dumps(result), status_code
Exemplo n.º 43
0
def objective_get():
    objective = Objective.query.get(request.args.get("objective_id"))
    return json.dumps(objective.as_dict(), sort_keys=True)
Exemplo n.º 44
0
def _ajax_failure(status_code=400, **errors):
    """Complete an AJAX request with listed errors"""
    assert status_code >= 400, "Error status code must be >= 400"
    result = {'success': False, 'errors': errors}
    return json.dumps(result), status_code
Exemplo n.º 45
0
def edit_question(id=0, service_layer=_service_layer):
    title = "CourseMe - Questions"
    form = forms.EditQuestion()
    objectiveform = forms.EditObjective(topic_choices=Topic.TopicChoices(g.user))
    question_objectives = []
    question = None
    #import pdb; pdb.set_trace()
    form_header = "Create new question:"
    if id > 0:
        question = Question.query.get(id)
        if question:
            if question.author_id != g.user.id:
                flash('You are not authorised to edit this question')
                return redirect(url_for('.questions'))
            elif request.method == 'GET':
                form_header = "Edit question:"
                g.user.subject_id = question.subject_id
                db.session.add(g.user)
                db.session.commit()
                form = forms.EditQuestion(
                    question=question.question,
                    answer=question.answer,
                    extension=question.extension
                )
                question_objectives = question.objectives
        else:
            flash('There is no such question to edit')
            return redirect(url_for('.questions'))

    if request.method == 'GET':
        objectives = service_layer.objectives.objectives_for_selection(g.user, g.user.subject_id).all()
        objectives.sort(key=operator.methodcaller(
            "score"))  #DJG - isn't there a way of doing this within the order_by of the query

        return render_template('edit_question.html',
                               title=title,
                               form_header=form_header,
                               form=form,
                               question_objectives=question_objectives,
                               objectiveform=objectiveform,
                               objectives=objectives,
                               edit_id=id)

    if request.method == 'POST':
        #import pdb; pdb.set_trace()
        form.question_objectives.choices = [(i, i) for i in form.question_objectives.data]

        if form.validate():
            objectives = []
            result = {}
            result['savedsuccess'] = False
            proceed = False

            select_list = form.question_objectives.data
            if select_list: objectives = service_layer.objectives.available_to(g.user, select_list)
            undefined_objectives = list(set(select_list) - set(obj.name for obj in
                                                               objectives))  #DJG - Need to trap undefined objectives and return savedsucess as json if failed

            if undefined_objectives:  #DJG - code repeat of above, how to avoid this
                is_are = 'is not already defined as an objetive' if len(
                    undefined_objectives) == 1 else 'are not already defined as objetives'
                result['objectives'] = ["'" + "', '".join(undefined_objectives) + "' " + is_are]
                return json.dumps(result)
            else:
                proceed = True

            if question and g.user.subject_id != question.subject_id:
                result['subject'] = [question.subject_id]
                proceed = False

            if proceed:
                if question:
                    question.question = form.question.data
                    question.answer = form.answer.data
                    question.objectives = objectives
                    question.extension = form.extension.data
                    question.visually_impaired = form.visually_impaired.data
                    question.last_updated = datetime.utcnow()
                    db.session.add(question)
                    db.session.commit()
                else:
                    question = Question.CreateQuestion(
                        question=form.question.data,
                        answer=form.answer.data,
                        subject=g.user.subject,
                        author=g.user,
                        objectives=objectives,
                        extension=form.extension.data,
                        visually_impaired=form.visually_impaired.data
                    )

                result['savedsuccess'] = True
                result['question_id'] = question.id
                flash("Question saved")

            return redirect(url_for('.questions'))

        else:
            form.errors['savedsuccess'] = False
            return json.dumps(form.errors)
Exemplo n.º 46
0
def selected_questions():
    questions = g.user.questions_selected
    catalogue = [question.as_dict() for question in questions]  #DJG - confused over best way to do this http://stackoverflow.com/questions/1958219/convert-sqlalchemy-row-object-to-python-dict
    return json.dumps(catalogue)
Exemplo n.º 47
0
def selected_questions():
    questions = g.user.questions_selected
    catalogue = [
        question.as_dict() for question in questions
    ]  #DJG - confused over best way to do this http://stackoverflow.com/questions/1958219/convert-sqlalchemy-row-object-to-python-dict
    return json.dumps(catalogue)
Exemplo n.º 48
0
def edit_question(id=0):
    title = "CourseMe - Questions"
    form = forms.EditQuestion()
    objectiveform = forms.EditObjective(
        topic_choices=Topic.TopicChoices(g.user))
    question_objectives = []
    question = None
    #import pdb; pdb.set_trace()
    form_header = "Create new question:"
    if id > 0:
        question = Question.query.get(id)
        if question:
            if question.author_id != g.user.id:
                flash('You are not authorised to edit this question')
                return redirect(url_for('.questions'))
            elif request.method == 'GET':
                form_header = "Edit question:"
                g.user.subject_id = question.subject_id
                db.session.add(g.user)
                db.session.commit()
                form = forms.EditQuestion(question=question.question,
                                          answer=question.answer,
                                          extension=question.extension)
                question_objectives = question.objectives
        else:
            flash('There is no such question to edit')
            return redirect(url_for('.questions'))

    if request.method == 'GET':
        objectives = g.user.visible_objectives().all()
        objectives.sort(
            key=operator.methodcaller("score")
        )  #DJG - isn't there a way of doing this within the order_by of the query

        return render_template('edit_question.html',
                               title=title,
                               form_header=form_header,
                               form=form,
                               question_objectives=question_objectives,
                               objectiveform=objectiveform,
                               objectives=objectives,
                               edit_id=id)

    if request.method == 'POST':
        #import pdb; pdb.set_trace()
        form.question_objectives.choices = [
            (i, i) for i in form.question_objectives.data
        ]

        if form.validate():
            objectives = []
            result = {}
            result['savedsuccess'] = False
            proceed = False

            select_list = form.question_objectives.data
            if select_list:
                objectives = g.user.visible_objectives(
                ).filter(Objective.name.in_(select_list)).all(
                )  #DJG - avoiding using the in_ operation when the list is empty as this is an inefficiency
            undefined_objectives = list(
                set(select_list) - set(obj.name for obj in objectives)
            )  #DJG - Need to trap undefined objectives and return savedsucess as json if failed

            if undefined_objectives:  #DJG - code repeat of above, how to avoid this
                is_are = 'is not already defined as an objetive' if len(
                    undefined_objectives
                ) == 1 else 'are not already defined as objetives'
                result['objectives'] = [
                    "'" + "', '".join(undefined_objectives) + "' " + is_are
                ]
                return json.dumps(result)
            else:
                proceed = True

            if question and g.user.subject_id != question.subject_id:
                result['subject'] = [question.subject_id]
                proceed = False

            if proceed:
                if question:
                    question.question = form.question.data
                    question.answer = form.answer.data
                    question.objectives = objectives
                    question.extension = form.extension.data
                    question.visually_impaired = form.visually_impaired.data
                    question.last_updated = datetime.utcnow()
                    db.session.add(question)
                    db.session.commit()
                else:
                    question = Question.CreateQuestion(
                        question=form.question.data,
                        answer=form.answer.data,
                        subject=g.user.subject,
                        author=g.user,
                        objectives=objectives,
                        extension=form.extension.data,
                        visually_impaired=form.visually_impaired.data)

                result['savedsuccess'] = True
                result['question_id'] = question.id
                flash("Question saved")

            return redirect(url_for('.questions'))

        else:
            form.errors['savedsuccess'] = False
            return json.dumps(form.errors)
Exemplo n.º 49
0
 def test_datetime_serialisation(self):
     dt = datetime(2015, 1, 11, 10, 30, 13, 23)
     obj = {"time": dt}
     result = json.loads(json.dumps(obj))
     self.assertEqual(result["time"], dt.isoformat())
Exemplo n.º 50
0
 def test_vanilla_round_trip(self):
     obj = {"number": 123}
     result = json.loads(json.dumps(obj))
     self.assertEqual(result, obj)
Exemplo n.º 51
0
def objective_assess(profile_id, objective_id, service_layer=_service_layer):
    return json.dumps(
        service_layer.objectives.assess(objective_id, profile_id, g.user.id,
                                        g.user))
Exemplo n.º 52
0
 def test_vanilla_round_trip(self):
     obj = {"number": 123}
     result = json.loads(json.dumps(obj))
     self.assertEqual(result, obj)
Exemplo n.º 53
0
def _ajax_success(status_code=200, **data):
    """Successfuly complete an AJAX request"""
    result = {'success': True, 'data': data}
    return json.dumps(result), status_code