changeset 19:f5555074f192

Add form for user and classifiers
author Cédric Krier <ced@b2ck.com>
date Tue, 24 Feb 2015 15:09:10 +0100
parents eb7646550417
children e5d8e642fb0a
files farmhouse.py templates/classifiers.html templates/index.html templates/layout.html templates/user.html
diffstat 5 files changed, 120 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/farmhouse.py	Tue Feb 24 01:13:47 2015 +0100
+++ b/farmhouse.py	Tue Feb 24 15:09:10 2015 +0100
@@ -20,6 +20,8 @@
 from flask.ext.bootstrap import Bootstrap
 from flask.ext.cache import Cache
 
+import requests
+
 app = Flask(__name__)
 cache = Cache(app, config={'CACHE_TYPE': 'simple'})
 Bootstrap(app)
@@ -87,17 +89,39 @@
     return render_atom(packages)
 
 
+@app.route('/user')
+def user():
+    user = request.args.get('user')
+    if not user:
+        return render_template('user.html')
+    else:
+        return redirect(url_for('user_atom', user=user))
+
+
 @app.route('/user/<user>')
 @cache.cached(timeout=2 * 60 * 60)
-def user(user):
+def user_atom(user=None):
     packages = get_user_packages(user)
     return render_atom(packages, title=user)
 
 
+@app.route('/classifiers')
+def classifiers():
+    classifiers = request.args.getlist('classifiers')
+    if not classifiers:
+        return render_template('classifiers.html',
+            classifiers=get_classifiers())
+    if len(classifiers) == 1:
+        classifier, = classifiers
+        return redirect(url_for('classifier_atom', classifier=classifier))
+    else:
+        return redirect(url_for('classifier_atom', classifiers=classifiers))
+
+
 @app.route('/classifier')
 @app.route('/classifier/<classifier>')
 @cache.cached(timeout=2 * 60 * 60, key_prefix=lambda: request.url)
-def classifier(classifier=None):
+def classifier_atom(classifier=None):
     classifiers = request.args.getlist('classifiers')
     if classifier:
         classifiers.append(classifier)
@@ -174,6 +198,12 @@
     return sorted({p for p, _ in client.browse(classifiers)})
 
 
+@cache.memoize(timeout=24 * 60 * 60)
+def get_classifiers():
+    r = requests.get('https://pypi.python.org/pypi?%3Aaction=list_classifiers')
+    return r.text.splitlines()
+
+
 class Release:
     def __init__(self, name, version, url):
         self.name = name
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/classifiers.html	Tue Feb 24 15:09:10 2015 +0100
@@ -0,0 +1,18 @@
+{% extends "layout.html" %}
+{% block form %}
+<div class="form-group">
+    <label for="classifiers" class="sr-only">
+        User
+    </label>
+    <select type="text" class="form-control"
+        id="classifiers" name="classifiers" size="10"
+        placeholder="Classifiers"
+        multiple autofocus>
+        {% for classifier in classifiers %}
+        <option value="{{ classifier }}">
+        {{ classifier }}
+        </option>
+        {% endfor %}
+    </select>
+</div>
+{% endblock %}
--- a/templates/index.html	Tue Feb 24 01:13:47 2015 +0100
+++ b/templates/index.html	Tue Feb 24 15:09:10 2015 +0100
@@ -1,45 +1,12 @@
-{% extends "bootstrap/base.html" %}
-{% block title %}Farmhouse{% endblock %}
-{% block navbar %}
-<div class="navbar navbar-default" role="navigation">
-    <div class="container">
-        <div class="navbar-header">
-            <a class="navbar-brand" href="/">Farmhouse</a>
-        </div>
-    </div>
-</div>
-{% for message in get_flashed_messages() %}
-<div class="alert alert-info">
-    <button class="close" data-dismiss="alert">×</button>
-    {{ message }}
-</div>
-{% endfor%}
-{% endblock %}
-{% block content %}
-<div class="container">
-    <div class="row">
-        <div class="col-md-12">
-            <h1 class="hidden-xs hidden-sm">Farmhouse</h1>
-            <p class="hidden-xs hidden-sm">
-            Atom feeds for Warehouse (pypi.python.org)
-            </p>
-            <form>
-                <div class="form-group">
-                    <label for="requirements" class="sr-only">
-                        Requirements
-                    </label>
-                    <textarea type="textarea" class="form-control"
-                        id="requirements" name="r" rows="5"
-                        placeholder="Requirements (case sensitive)"
-                        autofocus>{{ request.args.get('r', '') }}</textarea>
-                </div>
-                <button type="submit" class="btn btn-lg btn-primary btn-block">
-                    Fetch
-                </button>
-            </form>
-        </div>
-    </div>
+{% extends "layout.html" %}
+{% block form %}
+<div class="form-group">
+    <label for="requirements" class="sr-only">
+        Requirements
+    </label>
+    <textarea type="textarea" class="form-control"
+        id="requirements" name="r" rows="5"
+        placeholder="Requirements (case sensitive)"
+        autofocus>{{ request.args.get('r', '') }}</textarea>
 </div>
 {% endblock %}
-{% block scripts %}
-{% endblock%}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/layout.html	Tue Feb 24 15:09:10 2015 +0100
@@ -0,0 +1,48 @@
+{% extends "bootstrap/base.html" %}
+{% block title %}Farmhouse{% endblock %}
+{% block navbar %}
+<div class="navbar navbar-default" role="navigation">
+    <div class="container">
+        <div class="navbar-header">
+            <button type="button" class="navbar-toggle collapsed"
+                data-toggle="collapse" data-target="#options">
+                <span class="sr-only">Options</span>
+                <span class="icon-bar"></span>
+                <span class="icon-bar"></span>
+                <span class="icon-bar"></span>
+            </button>
+            <a class="navbar-brand" href="/">Farmhouse</a>
+        </div>
+        <div class="collapse navbar-collapse" id="options">
+            <ul class="nav navbar-nav">
+                <li><a href="/user">per User</a></li>
+                <li><a href="/classifiers">per Classifiers</a></li>
+            </ul>
+        </div>
+    </div>
+</div>
+{% for message in get_flashed_messages() %}
+<div class="alert alert-info">
+    <button class="close" data-dismiss="alert">×</button>
+    {{ message }}
+</div>
+{% endfor %}
+{% endblock %}
+{% block content %}
+<div class="container">
+    <div class="row">
+        <div class="col-md-12">
+            <h1 class="hidden-xs hidden-sm">Farmhouse</h1>
+            <p class="hidden-xs hidden-sm">
+            Atom feeds for Warehouse (pypi.python.org)
+            </p>
+            <form>
+                {% block form %}{% endblock %}
+                <button type="submit" class="btn btn-lg btn-primary btn-block">
+                    Fetch
+                </button>
+            </form>
+        </div>
+    </div>
+</div>
+{% endblock %}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/user.html	Tue Feb 24 15:09:10 2015 +0100
@@ -0,0 +1,12 @@
+{% extends "layout.html" %}
+{% block form %}
+<div class="form-group">
+    <label for="user" class="sr-only">
+        User
+    </label>
+    <input type="text" class="form-control"
+        id="user" name="user"
+        placeholder="User"
+        autofocus></input>
+</div>
+{% endblock %}