Add grid, CRUD views for Setting model.
				
					
				
			This required some tweaks to the base CRUD view etc.
This commit is contained in:
		
							parent
							
								
									86507cf660
								
							
						
					
					
						commit
						cc2c2573e5
					
				
					 5 changed files with 205 additions and 32 deletions
				
			
		|  | @ -1,9 +1,8 @@ | |||
| #!/usr/bin/env python | ||||
| # -*- coding: utf-8  -*- | ||||
| # -*- coding: utf-8 -*- | ||||
| ################################################################################ | ||||
| # | ||||
| #  Rattail -- Retail Software Framework | ||||
| #  Copyright © 2010-2012 Lance Edgar | ||||
| #  Copyright © 2010-2014 Lance Edgar | ||||
| # | ||||
| #  This file is part of Rattail. | ||||
| # | ||||
|  | @ -21,13 +20,21 @@ | |||
| #  along with Rattail.  If not, see <http://www.gnu.org/licenses/>. | ||||
| # | ||||
| ################################################################################ | ||||
| 
 | ||||
| """ | ||||
| CRUD View | ||||
| """ | ||||
| 
 | ||||
| from __future__ import unicode_literals | ||||
| 
 | ||||
| try: | ||||
|     from sqlalchemy.inspection import inspect | ||||
| except ImportError: | ||||
|     inspect = None | ||||
|     from sqlalchemy.orm import class_mapper | ||||
| 
 | ||||
| from pyramid.httpexceptions import HTTPFound, HTTPNotFound | ||||
| 
 | ||||
| from .core import View | ||||
| from pyramid.httpexceptions import HTTPFound | ||||
| 
 | ||||
| from ..forms import AlchemyForm | ||||
| from formalchemy import FieldSet | ||||
|  | @ -173,33 +180,35 @@ class CrudView(View): | |||
|     def create(self): | ||||
|         return self.crud(self.mapped_class) | ||||
| 
 | ||||
|     def get_model_from_request(self): | ||||
|         if inspect: | ||||
|             mapper = inspect(self.mapped_class) | ||||
|         else: | ||||
|             mapper = class_mapper(self.mapped_class) | ||||
|         assert len(mapper.primary_key) == 1 | ||||
|         key = self.request.matchdict[mapper.primary_key[0].key] | ||||
|         return self.get_model(key) | ||||
| 
 | ||||
|     def get_model(self, key): | ||||
|         model = Session.query(self.mapped_class).get(key) | ||||
|         return model | ||||
| 
 | ||||
|     def read(self): | ||||
|         key = self.request.matchdict['uuid'] | ||||
|         model = self.get_model(key) | ||||
|         model = self.get_model_from_request() | ||||
|         if not model: | ||||
|             return HTTPFound(location=self.home_url) | ||||
|             return HTTPNotFound() | ||||
|         return self.crud(model, readonly=True) | ||||
| 
 | ||||
|     def update(self): | ||||
|         uuid = self.request.matchdict['uuid'] | ||||
|         model = Session.query(self.mapped_class).get(uuid) if uuid else None | ||||
|         assert model | ||||
|         model = self.get_model_from_request() | ||||
|         if not model: | ||||
|             return HTTPNotFound() | ||||
|         return self.crud(model) | ||||
| 
 | ||||
|     def pre_delete(self, model): | ||||
|         pass | ||||
| 
 | ||||
|     def post_delete(self, model): | ||||
|         pass | ||||
| 
 | ||||
|     def delete(self): | ||||
|         uuid = self.request.matchdict['uuid'] | ||||
|         model = Session.query(self.mapped_class).get(uuid) if uuid else None | ||||
|         assert model | ||||
|         model = self.get_model_from_request() | ||||
|         if not model: | ||||
|             return HTTPNotFound() | ||||
|         result = self.pre_delete(model) | ||||
|         if result: | ||||
|             return result | ||||
|  | @ -208,3 +217,9 @@ class CrudView(View): | |||
|         self.post_delete(model) | ||||
|         self.flash_delete(model) | ||||
|         return HTTPFound(location=self.home_url) | ||||
| 
 | ||||
|     def pre_delete(self, model): | ||||
|         pass | ||||
| 
 | ||||
|     def post_delete(self, model): | ||||
|         pass | ||||
|  |  | |||
							
								
								
									
										122
									
								
								tailbone/views/settings.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										122
									
								
								tailbone/views/settings.py
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,122 @@ | |||
| # -*- coding: utf-8 -*- | ||||
| ################################################################################ | ||||
| # | ||||
| #  Rattail -- Retail Software Framework | ||||
| #  Copyright © 2010-2014 Lance Edgar | ||||
| # | ||||
| #  This file is part of Rattail. | ||||
| # | ||||
| #  Rattail is free software: you can redistribute it and/or modify it under the | ||||
| #  terms of the GNU Affero General Public License as published by the Free | ||||
| #  Software Foundation, either version 3 of the License, or (at your option) | ||||
| #  any later version. | ||||
| # | ||||
| #  Rattail is distributed in the hope that it will be useful, but WITHOUT ANY | ||||
| #  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||||
| #  FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for | ||||
| #  more details. | ||||
| # | ||||
| #  You should have received a copy of the GNU Affero General Public License | ||||
| #  along with Rattail.  If not, see <http://www.gnu.org/licenses/>. | ||||
| # | ||||
| ################################################################################ | ||||
| """ | ||||
| Settings Views | ||||
| """ | ||||
| 
 | ||||
| from __future__ import unicode_literals | ||||
| 
 | ||||
| from rattail.db import model | ||||
| 
 | ||||
| from tailbone.views import SearchableAlchemyGridView, CrudView | ||||
| 
 | ||||
| 
 | ||||
| class SettingsGrid(SearchableAlchemyGridView): | ||||
| 
 | ||||
|     mapped_class = model.Setting | ||||
|     config_prefix = 'settings' | ||||
|     sort = 'name' | ||||
| 
 | ||||
|     def filter_map(self): | ||||
|         return self.make_filter_map(ilike=['name', 'value']) | ||||
| 
 | ||||
|     def filter_config(self): | ||||
|         return self.make_filter_config( | ||||
|             include_filter_name=True, | ||||
|             filter_type_name='lk', | ||||
|             include_filter_value=True, | ||||
|             filter_type_value='lk') | ||||
| 
 | ||||
|     def sort_map(self): | ||||
|         return self.make_sort_map('name', 'value') | ||||
| 
 | ||||
|     def grid(self): | ||||
|         g = self.make_grid() | ||||
|         g.configure( | ||||
|             include=[ | ||||
|                 g.name, | ||||
|                 g.value, | ||||
|                 ], | ||||
|             readonly=True) | ||||
|         if self.request.has_perm('settings.view'): | ||||
|             g.viewable = True | ||||
|             g.view_route_name = 'settings.view' | ||||
|         if self.request.has_perm('settings.edit'): | ||||
|             g.editable = True | ||||
|             g.edit_route_name = 'settings.edit' | ||||
|         if self.request.has_perm('settings.delete'): | ||||
|             g.deletable = True | ||||
|             g.delete_route_name = 'settings.delete' | ||||
|         return g | ||||
| 
 | ||||
| 
 | ||||
| class SettingCrud(CrudView): | ||||
| 
 | ||||
|     mapped_class = model.Setting | ||||
|     home_route = 'settings' | ||||
| 
 | ||||
|     def fieldset(self, model): | ||||
|         fs = self.make_fieldset(model) | ||||
|         fs.configure( | ||||
|             include=[ | ||||
|                 fs.name, | ||||
|                 fs.value, | ||||
|                 ]) | ||||
|         if self.updating: | ||||
|             fs.name.set(readonly=True) | ||||
|         return fs | ||||
|          | ||||
| 
 | ||||
| def add_routes(config): | ||||
|     config.add_route('settings',                '/settings') | ||||
|     config.add_route('settings.create',         '/settings/new') | ||||
|     config.add_route('settings.view',           '/settings/{name}') | ||||
|     config.add_route('settings.edit',           '/settings/{name}/edit') | ||||
|     config.add_route('settings.delete',         '/settings/{name}/delete') | ||||
| 
 | ||||
| 
 | ||||
| def includeme(config): | ||||
|     add_routes(config) | ||||
| 
 | ||||
|     # Grid | ||||
|     config.add_view(SettingsGrid, | ||||
|                     route_name='settings', | ||||
|                     renderer='/settings/index.mako', | ||||
|                     permission='settings.list') | ||||
| 
 | ||||
|     # CRUD | ||||
|     config.add_view(SettingCrud, attr='create', | ||||
|                     route_name='settings.create', | ||||
|                     renderer='/settings/crud.mako', | ||||
|                     permission='settings.create') | ||||
|     config.add_view(SettingCrud, attr='read', | ||||
|                     route_name='settings.view', | ||||
|                     renderer='/settings/crud.mako', | ||||
|                     permission='settings.view') | ||||
|     config.add_view(SettingCrud, attr='update', | ||||
|                     route_name='settings.edit', | ||||
|                     renderer='/settings/crud.mako', | ||||
|                     permission='settings.edit') | ||||
|     config.add_view(SettingCrud, attr='delete', | ||||
|                     route_name='settings.delete', | ||||
|                     permission='settings.delete') | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Lance Edgar
						Lance Edgar