Skip to content

Commit

Permalink
🪲 Disable autosave on the code page when viewing a program (#6148)
Browse files Browse the repository at this point in the history
The feature for automatic saving and loading of programs is a bit too aggressive and loads a locally stored program even when we open the code page (/hedy or /tryit) to see a concrete user program. The proposed solution is to turn off the auto-loading when the code page (/hedy or /tryit) requested with a specific program id.

As a summary, if the code page is opened like this /hedy/1, the auto-saving should work as expected. If the code page is opened via /hedy/1/0dc61ace147b46ecbd2c71f1c3fc6c88 (logged as teacher 1), the auto-saving will be disabled. Note that it will be disabled also when moving to other adventure tabs. It will be enabled once we move away from this URL.

Fixes #6043

**How to test**
- Run Hedy locally and log in as teacher 1
- Navigate to /hedy/1 and in the introduction adventure add code to the editor, e.g. `print this is autosaved`. Move to another adventure tab, so that this program is saved locally (you should be able to see a message in the console)
- Go to For-Teachers and then to the overview of class 1. Then click on the eye icon of the Introduction submission of student1. When the program is opened on the program-view page, click the Edit button.
- Now the code page should be loaded with the student program loaded and NOT with your local program.
  • Loading branch information
boryanagoncharenko authored Feb 4, 2025
1 parent 9c66bb6 commit 3f2c029
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
8 changes: 7 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1452,12 +1452,14 @@ def index(level, program_id):
return utils.error_page(error=404, ui_message=gettext('no_such_level'))

loaded_program = None
suppress_save_and_load = False
if program_id:
result = g_db().program_by_id(program_id)
if not result or not get_current_user_program_permissions(result):
return utils.error_page(error=404, ui_message=gettext('no_such_program'))

loaded_program = Program.from_database_row(result)
suppress_save_and_load = True

# Initially all levels are available -> strip those for which conditions
# are not met or not available yet
Expand Down Expand Up @@ -1668,6 +1670,7 @@ def index(level, program_id):
adventures=adventures,
initial_tab=initial_tab,
current_user_name=current_user()['username'],
suppress_save_and_load=suppress_save_and_load,
))


Expand All @@ -1683,12 +1686,14 @@ def tryit(level, program_id):
return utils.error_page(error=404, ui_message=gettext('no_such_level'))

loaded_program = None
suppress_save_and_load = False
if program_id:
result = g_db().program_by_id(program_id)
if not result or not get_current_user_program_permissions(result):
return utils.error_page(error=404, ui_message=gettext('no_such_program'))

loaded_program = Program.from_database_row(result)
suppress_save_and_load = True

# Initially all levels are available -> strip those for which conditions
# are not met or not available yet
Expand Down Expand Up @@ -1899,6 +1904,7 @@ def tryit(level, program_id):
adventures=adventures,
initial_tab=initial_tab,
current_user_name=current_user()['username'],
suppress_save_and_load=suppress_save_and_load,
))


Expand Down Expand Up @@ -2069,7 +2075,7 @@ def render_code_in_editor(level):
adventures=adventures,
initial_tab='start',
current_user_name=current_user()['username'],
suppress_save_and_load_for_slides=True,
suppress_save_and_load=True,
))


Expand Down
6 changes: 3 additions & 3 deletions static/js/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export interface InitializeCodePageOptions {
readonly start_tutorial?: boolean;
readonly initial_tab: string;
readonly current_user_name?: string;
readonly suppress_save_and_load_for_slides?: boolean;
readonly suppress_save_and_load?: boolean;
readonly enforce_developers_mode?: boolean;
}

Expand Down Expand Up @@ -255,7 +255,7 @@ export function initializeCodePage(options: InitializeCodePageOptions) {
currentTab = ev.newTab;
const adventure = theAdventures[currentTab];

if (!options.suppress_save_and_load_for_slides) {
if (!options.suppress_save_and_load) {
// Load initial code from local storage, if available
const programFromLs = localLoad(currentTabLsKey());
// if we are in raw (used in slides) we don't want to load from local storage, we always want to show startcode
Expand All @@ -282,7 +282,7 @@ export function initializeCodePage(options: InitializeCodePageOptions) {
initializeShareProgramButtons();
initializeHandInButton();

if (options.suppress_save_and_load_for_slides) {
if (options.suppress_save_and_load) {
disableAutomaticSaving();
}

Expand Down
4 changes: 2 additions & 2 deletions static/js/appbundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -121287,7 +121287,7 @@ def note_with_error(value, err):
tabs.on("afterSwitch", (ev) => {
currentTab = ev.newTab;
const adventure = theAdventures[currentTab];
if (!options.suppress_save_and_load_for_slides) {
if (!options.suppress_save_and_load) {
const programFromLs = localLoad(currentTabLsKey());
if (programFromLs && adventure) {
adventure.editor_contents = programFromLs.code;
Expand All @@ -121307,7 +121307,7 @@ def note_with_error(value, err):
$("#hand_in_button").on("click", () => $("#hand_in_modal").show());
initializeShareProgramButtons();
initializeHandInButton();
if (options.suppress_save_and_load_for_slides) {
if (options.suppress_save_and_load) {
disableAutomaticSaving();
}
window.addEventListener("beforeunload", () => saveIfNecessary(), { capture: true });
Expand Down
4 changes: 2 additions & 2 deletions static/js/appbundle.js.map

Large diffs are not rendered by default.

0 comments on commit 3f2c029

Please sign in to comment.