Commit 0003372d authored by colmoneill's avatar colmoneill
parent 72d32cff
......@@ -11,6 +11,8 @@ import glob
import copy
import json
import random
import math
from bson import ObjectId
DEBUG = True
FLATPAGES_AUTO_RELOAD = DEBUG
......@@ -47,6 +49,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)
......@@ -122,12 +130,15 @@ def quiz(id):
def check_quiz(id):
ordering = json.loads(request.form['ord'])
quiz = copy.deepcopy(quizzes[id])
badge = quiz['badge']
badge = str('"' + badge + '"' )
print(badge)
# print(request.form)
quiz['questions'] = sorted(quiz['questions'], key=lambda q: ordering.index(quiz['questions'].index(q)))
print(quiz['questions'])
#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)
#print(answers)
if not len(answers.keys()):
return redirect(url_for('quiz', id=id))
......@@ -137,7 +148,19 @@ 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)
user = current_user.get_id()
if number_correct >= badge_ok:
#award badge
print('award badge ' + badge)
bdd.user.update({"user._id": ObjectId(user), "badges.name": badge}, {"$set": {"badges.$.lvl": 1}}, multi=True)
#db.exhibitions.update({"artists._id": ObjectId(artist_id)}, {"$set": {"artists.$": artist}}, multi=True)
else:
print('user got less than required amount')
return render_template('check_quiz.html', quiz=quiz, question_answer=zip(quiz['questions'], answers_list), correct=number_correct, total=len(answers_list))
......@@ -167,15 +190,6 @@ def dashboard():
return render_template('dashboard.html', user=user)
# Migrate to pymongo?
# from pymongo import MongoClient
# client = MongoClient('localhost', 27017)
# bdd = client.mydatabase
# bddusers = bdd.users
# bdd.user.update({"_id" : user, "badges.name" : badge }, {"$set" : { "badges.$.lvl" : "1" }})
# APP RUN
......
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