blob: 21291130397ee4696db18ec70a661d1901bef29a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
;;; grocy.el --- Grocy frontend -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2024 Óscar Nájera
;;
;; Author: Óscar Nájera <hi@oscarnajera.com>
;; Maintainer: Óscar Nájera <hi@oscarnajera.com>
;; Created: October 19, 2024
;; Modified: October 19, 2024
;; Version: 0.0.1
;; Keywords: abbrev bib c calendar comm convenience data docs emulations extensions faces files frames games hardware help hypermedia i18n internal languages lisp local maint mail matching mouse multimedia news outlines processes terminals tex tools unix vc wp
;; Homepage: https://github.com/titan/grocy
;; Package-Requires: ((emacs "29.1"))
;;
;; This file is not part of GNU Emacs.
;;
;;; Commentary:
;;
;; Grocy Emacs frontend
;;
;;; Code:
(require 'url)
(require 'dash)
(require 'yaml)
(require 'yaml-mode)
;; Silence byte-compiler.
(defvar url-http-end-of-headers)
(defvar grocy-units)
(defvar grocy-locations)
(defcustom grocy-server ""
"Grocy main domain."
:type 'string
:group 'grocy)
(defcustom grocy-api-key ""
"API key to authenticate to grocy server."
:type 'string
:group 'grocy)
(defun grocy--request (endpoint callback &optional method data)
"Do a GET request to api ENDPOINT and call CALLBACK on the resulting buffer.
Set METHOD to other request types and include the already encoded DATA."
(let ((url-request-method (or method "GET"))
(url-request-extra-headers
`(("GROCY-API-KEY" . ,grocy-api-key)
("accept" . "application/json")
("Content-Type" . "application/json")))
(url-request-data data))
(url-retrieve
(concat grocy-server "/api/" endpoint)
callback)))
(defun grocy-update-units ()
"Update localdb with units."
(grocy--request
"objects/quantity_units"
(lambda (_status)
(goto-char url-http-end-of-headers)
(let ((result (make-hash-table)))
(mapc
(lambda (unit)
(-let (((&hash "id" "name" "name_plural") unit))
(puthash id
(cons name name_plural)
result)))
(json-parse-buffer))
(setq grocy-units result)))))
(defun grocy-update-locations ()
"Update localdb with locations."
(grocy--request
"objects/locations"
(lambda (_status)
(goto-char url-http-end-of-headers)
(let ((result (make-hash-table)))
(mapc
(lambda (location)
(-let (((&plist :id) location))
(puthash id (cdddr location)
result)))
(json-parse-buffer :object-type 'plist))
(setq grocy-locations result)))))
(grocy-update-units)
(grocy-update-locations)
(defun grocy-stock--row (row)
"Compose ROW into tabulated entry for stock view."
(-let* (((&alist 'amount 'amount_opened 'product 'best_before_date) row)
((&alist 'name 'qu_id_stock) product)
(amount
(concat (number-to-string amount)
" "
(funcall
(if (= 1 amount) #'car #'cdr)
(gethash qu_id_stock grocy-units))
(when (< 0 amount_opened)
(concat " / "
(number-to-string amount_opened)
" open")))))
(list row (vector best_before_date amount name))))
(defun grocy-stock ()
"Show the grocery stock."
(interactive)
(grocy--request
"stock"
(lambda (_status)
(goto-char url-http-end-of-headers)
(let ((result (json-parse-buffer :object-type 'alist)))
(with-current-buffer (get-buffer-create "Grocy stock")
(tabulated-list-mode)
(setq tabulated-list-format [("Next due" 12 t)
("Amount" 20 t)
("Product" 20 t)]
;; tabulated-list-padding 2
tabulated-list-sort-key '("Next due"))
(thread-last
(cl-map 'list #'grocy-stock--row result)
(setq tabulated-list-entries))
(tabulated-list-init-header)
(tabulated-list-print)
(display-buffer (current-buffer)))))))
(defun grocy-products ()
"Render tabulated list view of products."
(interactive)
(grocy--request
"objects/products"
(lambda (_status)
(goto-char url-http-end-of-headers)
(let ((result (json-parse-buffer)))
(with-current-buffer (get-buffer-create "Grocy products")
(tabulated-list-mode)
(setq tabulated-list-format [("Name" 40 t)
("Location" 15 t)
("Unit" 8 t)
("min stock" 5 t)]
tabulated-list-sort-key '("Name"))
(thread-last
(cl-map 'list
(lambda (product)
(-let (((&hash "name" "location_id" "qu_id_stock"
"min_stock_amount") product))
(list product
(vector name
(car (gethash location_id grocy-locations))
(car (gethash qu_id_stock grocy-units))
(number-to-string min_stock_amount)))))
result)
(setq tabulated-list-entries))
(local-set-key "e" #'grocy-update-product)
(tabulated-list-init-header)
(tabulated-list-print)
(display-buffer (current-buffer)))))))
(defun grocy-update-product ()
"Update form for currently selected product."
(interactive)
(let* ((product (tabulated-list-get-id))
(id (gethash "id" product)))
(with-current-buffer (get-buffer-create "* Grocy update product *")
(erase-buffer)
(thread-first
product
(map-into 'alist)
(pp (current-buffer)))
(emacs-lisp-mode)
(keymap-local-set "C-c C-c"
(lambda () (interactive)
(grocy-update-object (format "products/%d" id))
(kill-buffer)))
(switch-to-buffer (current-buffer)))))
(defun grocy-buffer-obj->json (buffer)
"On the BUFFER read Lisp object and encode it into json."
(let ((json-null :null))
(with-current-buffer buffer
(goto-char (point-min))
(thread-first
(current-buffer)
(read)
(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))))
"PUT"
(grocy-buffer-obj->json (current-buffer))))
(defun grocy-new-product ()
(interactive)
(with-current-buffer (get-buffer-create "* Grocy new product *")
(erase-buffer)
(yaml-mode)
(keymap-local-set
"C-c C-c"
(lambda () (interactive)
(grocy--request
"objects/products"
(lambda (status)
(message "%S\n%S" status (buffer-string)))
"POST"
(grocy-buffer-yaml->json (current-buffer)))))
(insert "name: \n# ")
(maphash (lambda (key val) (insert (format "%d:'%s' " key (car val)))) grocy-locations)
(insert "\nlocation_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)))
(provide 'grocy)
;;; grocy.el ends here
|