Commit b47bce23 authored by Emma's avatar Emma

🎉 🚧 api list working

Pipeline #1976 failed with stages
from cordy import Cordy
import config
from urls import url_map
application = Cordy(config, url_map)
'NAME': 'cordy'
from functools import reduce
from peewee import DatabaseProxy
from playhouse.postgres_ext import PostgresqlExtDatabase
from routes import Mapper
class Cordy:
db = DatabaseProxy()
def __init__(self, config, mapper):
global db
self.config = config
self.mapper = Mapper()
if hasattr(self.config, 'DATABASE'):
def __call__(self, enviro, start):
from .utils import import_string
def _handle(enviro, start):
from .utils import import_string
to_call = self.mapper.match(enviro['REQUEST_URI'])
if to_call is None:
start('404 NOT FOUND', [('Content-Type', 'text/plain')])
controller = import_string(to_call['controller'])(enviro, start)
rv = getattr(controller, to_call['action'])()
if not isinstance(rv, list):
return [rv]
return rv
except Exception as e:
start('500 Internal Error', [('Content-Type', 'text/plain')])
return e.args
resolved_middlewares = [import_string(m) for m in reversed(self.config.MIDDLEWARES)]
chained = reduce(lambda h, m: m(h), resolved_middlewares, _handle)
return chained(enviro, start)
class Controller:
def __init__(self, enviro, start):
self.enviro = enviro
self.start = start
start('200 OK', [('Content-Type', 'text/html')])
from cordy.base import Controller
class CRUDViewSet(Controller):
Model = None
def __init__(self, enviro, start):
super().__init__(enviro, start)
start('200 OK', [('Content-Type', 'application/json')])
def index(self):
return [self.Model.get_serializer().dumps(, many=True).encode('utf-8')]
def create(self):
def update(self, id):
def delete(self, id):
self.start('204 EMPTY', [('Content-Type', 'application/json')])
from playhouse.migrate import migrate
class Migration:
operations = [[], []]
def __init__(self, migrator):
self.migrator = migrator
def _perform(self, operations):
def migrate(self):
operations = [
getattr(self.migrator, op[0])(*op[1:])
for op in self.operations[0]
def reverse(self):
operations = [
getattr(self.migrator, op[0])(*op[1:])
for op in self.operations[1]
from uuid import uuid4
from marshmallow_peewee import ModelSchema
from peewee import UUIDField
from playhouse.postgres_ext import Model as BaseModel
from cordy import Cordy
class Model(BaseModel):
_serializer = None
id = UUIDField(primary_key=True, default=uuid4)
class Meta:
database = Cordy.db
def get_serializer(cls):
if cls._serializer is None:
class Schema(ModelSchema):
class Meta:
model = cls
cls._serializer = Schema()
return cls._serializer
class BaseMiddleware:
def before(self, enviro):
def after(self, content, start):
def __init__(self, handler):
self.handler = handler
def __call__(self, enviro, start):
rv = self.handler(enviro, start)
self.after(rv, start)
return rv
class EnviroLogMiddleware(BaseMiddleware):
def before(self, enviro):
import pprint
from importlib import import_module
from routes.route import Route
## Copied from django
def import_string(dotted_path):
module_path, class_name = dotted_path.rsplit('.', 1)
except ValueError as err:
raise ImportError("%s doesn't look like a module path" % dotted_path) from err
module = import_module(module_path)
return getattr(module, class_name)
except AttributeError as err:
raise ImportError('Module "%s" does not define a "%s" attribute/class' % (
module_path, class_name)
) from err
def CrudAPI(name, controller):
return [
Route(f'{name}_home', f'/{name}/', controller=controller, action='index', conditions={'method': ['GET']})
from cordy.base import Controller as CordyController
from cordy.crud.controllers import CRUDViewSet
from .models import ToDo
class Controller(CordyController):
def index(self):
return [b'hello from controller']
class ToDoViewSet(CRUDViewSet):
Model = ToDo
from cordy.db.migrations import Migration as CordyMigration
class Migration(CordyMigration):
operations = [
from cordy.db.models import Model
import peewee
class ToDo(Model):
description = peewee.TextField()
from routes.route import Route
from cordy.utils import CrudAPI
url_map = [
Route('home', '/', controller='myapp.Controller', action='index'),
*CrudAPI('todos', 'myapp.ToDoViewSet'),
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment