本文整理匯總了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")