本文整理匯總了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)
#.........這裏部分代碼省略.........