コード例 #1
0
def vendor_insertion():
    cust = sdCustomer(name="vendor",
                      display_name="boss",
                      status=0,
                      cust_id=1)
    db.session.add(cust)
    db.session.commit()
コード例 #2
0
ファイル: base.py プロジェクト: kuochuwon/Trafficlight_demo
    def create_cust_user_user_group(self):
        cust = sdCustomer(name=self.cust_name,
                          display_name="Test Customer",
                          comment="For Unit Test")
        user = sdUser(name=self.user_name,
                      display_name="Test User",
                      password=self.user_pass,
                      comment="For Unit Test")
        user_group = sdUserGroup(name=self.user_name, display_name="Test User")

        role = sdRole(name="system_admin", display_name="System_Admin")

        cust.users.append(user)
        # add cust_id to usergroup
        cust.user_groups.append(user_group)
        # add u_ug table
        user.user_groups.append(user_group)
        # add role_ug table
        user_group.rel_role_ug.append(role)
        db.session.add(cust)
        db.session.commit()
        return user
コード例 #3
0
def seed():
    # Add system customer
    logger.debug("Adding system customer")
    sys_cust = db.session.query(sdCustomer).filter(sdCustomer.id == 0).first()
    if sys_cust:
        logger.debug("System customer existed, ignore")
    else:
        sys_cust = sdCustomer(id=0,
                              name="system",
                              display_name="System",
                              comment="System (Default)")
        db.session.add(sys_cust)

    # Add system administrator
    logger.debug("Adding system administrator")
    sys_user = db.session.query(sdUser).filter(sdUser.id == 0).first()
    if sys_user:
        logger.debug("System administrator existed, ignore")
    else:
        sys_cust.users.append(sdUser(id=0,
                                     name="admin",
                                     display_name="System Administrator",
                                     password="******",
                                     comment="System Administrator (Default)"))

    # Add default device_model
    logger.debug("Adding default device model")
    default_model = db.session.query(sdDeviceModel).filter(sdDeviceModel.id == 0).first()
    if default_model:
        logger.debug("Default device model existed, ignore")
    else:
        default_model = sdDeviceModel(id=0,
                                      name="DEFAULT",
                                      display_name="Default Model for all device model")
        db.session.add(default_model)

    # Commit transaction
    db.session.commit()
コード例 #4
0
ファイル: data.py プロジェクト: kuochuwon/Trafficlight_demo
    def user_for_issue(self):
        cust = sdCustomer(
            id=0,
            name="admin",
            display_name="Test Customer",
            comment="For Unit Test",
            issue_namespace="NTPC-{datetime:%Y%m%d}-{serial_no:05d}")
        self.cust = 0
        anonymous = sdUser(name="anonymous",
                           display_name="anonymous",
                           password="******",
                           comment="For Test Issue")
        usergroup = sdUserGroup(name="a group", display_name="Test User")

        role = db.session.query(sdRole).filter(sdRole.id == "1").first()

        self.user = 2

        cust.users.append(anonymous)
        anonymous.user_groups.append(usergroup)
        usergroup.rel_role_ug.append(role)
        db.session.add(cust)
        db.session.commit()
        return self
コード例 #5
0
ファイル: data.py プロジェクト: kuochuwon/Trafficlight_demo
 def insert_other_user(self):
     cust = sdCustomer(name="testcust2",
                       display_name="Test Customer",
                       comment="For Unit Test")
     db.session.add(cust)
     db.session.commit()
コード例 #6
0
def seed_ntpc():
    logger.debug("Seed NTPC sample data")
    data_file = os.path.join(os.getcwd(), "seed", "NTCLighting.csv")
    if not os.path.isfile(data_file):
        logger.debug(f"CSV file not existed: {data_file}")
        return

    clean_data()

    # add basic data
    cust = sdCustomer(name=__cust_name,
                      display_name=__cust_display,
                      comment=__cust_comment)
    user = sdUser(cust=cust,
                  name="admin",
                  display_name="Admin (NTPC)",
                  comment="NTPC Administrator",
                  password="******")
    model_led = sdDeviceModel(name=__model_led[0], display_name=__model_led[1])
    model_ctrl = sdDeviceModel(name=__model_ctrl[0],
                               display_name=__model_ctrl[1])
    db.session.add(cust)
    db.session.add(user)
    db.session.add(model_led)
    db.session.add(model_ctrl)
    db.session.commit()

    # add devices
    device_groups = {}
    with open(data_file, encoding='utf-8', newline='') as csv_file:
        rows = csv.reader(csv_file)
        index = 0
        row_count = 0
        for row in rows:
            index += 1
            if index == 1:
                # ignore header
                continue
            # convert twd97 to wgs84
            try:
                twd_x, twd_y = float(row[4]), float(row[5])
                (wgs_y, wgs_x) = twd97.towgs84(twd_x, twd_y)
            except Exception:
                logger.debug(f"twd or wgs error: {row}")
                continue
            # check if coordination out of range
            if wgs_x < 120 or wgs_x > 122 or wgs_y < 24 or wgs_y > 26:
                logger.debug(
                    f"wgs_x or wgs_y out of range: 120 <= x < 122 or 24 <= x < 26"
                )
                continue
            # add device
            row_count = row_count + 1
            # get or new device group
            group_name = row[0]
            device_group = device_groups.get(group_name)
            if not device_group:
                device_group = sdDeviceGroup(cust=cust,
                                             name=group_name,
                                             display_name=f"新北市{group_name}")
                device_groups[group_name] = device_group
            # new device
            device = sdDevice(
                cust=cust,
                name=f"{row_count}",
                display_name=f"{row[0]}_{row_count}",
                comment=f"New no is {row[1]}, Old no is {row[2]}",
                address=f"新北市{row[3]}",
                device_group=device_group,
                wgs_x=wgs_x,
                wgs_y=wgs_y)
            # assign controller and led
            device.controller = sdController(model=model_ctrl,
                                             cust=cust,
                                             serial_no=str(uuid.uuid4()))
            device.led = sdLed(model=model_led,
                               cust=cust,
                               serial_no=str(uuid.uuid4()))
            # add objects
            db.session.add(device)
            if (row_count % 10000) == 0:
                db.session.commit()
                logger.debug(f"Commit {row_count}")
        db.session.commit()
        logger.debug(f"Total device committed {row_count}")