Exemple #1
0
def delete_assignment(assignment_id):
    """
    Function remove assignment from assignment collection.
    :param assignment_id: id of assignment to be removed.
    :return: None
    """
    collection = db.get_collection("assignments")
    collection.remove({"id": assignment_id})
Exemple #2
0
def insert_message(message):
    """
    Function insert data to main_board collection in database.
    :param message: dictionary with message data
    :return: None
    """
    collection = db.get_collection("main_board")
    collection.insert_one(message)
Exemple #3
0
def get_all_assignments():
    """
    Function gets all the assignments from the assignments collection.
    :return:  list of assignments
    """
    collection = db.get_collection("assignments")
    assignments = collection.find({}, {"_id": 0})
    return list(assignments)
def get_last_uid():
    """
    Function returns the last uid from users collection
    :return: last uid
    """
    collection = db.get_collection("users")
    assignment_id = collection.find_one({}, {"uid": 1}, sort=[("uid", -1)])
    return assignment_id['uid']
def get_last_msg_id_from_room(participants):
    """
    Function get the last message id from chat_rooms collection document with specified participants.
    :param participants: list of users
    :return: last id
    """
    collection = db.get_collection("chat_rooms")
    room_info = collection.find_one({'participants': participants}, {"messages": 0, "_id": 0})
    last_msg_id = room_info['lastMsgId']
    return last_msg_id
Exemple #6
0
def insert_new_user(user_info):
    """
    Function insert user into users collection.
    :param user_info: dictionary with user data
    :return: None
    """
    collection = db.get_collection("users")
    user_info['state'] = {"online": False, "available": False}
    user_info['uid'] = get_last_uid() + 1
    collection.insert_one(user_info)
Exemple #7
0
def get_last_assignment_id():
    """
    Function gets the last id of assignment in the assignments collection.
    :return: id of last assignment
    """
    collection = db.get_collection("assignments")
    assignment_id = collection.find_one({}, {"id": 1}, sort=[("id", -1)])
    if not assignment_id:
        return 0
    return assignment_id['id']
def insert_message_to_room(participants, last_id, message):
    """
    Function insert the message to chat_rooms collection.
    :param participants: list of users
    :param last_id: id of last message
    :param message: dictionary with message data
    :return: None
    """
    collection = db.get_collection("chat_rooms")
    collection.update_one({'participants': participants},
                          {'$push': {'messages': message}, '$set': {"lastMsgId": last_id}})
Exemple #9
0
def insert_assignment(first_name, last_name, email):
    """
    Function inserts assignment to assignments collection.
    :param first_name: first name
    :param last_name: last name
    :param email: email
    :return: None
    """
    collection = db.get_collection("assignments")
    assignment_id = get_last_assignment_id() + 1
    collection.insert_one({"first_name": first_name, "last_name": last_name, "email": email, "id": assignment_id})
def check_credentials(username, password):
    """
    Function check if username collection contains the user with given username and password.
    :param username: username
    :param password: password
    :return: dictionary with user data if the credentials are correct, otherwise empty dictionary
    """
    collection = db.get_collection("users")
    user = collection.find_one({"username": username, "password": password})
    if user:
        return user
    return {}
def set_user_status(username, status):
    """
    Function set the state.available key to given status.
    :param username: name of user for which the status will be changed
    :param status: status
    :return: None
    """
    collection = db.get_collection("users")
    collection.update_one({'username': username},
                          {'$set': {
                              "state.available": status
                          }})
def get_user(username):
    """
    Function gets the user data from users collection with specified username.
    :param username: name of user
    :return: dictionary with user data
    """
    collection = db.get_collection("users")
    user = collection.find_one({"username": username}, {
        "password": 0,
        "_id": 0,
        "hash_code": 0,
        "uid": 0
    })
    return user
def get_users():
    """
    Function gets the users data from users collection.
    :return: list of users
    """
    collection = db.get_collection("users")
    users = list(
        collection.find({}, {
            "password": 0,
            "_id": 0,
            "hash_code": 0,
            "uid": 0
        }))
    return users
Exemple #14
0
def get_last_id():
    """
    Function gets the last id of the message in main_board collection.
    :return: last id of message
    """
    collection = db.get_collection("main_board")
    collection.find_one()
    last_record = collection.find_one({}, {
        "_id": False,
        'body': False,
        'date': False,
        'from': False
    },
                                      sort=[('_id', DESCENDING)])

    return last_record['msgId']
def get_room_messages(participants, limit=None, last_id=None):
    """
    Get data from chat_rooms collection with given participants list.
    :param participants: list of users
    :param limit: value by which the number of messages will be limited. Default is None
    :param last_id: value by which massages will be filtered. Default is None
    :return: list of messages
    """
    collection = db.get_collection("chat_rooms")
    room = collection.find_one({"participants": participants}, {"_id": 0, })
    if not room:
        create_uploads_directory(participants)
        collection.insert_one({"participants": participants, "lastMsgId": 1, "messages": []})
    messages = sorted(room['messages'], key=lambda k: k['msgId'], reverse=True)

    if last_id:
        messages = get_messages_with_id_lesser(messages, last_id)
        if limit:
            return messages[0:limit]
        return messages
    if limit:
        messages = messages[0:limit]
        return sorted(messages, key=lambda k: k['msgId'])
    return messages
Exemple #16
0
def get_messages(limit=None, last_id=None):
    """
    Function gets the main_board collection data from database.
    :param limit: value by which the number of messages will be limited. Default is None
    :param last_id: value by which massages will be filtered. Default is None
    :return: list of messages
    """
    collection = db.get_collection("main_board")
    if last_id:
        if limit:
            return list(
                collection.find({
                    "msgId": {
                        "$lt": last_id
                    }
                }, {
                    '_id': 0
                }).sort("msgId", -1).limit(limit))
        else:
            return list(
                collection.find({
                    "msgId": {
                        "$lt": last_id
                    }
                }, {
                    '_id': 0
                }).sort("msgId", -1))
    if limit:
        messages = list(
            collection.find({}, {
                '_id': 0
            }).sort("msgId", -1).limit(limit))
        messages = sorted(messages, key=lambda k: k['msgId'])
        return messages
    else:
        return list(collection.find({}, {'_id': 0}))