Commit 8484f566 authored by colmoneill's avatar colmoneill

* migrates to default badge dict on user creation

* creates default user on launch if admin does not exist
* attributes a default role to registered users
* restricts certain admin views to users that have the admin role
parent d4000d71
#!/usr/bin/python
# -*- coding: <utf-8> -*-
from flask import Flask, render_template, redirect, flash, request
from flask import Flask, render_template, redirect, flash, request, url_for
from flask_flatpages import FlatPages
import markdown
from flask_mongoengine import *
......@@ -65,12 +65,6 @@ 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')
class Role(db.Document, RoleMixin):
name = db.StringField(max_length=80, unique=True)
description = db.StringField(max_length=255)
......@@ -78,17 +72,39 @@ class Role(db.Document, RoleMixin):
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
# badges = {
# 'impression3d': '0',
# 'cnc': '0',
# 'laser': '0',
# 'maker': '0',
# 'electronique': '0'
# }
badges = {
"cnc": {
"lvl": "0"
},
"laser": {
"lvl": "0"
},
"impression3d": {
"lvl": "0"
},
"maker": {
"lvl": "0"
},
"electronique": {
"lvl": "0"
}
}
# 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)
......@@ -97,11 +113,11 @@ class User(db.Document, UserMixin):
email = db.StringField(max_length=255)
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))
# badges = db.ListField()
badges = db.ListField( db.ReferenceField(Badges))
badges = db.DictField(default = badges)
roles = db.ListField(db.ReferenceField(Role))
# badges = db.ListField( db.ReferenceField(Badges))
# dashboard_view_count = db.StringField(max_length=3, default=[0])
meta = {'strict': False}
class ExtendedRegisterForm(RegisterForm):
......@@ -117,17 +133,28 @@ user_datastore = MongoEngineUserDatastore(db, User, Role)
security = Security(app, user_datastore, register_form=ExtendedRegisterForm)
@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')
@app.before_first_request
def create_user():
# check that we have at least one user that has admin role, if existing, continue
if not user_datastore.get_user('admin@bemaker.eu'):
print('creating default admin user because unexisting')
user_datastore.create_user(email='admin@bemaker.eu', password='xedmewv)Faf8#yhQ', roles=['admin'])
@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)
# @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:
......@@ -140,7 +167,7 @@ def attribute_empty_badges(sender, user):
def index():
eventbrite_json = ur.urlopen(eventbrite_url_requete)
events = json.loads(eventbrite_json.read().decode())
return render_template("home.html" ,events=events)
return render_template("home.html" , events=events)
@app.route("/learn")
def learn():
......@@ -216,9 +243,9 @@ def check_quiz(id):
print('award badge ' + badge)
#bdd.user.update({"_id": ObjectId(user), "badges.name": badge}, {"$set": {"badges.$.lvl": 1}}, multi=True)
# .objects(id=post.id).update(title='Example Post')
update_Query = str('"set__badges__' + badge + '__lvl = 10"')
update_Query = str('"set__badges__' + badge + '__lvl = 1", upsert = True')
print(update_Query)
User.objects(id = C_user).update(update_Query ,True)#set__badges__laser__lvl = 22 , upsert=True) #{'badge.lvl' : 1 }, upsert = True )
User.objects(id = C_user).update(update_Query)#set__badges__laser__lvl = 22 , upsert=True) #{'badge.lvl' : 1 }, upsert = True )
# print({ "badges.name": badge,"_id": ObjectId(user)}, {"$set": {"badges.$.lvl": 2}})
#({'badges.name': 'electronique', '_id': ObjectId('5b3f847e5087266741cf1fef')}, {'$set': {'badges.$.lvl': 1}})
......@@ -258,15 +285,25 @@ def dashboard():
flash(message)
return render_template('dashboard.html', user=C_user)
def inaccessible_callback(self, name, **kwargs):
# redirect to login page if user doesn't have access
return redirect(url_for('security.login', next=request.url))
# class UserForm(form.Form):
# name = fields.StringField('Name')
# email = fields.StringField('Email')
# password = fields.StringField('Password')
class UserView(ModelView):
column_list = ('first_name', 'email' , 'roles', 'badges')
def is_accessible(self):
return current_user.has_role('admin')
def inaccessible_callback(self, name, **kwargs):
# redirect to login page if user doesn't have access
return redirect(url_for('security.login', next=request.url))
column_list = ('first_name', 'email' , 'roles')
column_sortable_list = ('email')
......@@ -276,13 +313,20 @@ class UserView(ModelView):
}
}
# form = ExtendedRegisterForm
class RoleView(ModelView):
def is_accessible(self):
return current_user.has_role('admin')
def inaccessible_callback(self, name, **kwargs):
# redirect to login page if user doesn't have access
return redirect(url_for('security.login', next=request.url))
# APP RUN
if __name__ == '__main__':
admin = Admin(app, name='bemaker', template_mode='bootstrap3')#,base_template='microblog_master.html')
admin.add_view(UserView(User))
admin.add_view(ModelView(Role))
admin.add_view(ModelView(Badges))
admin.add_view(RoleView(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