aboutsummaryrefslogtreecommitdiffstats
path: root/elisp
diff options
context:
space:
mode:
Diffstat (limited to 'elisp')
-rw-r--r--elisp/grocy.el73
1 files changed, 45 insertions, 28 deletions
diff --git a/elisp/grocy.el b/elisp/grocy.el
index c974cb3..4a9b444 100644
--- a/elisp/grocy.el
+++ b/elisp/grocy.el
@@ -21,8 +21,6 @@
(require 'url)
(require 'dash)
-(require 'yaml)
-(require 'yaml-mode)
;; Silence byte-compiler.
(defvar url-http-end-of-headers)
@@ -155,10 +153,39 @@ Set METHOD to other request types and include the already encoded DATA."
result)
(setq tabulated-list-entries))
(local-set-key "e" #'grocy-update-product)
+ (local-set-key "a" #'grocy-add-product-shopping-list)
(tabulated-list-init-header)
(tabulated-list-print)
(display-buffer (current-buffer)))))))
+(defun grocy--message-error-reply (status)
+ "Display `url-retrieve' response buffer and error STATUS."
+ (when status
+ (goto-char url-http-end-of-headers)
+ (json-pretty-print (point) (point-max))
+ (message "%S" status)
+ (display-buffer (current-buffer))))
+
+(defun grocy-add-product-shopping-list ()
+ "Add selected product to first shopping list."
+ (interactive)
+ (let* ((product (tabulated-list-get-id))
+ (id (gethash "id" product))
+ (name (gethash "name" product)))
+ (with-current-buffer (get-buffer-create "* Grocy add product to shopping list *")
+ (erase-buffer)
+ (emacs-lisp-mode)
+ (insert (format ";; %s\n(:product_id %d\n:list_id 1\n:product_amount 1\n;; :note \"\"\n)\n" name id))
+ (keymap-local-set "C-c C-c"
+ (lambda () (interactive)
+ (grocy--request
+ "stock/shoppinglist/add-product"
+ #'grocy--message-error-reply
+ "POST"
+ (grocy-buffer-obj->json (current-buffer)))
+ (kill-buffer)))
+ (switch-to-buffer (current-buffer)))))
+
(defun grocy-update-product ()
"Update form for currently selected product."
(interactive)
@@ -188,51 +215,41 @@ Set METHOD to other request types and include the already encoded DATA."
(json-encode)
(encode-coding-string 'utf-8)))))
-(defun grocy-buffer-yaml->json (buffer)
- "Read the YAML on BUFFER and encode it to json."
- (with-current-buffer buffer
- (thread-first
- (buffer-string)
- (yaml-parse-string)
- (json-encode)
- (encode-coding-string 'utf-8))) )
-
(defun grocy-update-object (object-endpoint)
"Update on the OBJECT-ENDPOINT with the Lisp object on buffer."
(grocy--request
(format "objects/%s" object-endpoint)
- (lambda (status)
- (when status
- (message "%S\n%S" status (buffer-string))))
+ #'grocy--message-error-reply
"PUT"
(grocy-buffer-obj->json (current-buffer))))
(defun grocy-new-product ()
+ "Add new product to the database."
(interactive)
(with-current-buffer (get-buffer-create "* Grocy new product *")
(erase-buffer)
- (yaml-mode)
+ (emacs-lisp-mode)
(keymap-local-set
"C-c C-c"
(lambda () (interactive)
(grocy--request
"objects/products"
- (lambda (status)
- (message "%S\n%S" status (buffer-string)))
+ #'grocy--message-error-reply
"POST"
- (grocy-buffer-yaml->json (current-buffer)))))
- (insert "name: \n# ")
+ (grocy-buffer-obj->json (current-buffer)))
+ (kill-buffer)))
+ (insert "(\n:name \n;; ")
(maphash (lambda (key val) (insert (format "%d:'%s' " key (car val)))) grocy-locations)
- (insert "\nlocation_id: \n\# ")
+ (insert "\n:location_id \n;; ")
(maphash (lambda (key val) (insert (format "%d:'%s' " key (car val)))) grocy-units)
- (insert "\nqu_id_stock: 3
-qu_id_purchase: 3
-qu_id_consume: 3
-qu_id_price: 3
-
-min_stock_amount: 0
-default_best_before_days: -1"))
- (grocy-buffer-yaml->json (current-buffer)))
+ (insert "\n:qu_id_stock 3
+:qu_id_purchase 3
+:qu_id_consume 3
+:qu_id_price 3
+
+:min_stock_amount 0
+:default_best_before_days -1\n)")
+ (switch-to-buffer (current-buffer))))
;; To do mass edit of things
;; (grocy--request