当前位置: 首页>>代码示例>>Python>>正文


Python Session.add_all方法代码示例

本文整理汇总了Python中kazhal.model.Session.add_all方法的典型用法代码示例。如果您正苦于以下问题:Python Session.add_all方法的具体用法?Python Session.add_all怎么用?Python Session.add_all使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在kazhal.model.Session的用法示例。


在下文中一共展示了Session.add_all方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _save

# 需要导入模块: from kazhal.model import Session [as 别名]
# 或者: from kazhal.model.Session import add_all [as 别名]
    def _save(self, result, user=None):
        addresses = []
        emails = []
        phones = []
        if user:
            for address in result["address"]:
                if address["id"]:
                    add = Session.query(Address).filter_by(id=address["id"]).one()
                    add.city = address["city"]
                    add.add1 = address["add1"]
                    add.add2 = address["add2"]
                    add.state = address["state"]
                    add.po_code = address["po_code"]
                    addresses.append(add)
                else:
                    addresses.append(
                        Address(
                            address["city"],
                            address["add1"],
                            address["add2"],
                            address["state"],
                            address["po_code"],
                            user,
                            None,
                        )
                    )
        else:
            for address in result["address"]:
                addresses.append(
                    Address(
                        address["city"],
                        address["add1"],
                        address["add2"],
                        address["state"],
                        address["po_code"],
                        None,
                        None,
                    )
                )
        if result.has_key("email"):
            if user:
                for email in result["email"]:
                    if email["id"]:
                        em = Session.query(Email).filter_by(email_address=email["id"]).one()
                        em.email_address = email["email_address"]
                        emails.append(em)
                    else:
                        emails.append(Email(email["email_address"], user))
            else:
                for email in result["email"]:
                    emails.append(Email(email["email_address"], None))
        for phone in result["phone"]:
            if user:
                if phone["id"]:
                    ph = Session.query(Phone).filter_by(phone_number=str(phone["id"])).one()
                    ph.p_type = phone["p_type"]
                    ph.phone_number = str(phone["phone_number"])
                    phones.append(ph)
                else:
                    phones.append(Phone(phone["p_type"], str(phone["phone_number"]), user))
            else:
                phones.append(Phone(phone["p_type"], str(phone["phone_number"]), None))

        photo = None
        if result["photo"]:
            picfile = result["photo"]
            permanent_file = open(
                os.path.join(config["pylons.paths"]["static_files"], "pics", picfile.filename.replace(os.sep, "_")),
                "wb",
            )
            shutil.copyfileobj(picfile.file, permanent_file)
            picfile.file.close()
            photo = Photo(permanent_file.name.split("/")[-1])

        if user:
            # do something to compare this two lists of Objects instead of asigning the whole list every time
            if user.first_name != result["first_name"]:
                user.first_name = result["first_name"]
            if user.last_name != result["last_name"]:
                user.last_name = result["last_name"]
            if user.birth_date != result["birth_date"]:
                user.birth_date = result["birth_date"]
            if user.SSN != result["SSN"]:
                user.SSN = result["SSN"]
            if "password" in result and result["password"] != "":
                user._set_password(result["password"])

            if addresses != []:
                try:
                    user.addresses = addresses
                    Session.add_all(addresses)
                    Session.commit()
                except:
                    Session.rollback()
                    return False

            if emails != []:
                try:
                    user.emails = emails
                    Session.add_all(emails)
#.........这里部分代码省略.........
开发者ID:vickyi,项目名称:PylonsSimpleCMS,代码行数:103,代码来源:user.py


注:本文中的kazhal.model.Session.add_all方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。