Exemple #1
0
def users_load():
    users = User.objects()
    list_users = []
    for user in users:
        list_users.append(user.to_dict())

    return list_users
Exemple #2
0
 def authenticate_user(self, username: str, password: str):
     user = User.objects(username=username).first()
     if not user:
         return None
     if not self.verify_password(password, user.password):
         return None
     return user
Exemple #3
0
def get_public_user_info(user_id: str):
    user = User.objects(id=ObjectId(user_id)).first()
    if user:
        return {
            "username": user.username,
            "name": user.name,
            "user_id": str(user.id),
            "avatar": user.avatar,
        }
    return None
Exemple #4
0
def get_members(room_id: str):
    members_id = RoomMember.objects(room_id=room_id)

    list_members = []
    for member in members_id:
        user = User.objects(id=ObjectId(member.member_id)).first()
        _user = user.to_dict()
        _user["is_owner"] = member.is_owner
        list_members.append(_user)

    return list_members
Exemple #5
0
def user_delete(user_id: str):
    user = User.objects(id=user_id)
    members = RoomMember.objects(member_id=str(user_id))
    if not members:
        user.delete()
        return True
    for member in members:
        if member.is_owner:
            return False

    user.delete()
    member.delete()

    return True
Exemple #6
0
def update_current_user(
    user_id: str,
    username: str,
    avatar: Optional[str] = None,
    name: Optional[str] = None,
):
    user = User.objects(id=ObjectId(user_id))
    fileds = {
        "username": username,
        "avatar": avatar,
        "name": name,
    }
    user.update(**fileds)

    return True
Exemple #7
0
def rooms_load():
    rooms = Room.objects()
    list_rooms = []
    for room in rooms:
        members = RoomMember.objects(room_id=str(room.id))
        messages_count = Messages.objects(room_id=str(room.id)).count()
        _room = room.to_dict()
        last_message = mess_logic.get_last_message(str(room.id))
        try:
            last_message['timestamp'] = parse(
                last_message['created_at']).strftime('%d %b,%Y %H:%M')
        except:
            pass
        _room['last_message'] = last_message
        _room["member"] = []
        _room["messages_count"] = messages_count
        for member in members:
            user = User.objects(id=ObjectId(member.member_id)).first()
            _room["member"].append(user.to_dict())
        list_rooms.append(_room)

    return list_rooms
Exemple #8
0
def disabe_user(username: str):
    user = User.objects(username=username).first()
    user.update(active=False)

    return True
Exemple #9
0
def user_disable(user_id: str):
    user = User.objects(id=ObjectId(user_id)).first()
    active = not user.active
    user.update(active=active)

    return True
Exemple #10
0
def get_user_by_id(user_id: str):
    return User.objects(id=ObjectId(user_id)).first()
Exemple #11
0
def user_create(username: str, password: str, name: str):
    user = User(username=username, password=password, name=name)
    return user.save()
Exemple #12
0
def get_user_id(username: str):
    user_id =  User.objects(username=username).first().id
    if user_id:
        return user_id
    return False
Exemple #13
0
def check_user_active(username: str):
    active = User.objects(username=username).first().active
    if active:
        return True

    return False
Exemple #14
0
def get_me(user: User = Depends(get_current_user)):
    return user.to_dict()
Exemple #15
0
def check_user_exist(user_id: str):
    user = User.objects(id=ObjectId(user_id)).first()
    if user:
        return True
    return False
Exemple #16
0
def get_user_profile(username: str):
    user = User.objects(username=username).first()

    return user.to_dict()
Exemple #17
0
def save_user(username: str, password: str, name: str):
    user = User(username=username, password=password, name=name)
    return user.save()
Exemple #18
0
def get_user(username: str):
    return User.objects(username=username).first()