Commit d4e2ed0b authored by erreur401's avatar erreur401

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

parents 1766ad22 b8405609
......@@ -11,21 +11,12 @@ 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
FLATPAGES_EXTENSION = '.md'
FLATPAGES_MARKDOWN_EXTENSIONS = ['markdown.extensions.extra', 'headerid']
app = Flask(__name__)
#app.config.from_pyfile('mysettings.cfg')
app.config.from_object(__name__)
pages = FlatPages(app)
# Flask Security settings
app.config['SECRET_KEY'] = 'super-secret'
app.config['SECURITY_REGISTERABLE'] = True
app.config['SECURITY_RECOVERABLE'] = True
......@@ -50,6 +41,12 @@ app.config['MONGODB_PORT'] = 27017
# Create database connection object
db = MongoEngine(app)
# Setup Flask-Security
user_datastore = MongoEngineUserDatastore(db, User, Role )
#security = Security(app, user_datastore)
security = Security(app, user_datastore, register_form=ExtendedRegisterForm)
class Role(db.Document, RoleMixin):
name = db.StringField(max_length=80, unique=True)
description = db.StringField(max_length=255)
......@@ -60,7 +57,7 @@ class User(db.Document, UserMixin):
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'}])
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)
......@@ -69,43 +66,10 @@ 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'],
'Great Wall of China':['China','Beijing','Shanghai','Tianjin'],
'Petra':['Ma\'an Governorate','Amman','Zarqa','Jerash'],
'Machu Picchu':['Cuzco Region','Lima','Piura','Tacna'],
'Egypt Pyramids':['Giza','Suez','Luxor','Tanta'],
'Colosseum':['Rome','Milan','Bari','Bologna'],
'Christ the Redeemer':['Rio de Janeiro','Natal','Olinda','Betim']
}
questions = copy.deepcopy(original_questions)
robotArmQuiz = copy.deepcopy(original_questions)
app = Flask(__name__)
#app.config.from_pyfile('mysettings.cfg')
app.config.from_object(__name__)
pages = FlatPages(app)
@app.route("/")
@app.route("/index")
......@@ -124,60 +88,24 @@ def learn():
@app.route('/learn/<path:path>/')
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]
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
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>'
print(page)
return render_template('flatpage.html', page=page)
@app.route("/contact")
def contact():
#return "Hello, World!"
return render_template("contact.html")
@app.route("/event")
def event():
#return "Hello, World!"
return render_template("event.html")
@app.errorhandler(404)
def not_found(error):
return render_template('error.html'), 404
##################
## QUIZZZZZ ###
##################
# Views
@app.route('/dashboard')
@login_required
def dashboard():
......@@ -189,6 +117,16 @@ 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
if __name__ == '__main__':
......
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