def from_bson(cls, document) -> Card: # С другой стороны, мы хотим абстрагировать весь # остальной код от того факта, что мы храним карточки # в монге. Но при этом id будет неизбежно везде # использоваться, так что сконвертируем-ка его в строку. document["id"] = str(document.pop("_id")) return Card(**document)
def create(self, card: Card) -> Card: card.id = str( self.collection.insert_one(self.to_bson(card)).inserted_id) return card
def from_bson(cls, document) -> Card: document["id"] = str(document.pop("_id")) return Card(**document)
wiring = Wiring() def create_or_update(card): try: card.id = wiring.card_dao.get_by_slug(card.slug).id card = wiring.card_dao.update(card) except CardNotFound: card = wiring.card_dao.create(card) wiring.task_queue.enqueue_call(parse_card_markup, kwargs={"card_id": card.id}) create_or_update( Card(slug="helloworld", name="Hello, world!", markdown=""" This is a hello-world page. It can't really compete with the [demo page](demo) """)) create_or_update( Card(slug="demo", name="Demo Card!", markdown=""" Hi there, habrovchanin. You've probably got here from the awkward ["Hello, world" card](helloworld). Well, **good news**! Finally you are looking at a **really cool card**! """))
def from_bson(cls, document): document['id'] = str(document.pop('_id')) return Card(**document)
from backend.wiring import Wiring wiring = Wiring() def create_or_update(card): try: card.id = wiring.card_dao.get_by_slug(card.slug).id card = wiring.card_dao.update(card) except CardNotFound: card = wiring.card_dao.create(card) wiring.task_queue.enqueue_call(parse_card_markup, kwargs={'card_id': card.id}) create_or_update( Card(slug='helloworld', name='Hello, World!', markdown=""" This is hello-world page. It can't really compete with the [demo page](demo). """)) create_or_update( Card(slug='demo', name='Demo Card!', markdown=""" Hi there, gay. You've probably got here from awkard ["Hello, World" card](helloworld). Well, **good news**! Finally you are looking at a **realy cool card**! """))