Commits

Anonymous committed 0e540a3

first version of flaskkit

Comments (0)

Files changed (12)

+glob:*.pyc
+glob:*.bak
+glob:Thumbs.db
+glob:_design/ui/*.xds
+glob:data/sessions/*.__wz_sess
+glob:data/sessions/*.sess
+glob:data/caches/longterm/*
+glob:data/logs/*.log
+glob:data/logs/*.log.*
+glob:stat_upto.htm
+glob:success_channels.csv
+glob:stat_summary.htm
+glob:messages.htm
+glob:errors_ydt.csv
+glob:errors2.htm
+glob:errors.htm
+glob:accostable/
+glob:errors.html
+glob:errors-05211000.htm
+glob:errors_chongtu.htm
+glob:errors*.htm
+glob:stat_summary2.csv
+glob:debug-*.html
+glob:stat*.htm
+glob:*.egg-info
+eggs
+bin
+.installed.cfg

flaskkit/__init__.py

+# -*- encoding:utf-8 -*-
+import os
+from flask import Flask
+from flaskkit import settings
+from mongokit import Connection
+
+
+# create the little application object
+app = Flask(__name__)
+# app.config.from_pyfile(os.environ['flaskkit_settings'] or "settings.py")
+app.config.from_object(settings)
+
+# connect to the database
+connection = Connection(app.config['MONGODB_HOST'],
+                        app.config['MONGODB_PORT'])
+
+# import views
+import flaskkit.views
+from flask import Flask
+from mongokit import Connection, Document
+
+# configuration
+MONGODB_HOST = 'localhost'
+MONGODB_PORT = 27017
+
+# create the little application object
+app = Flask(__name__)
+app.config.from_object(__name__)
+
+# connect to the database
+connection = Connection(app.config['MONGODB_HOST'],
+                        app.config['MONGODB_PORT'])
+

flaskkit/models.py

+# -*- encoding:utf-8 -*-
+from mongokit import Document
+import datetime 
+
+from flaskkit import connection 
+
+class Entry(Document):
+    structure = {
+            'title':unicode,
+            'body':unicode,
+            'created':datetime.datetime,
+    }
+    required_fields = ['title','body', 'created']
+    default_values = {'created':datetime.datetime.utcnow,}
+    use_dot_notation = True
+
+class Log(Document):
+    structure = {
+            'author':unicode,
+            'text':unicode,
+            'created':datetime.datetime,
+    }
+    required_fields = ['author','created']
+    default_values = {'author':u'wayhome','created':datetime.datetime.utcnow,}
+    use_dot_notation = True
+
+#connection.register([Entry,Log])
+connection.register([Entry])
+connection.register([Log])

flaskkit/settings.py

+DEBUG = True
+SECRET_KEY = 'development key'
+MONGODB_HOST = 'localhost'
+MONGODB_PORT = 27017
+USERNAME = 'admin'
+PASSWORD = 'default'
+

flaskkit/static/style.css

+body { font: 16px/1.4 "Lucida Grande", "Lucida Sans Unicode", Lucida, Verdana, sans-serif; background: #eee; }
+h1, h2, a { color: #377ba8; }
+h1, h2 { font-family: "Georgia", "Palatino", serif; margin: 0; }
+h1 { border-bottom: 2px solid #eee; }
+h2 { font-size: 1.2em; }
+
+#page #content { margin: 2em auto; width: 35em; border: 5px solid #ccc; padding: 0.8em; background: #fff; }
+#page #content nav { display: block; text-align: right; font-size: 0.8em; padding: 0.3em; margin-bottom: 1em; background: #fafafa; }
+#page #content form { font-size: 0.9em; border-bottom: 1px solid #ccc; margin-bottom: 0.8em; }
+#page #content label { font-weight: bold; color: #377ba8; }
+
+.flash { background: #cee5f5; padding: 0.5em; border: 1px solid #aacbe2; }
+.error { background: #f0d6d6; padding: 0.5em; }

flaskkit/templates/layout.html

+<!DOCTYPE html>
+<html>
+  <head>
+    <title>FlaskKit</title>
+    <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}" type="text/css">
+  </head>
+  <body id="page">
+    <div id="content">
+      <h1>FlaskKit</h1>
+      <nav>
+        {% if not session.logged_in %}
+          <a href="{{ url_for('login') }}">log in</a>
+        {% else %}
+          <a href="{{ url_for('logout') }}">log out</a>
+        {% endif %}
+      </nav>
+      {% for message in get_flashed_messages() %}
+        <p class="flash">{{ message }}</p>
+      {% endfor %}
+      {% block body %}{% endblock %}
+    </div>
+  </body>
+</html>

flaskkit/templates/login.html

+{% extends "layout.html" %}
+{% block body %}
+  <h2>Login</h2>
+  {% if error %}
+    <p class="error"><strong>Error:</strong> {{ error }}</p>
+  {% endif %}
+  <form action="{{ url_for('login') }}" method="post">
+    <p>
+      <label for="username">Username:</label><br />
+      <input type="text" size="30" name="username" />
+    </p>
+    <p>
+      <label for="password">Password:</label><br />
+      <input type="password" size="30" name="password" />
+    </p>
+    <p><input type="submit" value="Login" /></p>
+  </form>
+{% endblock %}

flaskkit/templates/show_entries.html

+{% extends "layout.html" %}
+{% block body %}
+  {% if session.logged_in %}
+    <form action="{{ url_for('add_entry') }}" method="post">
+      <p>
+        <label for="title">Title:</label><br />
+        <input type="text" size="50" name="title" />
+      </p>
+      <p>
+        <label for="text">Body:</label><br />
+        <textarea name="body" rows="10" cols="48"></textarea>
+      </p>
+      <p><input type="submit" value="Share" /></p>
+    </form>
+  {% endif %}
+  <section>
+    {% for entry in entries %}
+      <article>
+        <h2>{{ entry.title }}</h2>
+        {{ entry.body|markdown|safe }}
+      </article>
+    {% else %}
+      <p>Unbelievable. No entries so far.</p>
+    {% endfor %}
+  </section>
+{% endblock %}

flaskkit/views.py

+# -*- encoding:utf-8 -*-
+import markdown
+from flask import render_template,session,abort,flash,redirect,url_for,jsonify,request
+
+from flaskkit import app,connection
+from flaskkit.models import Entry,Log
+
+@app.route('/')
+def show_entries():
+    entries = connection.flaskkit.entries.find()
+    return render_template('show_entries.html', entries=entries)
+
+@app.route('/add', methods=['POST'])
+def add_entry():
+    if not session.get('logged_in'):
+        abort(401)
+    entry = connection.flaskkit.entries.Entry()
+    entry.title = request.form['title']
+    entry.body = request.form['body']
+    entry.save()
+    log = connection.logdb.logs.Log()
+    log.text = u"added entry: %s "%entry.title
+    log.save()
+    print log
+    flash('New entry was successfully posted')
+    return redirect(url_for('show_entries'))
+
+@app.route('/login', methods=['GET', 'POST'])
+def login():
+    error = None
+    if request.method == 'POST':
+        if request.form['username'] != app.config['USERNAME']:
+            error = 'Invalid username'
+        elif request.form['password'] != app.config['PASSWORD']:
+            error = 'Invalid password'
+        else:
+            session['logged_in'] = True
+            flash('You have been logged in')
+            return redirect(url_for('show_entries'))
+    return render_template('login.html', error=error)
+
+@app.route('/logout')
+def logout():
+    session.pop('logged_in', None)
+    flash('You have been logged out')
+    return redirect(url_for('show_entries'))
+
+
+@app.template_filter('markdown')
+def to_markdown(value):
+    """Converts a string into valid Markdown."""
+    return markdown.markdown(value)
+git+http://github.com/mitsuhiko/flask.git
+Markdown>=2.0.3
+Pygments>=1.3.1
+mongokit
+from flaskkit import app
+app.run(debug=True)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.