本文整理汇总了Python中database.User.get_or_create方法的典型用法代码示例。如果您正苦于以下问题:Python User.get_or_create方法的具体用法?Python User.get_or_create怎么用?Python User.get_or_create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类database.User
的用法示例。
在下文中一共展示了User.get_or_create方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: on_success
# 需要导入模块: from database import User [as 别名]
# 或者: from database.User import get_or_create [as 别名]
def on_success(self, data):
if "text" in data:
payload = {}
# skip if tweet is from bot itself
if data["user"]["id_str"] == settings.BOT_ID:
return
# skip tweet if it's just a RT
if "retweeted_status" in data:
return
payload["text"] = data["text"]
payload["tweet_id"] = data["id_str"]
payload["user"] = User.get_or_create(user_id=data["user"]["id_str"])[0]
if "media" in data["entities"]:
media_url = data["entities"]["media"][0]["media_url"]
url_in_tweet = data["entities"]["media"][0]["url"]
payload["text"] = payload["text"].replace(url_in_tweet, "")
r = requests.get(media_url)
payload["media"] = r.content
# unescape HTML entities
payload["text"] = html.unescape(payload["text"])
# storing tweet in database
logging.info("Adding new tweet to DB: {id} from {user}".format(id=payload["tweet_id"],
user=payload["user"].user_id))
tweet_db = Tweet(**payload)
try:
tweet_db.save()
except IntegrityError:
logging.error("Tweet already present in DB")
if "delete" in data:
logging.info("Received delete message, checking if corresponding tweet is stored: {id}".format(
id=data["delete"]["status"]["id_str"]))
instance = None
try:
instance = Tweet.get(Tweet.tweet_id == data["delete"]["status"]["id_str"])
logging.info("Tweet found! :)")
except DoesNotExist:
logging.info("Tweet not found! :(")
event_db = Event(event="delete",
user=User.get_or_create(user_id=data["delete"]["status"]["user_id_str"])[0],
tweet=instance)
event_db.save()
if instance:
# mark this tweet as deleted
instance.is_deleted = True
instance.save()
# add tweet to job queue
jobs_db = Job(tweet=instance)
try:
jobs_db.save()
except IntegrityError:
logging.error("Tweet is already marked for restoration")
if "status_withheld" in data:
logging.info("Received withheld content notice, checking if corresponding tweet is stored: {id}".format(
id=str(data["status_withheld"]["id"])))
instance = None
try:
instance = Tweet.get(Tweet.tweet_id == str(data["status_withheld"]["id"]))
logging.info("Tweet found! :)")
except DoesNotExist:
logging.info("Tweet not found! :(")
event_db = Event(event="withheld",
user=User.get_or_create(user_id=str(data["status_withheld"]["user_id"]))[0],
tweet=instance)
event_db.save()
if instance:
# mark this tweet as deleted
instance.is_withheld = True
instance.save()
# add tweet to job queue
jobs_db = Job(tweet=instance)
try:
jobs_db.save()
except IntegrityError:
logging.error("Tweet is already marked for restoration")