-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathclasses.py
More file actions
59 lines (46 loc) · 1.42 KB
/
classes.py
File metadata and controls
59 lines (46 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Avoid this:
# class Article:
# def __init__(self, title, body, tags, db):
# self.title = title
# self.body = body
# self.tags = tags or []
# self.db = db
# self.slug = None
# self.published = False
# def publish(self):
# if self.slug is None:
# self.slug = "-".join(self.title.lower().split())
# self.db.save_article(
# title=self.title,
# body=self.body,
# tags=self.tags,
# slug=self.slug,
# )
# self.published = True
# Favor this:
from dataclasses import dataclass, field
from datetime import datetime
@dataclass
class Article:
title: str
body: str
tags: list[str] = field(default_factory=list)
created_at: datetime = field(default_factory=datetime.utcnow)
published_at: datetime | None = None
@property
def is_published(self) -> bool:
return self.published_at is not None
@property
def slug(self) -> str:
return "-".join(self.title.lower().split())
def __str__(self) -> str:
status = "published" if self.is_published else "draft"
return f"{self.title} [{status}]"
class Publisher:
def __init__(self, db):
self._db = db
def publish(self, article: Article) -> None:
if article.is_published:
return
article.published_at = datetime.utcnow()
self._db.save(article)