-
-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #603 from gtxm/class_directives
Class-based directives
- Loading branch information
Showing
4 changed files
with
182 additions
and
8 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,30 @@ | ||
import envelopes | ||
import hug | ||
|
||
|
||
@hug.directive() | ||
class SMTP(object): | ||
|
||
def __init__(self, *args, **kwargs): | ||
self.smtp = envelopes.SMTP(host='127.0.0.1') | ||
self.envelopes_to_send = list() | ||
|
||
def send_envelope(self, envelope): | ||
self.envelopes_to_send.append(envelope) | ||
|
||
def cleanup(self, exception=None): | ||
if exception: | ||
return | ||
for envelope in self.envelopes_to_send: | ||
self.smtp.send(envelope) | ||
|
||
|
||
@hug.get('/hello') | ||
def send_hello_email(smtp: SMTP): | ||
envelope = envelopes.Envelope( | ||
from_addr=(u'[email protected]', u'From me'), | ||
to_addr=(u'[email protected]', u'To World'), | ||
subject=u'Hello', | ||
text_body=u"World!" | ||
) | ||
smtp.send_envelope(envelope) |
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,58 @@ | ||
import hug | ||
|
||
from sqlalchemy import create_engine, Column, Integer, String | ||
from sqlalchemy.ext.declarative.api import declarative_base | ||
from sqlalchemy.orm.session import Session | ||
from sqlalchemy.orm import scoped_session | ||
from sqlalchemy.orm import sessionmaker | ||
|
||
|
||
engine = create_engine("sqlite:///:memory:") | ||
|
||
session_factory = scoped_session(sessionmaker(bind=engine)) | ||
|
||
|
||
Base = declarative_base() | ||
|
||
|
||
class TestModel(Base): | ||
__tablename__ = 'test_model' | ||
id = Column(Integer, primary_key=True) | ||
name = Column(String) | ||
|
||
|
||
Base.metadata.create_all(bind=engine) | ||
|
||
|
||
@hug.directive() | ||
class Resource(object): | ||
|
||
def __init__(self, *args, **kwargs): | ||
self._db = session_factory() | ||
self.autocommit = True | ||
|
||
@property | ||
def db(self) -> Session: | ||
return self._db | ||
|
||
def cleanup(self, exception=None): | ||
if exception: | ||
self.db.rollback() | ||
return | ||
if self.autocommit: | ||
self.db.commit() | ||
|
||
|
||
@hug.directive() | ||
def return_session() -> Session: | ||
return session_factory() | ||
|
||
|
||
@hug.get('/hello') | ||
def make_simple_query(resource: Resource): | ||
for word in ["hello", "world", ":)"]: | ||
test_model = TestModel() | ||
test_model.name = word | ||
resource.db.add(test_model) | ||
resource.db.flush() | ||
return " ".join([obj.name for obj in resource.db.query(TestModel).all()]) |
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