diff options
author | Sam | 2013-05-10 01:31:50 +0000 |
---|---|---|
committer | Sam | 2013-05-10 01:31:50 +0000 |
commit | 8ff6e6a1b7f4e435eb06130b82cce579bcd32dcc (patch) | |
tree | 481f3ad5b6033bb90980a96251a5e8768a06dd69 /wqflask | |
parent | 408802949965f232224b07aee051477d06a30448 (diff) | |
download | genenetwork2-8ff6e6a1b7f4e435eb06130b82cce579bcd32dcc.tar.gz |
Have the beginnings of a user_manager working
Diffstat (limited to 'wqflask')
-rw-r--r-- | wqflask/secure_server.py | 35 | ||||
-rw-r--r-- | wqflask/wqflask/model.py | 39 | ||||
-rw-r--r-- | wqflask/wqflask/templates/admin/user_manager.html | 38 | ||||
-rw-r--r-- | wqflask/wqflask/templates/base.html | 25 | ||||
-rw-r--r-- | wqflask/wqflask/templates/security/_macros.html | 2 | ||||
-rw-r--r-- | wqflask/wqflask/templates/security/login_user.html | 7 | ||||
-rw-r--r-- | wqflask/wqflask/user_manager.py | 22 | ||||
-rw-r--r-- | wqflask/wqflask/views.py | 25 |
8 files changed, 138 insertions, 55 deletions
diff --git a/wqflask/secure_server.py b/wqflask/secure_server.py index 60f6db44..3487f35b 100644 --- a/wqflask/secure_server.py +++ b/wqflask/secure_server.py @@ -3,8 +3,6 @@ from __future__ import print_function, division, absolute_import from wqflask import app from flask import Flask, render_template -from flask.ext.sqlalchemy import SQLAlchemy -from flask.ext.security import Security, SQLAlchemyUserDatastore, UserMixin, RoleMixin # Setup mail @@ -12,38 +10,7 @@ from flask.ext.mail import Mail mail = Mail(app) -# Create database connection object -db = SQLAlchemy(app) - -# Define models -roles_users = db.Table('roles_users', - db.Column('user_id', db.Integer(), db.ForeignKey('user.id')), - db.Column('role_id', db.Integer(), db.ForeignKey('role.id'))) - -class Role(db.Model, RoleMixin): - id = db.Column(db.Integer(), primary_key=True) - name = db.Column(db.String(80), unique=True) - description = db.Column(db.String(255)) - -class User(db.Model, UserMixin): - id = db.Column(db.Integer(), primary_key=True) - email = db.Column(db.String(255), unique=True) - password = db.Column(db.String(255)) - active = db.Column(db.Boolean()) - confirmed_at = db.Column(db.DateTime()) - - last_login_at = db.Column(db.DateTime()) - current_login_at = db.Column(db.DateTime()) - last_login_ip_= db.Column(db.String(39)) - current_login_ip = db.Column(db.String(39)) - login_count = db.Column(db.Integer()) - - roles = db.relationship('Role', secondary=roles_users, - backref=db.backref('users', lazy='dynamic')) - -# Setup Flask-Security -user_datastore = SQLAlchemyUserDatastore(db, User, Role) -security = Security(app, user_datastore) +from wqflask.model import * # Create a user to test with ##@app.before_first_request diff --git a/wqflask/wqflask/model.py b/wqflask/wqflask/model.py new file mode 100644 index 00000000..be86ce77 --- /dev/null +++ b/wqflask/wqflask/model.py @@ -0,0 +1,39 @@ +from __future__ import print_function, division, absolute_import + +from flask.ext.sqlalchemy import SQLAlchemy +from flask.ext.security import Security, SQLAlchemyUserDatastore, UserMixin, RoleMixin + +from wqflask import app + +# Create database connection object +db = SQLAlchemy(app) + +# Define models +roles_users = db.Table('roles_users', + db.Column('user_id', db.Integer(), db.ForeignKey('user.id')), + db.Column('role_id', db.Integer(), db.ForeignKey('role.id'))) + +class Role(db.Model, RoleMixin): + id = db.Column(db.Integer(), primary_key=True) + name = db.Column(db.String(80), unique=True) + description = db.Column(db.String(255)) + +class User(db.Model, UserMixin): + id = db.Column(db.Integer(), primary_key=True) + email = db.Column(db.String(255), unique=True) + password = db.Column(db.String(255)) + active = db.Column(db.Boolean()) + confirmed_at = db.Column(db.DateTime()) + + last_login_at = db.Column(db.DateTime()) + current_login_at = db.Column(db.DateTime()) + last_login_ip = db.Column(db.String(39)) + current_login_ip = db.Column(db.String(39)) + login_count = db.Column(db.Integer()) + + roles = db.relationship('Role', secondary=roles_users, + backref=db.backref('users', lazy='dynamic')) + +# Setup Flask-Security +user_datastore = SQLAlchemyUserDatastore(db, User, Role) +security = Security(app, user_datastore) diff --git a/wqflask/wqflask/templates/admin/user_manager.html b/wqflask/wqflask/templates/admin/user_manager.html new file mode 100644 index 00000000..c49f0d94 --- /dev/null +++ b/wqflask/wqflask/templates/admin/user_manager.html @@ -0,0 +1,38 @@ +{% extends "base.html" %} +{% block title %}User Manager{% endblock %} +{% block content %} +<!-- Start of body --> + <header class="jumbotron subhead" id="overview"> + <div class="container"> + <h1>User Manager</h1> + </div> + </header> + + <div class="container"> + <div class="page-header"> + <h1>User Manager</h1> + </div> + + <table class="table table-hover"> + <thead> + <tr> + <th>Email</th> + <th>Confirmed at</th> + <th>Active</th> + </tr> + </thead> + {% for user in users %} + <tr> + <td title="{{ user.__dict__ }}">{{ user.email }}</td> + <td>{{ user.confirmed_at }}</td> + <td>{{ user.active }}</td> + </tr> + {% endfor %} + </table> + + + </div> + +<!-- End of body --> + +{% endblock %} diff --git a/wqflask/wqflask/templates/base.html b/wqflask/wqflask/templates/base.html index 6f4b8e1e..69117d89 100644 --- a/wqflask/wqflask/templates/base.html +++ b/wqflask/wqflask/templates/base.html @@ -66,8 +66,13 @@ <a href="/links">Links</a> </li> <li class=""> - <a href="/register" data-remote="/register" + {% if g.identity.name=="anon" %} + <a href="/login" data-remote="/login" data-target="#utility" data-toggle="modal">Login</a> + {% else %} + <a href="/logout" data-remote="/logout" + data-target="#utility" data-toggle="modal">Logout</a> + {% endif %} </li> </ul> </div> @@ -144,17 +149,17 @@ <!--http://stackoverflow.com/questions/14045515/how-can-i-reuse-one-bootstrap-modal-div--> <!-- Modal --> <div id="utility" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> - <div class="modal-header"> - <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> - <h3 id="myModalLabel">Click outside modal to close it</h3> - </div> + <!--<div class="modal-header">--> + <!-- <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>--> + <!-- <h3 id="myModalLabel">Register</h3>--> + <!--</div>--> <div class="modal-body"> - <p>One fine body…this is getting replaced with content that comes from passed-in href</p> - </div> - <div class="modal-footer"> - <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button> - <button class="btn btn-primary">Save changes</button> + <p>.</p> </div> + <!--<div class="modal-footer">--> + <!-- <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>--> + <!-- <button class="btn btn-primary">Save changes</button>--> + <!--</div>--> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> diff --git a/wqflask/wqflask/templates/security/_macros.html b/wqflask/wqflask/templates/security/_macros.html index 8575f3db..919b979a 100644 --- a/wqflask/wqflask/templates/security/_macros.html +++ b/wqflask/wqflask/templates/security/_macros.html @@ -13,4 +13,4 @@ {% macro render_field(field) %} <p>{{ field(**kwargs)|safe }}</p> -{% endmacro %}
\ No newline at end of file +{% endmacro %} diff --git a/wqflask/wqflask/templates/security/login_user.html b/wqflask/wqflask/templates/security/login_user.html index d781ce08..b5fcbfdc 100644 --- a/wqflask/wqflask/templates/security/login_user.html +++ b/wqflask/wqflask/templates/security/login_user.html @@ -5,8 +5,13 @@ {{ login_user_form.hidden_tag() }} {{ render_field_with_errors(login_user_form.email) }} {{ render_field_with_errors(login_user_form.password) }} + + {% if security.recoverable %} + <a href="{{ url_for_security('forgot_password') }}">Forgot your password?</a><br/> + {% endif %} + {{ render_field_with_errors(login_user_form.remember) }} {{ render_field(login_user_form.next) }} {{ render_field(login_user_form.submit) }} </form> -{% include "security/_menu.html" %}
\ No newline at end of file +<!--{% include "security/_menu.html" %}--> diff --git a/wqflask/wqflask/user_manager.py b/wqflask/wqflask/user_manager.py new file mode 100644 index 00000000..4d608dc7 --- /dev/null +++ b/wqflask/wqflask/user_manager.py @@ -0,0 +1,22 @@ +from __future__ import print_function, division, absolute_import + +"""Access things in template like this: + + x: {{ g.identity.name }} + security: {{ security.__dict__ }} + +""" + +from wqflask import model + +from flask import Flask, g + +#from app import db +print("globals are:", globals()) + + +class UserManager(object): + def __init__(self): + self.users = model.User.query.all() + print("Users are:", self.users) + diff --git a/wqflask/wqflask/views.py b/wqflask/wqflask/views.py index 8531561a..dadce4d5 100644 --- a/wqflask/wqflask/views.py +++ b/wqflask/wqflask/views.py @@ -35,6 +35,8 @@ from utility.benchmark import Bench from pprint import pformat as pf +from wqflask import user_manager + #import logging #logging.basicConfig(filename="/tmp/gn_log", level=logging.INFO) #_log = logging.getLogger("correlation") @@ -166,21 +168,21 @@ def marker_regression_page(): 'dataset', 'suggestive' ) - + start_vars = {} for key, value in initial_start_vars.iteritems(): if key in wanted or key.startswith(('value:')): start_vars[key] = value - + version = "v14" key = "marker_regression:{}:".format(version) + json.dumps(start_vars, sort_keys=True) with Bench("Loading cache"): result = Redis.get(key) - + #print("************************ Starting result *****************") #print("result is [{}]: {}".format(type(result), result)) #print("************************ Ending result ********************") - + if result: print("Cache hit!!!") with Bench("Loading results"): @@ -194,17 +196,17 @@ def marker_regression_page(): indent=" ") result = template_vars.__dict__ - + #for item in template_vars.__dict__.keys(): # print(" ---**--- {}: {}".format(type(template_vars.__dict__[item]), item)) - + #causeerror Redis.set(key, pickle.dumps(result)) Redis.expire(key, 60*60) - + with Bench("Rendering template"): rendered_template = render_template("marker_regression.html", **result) - + return rendered_template @@ -234,6 +236,11 @@ def get_temp_data(): temp_uuid = request.args['key'] return flask.jsonify(temp_data.TempData(temp_uuid).get_all()) +@app.route("/users") +def manage_users(): + template_vars = user_manager.UserManager() + return render_template("admin/user_manager.html", **template_vars.__dict__) + def json_default_handler(obj): '''Based on http://stackoverflow.com/a/2680060/1175849''' @@ -251,4 +258,4 @@ def json_default_handler(obj): # return None else: raise TypeError, 'Object of type %s with value of %s is not JSON serializable' % ( - type(obj), repr(obj))
\ No newline at end of file + type(obj), repr(obj)) |