From 8ce68f38f6825ad6852acb6f8e8cad2f0311f00d Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Thu, 2 Mar 2023 18:45:27 +0200 Subject: [PATCH] feat: add `to_nostr_event` for product --- models.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/models.py b/models.py index b2bab4d..412bbfc 100644 --- a/models.py +++ b/models.py @@ -153,6 +153,28 @@ class PartialProduct(BaseModel): class Product(PartialProduct): id: str + def to_nostr_event(self, pubkey: str) -> NostrEvent: + content = { + "stall_id": self.stall_id, + "name": self.name, + "description": self.description, + "image": self.image, + "price": self.price, + "quantity": self.quantity, + } + categories = [["t", tag] for tag in self.categories] + + event = NostrEvent( + pubkey=pubkey, + created_at=round(time.time()), + kind=30005, + tags=[["d", self.id]] + categories, + content=json.dumps(content, separators=(",", ":"), ensure_ascii=False), + ) + event.id = event.event_id + + return event + @classmethod def from_row(cls, row: Row) -> "Product": product = cls(**dict(row))