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

Draft model and autosave on Project edit form #352

Merged
merged 65 commits into from
Feb 19, 2019
Merged

Draft model and autosave on Project edit form #352

merged 65 commits into from
Feb 19, 2019

Conversation

iambibhas
Copy link
Contributor

@iambibhas iambibhas commented Feb 5, 2019

This PR introduces Draft model as proposed in hasgeek/baseframe#207 in Project edit form.

Depends on hasgeek/baseframe#208

funnel/models/draft.py Outdated Show resolved Hide resolved
funnel/models/draft.py Outdated Show resolved Hide resolved
elif request.method == 'POST':
if 'autosave' in request.form and request.form['autosave'] == 'true':
if 'revision' not in request.form:
return {'error': _("Form must contain a valid revision ID.")}, 400
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first autosave will not have a revision. There is nothing to revise.

table=Project.__tablename__, table_row_id=self.obj.uuid,
body={'form': request.form.items(multi=True)}, revision=uuid4()
)
db.session.add(draft)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This requires a CSRF check. Without the check, a malicious third party website can dump junk content into the Draft model by performing a cross-site POST (which is what CSRF protects from).

  1. Do a CSRF check using Form().validate_on_submit() (the base Form object has nothing but a CSRF field).
  2. Remove the CSRF field when saving to the Draft model. It will cause problems when restored later.

@jace
Copy link
Member

jace commented Feb 9, 2019

Change on this comment: #352 (comment)

  1. The field is form.revision, not revision.
  2. The comment is incorrect since there will be an empty field in the form, and this code is only check for the presence of the field, not the content.

Copy link
Member

@jace jace left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly stylistic issues.

funnel/views/mixins.py Outdated Show resolved Hide resolved
funnel/views/mixins.py Outdated Show resolved Hide resolved
funnel/views/mixins.py Outdated Show resolved Hide resolved
funnel/views/mixins.py Outdated Show resolved Hide resolved
funnel/views/mixins.py Show resolved Hide resolved
funnel/views/mixins.py Outdated Show resolved Hide resolved
funnel/views/mixins.py Outdated Show resolved Hide resolved
funnel/views/mixins.py Outdated Show resolved Hide resolved
funnel/views/mixins.py Outdated Show resolved Hide resolved
Copy link
Member

@jace jace left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More nitpicking. :) Should be done after this.


@property
def formdata(self):
return MultiDict(self.body['form']) if 'form' in self.body else MultiDict({})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return MultiDict(self.body.get('form', {}))


@formdata.setter
def formdata(self, value):
self.body = {'form': value}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.body['form'] = value. Don't mess with anything else in self.body.

if client_revision is None or (client_revision is not None and str(draft.revision) != client_revision):
# draft exists, but the form did not send a revision ID,
# OR revision ID sent by client does not match the last revision ID
return {'error': _("There has been changes to this draft since you last edited it. Please reload.")}, 400
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There have been changes to this item since you last edited it. Please reload this page.

for key in incoming_data.keys():
if existing[key] != incoming_data[key]:
existing[key] = incoming_data[key]
draft.body = {'form': existing}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set draft.formdata

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

draft.formdata = existing

existing = draft.formdata
for key in incoming_data.keys():
if existing[key] != incoming_data[key]:
existing[key] = incoming_data[key]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not required. You can simply do existing.update(incoming_data)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that doesn't work as existing is a MultiDict and the update() method doesn't replace keys, it just adds and extends.

elif draft is None and client_revision:
# The form contains a revision ID but no draft exists.
# Somebody is making autosave requests with an invalid draft ID.
return {'error': _("Invalid revision ID or the existing changes have been submitted already. Please reload.")}, 400
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can happen in the following scenario:

  1. Tab A is opened to edit the document
  2. Tab A makes some changes and creates a draft
  3. Tab B opens the same document and gets the draft
  4. Tab B makes some changes and updates the draft
  5. Tab B submits, updating the document and deleting the draft
  6. Tab A now edits and gets to this error

A variation of this happens if step 2 is skipped. Tab A will now make changes on an obsolete document, losing Tab B's changes. No error will be displayed. To fix this we have to track revision id of not just the draft, but the source document as well. This deserves to be a separate ticket since it's some way off from our original intent of providing autosave functionality.

db.session.commit()
return {'revision': draft.revision}
else:
return {'error': _("Invalid CSRF token")}, 400
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recall we had established a convention for JSON responses earlier:

  1. Success: {"status": "ok", "result": {...}}
  2. Error: {"status": "error", "error": "error_identifier", "error_description": "English message"}

The first one I'm ambivalent about, since HTTP status (200 vs 400) is a sufficient indicator of whether the request was successful or not. The second is relevant. The front-end should be able to look up a static error code and invoke custom behaviour. The error_description is shown when the front-end does not recognise the error.

form = ProjectForm(obj=self.obj, parent=self.obj.profile, model=Project, formdata=initial_formdata)

if not self.obj.timezone:
form.timezone.data = current_app.config.get('TIMEZONE')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated fix, but this should default to the user's timezone, not the app's. Use current_auth.user.timezone (it falls back to the app if the user has not specified a timezone).

@iambibhas iambibhas merged commit 85f91cd into master Feb 19, 2019
@iambibhas iambibhas deleted the autosave branch February 19, 2019 19:59
jace pushed a commit that referenced this pull request Apr 6, 2020
vidya-ram pushed a commit that referenced this pull request Nov 23, 2021
* Use ES6 JS. Use webpack to build and transpile ES6 code.

* Add scroll active menu and swipe functionality.

* Sort schedule event days.

* Change buttons design of the header.

* Add check to find if eventday is present.

* Avoid using global names.

* Add support to lazy load images on the home page

* Add script folder. Change to lazy load on hasgeek index page.

* Sync with master.

* added Makefile

* Updated package-lock.json.

* Fix the mui class name.

* added newline

* Remove default lat and lon values.

* split makefile

* Move view proposal btn to right.

* Add support for autosave (wip)

* Add autosave flag to form data.

* added draft model and initial view

* fixed edit endpoint

* Update server response flag in error cases.

* fixed creation of draft

* Check form data changes before sending to backend.

* Refactor checking for dirty fields of the form.

* added timestamp to draft model, fixed form submit workflow

* removed revision id validation

* removed print statement

* removed unused import

* updated draft model and using composite key

* Send last_revision_id to render_form

* various fixes

* update form post url

* Update form id

* fixed action url, only updating fields sent in request

* Fix form field selector

* Add missing semicolon.

* Change to draft_revision. Use updated form review field.

* added csrf check for draft autosave

* fixed draft model

* fixed draft delete and invalid revision ID handling

* updated down revision

* update down revision

* fixed revision check logic

* added DraftModelViewMixin

* fixed draftmixin

* minor fixes

* changed error format

* fixed multidict update

* Display the error message from the server.

* removed redundant statement
vidya-ram pushed a commit that referenced this pull request Nov 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants