def copyFromArticles(self, request): '''Copies articles and authors from legacy Articles Kind into new Article and Author kinds''' user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') for article in Articles().all(): if '@' not in article.author: author_email = article.author + '@gmail.com' else: author_email = article.author a_key = ndb.Key(Author, author_email) author = a_key.get() # create new Author if not there if not author: author = Author( key = a_key, authorID = str(Author.allocate_ids(size=1)[0]), displayName = author_email.split('@')[0], mainEmail = author_email, ) author.put() self.copyArticlesKind(article, author) return BooleanMessage(data=True)
def copyFromArticles(self, request): '''Copies articles and authors from legacy Articles Kind into new Article and Author kinds''' user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') for article in Articles().all(): if '@' not in article.author: author_email = article.author + '@gmail.com' else: author_email = article.author a_key = ndb.Key(Author, author_email) author = a_key.get() # create new Author if not there if not author: author = Author( key=a_key, authorID=str(Author.allocate_ids(size=1)[0]), displayName=author_email.split('@')[0], mainEmail=author_email, ) author.put() self.copyArticlesKind(article, author) return BooleanMessage(data=True)
def _getAuthorFromEmail(self, email): """Return user Author from datastore, creating new one if non-existent.""" # get Author from datastore or create new Author if not there a_key = ndb.Key(Author, email) author = a_key.get() # create new Author if not there if not author: author = Author( key = a_key, authorID = str(Author.allocate_ids(size=1)[0]), displayName = user.nickname(), mainEmail = user.email(), ) author.put() return author
def _getAuthorFromEmail(self, email): """Return user Author from datastore, creating new one if non-existent.""" # get Author from datastore or create new Author if not there a_key = ndb.Key(Author, email) author = a_key.get() # create new Author if not there if not author: author = Author( key=a_key, authorID=str(Author.allocate_ids(size=1)[0]), displayName=user.nickname(), mainEmail=user.email(), ) author.put() return author
def _getAuthorFromUser(self): """Return user Author from datastore, creating new one if non-existent.""" user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') # get Author from datastore or create new Author if not there user_id = getUserId(user) a_key = ndb.Key(Author, user_id) author = a_key.get() # create new Author if not there if not author: author = Author( key = a_key, authorID = str(Author.allocate_ids(size=1)[0]), displayName = user.nickname(), mainEmail = user.email(), ) author.put() return author
def _getAuthorFromUser(self): """Return user Author from datastore, creating new one if non-existent.""" user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') # get Author from datastore or create new Author if not there user_id = getUserId(user) a_key = ndb.Key(Author, user_id) author = a_key.get() # create new Author if not there if not author: author = Author( key=a_key, authorID=str(Author.allocate_ids(size=1)[0]), displayName=user.nickname(), mainEmail=user.email(), ) author.put() return author
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()
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()