Commit 7caadb45 authored by colmoneill's avatar colmoneill

connecting the quizzes and the badges

parent 0c844fbc
......@@ -9,7 +9,12 @@ from flask_security import Security, MongoEngineUserDatastore, UserMixin, RoleMi
from flask_mail import Mail
from flask_security.forms import RegisterForm, StringField
from wtforms.validators import DataRequired
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
bdd = client.mydatabase
bddusers = bdd.users
DEBUG = True
FLATPAGES_AUTO_RELOAD = DEBUG
......@@ -21,10 +26,73 @@ app = Flask(__name__)
app.config.from_object(__name__)
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_PASSWORD_HASH'] = 'bcrypt'
app.config['SECURITY_PASSWORD_SALT'] = '$2a$16$PnnIgfMwkOjGX4SkHqSOPO'
app.config['MAIL_SERVER'] = 'mail.gandi.net'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = 'automatic@colm.be'
app.config['MAIL_PASSWORD'] = 'testing#123'
app.config['MAIL_DEFAULT_SENDER'] = 'automatic@colm.be'
mail = Mail(app)
# MongoDB Config
app.config['MONGODB_DB'] = 'mydatabase' # change me before prod!
app.config['MONGODB_HOST'] = 'localhost'
app.config['MONGODB_PORT'] = 27017
# Create database connection object
db = MongoEngine(app)
class Role(db.Document, RoleMixin):
name = db.StringField(max_length=80, unique=True)
description = db.StringField(max_length=255)
class User(db.Document, UserMixin):
email = db.StringField(max_length=255)
password = db.StringField(max_length=255)
active = db.BooleanField(default=True)
confirmed_at = db.DateTimeField()
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'}])
dashboard_view_count = db.StringField(max_length=3)
first_name = db.StringField(max_length=255)
last_name = db.StringField(max_length=255)
class ExtendedRegisterForm(RegisterForm):
first_name = StringField('First Name', validators=[DataRequired()])
last_name = StringField('Last Name', validators=[DataRequired()])
# Setup Flask-Security
user_datastore = MongoEngineUserDatastore(db, User, Role )
#security = Security(app, user_datastore)
security = Security(app, user_datastore, register_form=ExtendedRegisterForm)
##################
## QUIZZZZZ ###
##################
def shuffle(q):
"""
This function is for shuffling
the dictionary elements.
"""
selected_keys = []
i = 0
while i < len(q):
current_selection = random.choice(list(q.keys()))
if current_selection not in selected_keys:
selected_keys.append(current_selection)
i = i+1
return selected_keys
original_questions = {
#Format is 'question':[options]
'Taj Mahal':['Agra','New Delhi','Mumbai','Chennai'],
......@@ -58,32 +126,35 @@ def page(path):
page = pages.get_or_404(path)
if 'quiz' in page.meta.keys():
quizName = page.meta['quiz']
global badge
badge = page.meta['badge']
if quizName in globals():
global quiz
quiz = globals()[quizName]
print(quiz)
questions_shuffled = shuffle(quiz)
for i in list(quiz.keys()):
random.shuffle(quiz[i])
else:
quiz = None
questions = None
questions_shuffled = None
return render_template('flatpage.html', page=page, quiz=quiz, q=questions_shuffled, o=quiz)
@app.route('/learn/check', methods=['POST'])
@login_required
def quiz_answers():
correct = 0
for i in quiz.keys():
answered = request.form[i]
if quiz[i][0] == answered:
correct = correct+1
return '<h1>Correct Answers: <u>'+str(correct)+'</u></h1>'
# @app.route("/login")
# def login():
# #return "Hello, World!"
# return render_template("login.html")
correct = 0
user = current_user.get_id()
print('user: ' + user)
for i in quiz.keys():
answered = request.form[i]
if quiz[i][0] == answered:
correct = correct+1
# User.objects.get(id=user).update(inc__badges, upsert=True)
# user_to_up = bdd.user.find({"_id": user, "badges.name": "badge" })
# print(user_to_up)
bdd.user.update({"_id" : user, "badges.name" : badge }, {"$set" : { "badges.$.lvl" : "1" }})
return '<h1>Correct Answers: <u>'+str(correct)+'</u></h1>'
@app.route("/contact")
def contact():
......@@ -96,112 +167,21 @@ def event():
#return "Hello, World!"
return render_template("event.html")
#@app.errorhandler(404)
#def not_found(error):
# return render_template('error.html'), 404
@app.errorhandler(404)
def not_found(error):
return render_template('error.html'), 404
##################
## QUIZZZZZ ###
##################
def shuffle(q):
"""
This function is for shuffling
the dictionary elements.
"""
selected_keys = []
i = 0
while i < len(q):
current_selection = random.choice(list(q.keys()))
if current_selection not in selected_keys:
selected_keys.append(current_selection)
i = i+1
return selected_keys
# /
# ______ _ _ _____ _ _
# | ___| | | | / ___| (_) |
# | |_ | | __ _ ___| | __ \ `--. ___ ___ _ _ _ __ _| |_ _ _
# | _| | |/ _` / __| |/ / `--. \/ _ \/ __| | | | '__| | __| | | |
# | | | | (_| \__ \ < /\__/ / __/ (__| |_| | | | | |_| |_| |
# \_| |_|\__,_|___/_|\_\ \____/ \___|\___|\__,_|_| |_|\__|\__, |
# ______ __/ |
# |______| |___/
# /
# Create 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_PASSWORD_HASH'] = 'bcrypt'
app.config['SECURITY_PASSWORD_SALT'] = '$2a$16$PnnIgfMwkOjGX4SkHqSOPO'
app.config['MAIL_SERVER'] = 'mail.gandi.net'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = 'automatic@colm.be'
app.config['MAIL_PASSWORD'] = 'testing#123'
app.config['MAIL_DEFAULT_SENDER'] = 'automatic@colm.be'
mail = Mail(app)
# MongoDB Config
app.config['MONGODB_DB'] = 'mydatabase'
app.config['MONGODB_HOST'] = 'localhost'
app.config['MONGODB_PORT'] = 27017
# Create database connection object
db = MongoEngine(app)
# class ExtendedRegisterForm(RegisterForm):
# first_name = db.StringField('First Name')
# last_name = db.StringField('Last Name')
class Role(db.Document, RoleMixin):
name = db.StringField(max_length=80, unique=True)
description = db.StringField(max_length=255)
class User(db.Document, UserMixin):
email = db.StringField(max_length=255)
password = db.StringField(max_length=255)
active = db.BooleanField(default=True)
confirmed_at = db.DateTimeField()
roles = db.ListField(db.ReferenceField(Role), default=[])
badges = db.ListField(default=[])
dashboard_view_count = db.StringField(max_length=3)
first_name = db.StringField(max_length=255)
last_name = db.StringField(max_length=255)
class ExtendedRegisterForm(RegisterForm):
first_name = StringField('First Name', validators=[DataRequired()])
last_name = StringField('Last Name', validators=[DataRequired()])
# Setup Flask-Security
user_datastore = MongoEngineUserDatastore(db, User, Role )
#security = Security(app, user_datastore)
security = Security(app, user_datastore, register_form=ExtendedRegisterForm)
# Create a user to test with
# Note the decorator, this runs only once on startup of the server.
# @app.before_first_request
# def create_user():
# user_datastore.create_user(email='test@gmail.com', password='password')
# Views
@app.route('/dashboard')
@login_required
def dashboard():
user = current_user.get_id()
#user = str(user)
print('user: ' + user)
db_user = User.objects.get(id=user).update(inc__dashboard_view_count=1, upsert=True)
message = 'Welcome to your dashboard ' + current_user.get_id()
......
......@@ -2,6 +2,7 @@ title: bras robotique
published: 2010-12-24
featured_image: page_images/test.jpg
quiz: robotArmQuiz
badge: electronique
......
{% extends "base.html" %}
{% block content %}
<div class="container">
<div class="all-col">
<h1>(404) page not found</h1>
<div class="separation-rule"></div>
<p>This page does not exist. Please check the URL spelling or revert to the navigation.</p>
</div>
</div>
{% endblock %}
......@@ -25,18 +25,9 @@
{{page}}
</div>
</div>
{% if page.quiz %}
<h1>7 Wonders of the World</h1>
<form action='/learn/check' method='POST'>
<ol>
{% for i in q %}
<li>Where is <u>{{i}}</u> located?</li>
{% for j in o[i] %}
<input type='radio' value='{{j}}' name='{{i}}' />{{j}}
{% endfor %}
{% endfor %}
</ol>
<input type="submit" value="submit" />
</form>
{% include 'quiz.html' %}
{% endif %}
{% endblock %}
{% extends "base.html" %}
{% block content %}
<h1>7 Wonders of the World</h1>
<form action='/learn/check' method='POST'>
<ol>
{% for i in q %}
<li>Where is <u>{{i}}</u> located?</li>
{% for j in o[i] %}
<input type='radio' value='{{j}}' name='{{i}}' />{{j}}
{% endfor %}
{% endfor %}
</ol>
<input type="submit" value="submit" />
</form>
{% endblock %}
{% if current_user.is_authenticated %}
<h1>Quiz !</h1>
<div class="tutoWrapper">
<div class="container">
<form action='/learn/check' method='POST'>
<ol>
{% for i in q %}
<li>Where is <u>{{i}}</u> located?</li>
{% for j in o[i] %}
<input type='radio' value='{{j}}' name='{{i}}' />{{j}}
{% endfor %}
{% endfor %}
</ol>
<input type="submit" value="voir mon resultat" />
</form>
</div>
</div>
{% else %}
<h1> Un quiz est disponible pour ce contenu, mais il vous faut <a href="/login">etre logge dans le systeme</a> afin de le suivre.</h1>
{% endif %}
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