Skip to content

Commit

Permalink
✨ Support naming backups as they are created
Browse files Browse the repository at this point in the history
closes #4
  • Loading branch information
cp2004 committed Dec 13, 2020
1 parent 41cb377 commit 4e9690c
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 11 deletions.
2 changes: 2 additions & 0 deletions octoprint_eeprom_marlin/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ def create_backup(self, name):
if not name:
# Custom backup names should be passed, otherwise auto-generate
name = util.build_backup_name()
else:
name = util.sanitize(name)

eeprom_data = self._eeprom_data.to_dict()
try:
Expand Down
4 changes: 1 addition & 3 deletions octoprint_eeprom_marlin/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@
"""
Contains the default settings for the plugin, to extract them from the main plugin class
"""
defaults = {
"use_m503": True,
}
defaults = {"use_m503": True, "custom_name": True}
32 changes: 29 additions & 3 deletions octoprint_eeprom_marlin/static/js/eeprom_marlin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ $(function () {
var self = this;

self.printerState = parameters[0];
self.settingsViewModel = parameters[1];

self.eeprom = (function () {
var eeprom = {};
Expand Down Expand Up @@ -850,9 +851,34 @@ $(function () {
});
};

self.backup_name = ko.observable();

self.new_backup = function () {
console.log(
self.settingsViewModel.settings.plugins.eeprom_marlin.custom_name()
);
if (
self.settingsViewModel.settings.plugins.eeprom_marlin.custom_name()
) {
// Trigger modal with custom name
$("#eepromBackupNameModal").modal("show");
} else {
self.create_backup();
}
};

self.create_backup = function () {
// TODO implement naming backups here
OctoPrint.simpleApiCommand("eeprom_marlin", "backup").done(
$("#eepromBackupNameModal").modal("hide");

var payload = {};
if (
self.settingsViewModel.settings.plugins.eeprom_marlin.custom_name()
) {
payload = { name: self.backup_name() };
}
self.backup_name("");

OctoPrint.simpleApiCommand("eeprom_marlin", "backup", payload).done(
function (response) {
let success = response.success;
if (!success) {
Expand Down Expand Up @@ -1002,7 +1028,7 @@ $(function () {

OCTOPRINT_VIEWMODELS.push({
construct: EEPROMMarlinViewModel,
dependencies: ["printerStateViewModel"],
dependencies: ["printerStateViewModel", "settingsViewModel"],
elements: ["#tab_plugin_eeprom_marlin_2"],
});
});
1 change: 1 addition & 0 deletions octoprint_eeprom_marlin/templates/backup/backup.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
</div>
</div>
{% include "snippets/upload_modal.jinja2" %}
{% include "snippets/backup_name_modal.jinja2" %}
2 changes: 1 addition & 1 deletion octoprint_eeprom_marlin/templates/backup/controls.jinja2
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="btn-group" data-bind="visible: backup_open()">
<button class="btn" data-bind="click: create_backup, enable: enable_buttons">
<button class="btn" data-bind="click: new_backup, enable: enable_buttons">
<i class="fa fa-fw fa-plus"></i>
{{ _(" New backup") }}
</button>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<div id="eepromBackupNameModal" class="modal hide fade" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3>Name your backup</h3>
</div>
<div class="modal-body">
<p>This name will be used when creating your backup</p>
<p>Note that it will be sanitised before use, any illegal characters will be replaced.</p>
<form class="form-horizontal" onsubmit="return false;">
<div class="control-group">
<label class="control-label">{{ _('Backup Name') }}</label>
<div class="controls">
<input type="text" data-bind="value: backup_name" placeholder="eeprom_backup...">
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
<button class="btn btn-primary" data-bind="click: create_backup">{{ _('Create backup') }}</button>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
</div>
</div>
</div>
<div class="control-group">
<div class="controls">
</div>
</div>
</form>
</div>
<div class="modal-footer">
Expand Down
51 changes: 51 additions & 0 deletions octoprint_eeprom_marlin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,54 @@ def backup_json_to_list(eeprom_data):
)

return eeprom_list


try:
# This is available in OP 1.6.0+
# TODO decide when to stop supporting 1.5.x and below
from octoprint.util.text import sanitize
except ImportError:
# We are below this version, use backported one instead
import re

from emoji import demojize
from octoprint.util import to_unicode
from octoprint.vendor.awesome_slugify import Slugify

_UNICODE_VARIATIONS = re.compile("[\uFE00-\uFE0F]", re.U)
_SLUGIFIES = {}

def sanitize(text, safe_chars="-_.", demoji=True):
"""
Sanitizes text by running it through slugify and optionally emoji translating.
Examples:
>>> sanitize("Hello World!") # doctest: +ALLOW_UNICODE
'Hello-World'
>>> sanitize("Hello World!", safe_chars="-_. ") # doctest: +ALLOW_UNICODE
'Hello World'
>>> sanitize("\u2764") # doctest: +ALLOW_UNICODE
'red_heart'
>>> sanitize("\u2764\ufe00") # doctest: +ALLOW_UNICODE
'red_heart'
>>> sanitize("\u2764", demoji=False) # doctest: +ALLOW_UNICODE
''
Args:
text: the text to sanitize
safe_chars: characters to consider safe and to keep after sanitization
emoji: whether to also convert emoji to text
Returns: the sanitized text
"""
slugify = _SLUGIFIES.get(safe_chars)
if slugify is None:
slugify = Slugify()
slugify.safe_chars = safe_chars
_SLUGIFIES[safe_chars] = slugify

text = to_unicode(text)
if demoji:
text = remove_unicode_variations(text)
text = demojize(text, delimiters=("", ""))
return slugify(text)

def remove_unicode_variations(text):
return _UNICODE_VARIATIONS.sub("", text)

0 comments on commit 4e9690c

Please sign in to comment.