Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[frontend] Fixing indentation problems + indentation preferences #995

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions deploy/db_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ def try_mongodb_opts(host="localhost", database_name='INGInious'):
database = try_mongodb_opts('db')

database.users.insert_one({"username": username,
"realname": realname,
"email": email,
"password": UserManager.hash_password(password),
"bindings": {},
"language": "en"})
"realname": realname,
"email": email,
"password": UserManager.hash_password(password),
"bindings": {},
"language": "en",
"code_indentation": "4"})
print('Superadmin user added!')
10 changes: 10 additions & 0 deletions inginious/frontend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ def get_app(config):
available_languages = {"en": "English"}
available_languages.update(available_translations)

# available indentation types
available_indentation_types = {
"2": {"text": "2 spaces", "indent": 2, "indentWithTabs": False},
"3": {"text": "3 spaces", "indent": 3, "indentWithTabs": False},
"4": {"text": "4 spaces", "indent": 4, "indentWithTabs": False},
"tabs": {"text": "tabs", "indent": 4, "indentWithTabs": True},
}

l10n_manager = L10nManager()

l10n_manager.translations["en"] = gettext.NullTranslations() # English does not need translation ;-)
Expand Down Expand Up @@ -242,6 +250,7 @@ def get_app(config):
template_helper.add_to_template_globals("_", _)
template_helper.add_to_template_globals("str", str)
template_helper.add_to_template_globals("available_languages", available_languages)
template_helper.add_to_template_globals("available_indentation_types", available_indentation_types)
template_helper.add_to_template_globals("get_homepath", get_homepath)
template_helper.add_to_template_globals("pkg_version", __version__)
template_helper.add_to_template_globals("allow_registration", config.get("allow_registration", True))
Expand Down Expand Up @@ -302,6 +311,7 @@ def flask_internalerror(e):
flask_app.allow_registration = config.get("allow_registration", True)
flask_app.allow_deletion = config.get("allow_deletion", True)
flask_app.available_languages = available_languages
flask_app.available_indentation_types = available_indentation_types
flask_app.welcome_page = config.get("welcome_page", None)
flask_app.terms_page = config.get("terms_page", None)
flask_app.privacy_page = config.get("privacy_page", None)
Expand Down
1 change: 1 addition & 0 deletions inginious/frontend/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ def configure_authentication(self, database):
"email": email,
"password": UserManager.hash_password(password),
"bindings": {},
"code_indentation": "4",
"language": "en"})

options["superadmins"].append(username)
Expand Down
3 changes: 2 additions & 1 deletion inginious/frontend/pages/admin/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ def POST_AUTH(self, *args, **kwargs):
"email": email,
"password": password,
"bindings": {},
"language": "en"})
"language": "en",
"code_indentation": "4"})
else:
feedback = _("Unknown action.")
if feedback:
Expand Down
3 changes: 1 addition & 2 deletions inginious/frontend/pages/lti.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ def GET(self):

user_profile = self.database.users.find_one({"ltibindings." + data["task"][0] + "." + data["consumer_key"]: data["username"]})
if user_profile:
self.user_manager.connect_user(user_profile["username"], user_profile["realname"], user_profile["email"],
user_profile["language"], user_profile.get("tos_accepted", False))
self.user_manager.connect_user(user_profile)

if self.user_manager.session_logged_in():
return redirect(self.app.get_homepath() + "/lti/task")
Expand Down
53 changes: 30 additions & 23 deletions inginious/frontend/pages/preferences/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ def save_profile(self, userdata, data):
msg = _("Incorrect email.")
return result, msg, error
else:
self.user_manager.connect_user(result["username"], result["realname"], result["email"],
result["language"], result.get("tos_accepted", False))
self.user_manager.set_session_username(data["username"])

profile_data_to_be_updated = {}

# Check if updating the password.
if self.app.allow_registration and len(data["passwd"]) in range(1, 6):
Expand All @@ -68,38 +69,44 @@ def save_profile(self, userdata, data):
return result, msg, error
else:
passwd_hash = UserManager.hash_password(data["passwd"])
result = self.database.users.find_one_and_update({"username": self.user_manager.session_username()},
{"$set": {"password": passwd_hash}},
return_document=ReturnDocument.AFTER)
profile_data_to_be_updated["password"] = passwd_hash

# Check if updating language
if data["language"] != userdata["language"]:
if data["language"] != userdata.get("language", "en"):
language = data["language"] if data["language"] in self.app.available_languages else "en"
result = self.database.users.find_one_and_update({"username": self.user_manager.session_username()},
{"$set": {"language": language}},
return_document=ReturnDocument.AFTER)
if not result:
profile_data_to_be_updated["language"] = language

# check if updating code indentation
if data["code_indentation"] != userdata.get("code_indentation", "4"):
code_indentation = data["code_indentation"] if data["code_indentation"] in self.app.available_indentation_types.keys() else "4"
profile_data_to_be_updated["code_indentation"] = code_indentation

# Checks if updating name
if data["realname"] != userdata.get("realname", ""):
if len(data["realname"]) > 0:
profile_data_to_be_updated["realname"] = data["realname"]
else:
error = True
msg = _("Incorrect username.")
msg = _("Name is too short.")
return result, msg, error
else:
self.user_manager.set_session_language(language)

# Checks if updating name
if len(data["realname"]) > 0:
result = self.database.users.find_one_and_update({"username": self.user_manager.session_username()},
{"$set": {"realname": data["realname"]}},
return_document=ReturnDocument.AFTER)
# updating profile in DB
if profile_data_to_be_updated:
self.database.users.find_one_and_update({"username": self.user_manager.session_username()},
{"$set": profile_data_to_be_updated},
return_document=ReturnDocument.AFTER)
if not result:
error = True
msg = _("Incorrect username.")
return result, msg, error
else:
self.user_manager.set_session_realname(data["realname"])
else:
error = True
msg = _("Name is too short.")
return result, msg, error
# updating session
if "language" in profile_data_to_be_updated:
self.user_manager.set_session_language(profile_data_to_be_updated["language"])
if "code_indentation" in profile_data_to_be_updated:
self.user_manager.set_session_code_indentation(profile_data_to_be_updated["code_indentation"])
if "realname" in profile_data_to_be_updated:
self.user_manager.set_session_realname(profile_data_to_be_updated["realname"])

msg = _("Profile updated.")

Expand Down
1 change: 1 addition & 0 deletions inginious/frontend/pages/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def register_user(self, data):
"activate": activate_hash,
"bindings": {},
"language": self.user_manager._session.get("language", "en"),
AlexandreDoneux marked this conversation as resolved.
Show resolved Hide resolved
"code_indentation": "4",
"tos_accepted": True
})
try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
<script src="../../addon/selection/active-line.js"></script>
<script src="../../addon/search/search.js"></script>
<script src="../../addon/dialog/dialog.js"></script>
<script src="../../addon/search/searchcursor.js"></script>
<style>
.CodeMirror {
border: 1px solid #eee;
Expand Down
28 changes: 19 additions & 9 deletions inginious/frontend/static/js/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,31 +77,41 @@ function registerCodeEditor(textarea, lang, lines, firstline=1)

var is_single = $(textarea).hasClass('single');

var keyMappings = {
'Ctrl-Enter': function() {
$('body,html').animate({
scrollTop: $('#task-submit').offset().top
}, 'fast');
}
}

if (user_indentation_type["text"] == "tabs") {
keyMappings["Tab"] = function(cm) { cm.execCommand("insertTab"); cm.execCommand("indentLess"); cm.execCommand("insertTab"); };
} else {
keyMappings["Tab"] = function(cm) { cm.execCommand("insertSoftTab");};
}



var editor = CodeMirror.fromTextArea(textarea, {
lineNumbers: true,
firstLineNumber: parseInt(firstline),
firstLineNumber: parseInt(firstline),
mode: mode["mime"],
foldGutter: true,
styleActiveLine: true,
matchBrackets: true,
autoCloseBrackets: true,
lineWrapping: true,
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
indentUnit: 4,
indentUnit: user_indentation_type["indent"],
indentWithTabs: user_indentation_type["indentWithTabs"],
tabSize: user_indentation_type["indent"],
viewportMargin: Infinity,
lint: function()
{
return []
},
extraKeys: {
'Ctrl-Enter': function() {
$('body,html').animate({
scrollTop: $('#task-submit').offset().top
}, 'fast');
},
},
extraKeys: keyMappings
});

if(is_single)
Expand Down
4 changes: 4 additions & 0 deletions inginious/frontend/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -210,5 +210,9 @@
<!-- Placed at the end of the document so the pages load faster -->
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
{{ template_helper.call('javascript_footer') | safe }}
<script>
var available_indentation_types = {{available_indentation_types | tojson}}
var user_indentation_type = available_indentation_types["{{user_manager.session_code_indentation()}}"]
</script>
</body>
</html>
8 changes: 8 additions & 0 deletions inginious/frontend/templates/preferences/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ <h2>{{ _("My profile") }}</h2>
{% endfor %}
</select>
</div>
<div class="col-sm-12">
<label class="control-label">{{ _("Code indentation :") }} </label>
<select name="code_indentation" class="form-control">
{% for code, indentation_data in available_indentation_types.items() %}
<option value="{{code}}" {% if code == user_manager.session_code_indentation() %} selected {% endif %}>{{ _(indentation_data["text"]) }}</option>
{% endfor %}
</select>
</div>
{% if allow_registration %}
<div class="col-sm-12">
<label class="control-label">{{ _("Old password :") }} </label>
Expand Down
Loading
Loading