diff --git a/__pycache__/config.cpython-35.pyc b/__pycache__/config.cpython-35.pyc new file mode 100644 index 0000000..fa47496 Binary files /dev/null and b/__pycache__/config.cpython-35.pyc differ diff --git a/app.db b/app.db new file mode 100644 index 0000000..2a6300d Binary files /dev/null and b/app.db differ diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..f558b5f --- /dev/null +++ b/app/__init__.py @@ -0,0 +1,8 @@ +from flask import Flask +from flask.ext.sqlalchemy import SQLAlchemy + +app = Flask(__name__) +app.config.from_object('config') +db = SQLAlchemy(app) + +from app import views, models \ No newline at end of file diff --git a/app/__pycache__/__init__.cpython-35.pyc b/app/__pycache__/__init__.cpython-35.pyc new file mode 100644 index 0000000..dafde68 Binary files /dev/null and b/app/__pycache__/__init__.cpython-35.pyc differ diff --git a/app/__pycache__/forms.cpython-35.pyc b/app/__pycache__/forms.cpython-35.pyc new file mode 100644 index 0000000..d242271 Binary files /dev/null and b/app/__pycache__/forms.cpython-35.pyc differ diff --git a/app/__pycache__/models.cpython-35.pyc b/app/__pycache__/models.cpython-35.pyc new file mode 100644 index 0000000..4d6a6bb Binary files /dev/null and b/app/__pycache__/models.cpython-35.pyc differ diff --git a/app/__pycache__/views.cpython-35.pyc b/app/__pycache__/views.cpython-35.pyc new file mode 100644 index 0000000..01f0e49 Binary files /dev/null and b/app/__pycache__/views.cpython-35.pyc differ diff --git a/app/forms.py b/app/forms.py new file mode 100644 index 0000000..3ea2d7e --- /dev/null +++ b/app/forms.py @@ -0,0 +1,8 @@ +from flask.ext.wtf import Form +from wtforms import StringField, BooleanField +from wtforms.validators import DataRequired + + +class LoginForm(Form): + openid = StringField('openid', validators=[DataRequired()]) + remember_me = BooleanField('remember_me', default=False) diff --git a/app/models.py b/app/models.py new file mode 100644 index 0000000..a2690ac --- /dev/null +++ b/app/models.py @@ -0,0 +1,9 @@ +from app import db + +class User(db.Model): + id = db.Column(db.Integer, primary_key = True) + nickname = db.Column(db.String(64), index = True, unique = True) + email = db.Column(db.String(120), index = True, unique = True) + + def __repr__(self): + return '' % (self.nickname) \ No newline at end of file diff --git a/app/templates/base.html b/app/templates/base.html new file mode 100644 index 0000000..1540718 --- /dev/null +++ b/app/templates/base.html @@ -0,0 +1,23 @@ + + + {% if title %} + {{title}} - microblog + {% else %} + microblog + {% endif %} + + +
HelloFlask: Home
+
+ {% with messages = get_flashed_messages() %} + {% if messages %} + + {% endif %} + {% endwith %} + {% block content %}{% endblock %} + + diff --git a/app/templates/index.html b/app/templates/index.html new file mode 100644 index 0000000..eea3bb6 --- /dev/null +++ b/app/templates/index.html @@ -0,0 +1,7 @@ +{% extends "base.html" %} +{% block content %} +

Hi, {{user.nickname}}!

+{% for post in posts %} +

{{post.author.nickname}} says: {{post.body}}

+{% endfor %} +{% endblock %} \ No newline at end of file diff --git a/app/templates/login.html b/app/templates/login.html new file mode 100644 index 0000000..567a9ed --- /dev/null +++ b/app/templates/login.html @@ -0,0 +1,35 @@ + +{% extends "base.html" %} + +{% block content %} + +

Sign In

+
+ {{ form.hidden_tag() }} +

+ Please enter your OpenID, or select one of the providers below:
+ {{ form.openid(size=80) }} + {% for error in form.openid.errors %} + [{{error}}] + {% endfor %}
+ |{% for pr in providers %} + {{ pr.name }} | + {% endfor %} +

+

{{ form.remember_me }} Remember Me

+

+
+{% endblock %} + diff --git a/app/views.py b/app/views.py new file mode 100644 index 0000000..e5f433e --- /dev/null +++ b/app/views.py @@ -0,0 +1,36 @@ +from flask import render_template, flash, redirect +from app import app +from .forms import LoginForm + + +@app.route('/') +@app.route('/index') +def index(): + user = {'nickname': 'Scott'} + posts = [ # fake array of posts + { + 'author': {'nickname': 'Lucine'}, + 'body': 'My beautiful girlfriend!' + }, + { + 'author': {'nickname': 'James'}, + 'body': 'The manager of Unigardens!' + } + ] + return render_template("index.html", + title='Home', + user=user, + posts=posts) + + +@app.route('/login', methods = ['GET', 'POST']) +def login(): + form = LoginForm() + if form.validate_on_submit(): + flash('Login requested for OpenID="' + form.openid.data + '", remember_me=' + str(form.remember_me.data)) + return redirect('/index') + return render_template('login.html', + title='Sign In', + form=form, + providers=app.config['OPENID_PROVIDERS']) + diff --git a/config.py b/config.py new file mode 100644 index 0000000..ecfa23e --- /dev/null +++ b/config.py @@ -0,0 +1,16 @@ +import os +basedir = os.path.abspath(os.path.dirname(__file__)) + +SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'app.db') +SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository') + + +CSRF_ENABLED = True +SECRET_KEY = 'password' + +OPENID_PROVIDERS = [ + { 'name': 'Google', 'url': 'https://www.google.com/accounts/o8/id' }, + { 'name': 'Yahoo', 'url': 'https://me.yahoo.com' }, + { 'name': 'AOL', 'url': 'http://openid.aol.com/' }, + { 'name': 'Flickr', 'url': 'http://www.flickr.com/' }, + { 'name': 'MyOpenID', 'url': 'https://www.myopenid.com' }] \ No newline at end of file diff --git a/db_create.py b/db_create.py new file mode 100644 index 0000000..b0850f8 --- /dev/null +++ b/db_create.py @@ -0,0 +1,12 @@ +#!flask/bin/python +from migrate.versioning import api +from config import SQLALCHEMY_DATABASE_URI +from config import SQLALCHEMY_MIGRATE_REPO +from app import db +import os.path +db.create_all() +if not os.path.exists(SQLALCHEMY_MIGRATE_REPO): + api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository') + api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) +else: + api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, api.version(SQLALCHEMY_MIGRATE_REPO)) \ No newline at end of file diff --git a/db_migrate.py b/db_migrate.py new file mode 100644 index 0000000..61dd068 --- /dev/null +++ b/db_migrate.py @@ -0,0 +1,15 @@ +#!flask/bin/python +import imp +from migrate.versioning import api +from app import db +from config import SQLALCHEMY_DATABASE_URI +from config import SQLALCHEMY_MIGRATE_REPO +migration = SQLALCHEMY_MIGRATE_REPO + '/versions/%03d_migration.py' % (api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) + 1) +tmp_module = imp.new_module('old_model') +old_model = api.create_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) +exec(old_model, tmp_module.__dict__) +script = api.make_update_script_for_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, tmp_module.meta, db.metadata) +open(migration, "wt").write(script) +api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) +print('New migration saved as ' + migration) +print('Current database version: ' + str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO))) diff --git a/db_repository/README b/db_repository/README new file mode 100644 index 0000000..6218f8c --- /dev/null +++ b/db_repository/README @@ -0,0 +1,4 @@ +This is a database migration repository. + +More information at +http://code.google.com/p/sqlalchemy-migrate/ diff --git a/db_repository/__init__.py b/db_repository/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/db_repository/__pycache__/__init__.cpython-35.pyc b/db_repository/__pycache__/__init__.cpython-35.pyc new file mode 100644 index 0000000..5d7327b Binary files /dev/null and b/db_repository/__pycache__/__init__.cpython-35.pyc differ diff --git a/db_repository/manage.py b/db_repository/manage.py new file mode 100644 index 0000000..554f89c --- /dev/null +++ b/db_repository/manage.py @@ -0,0 +1,5 @@ +#!/usr/bin/env python +from migrate.versioning.shell import main + +if __name__ == '__main__': + main() diff --git a/db_repository/migrate.cfg b/db_repository/migrate.cfg new file mode 100644 index 0000000..ec2db66 --- /dev/null +++ b/db_repository/migrate.cfg @@ -0,0 +1,25 @@ +[db_settings] +# Used to identify which repository this database is versioned under. +# You can use the name of your project. +repository_id=database repository + +# The name of the database table used to track the schema version. +# This name shouldn't already be used by your project. +# If this is changed once a database is under version control, you'll need to +# change the table name in each database too. +version_table=migrate_version + +# When committing a change script, Migrate will attempt to generate the +# sql for all supported databases; normally, if one of them fails - probably +# because you don't have that database installed - it is ignored and the +# commit continues, perhaps ending successfully. +# Databases in this list MUST compile successfully during a commit, or the +# entire commit will fail. List the databases your application will actually +# be using to ensure your updates to that database work properly. +# This must be a list; example: ['postgres','sqlite'] +required_dbs=[] + +# When creating new change scripts, Migrate will stamp the new script with +# a version number. By default this is latest_version + 1. You can set this +# to 'true' to tell Migrate to use the UTC timestamp instead. +use_timestamp_numbering=False diff --git a/db_repository/versions/001_migration.py b/db_repository/versions/001_migration.py new file mode 100644 index 0000000..96092d7 --- /dev/null +++ b/db_repository/versions/001_migration.py @@ -0,0 +1,19 @@ +from sqlalchemy import * +from migrate import * + + +from migrate.changeset import schema +pre_meta = MetaData() +post_meta = MetaData() + +def upgrade(migrate_engine): + # Upgrade operations go here. Don't create your own engine; bind + # migrate_engine to your metadata + pre_meta.bind = migrate_engine + post_meta.bind = migrate_engine + + +def downgrade(migrate_engine): + # Operations to reverse the above upgrade go here. + pre_meta.bind = migrate_engine + post_meta.bind = migrate_engine diff --git a/db_repository/versions/__init__.py b/db_repository/versions/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/db_repository/versions/__pycache__/001_migration.cpython-35.pyc b/db_repository/versions/__pycache__/001_migration.cpython-35.pyc new file mode 100644 index 0000000..9a45157 Binary files /dev/null and b/db_repository/versions/__pycache__/001_migration.cpython-35.pyc differ diff --git a/db_repository/versions/__pycache__/__init__.cpython-35.pyc b/db_repository/versions/__pycache__/__init__.cpython-35.pyc new file mode 100644 index 0000000..8349185 Binary files /dev/null and b/db_repository/versions/__pycache__/__init__.cpython-35.pyc differ diff --git a/run.py b/run.py new file mode 100644 index 0000000..178fd8a --- /dev/null +++ b/run.py @@ -0,0 +1,3 @@ +#!flask/bin/python +from app import app +app.run(debug=True)