-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config: setup RootFactory and traversal
First of all get rid of the "home" view, which gets replaced by the top node in the RootFactory. As a side effect, that means we need to refer to "/" directly in the login and logout views, we can't use request.route_url("home") anymore as I have not figured out a way to name the top node of the RootFactory, and I don't think you can. For now add a single view handler for all Resource rather than a specific one for UserResource etc. Perhaps that come later, but added UserResource to the code as an example. This view handler always produces json. The scope= argument of Model.query hasn't landed in Samba yet, but I'll see it gets added, as it's clearly needed to override the default. Closes #4
- Loading branch information
Showing
7 changed files
with
64 additions
and
3 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,5 @@ | ||
from .base import Resource | ||
from .root import RootFactory | ||
from .user import UserResource | ||
|
||
__all__ = ("Resource", "RootFactory", "UserResource") |
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,15 @@ | ||
from ldb import SCOPE_ONELEVEL | ||
from samba.netcmd.domain.models import Model | ||
|
||
|
||
class Resource(dict): | ||
model = Model | ||
|
||
def __init__(self, request, obj): | ||
if request.samdb: | ||
qs = self.model.query(request.samdb, base_dn=obj.dn, scope=SCOPE_ONELEVEL) | ||
data = {model.name: model.as_dict() for model in qs if model} | ||
else: | ||
data = {} | ||
|
||
super().__init__(**data) |
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,17 @@ | ||
from ldb import SCOPE_ONELEVEL | ||
from samba.netcmd.domain.models import Model | ||
|
||
from .base import Resource | ||
|
||
|
||
class RootFactory(dict): | ||
model = Model | ||
|
||
def __init__(self, request): | ||
if request.samdb: | ||
qs = self.model.query(request.samdb, scope=SCOPE_ONELEVEL) | ||
data = {obj.name: Resource(request, obj) for obj in qs if obj} | ||
else: | ||
data = {} | ||
|
||
super().__init__(**data) |
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,7 @@ | ||
from samba.netcmd.domain.models import User | ||
|
||
from .base import Resource | ||
|
||
|
||
class UserResource(Resource): | ||
model = User |
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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
from sambal.resources import RootFactory | ||
|
||
|
||
def includeme(config): | ||
config.add_static_view("static", "static", cache_max_age=3600) | ||
config.add_route("home", "/") | ||
config.set_root_factory(RootFactory) | ||
config.add_route("login", "/login/") | ||
config.add_route("logout", "/logout/") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from pyramid.view import view_config | ||
|
||
from sambal.resources import Resource, RootFactory | ||
|
||
|
||
@view_config(context=Resource, permission="read", renderer="json") | ||
@view_config(context=RootFactory, permission="read", renderer="json") | ||
def resource_view(context, request): | ||
"""Temporary view to produce JSON for every node. | ||
For this to work a custom JSON encoder is used to deal with the | ||
various objects that aren't JSON encode-able out of the box. | ||
""" | ||
return context |