Skip to content

Commit

Permalink
[update]
Browse files Browse the repository at this point in the history
  • Loading branch information
boke0 committed Mar 8, 2021
1 parent d9dd92b commit 8c0edb4
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 98 deletions.
13 changes: 8 additions & 5 deletions mitama/app/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import inspect
import re

from .http import Request, Response


class RoutingError(Exception):
pass
Expand Down Expand Up @@ -87,7 +85,7 @@ def match(self, request):
for route in self.routes:
request.subpath = path
result = route.match(request)
if result != False:
if result is not False:
request, result, method = result

def get_response_handler(result, method):
Expand All @@ -104,7 +102,10 @@ def handle(request):
def result(request):
return inst(request, method)

if i >= len(self.middlewares) or len(self.middlewares) == 0:
if (
i >= len(self.middlewares) or
len(self.middlewares) == 0
):
if callable(result):
return result(request)
else:
Expand All @@ -113,7 +114,9 @@ def result(request):
)
else:
if hasattr(request, "app"):
middleware = self.middlewares[i](request.app)
middleware = self.middlewares[i](
request.app
)
else:
middleware = self.middlewares[i]()
i += 1
Expand Down
21 changes: 14 additions & 7 deletions mitama/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from .driver.sqlite3 import get_test_engine
from .model import Model


class _QueryProperty:
def __init__(self, db):
self.db = db
Expand All @@ -51,7 +52,11 @@ def test(cls):
def set_engine(cls, engine):
cls.engine = engine
cls.metadata = MetaData(cls.engine)
cls.Model = declarative_base(cls=Model, name="Model", metadata=cls.metadata)
cls.Model = declarative_base(
cls=Model,
name="Model",
metadata=cls.metadata
)

@classmethod
def start_session(cls):
Expand Down Expand Up @@ -98,10 +103,12 @@ def __init__(self, model=None, metadata=None, query_class=Query):
self.Model = self.make_declarative_base(model, metadata)

def make_declarative_base(self, model=None, metadata=None):
if model == None:
if model is None:
model = self.manager.Model
if not isinstance(model, DeclarativeMeta):
model = declarative_base(cls=model, name="Model", metadata=metadata)
model = declarative_base(
cls=model, name="Model", metadata=metadata
)
if metadata is not None and model.metadata is not metadata:
model.metadata = metadata
else:
Expand Down Expand Up @@ -135,10 +142,10 @@ class BaseDatabase(_Database):

def __init__(self, prefix=None, model=None, metadata=None, query_class=Query):
super().__init__(
model = model,
metadata = metadata,
query_class = query_class
model=model,
metadata=metadata,
query_class=query_class
)
if prefix == None:
if prefix is None:
prefix = _inspect.getmodule(self.__class__).__package__
self.Model.prefix = prefix
26 changes: 16 additions & 10 deletions mitama/db/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@

import uuid

from sqlalchemy.ext.declarative import declarative_base, declared_attr
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.orm import ColumnProperty, class_mapper
from sqlalchemy.types import TypeDecorator

from mitama._extra import _classproperty, tosnake

from .types import Column, Group, Integer, LargeBinary, Node, String
from .types import Column, Integer, String
from .event import Event

def UUID(prefix = None):

def UUID(prefix=None):
def genUUID():
s = str(uuid.uuid4())
if prefix is not None:
s = prefix + "-" + s
return s
return genUUID


class Model:
prefix = None
_id = Column(String(64), default=UUID(), primary_key=True, nullable=False)
Expand Down Expand Up @@ -56,13 +58,13 @@ class Type(TypeDecorator):
impl = Integer

def process_bind_param(self, value, dialect):
if value == None:
if value is None:
return None
else:
return value._id

def process_result_value(self, value, dialect):
if value == None:
if value is None:
return None
else:
user = cls.retrieve(value)
Expand All @@ -72,7 +74,11 @@ def process_result_value(self, value, dialect):

@declared_attr
def __tablename__(cls):
return ("" if cls.prefix is None else cls.prefix + "_") + tosnake(cls.__name__)
return (
""
if cls.prefix is None
else cls.prefix + "_"
) + tosnake(cls.__name__)

def create(self):
self.query.session.add(self)
Expand Down Expand Up @@ -105,15 +111,15 @@ def listen(cls, evt):
setattr(cls, evt, Event())

@classmethod
def list(cls, cond=None):
if cond != None:
return cls.query.filter(cond).all()
def list(cls, *args):
if len(args) > 0:
return cls.query.filter(args).all()
else:
return cls.query.filter().all()

@classmethod
def retrieve(cls, id=None, **kwargs):
if id != None:
if id is not None:
node = cls.query.filter(cls._id == id).one()
elif len(kwargs) > 0:
q = cls.query
Expand Down
43 changes: 31 additions & 12 deletions mitama/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@
* sqlalchemy用にUser型とGroup型を作って、↓のクラスをそのまま使ってDB呼び出しできるようにしたい
"""

from sqlalchemy.ext.declarative import declarative_base, declared_attr
from sqlalchemy.schema import UniqueConstraint
from sqlalchemy import event

from mitama.db import BaseDatabase, func, ForeignKey, relationship, Table, backref
from mitama.db.types import Column, Group, Integer, LargeBinary
from mitama.db.types import Node as NodeType
from mitama.db.types import String
from mitama.noimage import load_noimage_group, load_noimage_user
from mitama._extra import _classproperty

from .core_db import db
from .nodes import User, Group, Node, UserGroup, UserInvite, AuthorizationError, Role, InnerRole, PushSubscription
from .nodes import (
User,
Group,
Node,
UserGroup,
UserInvite,
AuthorizationError,
Role,
InnerRole,
PushSubscription
)
from .permissions import permission, inner_permission


Permission = permission(db, [
{
"name": "権限管理",
Expand Down Expand Up @@ -78,7 +78,26 @@
}
])


def is_admin(node):
return Permission.is_accepted('admin', node)


db.create_all()


__all__ = [
User,
Group,
Node,
UserGroup,
UserInvite,
AuthorizationError,
Role,
InnerRole,
Permission,
InnerPermission,
PushSubscription,
permission,
inner_permission,
]
2 changes: 2 additions & 0 deletions mitama/models/core_db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from mitama.db import BaseDatabase


class Database(BaseDatabase):
pass


db = Database(prefix='mitama')
Loading

0 comments on commit 8c0edb4

Please sign in to comment.