Skip to content

Commit

Permalink
Closes #17
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Knupp committed Dec 21, 2013
1 parent b369b5b commit a603b0f
Show file tree
Hide file tree
Showing 18 changed files with 62 additions and 38 deletions.
15 changes: 7 additions & 8 deletions docs/admin.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ the creation and editing forms, objects related via foreign key (e.g. a
available values. This ensures that all database constraints are honored when
making changes via the admin.

To activate the admin interface (which adds an ``/admin`` endpoint to your
service, accessible via a browser), you'll need to call one additional function:
``model.activate_admin_classes``. By default, calling this function will make _all_
Models accessible in the admin. If you'd like to prevent this, simply call
``register()`` with ``use_admin=False`` for whichever Model/Models you don't
want to appear.
The admin interface (which adds an ``/admin`` endpoint to your
service, accessible via a browser), is enabled by default. To disable it, pass
`admin=False` as an argument in your call to `activate`.
By default, calling this function will make _all_ Models accessible in the admin.
If you'd like to prevent this, simply call ``register()`` with ``use_admin=False``
for whichever Model/Models you don't want to appear.

Getting Richer Information for Related Objects
----------------------------------------------
Expand All @@ -38,7 +38,7 @@ associated table. Currently, ``__str__`` simply returns the value of a Model's
``primary_key()`` attribute. By overriding ``__str__``, however, we can display
more useful information. After making the changes below::

from sandman.model import register, activate_admin_classes, Model
from sandman.model import register, Model

class Track(Model):
__tablename__ = 'Track'
Expand Down Expand Up @@ -77,7 +77,6 @@ more useful information. After making the changes below::
return self.Name

register((Artist, Album, Playlist, Genre, Track, MediaType))
activate_admin_classes()

Now, we get much more useful information in the columns mentioned, as you can
see here:
Expand Down
Binary file modified docs/generated/.doctrees/admin.doctree
Binary file not shown.
Binary file modified docs/generated/.doctrees/environment.pickle
Binary file not shown.
Binary file modified docs/generated/.doctrees/sandman.doctree
Binary file not shown.
Binary file modified docs/generated/.doctrees/using_sandman.doctree
Binary file not shown.
6 changes: 3 additions & 3 deletions docs/generated/_modules/sandman/model.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ <h1>Source code for sandman.model</h1><div class="highlight"><pre>
<span class="c"># Necessary to get Flask-Admin to register the relationship</span>
<span class="nb">setattr</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="s">&#39;__&#39;</span> <span class="o">+</span> <span class="n">cls</span><span class="o">.</span><span class="n">__name__</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span> <span class="n">relationship</span><span class="p">(</span><span class="n">cls</span><span class="o">.</span><span class="n">__name__</span><span class="p">,</span> <span class="n">backref</span><span class="o">=</span><span class="n">other</span><span class="o">.</span><span class="n">__name__</span><span class="o">.</span><span class="n">lower</span><span class="p">()))</span>

<span class="k">def</span> <span class="nf">activate</span><span class="p">(</span><span class="n">admin</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
<div class="viewcode-block" id="activate"><a class="viewcode-back" href="../../sandman.html#sandman.model.activate">[docs]</a><span class="k">def</span> <span class="nf">activate</span><span class="p">(</span><span class="n">admin</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Activate each registered model for non-admin use&quot;&quot;&quot;</span>
<span class="k">with</span> <span class="n">app</span><span class="o">.</span><span class="n">app_context</span><span class="p">():</span>
<span class="k">if</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">current_app</span><span class="p">,</span> <span class="s">&#39;endpoint_classes&#39;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
Expand All @@ -131,7 +131,7 @@ <h1>Source code for sandman.model</h1><div class="highlight"><pre>
<span class="k">with</span> <span class="n">app</span><span class="o">.</span><span class="n">app_context</span><span class="p">():</span>
<span class="k">for</span> <span class="n">cls</span> <span class="ow">in</span> <span class="p">(</span><span class="n">cls</span> <span class="k">for</span> <span class="n">cls</span> <span class="ow">in</span> <span class="n">current_app</span><span class="o">.</span><span class="n">classes</span> <span class="k">if</span> <span class="n">cls</span><span class="o">.</span><span class="n">use_admin</span> <span class="o">==</span> <span class="bp">True</span><span class="p">):</span>
<span class="n">admin</span><span class="o">.</span><span class="n">add_view</span><span class="p">(</span><span class="n">ModelView</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">db</span><span class="o">.</span><span class="n">session</span><span class="p">))</span>
<span class="n">webbrowser</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s">&#39;http://localhost:5000/admin&#39;</span><span class="p">)</span>
<span class="n">webbrowser</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s">&#39;http://localhost:5000/admin&#39;</span><span class="p">)</span>


<span class="c"># Redefine &#39;Model&#39; to be a sqlalchemy.ext.declarative.api.DeclarativeMeta</span>
Expand All @@ -140,7 +140,7 @@ <h1>Source code for sandman.model</h1><div class="highlight"><pre>
<span class="c"># from DeferredReflection and &quot;Resource&quot;, which was the exact same class</span>
<span class="c"># as &quot;Model&quot; in models.py. It caused confusion in the documentation, however,</span>
<span class="c"># since it wasn&#39;t clear that the Model class and the Resource class were</span>
<span class="c"># actually the same thing.</span>
<span class="c"># actually the same thing.</span></div>
<span class="n">sandman_model</span> <span class="o">=</span> <span class="n">Model</span>
<span class="n">Model</span> <span class="o">=</span> <span class="n">declarative_base</span><span class="p">(</span><span class="n">cls</span><span class="o">=</span><span class="p">(</span><span class="n">Model</span><span class="p">,</span> <span class="n">DeferredReflection</span><span class="p">))</span>
</pre></div>
Expand Down
15 changes: 7 additions & 8 deletions docs/generated/_sources/admin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ the creation and editing forms, objects related via foreign key (e.g. a
available values. This ensures that all database constraints are honored when
making changes via the admin.

To activate the admin interface (which adds an ``/admin`` endpoint to your
service, accessible via a browser), you'll need to call one additional function:
``model.activate_admin_classes``. By default, calling this function will make _all_
Models accessible in the admin. If you'd like to prevent this, simply call
``register()`` with ``use_admin=False`` for whichever Model/Models you don't
want to appear.
The admin interface (which adds an ``/admin`` endpoint to your
service, accessible via a browser), is enabled by default. To disable it, pass
`admin=False` as an argument in your call to `activate`.
By default, calling this function will make _all_ Models accessible in the admin.
If you'd like to prevent this, simply call ``register()`` with ``use_admin=False``
for whichever Model/Models you don't want to appear.

Getting Richer Information for Related Objects
----------------------------------------------
Expand All @@ -38,7 +38,7 @@ associated table. Currently, ``__str__`` simply returns the value of a Model's
``primary_key()`` attribute. By overriding ``__str__``, however, we can display
more useful information. After making the changes below::

from sandman.model import register, activate_admin_classes, Model
from sandman.model import register, Model

class Track(Model):
__tablename__ = 'Track'
Expand Down Expand Up @@ -77,7 +77,6 @@ more useful information. After making the changes below::
return self.Name

register((Artist, Album, Playlist, Genre, Track, MediaType))
activate_admin_classes()

Now, we get much more useful information in the columns mentioned, as you can
see here:
Expand Down
2 changes: 1 addition & 1 deletion docs/generated/_sources/sandman.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ sandman API
-------------------

.. automodule:: sandman.model
:members: register, activate_admin_classes
:members: register, activate

.. automodule:: sandman.model.models
:members:
Expand Down
8 changes: 7 additions & 1 deletion docs/generated/_sources/using_sandman.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The Simplest Application
We'll be using a subset of the Chinook test database as an example.
Create one file with the following contents (which I'll call ``runserver.py``)::

from sandman.model import register, Model
from sandman.model import register, activate, Model

class Artist(Model):
__tablename__ = 'Artist'
Expand All @@ -24,6 +24,7 @@ Create one file with the following contents (which I'll call ``runserver.py``)::

register((Artist, Album, Playlist))
register(Genre)
activate()

from sandman import app, db
app.config['SQLALCHEMY_DATABASE_URI'] = '<your database connection string (using SQLAlchemy)'
Expand All @@ -35,6 +36,11 @@ Then simply run::

and try curling your new REST API service!

Of course, you don't actually need to tell sandman about your tables; it's
perfectly capable of introspecting all of them. To use introspection to make
*all* of your database tables available via the admin and REST API, simply
remove all model code and call `activate()` without ever registering a model.

A Quick Guide to REST APIs
~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
15 changes: 7 additions & 8 deletions docs/generated/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ <h2>Activating the sandman Admin Interface<a class="headerlink" href="#activatin
<tt class="docutils literal"><span class="pre">Track</span></tt>&#8216;s associated <tt class="docutils literal"><span class="pre">Album</span></tt>) are auto-populated in a dropdown based on
available values. This ensures that all database constraints are honored when
making changes via the admin.</p>
<p>To activate the admin interface (which adds an <tt class="docutils literal"><span class="pre">/admin</span></tt> endpoint to your
service, accessible via a browser), you&#8217;ll need to call one additional function:
<tt class="docutils literal"><span class="pre">model.activate_admin_classes</span></tt>. By default, calling this function will make _all_
Models accessible in the admin. If you&#8217;d like to prevent this, simply call
<tt class="docutils literal"><span class="pre">register()</span></tt> with <tt class="docutils literal"><span class="pre">use_admin=False</span></tt> for whichever Model/Models you don&#8217;t
want to appear.</p>
<p>The admin interface (which adds an <tt class="docutils literal"><span class="pre">/admin</span></tt> endpoint to your
service, accessible via a browser), is enabled by default. To disable it, pass
<cite>admin=False</cite> as an argument in your call to <cite>activate</cite>.
By default, calling this function will make _all_ Models accessible in the admin.
If you&#8217;d like to prevent this, simply call <tt class="docutils literal"><span class="pre">register()</span></tt> with <tt class="docutils literal"><span class="pre">use_admin=False</span></tt>
for whichever Model/Models you don&#8217;t want to appear.</p>
</div>
<div class="section" id="getting-richer-information-for-related-objects">
<h2>Getting Richer Information for Related Objects<a class="headerlink" href="#getting-richer-information-for-related-objects" title="Permalink to this headline"></a></h2>
Expand All @@ -83,7 +83,7 @@ <h2>Getting Richer Information for Related Objects<a class="headerlink" href="#g
associated table. Currently, <tt class="docutils literal"><span class="pre">__str__</span></tt> simply returns the value of a Model&#8217;s
<tt class="docutils literal"><span class="pre">primary_key()</span></tt> attribute. By overriding <tt class="docutils literal"><span class="pre">__str__</span></tt>, however, we can display
more useful information. After making the changes below:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sandman.model</span> <span class="kn">import</span> <span class="n">register</span><span class="p">,</span> <span class="n">activate_admin_classes</span><span class="p">,</span> <span class="n">Model</span>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sandman.model</span> <span class="kn">import</span> <span class="n">register</span><span class="p">,</span> <span class="n">Model</span>

<span class="k">class</span> <span class="nc">Track</span><span class="p">(</span><span class="n">Model</span><span class="p">):</span>
<span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;Track&#39;</span>
Expand Down Expand Up @@ -122,7 +122,6 @@ <h2>Getting Richer Information for Related Objects<a class="headerlink" href="#g
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">Name</span>

<span class="n">register</span><span class="p">((</span><span class="n">Artist</span><span class="p">,</span> <span class="n">Album</span><span class="p">,</span> <span class="n">Playlist</span><span class="p">,</span> <span class="n">Genre</span><span class="p">,</span> <span class="n">Track</span><span class="p">,</span> <span class="n">MediaType</span><span class="p">))</span>
<span class="n">activate_admin_classes</span><span class="p">()</span>
</pre></div>
</div>
<p>Now, we get much more useful information in the columns mentioned, as you can
Expand Down
4 changes: 4 additions & 0 deletions docs/generated/genindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ <h2 id="A">A</h2>
<dt><a href="sandman.html#sandman.exception.InvalidAPIUsage.abort">abort() (sandman.exception.InvalidAPIUsage method)</a>
</dt>


<dt><a href="sandman.html#sandman.model.activate">activate() (in module sandman.model)</a>
</dt>

</dl></td>
<td style="width: 33%" valign="top"><dl>

Expand Down
2 changes: 1 addition & 1 deletion docs/generated/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ <h1>Welcome to sandman&#8217;s documentation!<a class="headerlink" href="#welcom
</li>
<li class="toctree-l1"><a class="reference internal" href="sandman.html">sandman API</a><ul>
<li class="toctree-l2"><a class="reference internal" href="sandman.html#module-sandman.exception"><tt class="docutils literal"><span class="pre">exception</span></tt> Module</a></li>
<li class="toctree-l2"><a class="reference internal" href="sandman.html#model-module"><tt class="docutils literal"><span class="pre">model</span></tt> Module</a></li>
<li class="toctree-l2"><a class="reference internal" href="sandman.html#module-sandman.model"><tt class="docutils literal"><span class="pre">model</span></tt> Module</a></li>
<li class="toctree-l2"><a class="reference internal" href="sandman.html#module-sandman.sandman"><tt class="docutils literal"><span class="pre">sandman</span></tt> Module</a></li>
</ul>
</li>
Expand Down
Binary file modified docs/generated/objects.inv
Binary file not shown.
14 changes: 10 additions & 4 deletions docs/generated/sandman.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ <h1>sandman API<a class="headerlink" href="#sandman-api" title="Permalink to thi
</dd></dl>

</div>
<div class="section" id="model-module">
<h2><tt class="xref py py-mod docutils literal"><span class="pre">model</span></tt> Module<a class="headerlink" href="#model-module" title="Permalink to this headline"></a></h2>
<span class="target" id="module-sandman.model"></span><p>The model module is repsonsible exposes the <tt class="xref py py-class docutils literal"><span class="pre">sandman.model.Model</span></tt> class,
<div class="section" id="module-sandman.model">
<span id="model-module"></span><h2><tt class="xref py py-mod docutils literal"><span class="pre">model</span></tt> Module<a class="headerlink" href="#module-sandman.model" title="Permalink to this headline"></a></h2>
<p>The model module is repsonsible exposes the <tt class="xref py py-class docutils literal"><span class="pre">sandman.model.Model</span></tt> class,
from which user models should derive. It also makes the <a class="reference internal" href="#sandman.model.register" title="sandman.model.register"><tt class="xref py py-func docutils literal"><span class="pre">register()</span></tt></a>
function available, which maps endpoints to their associated classes.</p>
<dl class="function">
Expand All @@ -95,6 +95,12 @@ <h2><tt class="xref py py-mod docutils literal"><span class="pre">model</span></
</table>
</dd></dl>

<dl class="function">
<dt id="sandman.model.activate">
<tt class="descclassname">sandman.model.</tt><tt class="descname">activate</tt><big>(</big><em>admin=True</em><big>)</big><a class="reference internal" href="_modules/sandman/model.html#activate"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#sandman.model.activate" title="Permalink to this definition"></a></dt>
<dd><p>Activate each registered model for non-admin use</p>
</dd></dl>

<span class="target" id="module-sandman.model.models"></span><p>The Model class is meant to be the base class for user Models. It represents
a table in the database that should be modeled as a resource.</p>
<dl class="class">
Expand Down Expand Up @@ -532,7 +538,7 @@ <h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">sandman API</a><ul>
<li><a class="reference internal" href="#module-sandman.exception"><tt class="docutils literal"><span class="pre">exception</span></tt> Module</a></li>
<li><a class="reference internal" href="#model-module"><tt class="docutils literal"><span class="pre">model</span></tt> Module</a></li>
<li><a class="reference internal" href="#module-sandman.model"><tt class="docutils literal"><span class="pre">model</span></tt> Module</a></li>
<li><a class="reference internal" href="#module-sandman.sandman"><tt class="docutils literal"><span class="pre">sandman</span></tt> Module</a></li>
</ul>
</li>
Expand Down
Loading

0 comments on commit a603b0f

Please sign in to comment.