Add very basic API views to expose customer, user tables
just so we can populate an "index grid table" in the UI, for now..
This commit is contained in:
		
							parent
							
								
									f43b6db427
								
							
						
					
					
						commit
						0c41395cfc
					
				
					 4 changed files with 198 additions and 0 deletions
				
			
		|  | @ -27,7 +27,10 @@ Tailbone Web API | |||
| from __future__ import unicode_literals, absolute_import | ||||
| 
 | ||||
| from .core import APIView, api | ||||
| from .master import APIMasterView | ||||
| 
 | ||||
| 
 | ||||
| def includeme(config): | ||||
|     config.include('tailbone.api.auth') | ||||
|     config.include('tailbone.api.customers') | ||||
|     config.include('tailbone.api.users') | ||||
|  |  | |||
							
								
								
									
										46
									
								
								tailbone/api/customers.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								tailbone/api/customers.py
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,46 @@ | |||
| # -*- coding: utf-8; -*- | ||||
| ################################################################################ | ||||
| # | ||||
| #  Rattail -- Retail Software Framework | ||||
| #  Copyright © 2010-2018 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 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 General Public License for more | ||||
| #  details. | ||||
| # | ||||
| #  You should have received a copy of the GNU General Public License along with | ||||
| #  Rattail.  If not, see <http://www.gnu.org/licenses/>. | ||||
| # | ||||
| ################################################################################ | ||||
| """ | ||||
| Tailbone Web API - Customer Views | ||||
| """ | ||||
| 
 | ||||
| from __future__ import unicode_literals, absolute_import | ||||
| 
 | ||||
| from rattail.db import model | ||||
| 
 | ||||
| from tailbone.api import APIMasterView | ||||
| 
 | ||||
| 
 | ||||
| class CustomerView(APIMasterView): | ||||
| 
 | ||||
|     model_class = model.Customer | ||||
| 
 | ||||
|     def normalize(self, customer): | ||||
|         return { | ||||
|             'id': customer.id, | ||||
|             'name': customer.name, | ||||
|         } | ||||
| 
 | ||||
| 
 | ||||
| def includeme(config): | ||||
|     CustomerView.defaults(config) | ||||
							
								
								
									
										101
									
								
								tailbone/api/master.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										101
									
								
								tailbone/api/master.py
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,101 @@ | |||
| # -*- coding: utf-8; -*- | ||||
| ################################################################################ | ||||
| # | ||||
| #  Rattail -- Retail Software Framework | ||||
| #  Copyright © 2010-2018 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 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 General Public License for more | ||||
| #  details. | ||||
| # | ||||
| #  You should have received a copy of the GNU General Public License along with | ||||
| #  Rattail.  If not, see <http://www.gnu.org/licenses/>. | ||||
| # | ||||
| ################################################################################ | ||||
| """ | ||||
| Tailbone Web API - Master View | ||||
| """ | ||||
| 
 | ||||
| from __future__ import unicode_literals, absolute_import | ||||
| 
 | ||||
| from tailbone.api import APIView, api | ||||
| from tailbone.db import Session | ||||
| 
 | ||||
| 
 | ||||
| class APIMasterView(APIView): | ||||
| 
 | ||||
|     @classmethod | ||||
|     def get_model_class(cls): | ||||
|         if hasattr(cls, 'model_class'): | ||||
|             return cls.model_class | ||||
|         raise NotImplementedError("must set `model_class` for {}".format(cls.__name__)) | ||||
| 
 | ||||
|     @classmethod | ||||
|     def get_model_key(cls): | ||||
|         if hasattr(cls, 'model_key'): | ||||
|             return cls.model_name | ||||
|         return cls.get_model_class().__name__.lower() | ||||
| 
 | ||||
|     @classmethod | ||||
|     def get_model_key_plural(cls): | ||||
|         if hasattr(cls, 'model_key_plural'): | ||||
|             return cls.model_key_plural | ||||
|         return '{}s'.format(cls.get_model_key()) | ||||
| 
 | ||||
|     @classmethod | ||||
|     def get_route_prefix(cls): | ||||
|         if hasattr(cls, 'route_prefix'): | ||||
|             return cls.route_prefix | ||||
|         return 'api.{}'.format(cls.get_model_key_plural()) | ||||
| 
 | ||||
|     @classmethod | ||||
|     def get_permission_prefix(cls): | ||||
|         if hasattr(cls, 'permission_prefix'): | ||||
|             return cls.permission_prefix | ||||
|         return cls.get_model_key_plural() | ||||
| 
 | ||||
|     @classmethod | ||||
|     def get_url_prefix(cls): | ||||
|         if hasattr(cls, 'url_prefix'): | ||||
|             return cls.url_prefix | ||||
|         return '/api/{}'.format(cls.get_model_key_plural()) | ||||
| 
 | ||||
|     @property | ||||
|     def Session(self): | ||||
|         return Session | ||||
| 
 | ||||
|     @api | ||||
|     def index(self): | ||||
|         objects = self.Session.query(self.model_class) | ||||
| 
 | ||||
|         sort = self.request.params.get('sort') | ||||
|         if sort: | ||||
|             # TODO: this is fragile, but what to do if bad params? | ||||
|             sortkey, sortdir = sort.split('|') | ||||
|             sortkey = getattr(self.model_class, sortkey) | ||||
|             objects = objects.order_by(getattr(sortkey, sortdir)()) | ||||
| 
 | ||||
|         data = [self.normalize(obj) for obj in objects] | ||||
|         return data | ||||
| 
 | ||||
|     def normalize(self, obj): | ||||
|         raise NotImplementedError("must implement `normalize()` method for: {}".format(self.__class__.__name__)) | ||||
| 
 | ||||
|     @classmethod | ||||
|     def defaults(cls, config): | ||||
|         route_prefix = cls.get_route_prefix() | ||||
|         url_prefix = cls.get_url_prefix() | ||||
|         permission_prefix = cls.get_permission_prefix() | ||||
| 
 | ||||
|         # index | ||||
|         config.add_route(route_prefix, '{}/'.format(url_prefix), request_method='GET') | ||||
|         config.add_view(cls, attr='index', route_name=route_prefix, | ||||
|                         renderer='json', permission='{}.list'.format(permission_prefix)) | ||||
							
								
								
									
										48
									
								
								tailbone/api/users.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								tailbone/api/users.py
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,48 @@ | |||
| # -*- coding: utf-8; -*- | ||||
| ################################################################################ | ||||
| # | ||||
| #  Rattail -- Retail Software Framework | ||||
| #  Copyright © 2010-2018 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 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 General Public License for more | ||||
| #  details. | ||||
| # | ||||
| #  You should have received a copy of the GNU General Public License along with | ||||
| #  Rattail.  If not, see <http://www.gnu.org/licenses/>. | ||||
| # | ||||
| ################################################################################ | ||||
| """ | ||||
| Tailbone Web API - User Views | ||||
| """ | ||||
| 
 | ||||
| from __future__ import unicode_literals, absolute_import | ||||
| 
 | ||||
| import six | ||||
| 
 | ||||
| from rattail.db import model | ||||
| 
 | ||||
| from tailbone.api import APIMasterView | ||||
| 
 | ||||
| 
 | ||||
| class UserView(APIMasterView): | ||||
| 
 | ||||
|     model_class = model.User | ||||
| 
 | ||||
|     def normalize(self, user): | ||||
|         return { | ||||
|             'username': user.username, | ||||
|             'person': six.text_type(user.person or ''), | ||||
|         } | ||||
| 
 | ||||
| 
 | ||||
| def includeme(config): | ||||
|     UserView.defaults(config) | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Lance Edgar
						Lance Edgar