Commit 6999ad20 authored by erreur401's avatar erreur401

Merge branch 'master' of gitlab.com:bemaker/eu.bemaker.flask

parents e2aa3d9e faf847ea
......@@ -94,6 +94,12 @@ app.config['MONGODB_PORT'] = 27017
# Create database connection object
db = MongoEngine(app)
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)
......@@ -101,14 +107,6 @@ class Role(db.Document, RoleMixin):
def __unicode__(self):
return self.name
# badges = {
# 'impression3d': '0',
# 'cnc': '0',
# 'laser': '0',
# 'maker': '0',
# 'electronique': '0'
# }
badges = {
"cnc": {
"lvl": "0"
......@@ -127,14 +125,6 @@ badges = {
}
}
# 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)
first_name = db.StringField(max_length=255)
......@@ -158,7 +148,6 @@ class ExtendedRegisterForm(RegisterForm):
# Setup Flask-Security
user_datastore = MongoEngineUserDatastore(db, User, Role)
#security = Security(app, user_datastore)
security = Security(app, user_datastore, register_form=ExtendedRegisterForm)
......@@ -178,7 +167,6 @@ def create_user():
def on_user_registered(sender, user, confirm_token):
default_role = user_datastore.find_role("user")
user_datastore.add_role_to_user(user, default_role)
# user_datastore.add_badges_to_user(user, default_badges)
# @user_logged_in.connect_via(app)
# def attribute_empty_badges(sender, user):
......@@ -223,7 +211,6 @@ def page(path):
quiz_dir = 'app/quizzes'
quizzes = {}
for quiz in os.listdir(quiz_dir):
#print('Loading, ' + quiz)
quizzes[quiz] = json.loads(open(os.path.join(quiz_dir, quiz)).read())
@app.route('/quizzes')
......@@ -252,13 +239,8 @@ def check_quiz(id):
badge = quiz['badge']
# print(badge)
badge =str(badge)
# print(request.form)
quiz['questions'] = sorted(quiz['questions'], key=lambda q: ordering.index(quiz['questions'].index(q)))
#print(quiz['questions'])
answers = dict( (int(k), quiz['questions'][int(k)]['options'][int(v)]) for k, v in request.form.items() if k != 'ord' )
#print(answers)
if not len(answers.keys()):
return redirect(url_for('quiz', id=id))
......@@ -267,21 +249,22 @@ def check_quiz(id):
answers[k] = [None, False]
answers_list = [ answers[k] for k in sorted(answers.keys()) ]
# print(len(answers_list))
number_correct = len(list(filter(lambda t: t[1], answers_list)))
# print(number_correct)
badge_ok = math.floor((len(answers_list) / 3) * 2)
# print(badge_ok)
C_user = current_user.get_id()
print(C_user)
if number_correct >= badge_ok:
#award badge
print('award badge ' + badge)
#bdd.user.update({"_id": ObjectId(user), "badges.name": badge}, {"$set": {"badges.$.lvl": 1}}, multi=True)
# dbObjBadge = User.objects(id = C_user, badges=[badge])
# print(dbObjBadge)
# update = bdd.user.update({"_id": ObjectId(C_user)}, {"$set": {"badges.badge" : 1}}, upsert=True)
update = bdd.user.update({"_id": C_user, "badges": { '$elemMatch': {"badges" : badge}}}, {"$set": { "badges.$.lvl": 333 }}, upsert = True)
print(update)
# .objects(id=post.id).update(title='Example Post')
update_Query = str('"set__badges__' + badge + '__lvl = 1", upsert = True')
print(update_Query)
User.objects(id = C_user).update(update_Query)#set__badges__laser__lvl = 22 , upsert=True) #{'badge.lvl' : 1 }, upsert = True )
## update_Query = str('"set__badges__' + badge + '__lvl = 1", upsert = True')
## print(update_Query)
## 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}})
......
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