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

Update for USE_TZ with recent Grappelli & TinyMCE #3

Open
wants to merge 4 commits into
base: main
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ changes, continuing will abandon them and start creating new save points.
The contents of a form are autosaved by examining input and textarea value fields. Some javascript-heavy custom
form widgets only write to the input field they replace when the form is submitted (instead of every time the data changes).
As a result, when autosave serializes the form data, the values can be out of date.

There is support for TinyMCE which will ask it to "save" (serialize)
the contents of its editors back to their sources (we only really care
about TEXTAREA elements) before we create the autosave backup.
13 changes: 11 additions & 2 deletions autosave/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _
from django.utils import timezone


class AdminAutoSaveMixin(object):
Expand Down Expand Up @@ -101,7 +102,13 @@ def autosave_js(self, request, object_id, extra_context=None):
updated = getattr(obj, self.autosave_last_modified_field, None)
# Make sure date modified time doesn't predate Unix-time.
# I'm pretty confident they didn't do any Django autosaving in 1969.
updated = max(updated, datetime(year=1970, month=1, day=1))
updated = max(
updated,
timezone.make_aware(
datetime(year=1970, month=1, day=1),
timezone=timezone.get_current_timezone(),
)
)

if obj and not self.has_change_permission(request, obj):
raise PermissionDenied
Expand All @@ -112,7 +119,9 @@ def autosave_js(self, request, object_id, extra_context=None):
'autosave_url': autosave_url,
'is_add_view': not(object_id),
'server_time_epoch': time.mktime(datetime.now().timetuple()),
'last_updated_epoch': time.mktime(updated.timetuple()) if updated else None,
'last_updated_epoch': time.mktime(
timezone.localtime(updated).timetuple()
) if updated else None,
'is_recovered_autosave': bool(request.GET.get('is_recovered')),
}

Expand Down
9 changes: 8 additions & 1 deletion autosave/static/autosave/js/autosave.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ var DjangoAutosave = (window.DjangoAutosave) ? DjangoAutosave : {};

// 'grp-' prefix to support both Admin and Grapelli 2.4
var $messagelist = $('.messagelist, .grp-messagelist');
var $container = $('#content, #content-inner');
var $container = $('#content, #content-inner, #grp-content');
if (!$messagelist.length) {
// Put messagelist in place if it's not already there
$messagelist = $('<ul class="messagelist grp-messagelist"/>').prependTo($container);
Expand All @@ -255,6 +255,13 @@ var DjangoAutosave = (window.DjangoAutosave) ? DjangoAutosave : {};
};

DjangoAutosave.captureForm = function() {
// If TinyMCE is running, ask it to save its editors' contents
// back to the places they came from.
if (typeof(tinymce) !== "undefined") {
tinymce.editors.forEach(function(editor) {
editor.save();
});
}
var $form = $('form');
var $fields = $form.find(':input:not([name="csrfmiddlewaretoken"])');
var field_list = [];
Expand Down
5 changes: 2 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
author='Jason Goldstein',
author_email='[email protected]',
url='https://github.com/theatlantic/django-autosave',
packages=['autosave', ],
package_data={ 'autosave': ['static/*',] },
packages=find_packages(),
package_data={ 'autosave': ['static/autosave/js/*',] },
description='Generic autosave for the Django Admin.',
long_description=open('README.md').read(),
classifiers=[
'Environment :: Web Environment',
'Intended Audience :: Developers',
Expand Down