Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 115 lines (94 sloc) 2.92 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
# -*- coding: utf-8 -*-
"""
Marcel views

:copyright: (c) 2011 By Ori Livneh
:license: GPLv3, see LICENSE for more details.
"""
   
from flask import (
    abort,
    flash,
    g,
    redirect,
    render_template,
    request,
    session,
    url_for
)

from flask.views import MethodView

from marcel import app, oid
from marcel.forms import EntryForm
from marcel.models import User, offers, requests, EntryManager


#
# Contexts
#

# Make the authentication status available in template contexts
@app.context_processor
def inject_authentication_status():
    return dict(authenticated=hasattr(g.user, 'uuid'))

# Embed the current user (if any) in the request context
@app.before_request
def lookup_current_user():
    g.user = None
    if 'openid' in session:
        g.user = User(openid=session['openid'])


#
# Login Views
#

@app.route('/login', methods=['GET', 'POST'])
@oid.loginhandler
def login():
    if g.user is not None:
        return redirect(oid.get_next_url())
    providers = app.config['OPENID_PROVIDERS']
    openid = None
    if request.method == 'GET' and 'provider' in request.args:
        openid = providers.get(request.args.get('provider'))
    elif request.method == 'POST':
        openid = request.form.get('openid')
    if openid:
        return oid.try_login(openid, ask_for=['email'])
    return render_template('login.html',
                            next=oid.get_next_url(),
                            error=oid.fetch_error())


@app.route('/logout')
def logout():
    session.pop('openid', None)
    flash('You were signed out')
    return redirect(oid.get_next_url())


@oid.after_login
def after_login(resp):
    session['openid'] = resp.identity_url
    user = User(openid=resp.identity_url)
    if not user.exists():
        user.set(identity_url=resp.identity_url, email=resp.email)
    flash('Successfully signed in')
    g.user = user
    return redirect(oid.get_next_url())


#
# Entry Views
#

class EntryAPI(MethodView):
    def get(self):
        form = EntryForm()
        return render_template('show_entries.html',
                               form=form,
                               offers=offers.all(),
                               requests=requests.all())

    def post(self):
        form = EntryForm()
        if form.validate():
            flash("Success")
            entry_manager = EntryManager(form.entry_type.data)
            entry_manager.add(
                user=g.user,
                summary=form.summary.data,
                details=form.details.data,
                contact_info=form.contact_info.data
            )
        else:
            flash("Error")
        return render_template('show_entries.html',
                               form=form,
                               offers=offers.all(),
                               requests=requests.all())


app.add_url_rule('/', view_func=EntryAPI.as_view('entries'))
Something went wrong with that request. Please try again.