def delete_category_by_slug(slug: str): # slugからIDを取得 c_id = [ cat.id for cat in db.collection('categories').where( 'slug', '==', slug).stream() ][0] # ドキュメントを削除 db.collection('categories').document(c_id).delete()
def delete_article_by_slug(slug: str): # slugからIDを取得 art_id = [ art.id for art in db.collection('articles').where('slug', '==', slug).stream() ][0] # ドキュメントを削除 db.collection('articles').document(art_id).delete()
def get_category_name(slug): # slugからIDを取得 c_id = [ cat.id for cat in db.collection('categories').where( 'slug', '==', slug).stream() ][0] # IDを用いてデータを取得 category = db.collection('categories').document(c_id).get().to_dict() return category['name'] # nameだけ返す
def update_category_by_slug(slug: str, new_name: str = None, new_slug: str = None): # slugからIDを取得 c_id = [ cat.id for cat in db.collection('categories').where( 'slug', '==', slug).stream() ][0] data = {} if new_name is not None: data['name'] = new_name if new_slug is not None: data['slug'] = new_slug db.collection('categories').document(c_id).update(data)
def get_articles(unreleased: bool = True): """ Get articles :param unreleased: True => all articles False => released articles :return: """ if unreleased: articles = [ art.to_dict() for art in db.collection('articles').order_by( 'last_update', direction=Query.DESCENDING).stream() ] else: articles = [ art.to_dict() for art in db.collection('articles').order_by( 'last_update', direction=Query.DESCENDING).stream() ] articles = [art for art in articles if art['released']] return articles
def get_whats_new(num: int = 5): """ get What's New """ articles = [ art.to_dict() for art in db.collection('articles').where( 'released', '==', True).limit(num).stream() ] # datetimeでソート articles = sorted(articles, key=lambda art: art['last_update'], reverse=True) return articles
def update_article_by_slug( original_slug, title, thumbnail, description, slug, contents, category, tags, release, ): # IDを取得 art_id = [ art.id for art in db.collection('articles').where( 'slug', '==', original_slug).stream() ][0] # 更新 db.collection('articles').document(art_id).update({ 'title': title, 'thumbnail': thumbnail, 'description': description, 'slug': slug, 'contents': contents, 'category': category, 'tags': tags, 'last_update': datetime.now(), 'released': release, })
def get_article(slug: str): """ Get an articles with slug :param slug: :return: """ article = [ art.to_dict() for art in db.collection('articles').where( 'slug', '==', slug).stream() ] if len(article) == 0: return None return article[0]
def add(self): db.collection('tags').add(self.to_dict())
def add(self): """ Add data to Firestore """ db.collection('categories').add(self.to_dict())
def get_tags(): """ Get all tags """ return [cat.to_dict() for cat in db.collection('tags').stream()]
def get_article_slugs(): return [ cat.to_dict()['slug'] for cat in db.collection('articles').stream() ]
def get_tag_slugs(): return [cat.to_dict()['slug'] for cat in db.collection('tags').stream()]
def get_category_slugs(): return [ cat.to_dict()['slug'] for cat in db.collection('categories').stream() ]
def get_categories(): """ Get all categories """ return [cat.to_dict() for cat in db.collection('categories').stream()]
def add(self): db.collection('articles').add(self.to_dict())