コード例 #1
0
ファイル: app.py プロジェクト: abelmaths/flask_v1
def create_pupil_via_school():
    """
    Create pupil account given a valid entry code
    Add pupil to the class whilst creating their account
    """
    username = request.json['username']
    password = request.json['password']
    entry_code = request.json['entry_code']
    firstname = request.json['firstname']
    surname = request.json['surname']
    
    # Check username and password etc.
    USER = mongo.load_by_username(username)
    if USER.exists():
        USER = {}
        return_data = {'message':'Username already taken'}
        return json.dumps({'status':0, 'data':return_data})
    
    # Create user
    mongo.create_user(
        username=username,
        firstname=firstname,
        surname=surname,
        password=password,
        display_name=None,
        is_teacher=False,
        email=None,
        confirmed=False)
    USER = mongo.load_by_username(username)
    session['user'] = USER.save_to_session()

    # Add to class
    classroom = mongo.load_by_arbitrary({'entry_code':entry_code}, 'classrooms')
    mongo.pupil_joins_classroom(USER, classroom)

    # Create submission objects (should refactor all of this code a bit)
    pupil_id = USER.get_id()
    pupil_name = USER.get_display_name()
    for homework_id in classroom.get_homework_ids():
        homework_object = mongo.load_by_arbitrary({'_id':homework_id}, 'homeworks')
        mongo.create_submission(pupil_id, pupil_name, homework_object)

    return_data = {}
    return json.dumps({'status':1, 'data':return_data})
コード例 #2
0
ファイル: app.py プロジェクト: abelmaths/flask_v1
def pupil_join_classroom_confirm():
    """
    Pupil joins classroom (already logged in, entry code for classroom)
    """
    entry_code = request.json['entry_code']
    
    # Add to class
    USER_db = mongo.load_by_id(ObjectId(USER.get_id()), 'users') # Load proper pupil from DB
    classroom = mongo.load_by_arbitrary({'entry_code':entry_code}, 'classrooms')
    mongo.pupil_joins_classroom(USER_db, classroom)

    # Create submission objects (should refactor all of this code a bit)
    pupil_id = USER.get_id()
    pupil_name = USER.get_display_name()
    for homework_id in classroom.get_homework_ids():
        homework_object = mongo.load_by_arbitrary({'_id':homework_id}, 'homeworks')
        mongo.create_submission(pupil_id, pupil_name, homework_object)

    return_data = {}
    return json.dumps({'status':1, 'data':return_data})