本文整理汇总了Python中mediawords.db.DatabaseHandler.commit方法的典型用法代码示例。如果您正苦于以下问题:Python DatabaseHandler.commit方法的具体用法?Python DatabaseHandler.commit怎么用?Python DatabaseHandler.commit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mediawords.db.DatabaseHandler
的用法示例。
在下文中一共展示了DatabaseHandler.commit方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_story
# 需要导入模块: from mediawords.db import DatabaseHandler [as 别名]
# 或者: from mediawords.db.DatabaseHandler import commit [as 别名]
def add_story(db: DatabaseHandler, story: dict, feeds_id: int, skip_checking_if_new: bool = False) -> Optional[dict]:
"""If the story is new, add story to the database with the feed of the download as story feed.
Returns created story or None if story wasn't created.
"""
story = decode_object_from_bytes_if_needed(story)
if isinstance(feeds_id, bytes):
feeds_id = decode_object_from_bytes_if_needed(feeds_id)
feeds_id = int(feeds_id)
if isinstance(skip_checking_if_new, bytes):
skip_checking_if_new = decode_object_from_bytes_if_needed(skip_checking_if_new)
skip_checking_if_new = bool(int(skip_checking_if_new))
if db.in_transaction():
raise McAddStoryException("add_story() can't be run from within transaction.")
db.begin()
db.query("LOCK TABLE stories IN ROW EXCLUSIVE MODE")
if not skip_checking_if_new:
if not is_new(db=db, story=story):
log.debug("Story '{}' is not new.".format(story['url']))
db.commit()
return None
medium = db.find_by_id(table='media', object_id=story['media_id'])
if story.get('full_text_rss', None) is None:
story['full_text_rss'] = medium.get('full_text_rss', False) or False
if len(story.get('description', '')) == 0:
story['full_text_rss'] = False
try:
story = db.create(table='stories', insert_hash=story)
except Exception as ex:
db.rollback()
# FIXME get rid of this, replace with native upsert on "stories_guid" unique constraint
if 'unique constraint \"stories_guid' in str(ex):
log.warning(
"Failed to add story for '{}' to GUID conflict (guid = '{}')".format(story['url'], story['guid'])
)
return None
else:
raise McAddStoryException("Error adding story: {}\nStory: {}".format(str(ex), str(story)))
db.find_or_create(
table='feeds_stories_map',
insert_hash={
'stories_id': story['stories_id'],
'feeds_id': feeds_id,
}
)
db.commit()
return story
示例2: extract_and_process_story
# 需要导入模块: from mediawords.db import DatabaseHandler [as 别名]
# 或者: from mediawords.db.DatabaseHandler import commit [as 别名]
def extract_and_process_story(db: DatabaseHandler,
story: dict,
extractor_args: PyExtractorArguments = PyExtractorArguments()) -> None:
"""Extract all of the downloads for the given story and then call process_extracted_story()."""
story = decode_object_from_bytes_if_needed(story)
stories_id = story['stories_id']
use_transaction = not db.in_transaction()
if use_transaction:
db.begin()
log.debug("Fetching downloads for story {}...".format(stories_id))
downloads = db.query("""
SELECT *
FROM downloads
WHERE stories_id = %(stories_id)s
AND type = 'content'
ORDER BY downloads_id ASC
""", {'stories_id': stories_id}).hashes()
# MC_REWRITE_TO_PYTHON: Perlism
if downloads is None:
downloads = []
for download in downloads:
log.debug("Extracting download {} for story {}...".format(download['downloads_id'], stories_id))
extract_and_create_download_text(db=db, download=download, extractor_args=extractor_args)
log.debug("Processing extracted story {}...".format(stories_id))
process_extracted_story(db=db, story=story, extractor_args=extractor_args)
if use_transaction:
db.commit()
示例3: _fetch_tweets_for_day
# 需要导入模块: from mediawords.db import DatabaseHandler [as 别名]
# 或者: from mediawords.db.DatabaseHandler import commit [as 别名]
def _fetch_tweets_for_day(
db: DatabaseHandler,
twitter_class: typing.Type[AbstractTwitter],
topic: dict,
topic_tweet_day: dict,
max_tweets: typing.Optional[int] = None) -> None:
"""
Fetch tweets for a single day.
If tweets_fetched is false for the given topic_tweet_days row, fetch the tweets for the given day by querying
the list of tweets from CH and then fetching each tweet from twitter.
Arguments:
db - db handle
twitter_class - AbstractTwitter class
topic - topic dict
topic_tweet_day - topic_tweet_day dict
max_tweets - max tweets to fetch for a single day
Return:
None
"""
if topic_tweet_day['tweets_fetched']:
return
ch_posts_data = topic_tweet_day['ch_posts']
ch_posts = ch_posts_data['posts']
if (max_tweets is not None):
ch_posts = ch_posts[0:max_tweets]
log.info("adding %d tweets for topic %s, day %s" % (len(ch_posts), topic['topics_id'], topic_tweet_day['day']))
# we can only get 100 posts at a time from twitter
for i in range(0, len(ch_posts), 100):
_add_tweets_to_ch_posts(twitter_class, ch_posts[i:i + 100])
ch_posts = list(filter(lambda p: _post_matches_pattern(topic, p), ch_posts))
log.info("%d tweets remaining after match" % (len(ch_posts)))
db.begin()
log.debug("inserting into topic_tweets ...")
[_store_tweet_and_urls(db, topic_tweet_day, ch_post) for ch_post in ch_posts]
topic_tweet_day['num_ch_tweets'] = len(ch_posts)
db.query(
"update topic_tweet_days set tweets_fetched = true, num_ch_tweets = %(a)s where topic_tweet_days_id = %(b)s",
{'a': topic_tweet_day['num_ch_tweets'], 'b': topic_tweet_day['topic_tweet_days_id']})
db.commit()
log.debug("done inserting into topic_tweets")
示例4: regenerate_api_key
# 需要导入模块: from mediawords.db import DatabaseHandler [as 别名]
# 或者: from mediawords.db.DatabaseHandler import commit [as 别名]
def regenerate_api_key(db: DatabaseHandler, email: str) -> None:
"""Regenerate API key -- creates new non-IP limited API key, removes all IP-limited API keys."""
email = decode_object_from_bytes_if_needed(email)
if not email:
raise McAuthProfileException('Email address is empty.')
# Check if user exists
try:
user = user_info(db=db, email=email)
except Exception:
raise McAuthProfileException("User with email address '%s' does not exist." % email)
db.begin()
# Purge all IP-limited API keys
db.query("""
DELETE FROM auth_user_api_keys
WHERE ip_address IS NOT NULL
AND auth_users_id = (
SELECT auth_users_id
FROM auth_users
WHERE email = %(email)s
)
""", {'email': email})
# Regenerate non-IP limited API key
db.query("""
UPDATE auth_user_api_keys
-- DEFAULT points to a generation function
SET api_key = DEFAULT
WHERE ip_address IS NULL
AND auth_users_id = (
SELECT auth_users_id
FROM auth_users
WHERE email = %(email)s
)
""", {'email': email})
message = AuthAPIKeyResetMessage(to=email, full_name=user.full_name())
if not send_email(message):
db.rollback()
raise McAuthProfileException("Unable to send email about reset API key.")
db.commit()
示例5: all_users
# 需要导入模块: from mediawords.db import DatabaseHandler [as 别名]
# 或者: from mediawords.db.DatabaseHandler import commit [as 别名]
def all_users(db: DatabaseHandler) -> List[CurrentUser]:
"""Fetch and return a list of users and their roles."""
# Start a transaction so that the list of users doesn't change while we run separate queries with user_info()
db.begin()
user_emails = db.query("""
SELECT email
FROM auth_users
ORDER BY auth_users_id
""").flat()
users = []
for email in user_emails:
users.append(user_info(db=db, email=email))
db.commit()
return users
示例6: _update_media_normalized_urls
# 需要导入模块: from mediawords.db import DatabaseHandler [as 别名]
# 或者: from mediawords.db.DatabaseHandler import commit [as 别名]
def _update_media_normalized_urls(db: DatabaseHandler) -> None:
"""Keep normalized_url field in media table up to date.
Set the normalized_url field of any row in media for which it is null. Take care to lock the process
so that only one process is doing this work at a time.
"""
# put a lock on this because the process of generating all media urls will take a couple hours, and we don't
# want all workers to do the work
locked = False
while not locked:
if not _normalized_urls_out_of_date(db):
return
db.begin()
# poll instead of block so that we can releae the transaction and see whether someone else has already
# updated all of the media
locked = get_session_lock(db, 'MediaWords::TM::Media::media_normalized_urls', 1, wait=False)
if not locked:
db.commit()
log.info("sleeping for media_normalized_urls lock...")
time.sleep(1)
log.warning("updating media_normalized_urls ...")
media = db.query("select * from media where normalized_url is null").hashes()
i = 0
total = len(media)
for medium in media:
i += 1
normalized_url = mediawords.util.url.normalize_url_lossy(medium['url'])
if normalized_url is None:
normalized_url = medium['url']
log.info("[%d/%d] adding %s (%s)" % (i, total, medium['name'], normalized_url))
db.update_by_id('media', medium['media_id'], {'normalized_url': normalized_url})
db.commit()
示例7: merge_foreign_rss_stories
# 需要导入模块: from mediawords.db import DatabaseHandler [as 别名]
# 或者: from mediawords.db.DatabaseHandler import commit [as 别名]
def merge_foreign_rss_stories(db: DatabaseHandler, topic: dict) -> None:
"""Move all topic stories with a foreign_rss_links medium from topic_stories back to topic_seed_urls."""
topic = decode_object_from_bytes_if_needed(topic)
stories = db.query(
"""
select s.*
from stories s, topic_stories ts, media m
where
s.stories_id = ts.stories_id and
s.media_id = m.media_id and
m.foreign_rss_links = true and
ts.topics_id = %(a)s and
not ts.valid_foreign_rss_story
""",
{'a': topic['topics_id']}).hashes()
for story in stories:
download = db.query(
"select * from downloads where stories_id = %(a)s order by downloads_id limit 1",
{'a': story['stories_id']}).hash()
content = ''
try:
content = mediawords.dbi.downloads.fetch_content(db, download)
except Exception:
pass
db.begin()
db.create('topic_seed_urls', {
'url': story['url'],
'topics_id': topic['topics_id'],
'source': 'merge_foreign_rss_stories',
'content': content
})
db.query(
"delete from topic_stories where stories_id = %(a)s and topics_id = %(b)s",
{'a': story['stories_id'], 'b': topic['topics_id']})
db.commit()
示例8: activate_user_via_token
# 需要导入模块: from mediawords.db import DatabaseHandler [as 别名]
# 或者: from mediawords.db.DatabaseHandler import commit [as 别名]
def activate_user_via_token(db: DatabaseHandler, email: str, activation_token: str) -> None:
"""Change password with a password token sent by email."""
email = decode_object_from_bytes_if_needed(email)
activation_token = decode_object_from_bytes_if_needed(activation_token)
if not email:
raise McAuthRegisterException("Email is empty.")
if not activation_token:
raise McAuthRegisterException('Password reset token is empty.')
# Validate the token once more (was pre-validated in controller)
if not password_reset_token_is_valid(db=db, email=email, password_reset_token=activation_token):
raise McAuthRegisterException('Activation token is invalid.')
db.begin()
# Set the password hash
db.query("""
UPDATE auth_users
SET active = TRUE
WHERE email = %(email)s
""", {'email': email})
# Unset the password reset token
db.query("""
UPDATE auth_users
SET password_reset_token_hash = NULL
WHERE email = %(email)s
""", {'email': email})
user = user_info(db=db, email=email)
message = AuthActivatedMessage(to=email, full_name=user.full_name())
if not send_email(message):
db.rollback()
raise McAuthRegisterException("Unable to send email about an activated user.")
db.commit()
示例9: update_user
# 需要导入模块: from mediawords.db import DatabaseHandler [as 别名]
# 或者: from mediawords.db.DatabaseHandler import commit [as 别名]
def update_user(db: DatabaseHandler, user_updates: ModifyUser) -> None:
"""Update an existing user."""
if not user_updates:
raise McAuthProfileException("Existing user is undefined.")
# Check if user exists
try:
user = user_info(db=db, email=user_updates.email())
except Exception:
raise McAuthProfileException('User with email address "%s" does not exist.' % user_updates.email())
db.begin()
if user_updates.full_name() is not None:
db.query("""
UPDATE auth_users
SET full_name = %(full_name)s
WHERE email = %(email)s
""", {
'full_name': user_updates.full_name(),
'email': user_updates.email(),
})
if user_updates.notes() is not None:
db.query("""
UPDATE auth_users
SET notes = %(notes)s
WHERE email = %(email)s
""", {
'notes': user_updates.notes(),
'email': user_updates.email(),
})
if user_updates.active() is not None:
db.query("""
UPDATE auth_users
SET active = %(active)s
WHERE email = %(email)s
""", {
'active': bool(int(user_updates.active())),
'email': user_updates.email(),
})
if user_updates.password() is not None:
try:
change_password(
db=db,
email=user_updates.email(),
new_password=user_updates.password(),
new_password_repeat=user_updates.password_repeat(),
do_not_inform_via_email=True,
)
except Exception as ex:
db.rollback()
raise McAuthProfileException("Unable to change password: %s" % str(ex))
if user_updates.weekly_requests_limit() is not None:
db.query("""
UPDATE auth_user_limits
SET weekly_requests_limit = %(weekly_requests_limit)s
WHERE auth_users_id = %(auth_users_id)s
""", {
'weekly_requests_limit': user_updates.weekly_requests_limit(),
'auth_users_id': user.user_id(),
})
if user_updates.weekly_requested_items_limit() is not None:
db.query("""
UPDATE auth_user_limits
SET weekly_requested_items_limit = %(weekly_requested_items_limit)s
WHERE auth_users_id = %(auth_users_id)s
""", {
'weekly_requested_items_limit': user_updates.weekly_requested_items_limit(),
'auth_users_id': user.user_id(),
})
if user_updates.role_ids() is not None:
db.query("""
DELETE FROM auth_users_roles_map
WHERE auth_users_id = %(auth_users_id)s
""", {'auth_users_id': user.user_id()})
for auth_roles_id in user_updates.role_ids():
db.insert(table='auth_users_roles_map', insert_hash={
'auth_users_id': user.user_id(),
'auth_roles_id': auth_roles_id,
})
db.commit()
示例10: add_user
# 需要导入模块: from mediawords.db import DatabaseHandler [as 别名]
# 或者: from mediawords.db.DatabaseHandler import commit [as 别名]
def add_user(db: DatabaseHandler, new_user: NewUser) -> None:
"""Add new user."""
if not new_user:
raise McAuthRegisterException("New user is undefined.")
# Check if user already exists
user_exists = db.query("""
SELECT auth_users_id
FROM auth_users
WHERE email = %(email)s
LIMIT 1
""", {'email': new_user.email()}).hash()
if user_exists is not None and 'auth_users_id' in user_exists:
raise McAuthRegisterException("User with email '%s' already exists." % new_user.email())
# Hash + validate the password
try:
password_hash = generate_secure_hash(password=new_user.password())
if not password_hash:
raise McAuthRegisterException("Password hash is empty.")
except Exception as ex:
log.error("Unable to hash a new password: {}".format(ex))
raise McAuthRegisterException('Unable to hash a new password.')
db.begin()
# Create the user
db.create(
table='auth_users',
insert_hash={
'email': new_user.email(),
'password_hash': password_hash,
'full_name': new_user.full_name(),
'notes': new_user.notes(),
'active': bool(int(new_user.active())),
}
)
# Fetch the user's ID
try:
user = user_info(db=db, email=new_user.email())
except Exception as ex:
db.rollback()
raise McAuthRegisterException("I've attempted to create the user but it doesn't exist: %s" % str(ex))
# Create roles
try:
for auth_roles_id in new_user.role_ids():
db.create(table='auth_users_roles_map', insert_hash={
'auth_users_id': user.user_id(),
'auth_roles_id': auth_roles_id,
})
except Exception as ex:
raise McAuthRegisterException("Unable to create roles: %s" % str(ex))
# Update limits (if they're defined)
if new_user.weekly_requests_limit() is not None:
db.query("""
UPDATE auth_user_limits
SET weekly_requests_limit = %(weekly_requests_limit)s
WHERE auth_users_id = %(auth_users_id)s
""", {
'auth_users_id': user.user_id(),
'weekly_requests_limit': new_user.weekly_requests_limit(),
})
if new_user.weekly_requested_items_limit() is not None:
db.query("""
UPDATE auth_user_limits
SET weekly_requested_items_limit = %(weekly_requested_items_limit)s
WHERE auth_users_id = %(auth_users_id)s
""", {
'auth_users_id': user.user_id(),
'weekly_requested_items_limit': new_user.weekly_requested_items_limit(),
})
# Subscribe to newsletter
if new_user.subscribe_to_newsletter():
db.create(table='auth_users_subscribe_to_newsletter', insert_hash={'auth_users_id': user.user_id()})
if not new_user.active():
send_user_activation_token(
db=db,
email=new_user.email(),
activation_link=new_user.activation_url(),
subscribe_to_newsletter=new_user.subscribe_to_newsletter(),
)
db.commit()