From 1cf6894c7416f879fcd114ae7358bb7d4169a677 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 9 May 2013 22:28:22 +0000 Subject: Working on user registration --- wqflask/secure_server.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'wqflask/secure_server.py') diff --git a/wqflask/secure_server.py b/wqflask/secure_server.py index 5448dbeb..60f6db44 100644 --- a/wqflask/secure_server.py +++ b/wqflask/secure_server.py @@ -7,6 +7,11 @@ from flask.ext.sqlalchemy import SQLAlchemy from flask.ext.security import Security, SQLAlchemyUserDatastore, UserMixin, RoleMixin +# Setup mail +from flask.ext.mail import Mail +mail = Mail(app) + + # Create database connection object db = SQLAlchemy(app) @@ -26,13 +31,13 @@ class User(db.Model, UserMixin): 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')) -- cgit v1.2.3 From 8ff6e6a1b7f4e435eb06130b82cce579bcd32dcc Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 10 May 2013 01:31:50 +0000 Subject: Have the beginnings of a user_manager working --- wqflask/secure_server.py | 35 +------------------ wqflask/wqflask/model.py | 39 ++++++++++++++++++++++ wqflask/wqflask/templates/admin/user_manager.html | 38 +++++++++++++++++++++ wqflask/wqflask/templates/base.html | 25 ++++++++------ wqflask/wqflask/templates/security/_macros.html | 2 +- wqflask/wqflask/templates/security/login_user.html | 7 +++- wqflask/wqflask/user_manager.py | 22 ++++++++++++ wqflask/wqflask/views.py | 25 +++++++++----- 8 files changed, 138 insertions(+), 55 deletions(-) create mode 100644 wqflask/wqflask/model.py create mode 100644 wqflask/wqflask/templates/admin/user_manager.html create mode 100644 wqflask/wqflask/user_manager.py (limited to 'wqflask/secure_server.py') 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 %} + +
+
+

User Manager

+
+
+ +
+ + + + + + + + + + + {% for user in users %} + + + + + + {% endfor %} +
EmailConfirmed atActive
{{ user.email }}{{ user.confirmed_at }}{{ user.active }}
+ + +
+ + + +{% 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 @@ Links
  • - Login + {% else %} + Logout + {% endif %}
  • @@ -144,17 +149,17 @@ 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) %}

    {{ field(**kwargs)|safe }}

    -{% 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 %} + Forgot your password?
    + {% endif %} + {{ render_field_with_errors(login_user_form.remember) }} {{ render_field(login_user_form.next) }} {{ render_field(login_user_form.submit) }} -{% include "security/_menu.html" %} \ No newline at end of file + 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)) -- cgit v1.2.3