本文整理汇总了Python中models.Article.allocate_ids方法的典型用法代码示例。如果您正苦于以下问题:Python Article.allocate_ids方法的具体用法?Python Article.allocate_ids怎么用?Python Article.allocate_ids使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Article
的用法示例。
在下文中一共展示了Article.allocate_ids方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: createArticle
# 需要导入模块: from models import Article [as 别名]
# 或者: from models.Article import allocate_ids [as 别名]
def createArticle(self, request):
"""Create new Article object, returning ArticleForm/request."""
for required in ['title', 'content']:
if not getattr(request, required):
raise endpoints.BadRequestException("Article '%s' field required" % required)
# copy ArticleForm/ProtoRPC Message into dict
data = {field.name: getattr(request, field.name) for field in request.all_fields()}
if data['view'] == None:
del data['view']
else:
data['view'] = str(data['view'])
author = self._getAuthorFromUser()
data['authorName'] = author.displayName
article_id = Article.allocate_ids(size=1, parent=author.key)[0]
article_key = ndb.Key(Article, article_id, parent=author.key)
data['key'] = article_key
# create Article
article_key = Article(**data).put()
# send email to author confirming creation of Article
taskqueue.add(params={'email': author.mainEmail,
'ArticleInfo': repr(request)},
url='/tasks/send_confirmation_email'
)
return self._copyArticleToForm(article_key.get(), author=author)
示例2: copyArticlesKind
# 需要导入模块: from models import Article [as 别名]
# 或者: from models.Article import allocate_ids [as 别名]
def copyArticlesKind(self, article, author):
"""Create new Article and Comment objects from old Articles object, returning True if success."""
article_id = Article.allocate_ids(size=1, parent=author.key)[0]
article_key = ndb.Key(Article, article_id, parent=author.key)
a = article_key.get()
if a:
return
# copy ArticleForm/ProtoRPC Message into dict
data = db.to_dict(article)
data['key'] = article_key
if 'comments' in data:
for comment in data['comments']:
#Create new Comment object
comment_author_email = str(loads(str(comment))[1])
a_key = ndb.Key(Author, comment_author_email or 'unknown')
comment_author = a_key.get()
# create new Author if not there
if not comment_author:
comment_author = Author(
key = a_key,
authorID = str(Author.allocate_ids(size=1)[0]),
displayName = comment_author_email.split('@')[0],
mainEmail = comment_author_email,
)
comment_author.put()
comment_data = {
'comment': loads(str(comment))[0],
'authorName': comment_author.displayName if comment_author else 'unknown',
'authorID': comment_author.authorID if comment_author else 'unknown',
'dateCreated': loads(str(comment))[2]
}
comment_id = Comment.allocate_ids(size=1, parent=article_key)[0]
comment_key = ndb.Key(Comment, comment_id, parent=article_key)
comment_data['key'] = comment_key
# create Comment
Comment(**comment_data).put()
del data['comments']
if 'tags' in data:
#del data['tags']
try:
data['tags'] = str(data['tags']).split(', ')
except UnicodeEncodeError:
del data['tags']
if 'tags' in data and data['tags'] == [""]:
del data['tags']
if 'id' in data:
del data['id']
if data['view'] == None:
del data['view']
else:
data['view'] = {'Publish': 'PUBLISHED', 'Preview': 'NOT_PUBLISHED', 'Retract': 'RETRACTED'}[str(data['view'])]
data['legacyID'] = str(article.key().id())
data['authorName'] = author.displayName
del data['author']
data['dateCreated'] = data['date']
del data['date']
# create Article
Article(**data).put()