-
Notifications
You must be signed in to change notification settings - Fork 17
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
7 changed files
with
154 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
MONGO_HOST = "mongo" | ||
MONGO_PORT = 27017 | ||
MONGO_DATABASE = "core" |
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
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,38 @@ | ||
import abc | ||
from typing import Iterable | ||
|
||
|
||
class Card(object): | ||
def __init__(self, id: str = None, slug: str = None, name: str = None, markdown: str = None, html: str = None): | ||
self.id = id | ||
self.slug = slug | ||
self.name = name | ||
self.markdown = markdown | ||
self.html = html | ||
|
||
|
||
class CardDAO(object, metaclass=abc.ABCMeta): | ||
|
||
@abc.abstractmethod | ||
def create(self, card: Card) -> Card: | ||
pass | ||
|
||
@abc.abstractmethod | ||
def update(self, card: Card) -> Card: | ||
pass | ||
|
||
@abc.abstractmethod | ||
def get_all(self) -> Iterable[Card]: | ||
pass | ||
|
||
@abc.abstractmethod | ||
def get_by_id(self, card_id: str) -> Card: | ||
pass | ||
|
||
@abc.abstractmethod | ||
def get_by_slug(self, slug: str) -> Card: | ||
pass | ||
|
||
|
||
class CardNotFound(Exception): | ||
pass |
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,63 @@ | ||
from typing import Iterable | ||
|
||
import bson | ||
import bson.errors | ||
from pymongo.collection import Collection | ||
from pymongo.database import Database | ||
|
||
from backend.storage.card import Card, CardDAO, CardNotFound | ||
|
||
|
||
class MongoCardDAO(CardDAO): | ||
|
||
def __init__(self, mongo_database: Database): | ||
self.mongo_database = mongo_database | ||
self.collection.create_index("slug", unique=True) | ||
|
||
@property | ||
def collection(self) -> Collection: | ||
return self.mongo_database["cards"] | ||
|
||
@classmethod | ||
def to_bson(cls, card: Card): | ||
result = { | ||
k: v | ||
for k, v in card.__dict__.items() | ||
if v is not None | ||
} | ||
if "id" in result: | ||
result["_id"] = bson.ObjectId(result.pop("id")) | ||
return result | ||
|
||
@classmethod | ||
def from_bson(cls, document) -> Card: | ||
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 update(self, card: Card) -> Card: | ||
card_id = bson.ObjectId(card.id) | ||
self.collection.update_one({"_id": card_id}, {"$set": self.to_bson(card)}) | ||
return card | ||
|
||
def get_all(self) -> Iterable[Card]: | ||
for document in self.collection.find(): | ||
yield self.from_bson(document) | ||
|
||
def get_by_id(self, card_id: str) -> Card: | ||
try: | ||
return self._get_by_query({"_id": bson.ObjectId(card_id)}) | ||
except bson.errors.InvalidId: | ||
raise ValueError | ||
|
||
def get_by_slug(self, slug: str) -> Card: | ||
return self._get_by_query({"slug": slug}) | ||
|
||
def _get_by_query(self, query) -> Card: | ||
document = self.collection.find_one(query) | ||
if document is None: | ||
raise CardNotFound() | ||
return self.from_bson(document) |
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,24 @@ | ||
import os | ||
|
||
from pymongo import MongoClient | ||
from pymongo.database import Database | ||
|
||
import backend.dev_settings | ||
from backend.storage.card import CardDAO | ||
from backend.storage.card_impl import MongoCardDAO | ||
|
||
|
||
class Wiring(object): | ||
|
||
def __init__(self, env=None): | ||
if env is None: | ||
env = os.environ.get("APP_ENV", "dev") | ||
self.settings = { | ||
"dev": backend.dev_settings, | ||
}[env] | ||
|
||
self.mongo_client: MongoClient = MongoClient( | ||
host=self.settings.MONGO_HOST, | ||
port=self.settings.MONGO_PORT) | ||
self.mongo_database: Database = self.mongo_client[self.settings.MONGO_DATABASE] | ||
self.card_dao: CardDAO = MongoCardDAO(self.mongo_database) |
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 |
---|---|---|
|
@@ -2,3 +2,4 @@ flask | |
flask-cors | ||
gevent | ||
gunicorn | ||
pymongo |