Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
sdispater committed Oct 6, 2016
2 parents ccddbe9 + f1d6e2a commit a995232
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 62 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ nosetests.xml

.DS_Store
.idea/*
.cache/*

test.py
app.py
Expand Down
7 changes: 6 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
include README.rst LICENSE
include README.rst LICENSE requirements.txt
recursive-exclude tests *
recursive-exclude benchmark *
recursive-exclude seeders *
recursive-exclude seeds *
recursive-exclude migrations *
36 changes: 36 additions & 0 deletions docs/_static/theme_overrides.css
Original file line number Diff line number Diff line change
Expand Up @@ -955,3 +955,39 @@ tt.code .punctuation, code.code .punctuation { color: #878787 }
tt.code .string, code.code .string { color: #59B6CF }
tt.code .number, code.code .number { color: #E9767F }
tt.code .integer, code.code .integer { color: #E9767F }


/* Dark Orator */

div[class^="highlight"] {
border: 0;
background: #152B39;
border-radius: 3px;
}

div[class^="highlight"] > pre {
border: 0;
color: #98AEC5;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
padding: 4% 4%;
background: none;
}

.highlight .nn, .highlight .nc { color: #89C6BF; }
.highlight .c, .highlight .c1 { color: #727995; font-style: normal; }
.highlight .sd { color: #727995; font-style: normal; }
.highlight .p, .highlight .o, code.code .operator { color: #687E95 }
.highlight .n, code.code .name { color: #98AEC5 }
.highlight .l { color: #98AEC5; }

.note .last pre,
.warning .last pre,
.tip .last pre,
.caution .last pre,
.versionchanged .last pre,
.versionadded .last pre {
padding-bottom: 4%;
margin-bottom: 0;
}
52 changes: 40 additions & 12 deletions docs/basic_usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ You first need to make a migration file to create the table:

.. code-block:: text
python db.py make:migration create_users_table --table users --create
python db.py make:migration create_users_table --table users --create
This will add a file in the ``migrations`` folder named ``create_users_table``
and prefixed by a timestamp:
Expand Down Expand Up @@ -105,9 +105,9 @@ Then, you can run the migration:

.. code-block:: text
python db.py migrations:run
python db.py migrate
Confirm and you database and table will be created.
Confirm and you database and the table will be created.

Once your database set up, you can create some users:

Expand All @@ -127,6 +127,18 @@ initiate them and save them later:
# Do something else...
admin.save()
.. note::

Optionally you can use a transaction.

.. code-block:: python
from your_application import db, User
with db.transaction():
admin = User.create(name='admin', email='[email protected]')
guest = Guest.create(name='guest', email='[email protected]')
You can now retrieve them easily from the database:

.. code-block:: python
Expand All @@ -145,32 +157,37 @@ Let's create a ``Post`` model with the ``User`` model as a parent:

.. code-block:: python
from orator.orm import belongs_to
class Post(db.Model):
__fillable__ = ['title', 'content']
@property
@belongs_to
def user(self):
return self.belongs_to('users')
return User
And we add the ``posts`` relationship to the ``User`` model:

.. code-block:: python
from orator.orm import has_many
class User(db.Model):
@property
@has_many
def posts(self):
return self.has_many('posts')
return Post
Before we can play with these models we need to create the ``posts`` table
and set up the relationship at database level:

.. code-block:: text
python db.py migrations:make create_posts_table --table posts --create
python db.py make:migration create_posts_table --table posts --create
And we modify the generated file to look like this:

Expand All @@ -179,7 +196,7 @@ And we modify the generated file to look like this:
from orator.migrations import Migration
class CreateTableUsers(Migration):
class CreatePostsTable(Migration):
def up(self):
"""
Expand All @@ -189,7 +206,7 @@ And we modify the generated file to look like this:
table.increments('id')
table.string('title')
table.text('content')
table.integer('user_id')
table.integer('user_id', unsigned=True)
table.timestamps()
table.foreign('user_id').references('id').on('users')
Expand All @@ -204,7 +221,7 @@ Finally we run it:

.. code-block:: text
python db.py migrations:run
python db.py migrate
We can now instantiate some posts:

Expand All @@ -226,6 +243,17 @@ and associate them with users:
# Associate from post.user relation
guest_post.user().associate(guest)
.. note::

You can also create the posts directly.

.. code-block:: python
admin.posts().create(
title='Admin Post',
description='This is a restricted post'
)
Relationships properties are `dynamic properties <http://orator-orm.com/docs/orm.html#dynamic-properties>`_
meaning that ``user.posts`` is the underlying collection of posts so we can do things like:

Expand Down Expand Up @@ -277,7 +305,7 @@ or by changing the default ``Paginator`` current page resolver:
What's more?
============

Like said in introduction Flask-Orator is a wrapper around `Orator <http://orator-orm.com>`_ to integrate it
Like said in the introduction Flask-Orator is a wrapper around `Orator <http://orator-orm.com>`_ to integrate it
more easily with Flask applications. So, basically, everything you can do with Orator
is also possible with Flask-Orator.

Expand Down
20 changes: 10 additions & 10 deletions docs/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ Migrations
Creating Migrations
-------------------

To create a migration, you can use the ``migrations:make`` command on the CLI:
To create a migration, you can use the ``make:migration`` command on the CLI:

.. code-block:: bash
python db.py migrations:make create_users_table
python db.py make:migration create_users_table
This will create a migration file that looks like this:

Expand Down Expand Up @@ -55,16 +55,16 @@ If you want the migrations to be stored in another folder, use the ``--path/-p``

.. code-block:: bash
python db.py migrations:make create_users_table -p my/path/to/migrations
python db.py make:migration create_users_table -p my/path/to/migrations
The ``--table`` and ``--create`` options can also be used to indicate the name of the table,
and whether the migration will be creating a new table:

.. code-block:: bash
python db.py migrations:make add_votes_to_users_table --table=users
python db.py make:migration add_votes_to_users_table --table=users
python db.py migrations:make create_users_table --table=users --create
python db.py make:migration create_users_table --table=users --create
These commands would respectively create the following migrations:

Expand Down Expand Up @@ -114,11 +114,11 @@ These commands would respectively create the following migrations:
Running Migrations
------------------

To run all outstanding migrations, just use the ``migrations:run`` command:
To run all outstanding migrations, just use the ``migrate`` command:

.. code-block:: bash
python db.py migrations:run
python db.py migrate
Rolling back migrations
Expand All @@ -129,14 +129,14 @@ Rollback the last migration operation

.. code-block:: bash
python db.py migrations:rollback
python db.py migrate:rollback
Rollback all migrations
~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: bash
python db.py migrations:reset
python db.py migrate:reset
Getting migrations status
Expand All @@ -146,7 +146,7 @@ To see the status of the migrations, just use the ``migrations:status`` command:

.. code-block:: bash
python db.py migrations:status
python db.py migrate:status
This would output something like this:

Expand Down
62 changes: 34 additions & 28 deletions flask_orator/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
# -*- coding: utf-8 -*-

from flask import current_app, request, jsonify as base_jsonify, Response
from flask import current_app, request, jsonify as base_jsonify, make_response
from orator import DatabaseManager, Model as BaseModel
from orator.pagination import Paginator
from orator.commands.migrations import (
InstallCommand, MigrateCommand,
MigrateMakeCommand, RollbackCommand,
StatusCommand, ResetCommand
)
from orator.commands.application import application as orator_application
from orator.commands.command import Command
from orator.exceptions.orm import ModelNotFound
from cleo import Application


Expand All @@ -20,10 +17,11 @@

class Orator(object):

def __init__(self, app=None):
def __init__(self, app=None, manager_class=DatabaseManager):
self.Model = BaseModel
self.cli = None
self._db = None
self._manager_class = manager_class

if app is not None:
self.init_app(app)
Expand All @@ -39,37 +37,48 @@ def init_app(self, app):
self._config = app.config['ORATOR_DATABASES']

# Initializing database manager
self._db = DatabaseManager(self._config)
self._db = self._manager_class(self._config)

self.Model.set_connection_resolver(self._db)

# Setting current page resolver
def current_page_resolver():
return int(request.args.get('page', 1))

Paginator.current_page_resolver(current_page_resolver)
Paginator.current_page_resolver(self._current_page_resolver)

# Setting commands
self.init_commands()

def _current_page_resolver(self):
return int(request.args.get('page', 1))

def init_commands(self):
self.cli = Application(orator_application.get_name(),
orator_application.get_version())

self.cli.add(InstallCommand(self))
self.cli.add(MigrateCommand(self))
self.cli.add(MigrateMakeCommand(self))
self.cli.add(RollbackCommand(self))
self.cli.add(StatusCommand(self))
self.cli.add(ResetCommand(self))
self.cli = Application(
orator_application.get_name(),
orator_application.get_version(),
complete=True
)

for command in orator_application.all().values():
if isinstance(command, Command):
self.cli.add(command.__class__(self._db))
else:
self.cli.add(command)

def register_handlers(self, app):
self._register_error_handlers(app)

teardown = app.teardown_appcontext

@teardown
def disconnect(_):
return self._db.disconnect()

def _register_error_handlers(self, app):
@app.errorhandler(ModelNotFound)
def model_not_found(error):
response = make_response(error.message, 404)

return response

def __getattr__(self, item):
return getattr(self._db, item)

Expand All @@ -81,13 +90,10 @@ def jsonify(obj, **kwargs):
indent = 2

if hasattr(obj, 'to_json'):
response = Response(obj.to_json(indent=indent),
mimetype='application/json',
**kwargs)
elif isinstance(obj, list):
response = Response(json.dumps(obj, indent=indent),
mimetype='application/json',
**kwargs)
response = current_app.response_class(
obj.to_json(indent=indent),
mimetype='application/json'
)
else:
response = base_jsonify(obj, **kwargs)

Expand Down
Loading

0 comments on commit a995232

Please sign in to comment.