-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mucking around with resource based CRUD, not done, working in progress
- Loading branch information
1 parent
a1210b4
commit 0cd0916
Showing
17 changed files
with
341 additions
and
77 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 |
---|---|---|
|
@@ -10,7 +10,7 @@ pyramid.includes = | |
pyramid_debugtoolbar | ||
pyramid_tm | ||
|
||
sqlalchemy.url = sqlite:///%(here)s/pyconca.db | ||
sqlalchemy.url = mysql://pyconca:[email protected]/pyconca?charset=utf8 | ||
|
||
[server:main] | ||
use = egg:waitress#main | ||
|
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
Empty file.
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,25 @@ | ||
from pyconca.models import DBSession | ||
|
||
class BaseDao(object): | ||
|
||
def __init__(self, model): | ||
self.model = model | ||
|
||
def get(self, id): | ||
return self._query().filter_by(id=id).first() | ||
|
||
def index(self): | ||
return self._query().all() | ||
|
||
def delete(self, instance): | ||
DBSession.delete(instance) | ||
|
||
def create(self): | ||
return self.model() | ||
|
||
def save(self, instance): | ||
DBSession.add(instance) | ||
DBSession.flush() | ||
|
||
def _query(self): | ||
return DBSession.query(self.model) |
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 pyconca.dao.base_dao import BaseDao | ||
from pyconca.models import User | ||
|
||
class UserDao(BaseDao): | ||
|
||
def __init__(self): | ||
BaseDao.__init__(self, 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,28 +1,25 @@ | ||
from sqlalchemy import ( | ||
Column, | ||
Integer, | ||
Text, | ||
) | ||
|
||
from sqlalchemy import Column | ||
from sqlalchemy import Integer | ||
from sqlalchemy import String | ||
from sqlalchemy.ext.declarative import declarative_base | ||
|
||
from sqlalchemy.orm import ( | ||
scoped_session, | ||
sessionmaker, | ||
) | ||
from sqlalchemy.orm import scoped_session | ||
from sqlalchemy.orm import sessionmaker | ||
|
||
from zope.sqlalchemy import ZopeTransactionExtension | ||
|
||
DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension())) | ||
Base = declarative_base() | ||
|
||
class MyModel(Base): | ||
__tablename__ = 'models' | ||
id = Column(Integer, primary_key=True) | ||
name = Column(Text, unique=True) | ||
value = Column(Integer) | ||
class User(Base): | ||
__tablename__ = 'user' | ||
__table_args__ = { | ||
'mysql_engine': 'InnoDB', | ||
'mysql_charset': 'utf8' | ||
} | ||
|
||
def __init__(self, name, value): | ||
self.name = name | ||
self.value = value | ||
id = Column(Integer, primary_key=True) | ||
username = Column(String(length=30), unique=True, nullable=False) | ||
first_name = Column(String(length=100)) | ||
last_name = Column(String(length=100)) | ||
email = Column(String(length=100)) | ||
|
Empty file.
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,88 @@ | ||
from pyramid.httpexceptions import HTTPFound | ||
from pyramid.renderers import get_renderer | ||
from pyramid.url import route_url | ||
|
||
import logging | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
def site_layout(): | ||
renderer = get_renderer('pyconca:templates/layout.pt') | ||
layout = renderer.implementation().macros['layout'] | ||
return layout | ||
|
||
class BaseView(object): | ||
|
||
def __init__(self, request): | ||
self.request = request | ||
self._configure() | ||
|
||
def index(self): | ||
response_ = self._build_response() | ||
models = self.dao.index() | ||
self._wrap_model_list(models) | ||
response_[self.name + '_list'] = models | ||
return response_ | ||
|
||
def show(self): | ||
id = self.request.matchdict['id'] | ||
response_ = self._build_response() | ||
model = self.dao.get(id) | ||
self._wrap_model(model) | ||
response_[self.name] = model | ||
return response_ | ||
|
||
def delete(self): | ||
id = self.request.matchdict['id'] | ||
model = self.dao.get(id) | ||
if self.is_post: | ||
self.dao.delete(model) | ||
index_url = self._route_url('index') | ||
return HTTPFound(location=index_url) | ||
|
||
def update(self): | ||
id = self.request.matchdict['id'] | ||
model = self.dao.get(id) | ||
return self._save(model, is_create=False) | ||
|
||
def create(self): | ||
model = self.dao.create() | ||
return self._save(model, is_create=True) | ||
|
||
def _save(self, model, is_create): | ||
if self.is_post: | ||
self._populate(model, is_create) | ||
self.dao.save(model) | ||
show_url = self._route_url('show', id=model.id) | ||
return HTTPFound(location=show_url) | ||
response_ = self._build_response() | ||
self._wrap_model(model, is_create) | ||
response_[self.name] = model | ||
return response_ | ||
|
||
def _wrap_model_list(self, models): | ||
for model in models: | ||
self._wrap_model(model) | ||
|
||
def _wrap_model(self, model, is_create=False): | ||
if is_create: | ||
model.save_url = self._route_url('create') | ||
else: | ||
model.show_url = self._route_url('show', id=model.id) | ||
model.update_url = self._route_url('update', id=model.id) | ||
model.delete_url = self._route_url('delete', id=model.id) | ||
model.save_url = model.update_url | ||
|
||
def _build_response(self): | ||
return { | ||
'layout': site_layout(), | ||
'index_url': self._route_url('index'), | ||
'create_url': self._route_url('create'), | ||
} | ||
|
||
def _route_url(self, action, **kwargs): | ||
return route_url(self.name + '_' + action, self.request, **kwargs) | ||
|
||
@property | ||
def is_post(self): | ||
return self.request.environ['REQUEST_METHOD'] == 'POST' |
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 pyconca.dao.user_dao import UserDao | ||
from pyconca.resources.base_view import BaseView | ||
|
||
class UserView(BaseView): | ||
|
||
def _configure(self): | ||
self.name = 'user' | ||
self.dao = UserDao() | ||
|
||
def _populate(self, user, is_create): | ||
user.first_name = self.request.params['first_name'] | ||
user.last_name = self.request.params['last_name'] | ||
user.username = self.request.params['username'] | ||
user.email = self.request.params['email'] |
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
Empty file.
Empty file.
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,52 @@ | ||
<div metal:use-macro="layout"> | ||
<div metal:fill-slot="content"> | ||
|
||
<div class="row-fluid"> | ||
<div class="span12"> | ||
<br> | ||
<h1> | ||
<a tal:attributes="href index_url">users</a> / | ||
<span tal:condition="user.id" tal:replace="user.username"> | ||
<span tal:condition="not user.id">create</span> | ||
</h1> | ||
<hr> | ||
<form class="form-horizontal" action="" method=""> | ||
|
||
<div class="control-group"> | ||
<label class="control-label" for="first_name">First Name</label> | ||
<div class="controls"> | ||
<input type="text" maxlength="100" name="first_name" value="${user.first_name}"> | ||
</div> | ||
</div> | ||
|
||
<div class="control-group"> | ||
<label class="control-label" for="last_name">Last Name</label> | ||
<div class="controls"> | ||
<input type="text" maxlength="100" name="last_name" value="${user.last_name}"> | ||
</div> | ||
</div> | ||
|
||
<div class="control-group"> | ||
<label class="control-label" for="username">Username</label> | ||
<div class="controls"> | ||
<input type="text" maxlength="30" name="username" value="${user.username}"> | ||
</div> | ||
</div> | ||
|
||
<div class="control-group"> | ||
<label class="control-label" for="email">Email Address</label> | ||
<div class="controls"> | ||
<input type="text" maxlength="100" name="email" value="${user.email}"> | ||
</div> | ||
</div> | ||
|
||
<input class="btn btn-primary" type="submit" value="Save" | ||
onclick="this.form.action='${user.save_url}';this.form.method='POST';"/> | ||
<input class="btn" type="submit" value="Cancel" | ||
onclick="this.form.action='${index_url}';this.form.method='GET';"/> | ||
</form> | ||
</div> | ||
</div> | ||
|
||
</div><!-- content slot --> | ||
</div><!-- layout template --> |
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,49 @@ | ||
<div metal:use-macro="layout"> | ||
<div metal:fill-slot="content"> | ||
|
||
<div class="row-fluid"> | ||
<div class="span9"> | ||
<br> | ||
<h1>users</h1> | ||
<br> | ||
</div> | ||
<div class="span3"> | ||
<br> | ||
<form action="" method=""> | ||
<input class="btn btn-primary pull-right" type="submit" value="New User" | ||
onclick="this.form.action='${create_url}';this.form.method='GET';"/> | ||
</form> | ||
</div> | ||
<hr> | ||
</div> | ||
|
||
<div class="row-fluid"> | ||
<div class="span12"> | ||
<table class="table table-striped table-bordered"> | ||
<tr> | ||
<th>Name</th> | ||
<th>Username</th> | ||
<th>Email</th> | ||
</tr> | ||
<div tal:repeat="user user_list"> | ||
<tr> | ||
<td> | ||
<a tal:attributes="href user.show_url"> | ||
<span tal:replace="user.first_name"> | ||
<span tal:replace="user.last_name"> | ||
</a> | ||
</td> | ||
<td> | ||
<span tal:replace="user.username"> | ||
</td> | ||
<td> | ||
<span tal:replace="user.email"> | ||
</td> | ||
</tr> | ||
</div> | ||
</table> | ||
</div> | ||
</div> | ||
|
||
</div><!-- content slot --> | ||
</div><!-- layout template --> |
Oops, something went wrong.