-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
135 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,3 +134,4 @@ python3.8/ | |
.env | ||
.vscode/ | ||
temp/* | ||
database/* |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
''' | ||
Dataclass of posts database in directory 'database' with | ||
every post named with date. | ||
''' | ||
from dataclasses import asdict, dataclass, field | ||
from datetime import date | ||
import json | ||
import os | ||
|
||
from models.Post import Post | ||
|
||
|
||
@dataclass | ||
class PostDatabase: | ||
''' Dataclass of posts database in directory 'database' with | ||
every post named with date. ''' | ||
path: str = 'database' | ||
|
||
def __post_init__(self): | ||
''' Post init. ''' | ||
# Check if path exists | ||
if (not os.path.exists(self.path)): | ||
# Create path | ||
os.makedirs(self.path) | ||
|
||
def __createdPostName(self, date: date) -> str: | ||
''' Get created post filename. ''' | ||
return f'Created{date}.json' | ||
|
||
def __postedPostName(self, date: date) -> str: | ||
''' Get posted post filename. ''' | ||
return f'Posted{date}.json' | ||
|
||
def IsCreated(self, date: date) -> bool: | ||
''' Get post by date. ''' | ||
# Created post filename | ||
createdPostPath = os.path.join(self.path, self.__createdPostName(date)) | ||
# Posted post filename | ||
postedPostPath = os.path.join(self.path, self.__postedPostName(date)) | ||
|
||
return os.path.exists(createdPostPath) or os.path.exists(postedPostPath) | ||
|
||
def IsPosted(self, date: date) -> bool: | ||
''' Get post by date. ''' | ||
# Posted post filename | ||
postedPostPath = os.path.join(self.path, self.__postedPostName(date)) | ||
return os.path.exists(postedPostPath) | ||
|
||
def GetPost(self, date: date) -> Post: | ||
''' Get post by date. ''' | ||
# Get file path | ||
filePath = os.path.join(self.path, self.__createdPostName(date)) | ||
|
||
# Check if file exists | ||
if (not os.path.exists(filePath)): | ||
# Return None | ||
return None | ||
|
||
# Load file | ||
with open(filePath, 'r') as fileObject: | ||
# Load json | ||
data = json.load(fileObject) | ||
|
||
# Return post | ||
return Post(**data) | ||
|
||
def AddCreated(self, post: Post) -> bool: | ||
''' Save post. ''' | ||
# Get file path | ||
filePath = os.path.join(self.path, self.__createdPostName(post.date)) | ||
|
||
# Save file | ||
with open(filePath, 'w') as fileObject: | ||
json.dump(asdict(post), fileObject, | ||
indent=4, ensure_ascii=False) | ||
|
||
# Return success | ||
return True | ||
|
||
def AddPosted(self, post: Post) -> bool: | ||
''' Save post. ''' | ||
# Get file path | ||
filePath = os.path.join(self.path, self.__postedPostName(post.date)) | ||
|
||
# Save file | ||
with open(filePath, 'w') as fileObject: | ||
# Dump post as json | ||
json.dump(asdict(post), fileObject, indent=4, ensure_ascii=False) | ||
|
||
# Return success | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters