[copr] master: Create the API namespace (b6c2504)

bkabrda at fedoraproject.org bkabrda at fedoraproject.org
Mon Jan 28 09:29:54 UTC 2013


Repository : http://git.fedorahosted.org/cgit/copr.git

On branch  : master

>---------------------------------------------------------------

commit b6c2504faaf150a669823b739fe966333c960a3e
Author: Pierre-Yves Chibon <pingou at pingoured.fr>
Date:   Mon Jan 28 09:04:24 2013 +0100

    Create the API namespace
    
    This is the namespace that we will use to describe and expose the copr API.
    This commit introduce it, move some code from misc to the api_general and
    start with two API functions to list someone's copr and add a new one.
    
    These two functions are documented in the /api/ (ie in the api.html template).


>---------------------------------------------------------------

 coprs_frontend/coprs/__init__.py                 |    3 +
 coprs_frontend/coprs/templates/api.html          |   42 ++++++++++
 coprs_frontend/coprs/views/api_ns/__init__.py    |    3 +
 coprs_frontend/coprs/views/api_ns/api_general.py |   96 ++++++++++++++++++++++
 coprs_frontend/coprs/views/misc.py               |   19 ----
 5 files changed, 144 insertions(+), 19 deletions(-)

diff --git a/coprs_frontend/coprs/__init__.py b/coprs_frontend/coprs/__init__.py
index d5d732a..f239b65 100644
--- a/coprs_frontend/coprs/__init__.py
+++ b/coprs_frontend/coprs/__init__.py
@@ -22,6 +22,8 @@ import coprs.filters
 import coprs.log
 import coprs.models
 
+from coprs.views import api_ns
+from coprs.views.api_ns import api_general
 from coprs.views import coprs_ns
 from coprs.views.coprs_ns import coprs_builds
 from coprs.views.coprs_ns import coprs_general
@@ -29,6 +31,7 @@ from coprs.views import backend_ns
 from coprs.views.backend_ns import backend_general
 from coprs.views import misc
 
+app.register_blueprint(api_ns.api_ns)
 app.register_blueprint(coprs_ns.coprs_ns)
 app.register_blueprint(misc.misc)
 app.register_blueprint(backend_ns.backend_ns)
diff --git a/coprs_frontend/coprs/templates/api.html b/coprs_frontend/coprs/templates/api.html
index fb20f0d..2800fd3 100644
--- a/coprs_frontend/coprs/templates/api.html
+++ b/coprs_frontend/coprs/templates/api.html
@@ -33,6 +33,48 @@
 
     <h3>The API</h3>
 
+    <h4>List someone's copr</h4>
+
+    <p>To list the coprs owned by someone one argument is required:</p>
+    <table>
+        <tr>
+            <th>name</th> <td>The name of the user you would to list the
+                copr of.</td>
+        </tr>
+    </table>
+    <p>URL will therefore look like:</p>
+    <pre style="font-size:120%">
+    /list/&lt;username&gt;/
+    </pre>
+
+    <h4>Add a new copr</h4>
+
+    <p><span style="font-style:italic">Login required</span></p>
+
+    <p>To add a new copr some informations are required:</p>
+    <table>
+        <tr>
+            <th>name</th> <td>The name of the copr to create.</td>
+        </tr><tr>
+            <th>chroots</th> <td>A comma separated list of chroots to
+                use in the copr.</td>
+        </tr><tr>
+            <th>repos</th> <td>A comma separated list of repositories
+                that this new copr should have access to.</td>
+        </tr><tr>
+            <th>initial_pkgs</th> <td>A comma separated list of initial
+                packages to build in this new copr.</td>
+        </tr>
+    </table>
+
+    <p>URL will therefore look like:</p>
+    <pre style="font-size:120%">
+    /add/&lt;name&gt;/&lt;chroots&gt;/
+    /add/&lt;name&gt;/&lt;chroots&gt;/&lt;repos&gt;/
+    /add/&lt;name&gt;/&lt;chroots&gt;/&lt;repos&gt;/&lt;initial_pkgs&gt;/
+    </pre>
+
+
     <p>To come here, more information about the API itself.</p>
 
   </div>
diff --git a/coprs_frontend/coprs/views/api_ns/__init__.py b/coprs_frontend/coprs/views/api_ns/__init__.py
new file mode 100644
index 0000000..ce13abc
--- /dev/null
+++ b/coprs_frontend/coprs/views/api_ns/__init__.py
@@ -0,0 +1,3 @@
+import flask
+
+api_ns = flask.Blueprint('api_ns', __name__, url_prefix = '/api')
diff --git a/coprs_frontend/coprs/views/api_ns/api_general.py b/coprs_frontend/coprs/views/api_ns/api_general.py
new file mode 100644
index 0000000..a6a6c94
--- /dev/null
+++ b/coprs_frontend/coprs/views/api_ns/api_general.py
@@ -0,0 +1,96 @@
+import time
+
+import flask
+
+from coprs import db
+from coprs import exceptions
+
+from coprs.views.misc import login_required
+
+from coprs.views.api_ns import api_ns
+
+from coprs.logic import builds_logic
+from coprs.logic import coprs_logic
+
+
+ at api_ns.route('/')
+def api_home():
+    """ Renders the home page of the api.
+    This page provides information on how to call/use the API.
+    """
+    return flask.render_template('coprs/api.html')
+
+
+ at api_ns.route('/new/', methods = ["GET", "POST"])
+ at login_required
+def api_new_token():
+    """ Method use to generate a new API token for the current user.
+    """
+    user = flask.g.user
+    user.api_token = generate_api_token(app.config['API_TOKEN_LENGTH'])
+    user.api_token_expiration = datetime.date.today() \
+        + datetime.timedelta(days=30)
+    db.session.add(user)
+    db.session.commit()
+    flask.g.user = user
+    return flask.redirect(flask.url_for('misc.api'))
+
+
+ at api_ns.route('/add/<name>/<chroots>/',
+    defaults={'repos':"", 'initial_pkgs':""})
+ at api_ns.route('/add/<name>/<chroots>/<repos>/',
+    defaults={'initial_pkgs':None})
+ at api_ns.route('/add/<name>/<chroots>/<repos>/<initial_pkgs>/')
+ at login_required
+def api_add_copr(name, chroots, repos="", initial_pkgs=""):
+    """ Receive information from the user on how to create its new copr,
+    check their validity and create the corresponding copr.
+
+    :arg name: the name of the copr to add
+    :arg chroots: a comma separated list of chroots to use
+    :kwarg repos: a comma separated list of repository that this copr
+        can use.
+    :kwarg initial_pkgs: a comma separated list of initial packages to
+        build in this new copr
+
+    """
+    infos = []
+    copr = coprs_logic.CoprsLogic.add_coprs(
+        name=name.strip(),
+        repos=" ".join([repo.strip() for repo in repos.split(',')]),
+        owner=flask.g.user,
+        selected_chroots=[chroot.strip()
+            for chroot in chroots.split(',')]
+        )
+    infos.append('New copr was successfully created.')
+
+    if initial_pkgs:
+        builds_logic.BuildsLogic.add_build(
+            pkgs=" ".join([pkg.strip() for pkg in initial_pkgs.split(',')]),
+            copr=copr,
+            owner=flask.g.user)
+        infos.append('Initial packages were successfully submitted '
+                'for building.')
+    return '{"output" : "%s"}' % ("\n".join(infos))
+
+
+ at api_ns.route('/list/<username>/', methods=['GET'])
+def api_list_copr(username):
+    """ Return the list of coprs owned by the given user.
+
+    :arg username: the username of the person one would like to the
+        coprs of.
+    """
+    query = coprs_logic.CoprsLogic.get_multiple(flask.g.user,
+        user_relation = 'owned', username = username)
+    output = '{"repos":[\n'
+    repos = query.all()
+    cnt = 0
+    for repo in repos:
+        output += '{"name" : "%s", "repos" : "%s"}\n' % (
+            repo.name, repo.repos)
+        cnt = cnt + 1
+        if cnt < len(repos):
+            output += ','
+    output += "]}\n"
+    return output
diff --git a/coprs_frontend/coprs/views/misc.py b/coprs_frontend/coprs/views/misc.py
index e10b07f..c5cffc1 100644
--- a/coprs_frontend/coprs/views/misc.py
+++ b/coprs_frontend/coprs/views/misc.py
@@ -119,22 +119,3 @@ def backend_authenticated(f):
             return 'You have to provide the correct password', 401
         return f(*args, **kwargs)
     return decorated_function
-
-
- at misc.route('/api/')
-def api():
-    return flask.render_template('api.html',
-                                 user=flask.g.user)
-
-
- at misc.route('/api/new/', methods = ["GET", "POST"])
- at login_required
-def api_new_token():
-    user = flask.g.user
-    user.api_token = generate_api_token(app.config['API_TOKEN_LENGTH'])
-    user.api_token_expiration = datetime.date.today() \
-        + datetime.timedelta(days=30)
-    db.session.add(user)
-    db.session.commit()
-    flask.g.user = user
-    return flask.redirect(flask.url_for('misc.api'))



More information about the copr-devel mailing list