Commit 178455cc authored by colmoneill's avatar colmoneill

moving all of the classes structures to respect the MongoEngine empiric ways,...

moving all of the classes structures to respect the MongoEngine empiric ways, mostly to comply with Flask-Admin
parent 3649a08e
......@@ -4,7 +4,9 @@ from flask import Flask, render_template, redirect, flash, request
from flask_flatpages import FlatPages
import markdown
from flask_mongoengine import *
from flask_security import Security, MongoEngineUserDatastore, UserMixin, RoleMixin, login_required, current_user
from flask_security import Security, MongoEngineUserDatastore, UserMixin, RoleMixin, login_required, current_user, user_registered
from flask_login import user_logged_in
from flask_mail import Mail
from flask_security.forms import RegisterForm, StringField
from wtforms.validators import DataRequired
......@@ -20,7 +22,7 @@ from bson import ObjectId
from flask_admin import Admin
from flask_admin.form import rules
from flask_admin.contrib.pymongo import ModelView, filters
from flask_admin.contrib.mongoengine import ModelView, filters
from flask_admin.model.fields import InlineFormField, InlineFieldList
from wtforms import form, fields
......@@ -42,7 +44,7 @@ pages = FlatPages(app)
app.config['SECRET_KEY'] = 'super-secret'
app.config['SECURITY_REGISTERABLE'] = True
app.config['SECURITY_RECOVERABLE'] = True
#app.config['SECURITY_TRACKABLE'] = True
# app.config['SECURITY_TRACKABLE'] = True
app.config['SECURITY_PASSWORD_HASH'] = 'bcrypt'
app.config['SECURITY_PASSWORD_SALT'] = '$2a$16$PnnIgfMwkOjGX4SkHqSOPO'
......@@ -63,26 +65,30 @@ app.config['MONGODB_PORT'] = 27017
# Create database connection object
db = MongoEngine(app)
@app.before_first_request
def before_first_request():
user_datastore.find_or_create_role(name='admin', description='Administrator, adminstration view, sees these backend views ')
user_datastore.find_or_create_role(name='user', description='default for all users, status for students, teachers, anybody who uses the register form')
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
bdd = client.mydatabase
bddusers = bdd.users
class Role(db.Document, RoleMixin):
name = db.StringField(max_length=80, unique=True)
description = db.StringField(max_length=255)
# class Badges(db.Document):
# badge = db.StringField()
# lvl = db.StringField()
# badges_default =[
# {'cnc': '0'},
# {'laser': '0'},
# {'electronique': '0'},
# {'impression3d':'0'},
# {'maker':'0'}
# ]
def __unicode__(self):
return self.name
# badges = (('impression3d', '0'),
# ('cnc', '0'),
# ('laser', '0'))
# http://docs.mongoengine.org/guide/defining-documents.html#field-arguments
class Badges(db.Document):
name = db.StringField(unique=True)
value = db.StringField()
def __unicode__(self):
return self.name
class User(db.Document, UserMixin):
active = db.BooleanField(default=True)
......@@ -92,23 +98,43 @@ class User(db.Document, UserMixin):
password = db.StringField(max_length=255)
confirmed_at = db.DateTimeField()
# badges = db.DictField(default=[('cnc', 0),('laser', 0),('electronique', 0),('cad', 0),('impression3d', 0), ('maker', 0) ] )
roles = db.ListField( db.ReferenceField(Role), default=[])
badges = db.ListField(default=[{'name':'cnc', 'lvl': 0},{'name':'laser', 'lvl': 0},{'name':'electronique', 'lvl': 0},{'name':'cad', 'lvl': 0},{'name':'impression3d', 'lvl': 0},{'name':'maker', 'lvl': 0}])
# badges = db.ListField( db.embed(Badges))
dashboard_view_count = db.StringField(max_length=3)
roles = db.ListField( db.ReferenceField(Role))
# badges = db.ListField()
badges = db.ListField( db.ReferenceField(Badges))
# dashboard_view_count = db.StringField(max_length=3, default=[0])
class ExtendedRegisterForm(RegisterForm):
first_name = StringField('First Name', validators=[DataRequired()])
last_name = StringField('Last Name', validators=[DataRequired()])
def __unicode__(self):
return self.name
# Setup Flask-Security
user_datastore = MongoEngineUserDatastore(db, User, Role)
#security = Security(app, user_datastore)
security = Security(app, user_datastore, register_form=ExtendedRegisterForm)
@user_registered.connect_via(app)
def on_user_registered(sender, user, confirm_token):
default_role = user_datastore.find_role("user")
user_datastore.add_role_to_user(user, default_role)
default_badges = { "cnc" : { "lvl" : "0"}, "laser" : {"lvl" : "0"}, "impression3d" : {"lvl" : "0"} }
# user_datastore.add_badges_to_user(user, default_badges)
@user_logged_in.connect_via(app)
def attribute_empty_badges(sender, user):
print("user logged in")
print(user)
# if badg:
# print("user already has badges")
# else:
# print("no badges found, adding default array of badges with values at 0")
# default_badges = { "cnc" : { "lvl" : "0"}, "laser" : {"lvl" : "0"}, "impression3d" : {"lvl" : "0"} }
# User.objects(id = user).update(set__badges = default_badges, upsert=True)
@app.route("/")
@app.route("/index")
def index():
......@@ -227,7 +253,7 @@ def not_found(error):
def dashboard():
C_user = current_user.get_id()
print('user: ' + C_user)
db_user = User.objects.get(id=C_user).update(inc__dashboard_view_count=1, upsert=True)
#db_user = User.objects.get(id=C_user).update(inc__dashboard_view_count=1, upsert=True)
message = 'Welcome to your dashboard ' + current_user.get_id()
flash(message)
return render_template('dashboard.html', user=C_user)
......@@ -240,16 +266,23 @@ def dashboard():
class UserView(ModelView):
column_list = ('first_name', 'email' , 'badges')
column_list = ('first_name', 'email' , 'roles', 'badges')
column_sortable_list = ('email')
form = ExtendedRegisterForm
form_ajax_refs = {
'roles': {
'fields': ['name']
}
}
# form = ExtendedRegisterForm
# APP RUN
if __name__ == '__main__':
admin = Admin(app, name='bemaker', template_mode='bootstrap3')#,base_template='microblog_master.html')
admin.add_view(UserView(bdd.user, 'User'))
admin.add_view(UserView(User))
admin.add_view(ModelView(Role))
admin.add_view(ModelView(Badges))
app.run(host='0.0.0.0', debug=True)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment