Simple admin web interface with CRUD operation for docker images. You can delete and view it. In example uses default crud templates from ps_crud module.
$ cd examples/docker_crud/
$ pip install -e .
$ pserve development.ini --reload
and goto http://localhost:6543/admin/
pyramid_sacrud
copes with the resource technology, especially those who
adhere to the principle of Pyramid traversal
.
In this example, we create resources, associate them with the own views, but
inherit the templates from pyramid_sacrud
and ps_crud.
Also entry point for all resources from pyramid_sacrud
settings is
GroupResource
.
When you pass the Docker
in pyramid_sacrud
settings, it automatically
derives from GroupResource
.
Docker
is a base resource and it not displayed in the Web interface,
therefore it doesn’t require binding to the view.
class Docker(object):
breadcrumb = True
def __init__(self):
self.cli = docker.Client(base_url='unix://var/run/docker.sock')
@property
def verbose_name(self):
return self.__class__.__name__ + "'s"
@property
def __name__(self):
return self.verbose_name
Image
represent a list of all docker images in Web interface.
class Image(Docker):
def _get_id(self, row):
return row['Id']
@property
def _columns(self):
class Column:
def __init__(self, name, value):
self.name = name
self.value = value
def value(self, row):
return self.value(row)
return (
Column("id", lambda x: x["Id"][:10]),
Column("created",
lambda x: time.strftime("%D %H:%M",
time.localtime(
int(x["Created"])
))),
Column("tag", lambda x: x["RepoTags"][0])
)
def __getitem__(self, name):
if name == 'mass_action':
return self.ps_crud["crud"]["mass_action"]
elif name == 'update':
return self.ps_crud["crud"]["update"]
@property
def ps_crud(self):
mass_action = MassAction()
mass_action.__parent__ = self
update = UpdateFactory()
update.__parent__ = self
return {
"get_id": self._get_id,
"columns": self._columns,
"crud": {
"mass_action": mass_action,
"update": update,
}
}
@property
def all(self):
return self.cli.images()
It uses view:
def admin_docker_list_view(context, request):
"""Show list of docker images."""
return {
'paginator': Page(
context.all,
url_maker=lambda p: request.path_url + "?page=%s" % p,
page=int(request.params.get('page', 1)),
items_per_page=6
)
}
With template ps_crud/list.jinja2
from extension module ps_crud
.
@view_config(
context=Image,
renderer='ps_crud/list.jinja2',
route_name=PYRAMID_SACRUD_VIEW
)
def admin_docker_list_view(context, request):
"""Show list of docker images."""
UpdateFactory
designed to reflect a particular image.
class UpdateFactory(object):
__name__ = "update"
def __getitem__(self, name):
return self.Update(name, self)
def __call__(self, row):
return self[row["Id"]]
class Update(Docker):
__name__ = None
def get_image(self, img_id):
for img in self.cli.images():
if img['Id'] == img_id:
return img
def __init__(self, name, parent):
Docker.__init__(self)
self.__name__ = name
self.__parent__ = parent
self.img = self.get_image(name)
self.ps_crud = parent.__parent__.ps_crud
def admin_docker_update_view(context, request):
from pprint import pformat
return {'data': '<pre>' + pformat(context.img) + '</pre>'}
{% extends "sacrud/base.jinja2" %}
{% block menu_logo %}
<img src="/static/docker.png" width="300px" />
{% endblock %}
{% extends "sacrud/redefineme.jinja2" %}
{% block title %} - {{ _(context.verbose_name) }}{% endblock %}
{% block body %}
<div id="grid_view"></div>
<h4>{{ _(context.verbose_name) }}</h4>
<div class="bordered highlight hoverable z-depth-1">
{{ data|safe }}
</div>
{% endblock %}