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
4 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
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,19 @@ | ||
from eve import Eve | ||
|
||
from eve_sqlalchemy import SQL | ||
from eve_sqlalchemy.examples.viewonly.domain import Base, Users, Organizations | ||
from eve_sqlalchemy.validation import ValidatorSQL | ||
|
||
app = Eve(validator=ValidatorSQL, data=SQL) | ||
|
||
db = app.data.driver | ||
Base.metadata.bind = db.engine | ||
db.Model = Base | ||
db.create_all() | ||
|
||
db.session.add(Organizations(name='ACME', users=[Users(), Users()])) | ||
db.session.add(Organizations(name='Eve', users=[Users()])) | ||
db.session.commit() | ||
|
||
# using reloader will destroy in-memory sqlite db | ||
app.run(debug=True, use_reloader=False) |
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,75 @@ | ||
from sqlalchemy import ( | ||
Column, DateTime, ForeignKey, Integer, String, Table, func, | ||
) | ||
from sqlalchemy.ext.declarative import declarative_base, declared_attr | ||
from sqlalchemy.orm import relationship | ||
|
||
Base = declarative_base() | ||
|
||
|
||
def get_current_user_id(): | ||
return 1 | ||
|
||
|
||
class CommonColumns(Base): | ||
__abstract__ = True | ||
_created = Column(DateTime, default=func.now()) | ||
_updated = Column(DateTime, default=func.now(), onupdate=func.now()) | ||
_etag = Column(String(40)) | ||
|
||
@declared_attr | ||
def created_by_id(cls): | ||
return Column(Integer, | ||
ForeignKey('users.id'), | ||
default=get_current_user_id) | ||
|
||
@declared_attr | ||
def updated_by_id(cls): | ||
return Column(Integer, | ||
ForeignKey('users.id'), | ||
default=get_current_user_id, | ||
onupdate=get_current_user_id) | ||
|
||
@declared_attr | ||
def created_by(cls): | ||
return relationship( | ||
'Users', | ||
foreign_keys='{}.created_by_id'.format(cls.__name__)) | ||
|
||
@declared_attr | ||
def updated_by(cls): | ||
return relationship( | ||
'Users', | ||
foreign_keys='{}.updated_by_id'.format(cls.__name__)) | ||
|
||
@declared_attr | ||
def organization(cls): | ||
return relationship( | ||
'Organizations', | ||
primaryjoin='{}.created_by_id==Users.id'.format(cls.__name__), | ||
secondary='join(Users, Organizations, Users.organization_id == Organizations.id)', # noqa | ||
viewonly=True) | ||
|
||
|
||
class Users(Base): | ||
__tablename__ = 'users' | ||
_created = Column(DateTime, default=func.now()) | ||
_updated = Column(DateTime, default=func.now(), onupdate=func.now()) | ||
id = Column(Integer, primary_key=True, autoincrement=True) | ||
organization_id = Column(Integer, | ||
ForeignKey('organizations.id'), | ||
nullable=False) | ||
organization = relationship('Organizations', | ||
back_populates='users') | ||
|
||
|
||
class Organizations(Base): | ||
__tablename__ = 'organizations' | ||
id = Column(Integer, primary_key=True, autoincrement=True) | ||
name = Column(String(128)) | ||
users = relationship('Users', back_populates='organization') | ||
|
||
|
||
class Nodes(CommonColumns): | ||
__tablename__ = 'nodes' | ||
id = Column(Integer, primary_key=True, autoincrement=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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from eve_sqlalchemy.config import DomainConfig, ResourceConfig | ||
from eve_sqlalchemy.examples.viewonly.domain import Users, Organizations, Nodes | ||
|
||
DEBUG = True | ||
SQLALCHEMY_DATABASE_URI = 'sqlite://' | ||
SQLALCHEMY_TRACK_MODIFICATIONS = False | ||
RESOURCE_METHODS = ['GET', 'POST'] | ||
ITEM_METHODS = ['GET', 'PATCH'] | ||
|
||
# The following two lines will output the SQL statements executed by | ||
# SQLAlchemy. This is useful while debugging and in development, but is turned | ||
# off by default. | ||
# -------- | ||
# SQLALCHEMY_ECHO = True | ||
# SQLALCHEMY_RECORD_QUERIES = True | ||
|
||
# The default schema is generated using DomainConfig: | ||
DOMAIN = DomainConfig({ | ||
'users': ResourceConfig(Users), | ||
'organizations': ResourceConfig(Organizations), | ||
'nodes': ResourceConfig(Nodes), | ||
}).render() |