Пример #1
0
def main():
    MongoDBConnection.initialize_connection('db', 27017)

    app = connexion.App(__name__, specification_dir='./swagger/')
    CORS(app.app)
    app.app.json_encoder = encoder.JSONEncoder
    app.add_api('swagger.yaml',
                arguments={'title': 'YoungRes data comsuption API'},
                pythonic_params=True)
    app.run(port=8080)
Пример #2
0
def main():

    #initilize connection with MongoDB
    MongoDBConnection.initialize_connection('db', 27017)

    # start the api server
    app = connexion.App(__name__, specification_dir='./swagger/')
    CORS(app.app)
    app.app.json_encoder = encoder.JSONEncoder
    app.add_api('swagger.yaml',
                arguments={'title': 'YoungRes gameplay API'},
                pythonic_params=True)
    app.run(port=8080)
Пример #3
0
def get_filter_values(table, column):

    if table == 'groups':
        values = MongoDBConnection.get_groups_collection().distinct(column)
    elif table == 'students':
        values = MongoDBConnection.get_students_collection().distinct(column)
    elif table == 'test':
        values = MongoDBConnection.get_tests_collection().distinct(column)
    elif table == 'saved_state':
        values = MongoDBConnection.get_saved_state_collection().distinct(
            column)
    elif table == 'games':
        values = MongoDBConnection.get_games_collection().distinct(column)
    elif table == 'chapters':
        values = MongoDBConnection.get_chapters_collection().distinct(column)
    elif table == 'events':
        values = MongoDBConnection.get_events_collection().distinct(column)
    elif table == 'decisions':
        values = MongoDBConnection.get_decisions_collection().distinct(column)

    try:
        values = list(map(int, values))
        return [min(values), max(values)]
    except:
        return values
Пример #4
0
def get_events(game_code, version, chapter_code) -> pymongo.cursor.Cursor:
    db = MongoDBConnection.get_events_collection()
    return db.find({
        'chapterCode': chapter_code,
        'gameCode': game_code,
        'version': version
    })
Пример #5
0
def get_student_and_group(student_code: str) -> dict:
    db = MongoDBConnection.get_students_collection()
    cursor = db.aggregate([{
        "$match": {
            "studentCode": student_code
        }
    }, {
        "$lookup": {
            "from": "groups",
            "localField": "groupCode",
            "foreignField": "groupCode",
            "as": "group"
        }
    }, {
        "$unwind": "$group"
    }, {
        "$project": {
            "_id": 0,
            "studentCode": 1,
            "sex": 1,
            "age": 1,
            "country": "$group.country",
            "city": "$group.city",
            "groupCode": "$group.groupCode"
        }
    }])
    return list(cursor)[0] if cursor is not None else None
Пример #6
0
def get_group_filter_type(field):

    return MongoDBConnection.get_filters_collection().find_one(
        {
            'table': 'groups',
            'field': field
        }, {'_id': 0})
Пример #7
0
def get_game_students(game_code: str, version: str) -> List[str]:
    db = MongoDBConnection.get_events_collection()
    events = [
        r['eventCode']
        for r in db.find({
            'gameCode': game_code,
            'version': version
        }, {'eventCode': 1})
    ]

    db = MongoDBConnection.get_decisions_collection()
    return db.find({
        'eventCode': {
            '$in': events
        }
    }, {
        'studentCode': 1
    }).distinct('studentCode')
Пример #8
0
def get_group_filter_values(group_ids: List[str], column: str) -> List[str]:
    db = MongoDBConnection.get_groups_collection()
    return db.find({
        'groupCode': {
            '$in': group_ids
        }
    }, {
        '_id': 0,
        column: 1
    }).distinct(column)
Пример #9
0
def post_saved_state_scene(student_code: str, game_code: str, version: str, variable_name: str, value: str):

    #docs = MongoDBConnection.get_saved_states_collection().find({'studentCode':str(student_code), 'gameCode':str(game_code), 'version':str(version)})
    savedStateVariables = MongoDBConnection.get_saved_states_collection().find_one({'studentCode':student_code, 'gameCode':game_code, 'version':version})
    if savedStateVariables is not None:
        savedStateVariables['variables'][variable_name] = value
        MongoDBConnection.get_saved_states_collection().find_one_and_replace({'studentCode':student_code, 'gameCode':game_code, 'version':version}, savedStateVariables)
    else:
        # make default saved state
        savedStateVariables = {
            'stateCode': student_code,
            'studentCode': student_code,
            'gameCode': game_code,
            'version': version,
            'variables': {
                variable_name: value
            }
        }
        MongoDBConnection.get_saved_states_collection().insert_one(savedStateVariables)
Пример #10
0
def get_student_filter_values(student_ids: List[str],
                              column: str) -> List[str]:
    db = MongoDBConnection.get_students_collection()
    return db.find({
        'studentCode': {
            '$in': student_ids
        }
    }, {
        '_id': 0,
        column: 1
    }).distinct(column)
Пример #11
0
def get_decisions(student_ids: List[str],
                  event_ids: List[str]) -> pymongo.cursor.Cursor:
    db = MongoDBConnection.get_decisions_collection()
    decisions = db.find({
        'studentCode': {
            '$in': student_ids
        },
        'eventCode': {
            '$in': event_ids
        }
    })
    return decisions
Пример #12
0
def get_chapter(game_code, version, chapter_code):

    gameChapter = MongoDBConnection.get_chapters_collection().find_one({
        'chapterCode':
        chapter_code,
        'gameCode':
        game_code,
        'version':
        version
    })

    return gameChapter
Пример #13
0
def get_filter_type_dict(table: str) -> Dict[str, str]:
    """
    Returns a dictionary where the keys are the different 'field' in the table
    and the values are the types of those fields
    """
    db = MongoDBConnection.get_filters_collection()

    f_dict = {}
    with db.find({'table': table}, {'field': 1, 'type': 1}) as cursor:
        for row in cursor:
            f_dict[row['field']] = row['type']

    return f_dict
Пример #14
0
def post_decision(student_code: str, event_code: str, decision: Decision):

    ids = MongoDBConnection.get_decisions_collection().find().distinct('decisionCode')

    finalDecision = {}
    for dec in decision._decision:
        finalDecision[dec._key] = dec._value

    _existing = MongoDBConnection.get_decisions_collection().find_one({'eventCode':event_code, 'studentCode':student_code})

    if _existing is not None:
        MongoDBConnection.get_decisions_collection().update_one({'eventCode':event_code, 'studentCode':student_code},{'$set': {'fields':finalDecision}})
    else:
        MongoDBConnection.get_decisions_collection().insert_one({'eventCode':event_code, 'studentCode':student_code,'fields':finalDecision})
Пример #15
0
def get_event_by_id(_id):

    event = MongoDBConnection.get_events_collection().find_one(
        {'eventCode': _id})

    return event
Пример #16
0
def get_saved_state(student_code: str, game_code: str, version: str, variable_name: str):
    savedStateVariables = MongoDBConnection.get_saved_states_collection().find_one({'studentCode':student_code, 'gameCode':game_code, 'version':version})
    if savedStateVariables is not None and variable_name in savedStateVariables['variables']:
        return savedStateVariables['variables'][variable_name]
    else:
        return None
Пример #17
0
def get_students_in_groups(group_ids: List[str]) -> pymongo.cursor.Cursor:
    db = MongoDBConnection.get_students_collection()
    return db.find({'groupCode': {'$in': group_ids}})
Пример #18
0
def student_exists(studentCode: str) -> bool:
    db = MongoDBConnection.get_students_collection()
    return db.find_one({'studentCode': studentCode}) is not None
Пример #19
0
def get_start_scene(game_code: str, version: str, chapter_code: str) -> Dict[str, str]:
    gameChapters = MongoDBConnection.get_chapters_collection().find_one({'chapterCode':chapter_code, 'gameCode':game_code,'version':version})
    if gameChapters is not None:
        return {'startScene': gameChapters['startScene'], 'startX': gameChapters['startX'], 'startY': gameChapters['startY']}
    else:
        return None
Пример #20
0
def get_games():

    games = MongoDBConnection.get_games_collection().find({})

    return games
Пример #21
0
def get_game_countries(game_code, version, students):
    db = MongoDBConnection.get_students_collection()
    groups = db.find({"studentCode": {"$in": students}}).distinct('groupCode')

    db = MongoDBConnection.get_groups_collection()
    return db.find({"groupCode": {"$in": groups}}).distinct('country')
Пример #22
0
def get_filter(id):
    return MongoDBConnection.get_filters_collection().find_one(
        {'filterCode': id})
Пример #23
0
def get_user_credentials(username):

    return MongoDBConnection.get_credentials_collection().find_one(
        {"username": username})
Пример #24
0
def get_filters(ids: List[str], table: str = None) -> pymongo.cursor.Cursor:
    query = {'filterCode': {'$in': ids}}
    if table is not None:
        query['table'] = table

    return MongoDBConnection.get_filters_collection().find(query, {'_id': 0})
Пример #25
0
def get_group(id):
    return MongoDBConnection.get_groups_collection().find_one(
        {'groupCode': id})
Пример #26
0
def get_groups(ids: List[str]) -> pymongo.cursor.Cursor:
    return MongoDBConnection.get_groups_collection().find(
        {'groupCode': {
            '$in': ids
        }})
Пример #27
0
def get_groups_pass_filter(query: dict) -> List[str]:
    db = MongoDBConnection.get_groups_collection()
    return db.find(query, {'groupCode': 1}).distinct('groupCode')
Пример #28
0
def get_student(student_code):
    student = MongoDBConnection.get_students_collection().find_one(
        {'studentCode': student_code})
    return student