A minimal Pyramid scaffold that aims to provide a starter template to build small to large web services. To achieve this goal the following design decisions are made for you -
- Completely decoupled from client-side coding
- Use only JSON renderer
- Use only url traversal
- Focus on resource management
Following packages are included in the setup.py -
- Pyramid itself
- SQLAlchemy
- Alembic
- A predetermined project structure (See Project Structure).
- Saves hours of google search and typing.
- Open source and free.
- IDE (i.o PyCharm) friendly.
- One configuration file per environment(i.e development and production).
- A simple Makefile to make life even easier(See the usage of the Makefile).
- A built-in
ModelBasefor SQLAlchemy (See features of theModelBase). - Uses
CookieCutterproject templating system.
- A Linux OS
- Python 3+
- CookieCutter
After installing cookiecutter, run -
cookiecutter https://github.com/asif-mahmud/pyramid_runner.git
(Obsolete)
- Create a
Python virtual environtmentfirst. - Clone the git repo.
- Open a
Terminalinsidepyramid_runner. - Activate the
virtual environtmentby runningsource <your-venv-path>/bin/activate. - Run
python setup.py install
- # - Stands for Folder.
- + - Stands for File.
- All
__init__.pyare ignored. - All
README.md's are ignored. - Learn more about the purpose of the folders in their
README.mdfile.
# project_name
|
|-# database
|
|-# project_name - Pyramid application code
| |
| |-# models
| |-# views
| |-# tests
| |-# security
| |-# resources - Includes all your resource classes including Root resource.
|
|-# alembic
| |
| |-# versions
| |-+ script.py.mako
| |-+ env.py
|
|-# wsgi - Will contain wsgi servers's log files and pid file.
|
|-+ setup.py - Python distribution setup file.
|-+ development.ini - Development environment config file.
|-+ production.ini - Production environment config file.
|-+ alembic.ini - Pyramid project friendly alembic config file.
|-+ MANIFEST.in
|-+ CHANGES.txt
|-+ Makefile - Provides some useful shortcut commands.
The Makefile inside the project folder provides some easy shortcut commands-
make setup: This command must be run once at the beginnng.make revision: Make a database revision by Alembic.make upgrade: Migrate your database to latest revision.make downgrade: Migrate your database to a previous revision.make run: Runpserveto serve local server. Additionally it will start watchingSASSsources and angular apps.make test: Runpytestto test the project.
It is a default Model base for all of your SQLAlchemy Models. But you don't have to inherit it, instead you
inherit the infamous Base class. It provides the following class level attributes -
id: AnInteger Columnasprimary_key. Feel free to override it anytime.created_on: ADateTime Columnto provide when the entry was created. Defaults to current time.updated_on: ADateTime Columnto provide when the entry was updated. Always the last update time.__tablename__: You don't have to write__tablename__whenever you create aModel__json__: Method to represent the model asJSONobject.
Root resource inherits this class. See the default resources.root.Root resource.
A helper base class for child nodes in the resource tree. __name__ and __parent__
attributes are handled in the __init__ method. So a container child need to
implement child_instance and/or child_class (Updated in version 4.0.0)
methods of it's own to generate it's requested child node. Decisions made here are -
Parent/Containernode is responsible to generateChildnode.Parent/Containernode is responsible to give theChildinstance a name.
A helper class for view classes. What it does -
- Sets the
requestattribute in__init__method. - Adds a default empty view for
OPTIONSHTTP requests.
(new in version 4.0.0)
A subclass of dict to provide consistent response from different views.
It includes the following keys-
- code : An integer code mostly representing HTTP response code. Dafaults 200.
- status: A string status like "OK" or "ERROR". Defaults "OK".
- msg: A string containing a message for the client. Defaults "".
- error: A string containing any error message to send to the client. Defaults "".
These keys can be accessed both as dictionary keys or class attributes.
(new in version 6.0.0)
A subclass of BaseJSONResponse. This is helpful to quickly define
a status(error or success) report standard for the API.
A base class to retreive authenticated user info from request params.
NOTE: Removed from version 3.9+
Base class for ticket based authentication policy.
NOTE: Removed from version 3.9+
- Added
log_errormethod toBaseResource.
- Added
BaseStatusReportand some subclasses of it. Seeviews.errorspackage. - Updated project README.md to better reflect the setup procedure.
- Fixed some stylings.
BaseTestandBaseFunctionalTestis being merged into singleBaseTest
- Little fixes.
- Added a
.gitignorefile.
- Test base class now uses
development.inifile to read the configurations. So tests will run with same configurations as the development versions. - Initializing database by inserting some test data is made easier by introducing
init_dbandclean_dbmethods inBaseTestclass. Child classes will just implement these methods to insert test data into database by normalsession.addorsession.deletemethods.
- More pythonic all the way. Improved
importstatements. BaseView,BaseChildhas more functionalities.
- Added
pyramid_jwtforJWTauthentication suitable forCORS.
- Using
Gunicorninstead of waitress as development and production server.
- Added
CORSheaders onNewResponseevent.
- Using
cookiecutterproject templating system.
- Added a basic stateless security system.
ModelBaseincludes an abstract__json__(request)method.
- Working version with 3 utility classes -
BaseRootBaseChildBaseView
- Added an example functional test for
home_view.
- Git initialization.
