本文整理汇总了Python中models.session.add函数的典型用法代码示例。如果您正苦于以下问题:Python add函数的具体用法?Python add怎么用?Python add使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了add函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: exec_global
def exec_global(self, request):
"""
Execute a global-scoped command.
"""
command = request["command"]
if command == "go back to perm":
liq.set_var("selected", "perm")
liq.send_command("perm-switch.skip")
self.send_feedback(request)
elif command == "rescan":
result = Utilities.rescan()
self.send_feedback(request, result)
elif command == "select":
studio = session.query(Studio).find_by(slug=request["studio"].decode('utf-8'))
if studio is not None:
studio.selected = True
session.add(studio)
session.commit()
liq.set_var("selected", str(studio.slug))
sylog("INFO: Selecting " + studio.slug)
self.send_feedback(request)
else:
self.send_feedback(request, "error", "Unknown studio %s" % request["studio"])
示例2: item_edit
def item_edit(item_title):
categories = session.query(Category).all()
item = session.query(Item).filter_by(
title=item_title).one()
user = session.query(User).filter_by(
id=login_session.get('user_id')).one()
if item.user is not None and item.user.id != user.id:
return redirect(url_for('home'))
if request.method == 'POST':
form = vh.ItemForm(request.form, item)
file_storage_err = vh.store_item_pic(
item, request.files['picture'])
if (not form.validate() or file_storage_err is not None):
return render_template('item_edit.html',
form=form,
file_err=file_storage_err)
form.populate_obj(item)
try:
session.add(item)
session.commit()
except ValueError as e:
return "Database validation error: " + str(e)
except SQLAlchemyError as e:
# todo: log error, but don't display detailed message
# for security reasons
return "Database error: " + str(e)
return redirect(url_for('home'))
else:
form = vh.ItemForm(obj=item)
return render_template('item_edit.html',
form=form,
file_err=None)
示例3: recieve_message
def recieve_message():
try:
message = request.form.get('message')
json_resp = False
if not message:
message = request.get_json()['message']
json_resp = True
message_data = Message(message=message)
session.add(message_data)
session.commit()
if json_resp:
resp = {
'status': 'success',
'message': message
}
return jsonify(**resp)
else:
return redirect(url_for('dashboard'))
except Exception as e:
resp = {
'status': 'failed',
'error': str(e)
}
return jsonify(**resp)
示例4: addEdit
def addEdit(self, _thread_id, _content, _submitted=False):
existingList = (
session.query(bot_comments_anetpool)
.filter_by(thread_id=_thread_id)
.order_by(desc(bot_comments_anetpool.edit_id))
.first()
)
if existingList is None:
row = bot_comments_anetpool()
row.thread_id = _thread_id
_from_template = self.anetpool_template.split("	", 1)
row.content = _from_template[0] + _content + "	" + _from_template[1]
row.submitted = _submitted
session.add(row)
else:
if len(existingList.content) + len(_content) >= 10000:
row = bot_comments_anetpool()
row.thread_id = _thread_id
_from_template = self.anetpool_template.split("	", 1)
row.content = _from_template[0] + _content + "	" + _from_template[1]
row.submitted_id = "e" + str(existingList.edit_id)
row.submitted = _submitted
session.add(row)
else:
_from_save = existingList.content.split("	", 1)
session.query(bot_comments_anetpool).filter_by(edit_id=existingList.edit_id).update(
{"content": _from_save[0] + _content + "	" + _from_save[1], "submitted": False}
)
session.commit()
示例5: add_comment
def add_comment(self, _thing_id, _content, _submitted=False, _signature=None):
if _signature is None:
_signature = self.signature
last_id = _thing_id
extra_len = len("\n\n--- continued below ---") + len(_signature)
while True:
if len(_content) <= 0:
return
content_parts = _content.split("\n")
stiched_content = ""
for part in content_parts:
if len(stiched_content) + len(part + "\n") + extra_len <= 10000:
stiched_content += part + "\n"
else:
break
row = bot_comments()
row.thing_id = last_id
row.submitted = _submitted
if _content[len(stiched_content) :] == "":
row.content = stiched_content + _signature
else:
row.content = stiched_content + "\n\n--- continued below ---" + _signature
session.add(row)
session.commit()
last_id = "i{}".format(session.query(bot_comments).order_by(desc(bot_comments.id)).first().id)
_content = _content[len(stiched_content) :]
示例6: insert_test
def insert_test():
tom = Teacher(name='Tom', subject='Physics')
session.add(tom)
session.commit()
lucy = Student(name='Lucy', head_teacher_id=tom.id)
session.add(lucy)
session.commit()
示例7: add_admin
def add_admin(emailormobile):
user = session.query(User).filter(or_(User.email == emailormobile,User.mobile == emailormobile)).first()
if not user:
raise
admin = Admin(admin_id = ut.create_admin_id(),user_id = user.user_id,created = time.time()*1000,updated = time.time()*1000)
session.add(admin)
session.commit()
示例8: addToDatabase
def addToDatabase(parent, subtree, i):
subComments = subtree["data"]["children"]
if len(subComments) == 0:
pass
# print "List EMPTY"
w = 0
for j in subComments:
comment = Comment()
comment.reddit_id = j["data"]["id"]
comment.text = j["data"]["body"]
comment.user_name = j["data"]["author"]
comment.upvotes = int(j["data"]["ups"])
comment.downvotes = j["data"]["downs"]
comment.date_of_last_sweep = datetime.now()
comment.parent = parent
comment.post_id = parent.post_id
session.add(comment)
if j["data"]["replies"] == "":
comment.weight = comment.upvotes
# print "Dict Empty"
# print type(j["data"]["replies"])
else:
addToDatabase(comment, j["data"]["replies"], i + 1)
comment.weight = comment.weight + comment.upvotes
w = comment.weight + w
parent.weight = w
"""
示例9: load_baskets
def load_baskets(session):
user = 1
basket_names = ["Example 1"]
for name in basket_names:
name = models.Baskets(name=name)
session.add(name)
session.commit()
示例10: message_good
def message_good(form):
global __HEXDIGEST__
_json = json.dumps(form.to_dict())
mess = EmailMessage(_json)
mess.hexdigest = __HEXDIGEST__
session.add(mess)
session.commit()
示例11: get_page_links1
def get_page_links1(webUrl):
'''
Description: get images' first layer links
'''
init_db()
htmlContent = requests.get(webUrl, headers=headers)
soup = BeautifulSoup(htmlContent.text, "html.parser")
wp_page_numbers_div = soup.find("div", {"id":"wp_page_numbers"})
endPageTag = wp_page_numbers_div.find_all("a")[-1]
endPageLink = endPageTag.get('href')
if endPageLink:
regex = r"(\D+\d+\D+)(\d+)(\D+)"
m = re.match(regex, endPageLink)
if m:
pageNumber = int(m.groups()[1]) #get page number
for index in xrange(1, pageNumber+1):
pageLink = "%s"*4 %(webUrl, m.group(1), index, m.group(3))
#pageLinks.append(pageLink)
page_link1_queue.put(pageLink)
query = session.query(FirstLevelLinks)
query_result = query.filter(FirstLevelLinks.url==pageLink).first()
if query_result:
continue
else:
first_level_links = FirstLevelLinks(url=pageLink)
session.add(first_level_links)
session.flush()
session.commit()
return page_link1_queue
else:
return None
示例12: mod
def mod():
global __HEXDIGEST__
if request.method == "GET":
mess = session.query(EmailMessage).get(__HEXDIGEST__)
if mess is None or mess.approved == True:
flash('No new messages to edit')
return render_template('edit.html')
dct = json.loads(mess.everything)
form = EditForm()
form.subject.data = dct.get('subject')
form.text.data = dct.get('body-plain')
form.approved.data = mess.approved
return render_template('edit.html', form=form)
else:
form = EditForm(request.form)
mess = session.query(EmailMessage).get(__HEXDIGEST__)
if form.approved.data == True:
send_message(form.data)
mess.approved = True
session.add(mess)
session.commit()
flash('message sent')
return render_template('edit.html')
flash("bad form")
return "bad form"
示例13: websocket_to_database
def websocket_to_database():
try:
websocket = yield from websockets.connect("wss://ws-feed.exchange.coinbase.com")
except gaierror:
db_logger.error('socket.gaierror - had a problem connecting to Coinbase feed')
return
yield from websocket.send('{"type": "subscribe", "product_id": "BTC-USD"}')
while True:
message = yield from websocket.recv()
if message is None:
file_logger.error('Websocket message is None!')
break
try:
message = json.loads(message)
except TypeError:
db_logger.error('JSON did not load, see ' + str(message))
continue
if message['type'] != 'match':
continue
new_message = Messages()
for key in message:
if hasattr(new_message, key):
setattr(new_message, key, message[key])
else:
db_logger.error(str(key) + ' is missing, see ' + str(message))
continue
try:
session.add(new_message)
session.commit()
except IntegrityError:
session.rollback()
except DatabaseError:
file_logger.error('Database Error')
session.rollback()
示例14: get_whole_page_content
def get_whole_page_content(amount):
'''
get_whold_page_content()
amount : get_page_amount()
return : write sqlite
'''
count = 0
for page_num in range(1, amount+1):
print u'Scraping page <%3d>...' % page_num
url = build_url(page_num)
print u'Now in: %s' % url
# get page_content
content_lst = get_content_from_page(url)
print u'Get <%3d> pices from this page.\n' % len(content_lst)
for author, content in content_lst:
content_obj = Hot(author=author, content=content)
session.add(content_obj)
count += 1
if count >= 100:
session.commit()
count = 0
# couldn't be too soon
time.sleep(3)
session.commit()
return
示例15: get_page_links2
def get_page_links2():
'''
Description: get images' second layer links
'''
pageLink = page_link1_queue.get()
print "page links2 process id:%s" %os.getpid()
print "Starting to crawl : %s" %pageLink
if pageLink:
#picture_urls = []
response = requests.get(pageLink, headers=headers)
soup = BeautifulSoup(response.text, "html.parser")
picture_divs = soup.find_all("div", {"class":"pic"})
for picture_div in picture_divs:
picture_url = picture_div.find("a").get("href")
page_link2_queue.put(picture_url)
#picture_urls.append(picture_url)
query = session.query(SecondLevelLinks)
query_result = query.filter(SecondLevelLinks.url==picture_url).first()
if query_result:
continue
else:
second_level_links = SecondLevelLinks(url=picture_url)
session.add(second_level_links)
session.flush()
session.commit()
return page_link2_queue
else:
return None