Skip to content

Commit

Permalink
feat: add new API routes for models and entities
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Apr 8, 2021
1 parent 73a8063 commit 23d3e9e
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/appier_extras/parts/admin/part.py
Expand Up @@ -261,6 +261,7 @@ def routes(self):
(("GET",), "/admin/accounts/<str:username>/mail", self.mail_account),
(("GET",), "/admin/accounts/<str:username>/avatar", self.avatar_account),
(("GET",), "/admin/models", self.list_models),
(("GET",), "/admin/models.json", self.list_models_json, None, True),
(("GET",), "/admin/models/<str:model>.json", self.show_model_json, None, True),
(("GET",), "/admin/models/<str:model>.csv", self.show_model_csv),
(("GET",), "/admin/models/<str:model>", self.show_model),
Expand All @@ -269,6 +270,7 @@ def routes(self):
(("GET", "POST"), "/admin/models/<str:model>/view/<str:view>", self.view_model),
(("GET",), "/admin/models/<str:model>/new", self.new_entity),
(("POST",), "/admin/models/<str:model>", self.create_entity),
(("POST",), "/admin/models/<str:model>.json", self.create_entity_json),
(("GET",), "/admin/models/<str:model>/<str:_id>.json", self.show_entity_json, None, True),
(("GET",), "/admin/models/<str:model>/<str:_id>", self.show_entity),
(("GET",), "/admin/models/<str:model>/<str:_id>/edit", self.edit_entity),
Expand Down Expand Up @@ -303,10 +305,11 @@ def routes(self):
(("GET", "POST"), "/api/admin/oauth/login", self.oauth_login_api, None, True),
(("POST",), "/api/admin/database/reset", self.database_reset_api, None, True),
(("GET",), "/api/admin/accounts/me", self.me_account_api, None, True),
(("GET",), "/api/admin/models", self.list_models_api, None, True),
(("GET",), "/api/admin/sessions/me", self.show_session_me_api, None, True),
(("GET",), "/api/admin/sessions/<str:sid>", self.show_session_api, None, True),
(("GET",), "/api/admin/models", self.list_models_api, None, True),
(("GET",), "/api/admin/models/<str:model>", self.show_model_api, None, True),
(("POST",), "/api/admin/models/<str:model>", self.create_entity_api, None, True),
(("GET",), "/api/admin/models/<str:model>/<str:_id>", self.show_entity_api, None, True)
]

Expand Down Expand Up @@ -1519,6 +1522,15 @@ def list_models(self):
section = "admin"
)

@appier.ensure(token = "admin", context = "admin")
def list_models_json(self):
models = []
for _section, models_c in self.admin_part.models_d.items():
models_c = self._available(models_c)
for model_c in models_c:
models.append(dict(name = model_c._under()))
return models

@appier.ensure(token = "admin", context = "admin")
def show_model(self, model):
appier.ensure_login(self, token = "admin.models." + model)
Expand Down Expand Up @@ -1815,6 +1827,14 @@ def create_entity(self, model):
self.url_for("admin.show_model", model = model._under())
)

@appier.ensure(token = "admin", context = "admin")
def create_entity_json(self, model):
model = self.get_model(model)
entity = model.new(safe = False)
entity.save()
entity = entity.map()
return entity

@appier.ensure(token = "admin", context = "admin")
def show_entity(self, model, _id):
appier.ensure_login(self, token = "admin.models." + model)
Expand Down Expand Up @@ -2313,11 +2333,6 @@ def me_account_api(self):
account = account_c.from_session(map = True)
return account

@appier.ensure(context = "admin")
def list_models_api(self):
models = [1, 2, 3]
return models

@appier.ensure(context = "admin")
def show_session_me_api(self):
sid = self.session.sid
Expand All @@ -2330,10 +2345,18 @@ def show_session_api(self, sid):
session_s = self.request.session_c.get_s(sid)
return dict(session_s.items())

@appier.ensure(context = "admin")
def list_models_api(self):
return self.list_models_json()

@appier.ensure(context = "admin")
def show_model_api(self, model):
return self.show_model_json(model)

@appier.ensure(context = "admin")
def create_entity_api(self, model):
return self.create_entity_json(model)

@appier.ensure(context = "admin")
def show_entity_api(self, model, _id):
return self.show_entity_json(model, _id)
Expand Down

0 comments on commit 23d3e9e

Please sign in to comment.