Skip to content

Commit

Permalink
Begin example with docker CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
uralbash committed Feb 15, 2016
1 parent 9a35170 commit 79321e4
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.3.3 (2016-??-??)
==================

- ??

0.3.2 (2016-02-07)
==================

Expand Down
1 change: 1 addition & 0 deletions examples/custom_resource/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Usage
-----

Simple admin web interface with custom resource 'Bear'.
It uses custom template ``bear.jinja2`` to display resource ``Bear``.

.. code-block:: bash
Expand Down
13 changes: 13 additions & 0 deletions examples/docker_crud/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Usage
-----

Simple admin web interface with CRUD operation for docker container
and images. You can delete create and edit it. In example uses default
crud templates from ``pyramid_sacrud/templates/sacrud/crud/``.

.. code-block:: bash
$ cd examples/docker-crud/
$ python main.py
and goto http://localhost:6543/admin/
70 changes: 70 additions & 0 deletions examples/docker_crud/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from zope.interface import implementer
from docker import Client

from pyramid.view import view_config
from pyramid.config import Configurator
from pyramid_sacrud import PYRAMID_SACRUD_VIEW
from pyramid_sacrud.interfaces import ISacrudResource
from paginate import Page


@implementer(ISacrudResource)
class Docker(object):

breadcrumb = True

def __init__(self):
self.cli = 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


class Container(Docker):

def get_id(self, row):
return row['Id']

@property
def all(self):
return self.cli.images()


@view_config(
context=Docker,
renderer='sacrud/crud/list.jinja2',
route_name=PYRAMID_SACRUD_VIEW
)
def admin_docker_list_view(context, request):
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
)
}

if __name__ == '__main__':
config = Configurator()

# Setting up pyramid_sacrud
config.include('pyramid_sacrud', route_prefix='admin')
settings = config.get_settings()
settings['pyramid_sacrud.models'] = (
('Docker', [Container(), ]),
)

# Make app
config.scan('.')
app = config.make_wsgi_app()

# Start app
from wsgiref.simple_server import make_server
server = make_server('0.0.0.0', 6543, app)
server.serve_forever()
3 changes: 3 additions & 0 deletions examples/docker_crud/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
docker-py
pyramid
pyramid_sacrud
31 changes: 17 additions & 14 deletions pyramid_sacrud/templates/sacrud/crud/list.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@
{% block body %}
<div id="grid_view"></div>
<h4>{{ _(context.verbose_name) }}</h4>
<a class="create-button btn waves-effect waves-light"
href="/{{ request.sacrud_prefix }}/{{ request.resource_path(context.get_create_resource()) }}">
<i class="material-icons left">add_circle_outline</i>
{{ _ps('Create') }}
</a>
{% if context.get_create_resource %}
<a class="create-button btn waves-effect waves-light"
href="/{{ request.sacrud_prefix }}/{{ request.resource_path(context.get_create_resource()) }}">
<i class="material-icons left">add_circle_outline</i>
{{ _ps('Create') }}
</a>
{% endif %}
<a class="delete-button waves-effect waves-light btn disabled">
<i class="material-icons left">remove_circle_outline</i>
{{ _ps('Delete') }}
</a>

<div class="container"><br/></div>
<form id="sacrud-form" method="post"
action="/{{ request.sacrud_prefix }}/{{ request.resource_path(context.get_mass_action_resource()['delete']) }}">
<form id="sacrud-form" method="post">
<table class="bordered highlight hoverable z-depth-1">
{% if paginator.items.__len__() > 0 %}
<thead>
Expand All @@ -44,16 +45,18 @@
</thead>
<tbody>
{% for row in paginator %}
{% set pk=context.get_pk(row) %}
<tr id="{{ context.table_name }}_{{ pk }}">
{% set id=context.get_id(row) %}
<tr id="{{ context.table_name }}_{{ id }}">
<td width="1">
<input id="{{ pk }}" type="checkbox" name="selected_item" value="{{ pk }}" />
<label for="{{ pk }}"></label>
<input id="{{ id }}" type="checkbox" name="selected_item" value="{{ id }}" />
<label for="{{ id }}"></label>
</td>
<td width="1">
<a href="/{{ request.sacrud_prefix }}/{{ request.resource_path(context.get_update_resource(row)) }}">
<i class="material-icons left">edit</i>
</a>
{% if context.get_update_resource %}
<a href="/{{ request.sacrud_prefix }}/{{ request.resource_path(context.get_update_resource(row)) }}">
<i class="material-icons left">edit</i>
</a>
{% endif %}
</td>
{% for col in context.visible_columns %}
<td>{{ col.value(row)|string|truncate(200) }}</td>
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def run_tests(self):

setup(
name='pyramid_sacrud',
version="0.3.2",
version="0.3.3.dev0",
url='http://github.com/sacrud/pyramid_sacrud/',
author='Svintsov Dmitry',
author_email='sacrud@uralbash.ru',
Expand Down

0 comments on commit 79321e4

Please sign in to comment.