Beispiel #1
0
def install_chat(session, PORT):
    """
    This is a procedure for first setup chat application.
    It use the command line for setup first user - root user, whom will has fool permissions.

    """
    print "It's first run of the CHAT SERVER.\nSo you need to answer few questions..."
    login = raw_input("Please type the nick-name (Login) of the root user: "******"\nPlease type your fool name (Screen Name):")
    password = make_server_password()

    root_perm = Perm("root", 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
    admin_perm = Perm("admin", 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0)
    authorised_perm = Perm("auth_user", 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0)
    guest_perm = Perm("guest", 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0)

    default_room = Room("default")
    a = Associations()
    a.user = User(login, name, password, 1)
    a.perm = root_perm
    default_room.user.append(a)
    session.add(a)
    session.add(root_perm)
    session.add(admin_perm)
    session.add(authorised_perm)
    session.add(guest_perm)
    session.add(default_room)

    session.commit()
    session.close()

    print "\nThanks."
    print "\nChat server started on port " + str(PORT)
    logging.info("chat was installed")
Beispiel #2
0
def auth_user(login, password, type):
    """
    @rtype : object
    """
    session = Session()
    users = session.query(User).order_by(User.id)
    users_logins = []
    for instance in users:
        users_logins.append(instance.login)
    for instance in users:
        if login in users_logins and login == instance.login:
            user = instance
            if not check_pass(user, password):
                return False
            else:
                return start_sys(user, session)

        elif not login in users_logins:
            if type == "guest":
                default_room = session.query(Room).filter(Room.name == "default").one()
                guest_perm = session.query(Perm).filter(Perm.name == "guest").one()
                a = Associations()
                a.user = User(login, login, None, 0)
                u = a.user
                a.perm = guest_perm
                default_room.user.append(a)
                session.add(a)
                session.commit()
                return start_sys(u, session)
        else:
            continue

    session.commit()
    session.close()
Beispiel #3
0
def get_all_user_objects(username=None):
    session = SessionLocal()
    if username and username.strip() != "":
        return session.query(User).filter(User.email.like("%" + username +
                                                          "%"))
    session.close()
    return session.query(User).filter()
Beispiel #4
0
def session_scope(versioned=True, ignore_soft_deletes=True, namespace_id=None):
    """ Provide a transactional scope around a series of operations.

    Takes care of rolling back failed transactions and closing the session
    when it goes out of scope.

    Note that sqlalchemy automatically starts a new database transaction when
    the session is created, and restarts a new transaction after every commit()
    on the session. Your database backend's transaction semantics are important
    here when reasoning about concurrency.

    Parameters
    ----------
    versioned : bool
        Do you want to enable the transaction log?
    ignore_soft_deletes : bool
        Whether or not to ignore soft-deleted objects in query results.
    namespace_id : int
        Namespace to limit query results with.

    Yields
    ------
    InboxSession
        The created session.
    """
    session = InboxSession(versioned, ignore_soft_deletes, namespace_id)
    try:
        yield session
        session.commit()
    except:
        session.rollback()
        raise
    finally:
        session.close()
Beispiel #5
0
def scheduled_job(msg, number, carrier):
    send(msg, number, carrier)
    session = db_session.create_session()
    ev = session.query(Events).filter(Events.event == msg).first()
    ev.sent = True
    session.commit()
    session.close()
Beispiel #6
0
def get_session() -> sqlalchemy.orm.session.Session:
    session = None
    try:
        session = Session()
        yield session
    finally:
        if session is not None:
            session.close()
Beispiel #7
0
def get_product(product_id=None, product_name=None):
    session = SessionLocal()
    if product_id is not None:
        obj = session.query(Product).filter(Product.product_id == product_id)
    elif product_name is not None:
        obj = session.query(Product).filter(
            Product.productName == product_name)
    session.close()
    return False if obj.count() == 0 else obj[0]
Beispiel #8
0
def get_category(category_id=None, category_name=None):
    session = SessionLocal()
    if category_id is not None:
        obj = session.query(Category).filter(
            Category.category_id == category_id)
    elif category_name is not None:
        obj = session.query(Category).filter(
            Category.categoryName == category_name)
    session.close()
    return False if obj.count() == 0 else obj[0]
Beispiel #9
0
def main():  # 主函数
    engine = sqlalchemy.create_engine(MYSQL_URL, encoding="utf8",
                                      echo=True)  # 数据库引擎
    sqlalchemy.orm.session.Session = sqlalchemy.orm.sessionmaker(
        bind=engine)  # 创建Session
    session = sqlalchemy.orm.session.Session()
    result = session.execute(SQL, [{"start": 0, "size": 20}])
    for row in result.fetchall():
        print(row)

    session.close()
Beispiel #10
0
def update_product(validated_data, product):
    session = SessionLocal()
    updated_validated_data = dict()
    for key, value in validated_data.__dict__.items():
        if value:
            updated_validated_data[key] = value
    updated_validated_data["updated_on"] = datetime.datetime.now()
    session.query(Product).filter(Product.product_id == product.product_id
                                  ).update(updated_validated_data)
    session.commit()
    session.close()
Beispiel #11
0
def update_category(validated_data, category):
    session = SessionLocal()
    updated_validated_data = dict()
    for key, value in validated_data.__dict__.items():
        if value:
            updated_validated_data[key] = value
    updated_validated_data["updated_on"] = datetime.datetime.now()
    session.query(Category).filter(Category.category_id == category.category_id
                                   ).update(updated_validated_data)
    session.commit()
    session.close()
Beispiel #12
0
def add_u_to_the_r(uname, room_name, perm_name):
    session = Session()
    new_u = Associations()
    user = session.query(User.id).filter(User.name == uname).scalar()
    room = session.query(Room.id).filter(Room.name == room_name).scalar()
    perm = session.query(Perm.id).filter(Perm.name == perm_name).scalar()

    new_u.user_id = user
    new_u.perm_id = perm
    new_u.room_id = room
    session.add(new_u)
    session.commit()

    room_obj = session.query(Room).get(room)
    room_users = []
    the_room = {}
    for r_user in room_obj.user:
        room_users.append(session.query(User.name).filter(User.id == r_user.user_id).one()[0])
    for r_user in room_obj.user:
        if r_user.user_id == user:
            the_room = dict(
                room_name=room_obj.name,
                perm=session.query(
                    Perm.id,
                    Perm.name,
                    Perm.add_user,
                    Perm.create_room,
                    Perm.create_vote,
                    Perm.delete_room,
                    Perm.delete_user,
                    Perm.delete_vote,
                    Perm.make_secure,
                    Perm.make_unsecure,
                    Perm.voting,
                    Perm.edit_perm,
                    Perm.edit_perm_def,
                )
                .filter(Perm.id == r_user.perm_id)
                .one()
                .__dict__,
                users=room_users,
                secure=room_obj.secure,
                auth=room_obj.auth,
            )
    add_user_obj = {"user_name": uname, "room": the_room}

    session.close()
    return add_user_obj