PyMVC
© 2018 SiLeader and Cerussite.
Overview
PyMVC is MVC-like server side framework for python.
This framework is using Flask.
How to
How to Install
pip install pymvc
How to Use
import pymvc
pymvc.settings.database.database = "pymmvc_example"
class TopController(pymvc.Controller):
VIEW = "top.html"
def get(self, **kwargs):
pass
pymvc.add_route("/", TopController)
@pymvc.route("/users/<id>")
class UserController(pymvc.Controller):
VIEW = "user.html"
def get(self, id):
return pymvc.render(id=id)
class User(pymvc.Model):
name = pymvc.StringType()
id = pymvc.UniqueIdType(primary=True)
if __name__ == '__main__':
pymvc.app.run()
pymvc.app
is Flask instance.
- set database name. (use
pymvc.settings.database.database
property) - create classes
- Controller class
- Model class
- register controller classes to router.
- start app
- call
run()
method. - use
pymvc.app
as WSGI app.
Controller
Controller class has VIEW
(class variable) and get
, post
, put
and delete
instance methods.
if you want to support GET method, override get
method.
these functions' default operation is return abort(404)
.
Model
Model class is ORM for MongoDB (using pymongo).
if inherit it, it creates collection.
collection's data is specified as class variable.
import pymvc
class Other1(pymvc.Model):
pass
class ModelExample(pymvc.Model):
string_data = pymvc.StringType()
int_data = pymvc.IntType()
float_data = pymvc.FloatType()
unique_data = pymvc.UniqueIdType()
foreign_data1 = pymvc.ForeignType(Other1)
foreign_data2 = pymvc.ForeignType("Other2")
class Other2(pymvc.Model):
pass
collection name is snake case of class name. (e.g. User: user, UserInfo: user_info)
Model data type
model data types' constructor parameters are primary
and default
.
if primary
is True
, this value is marked as primary key.
default
is default value.
View
PyMVC add some Jinja2 function.
function | operation |
---|
load_one(model, primary=None, **query) | load one data (using find_one) |
load_many(model, primary=None, **query) | load all data match query and primary data |
model
is require parameter.
primary
is primary key value.
key value hint is **query
.
Dependencies
License
Apache License 2.0