From 983d40f2c16af5bf81d31f3d935b88a4aff18d67 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Tue, 12 Jan 2021 23:28:13 +0100 Subject: [PATCH] save WIP --- cookbook/templates/test.html | 8 +++- recipes/settings.py | 4 +- vue/src/apps/RecipeView/RecipeView.vue | 52 ++++++++++++++++----- vue/src/components/Ingredient.vue | 44 +++++++++++++++--- vue/src/components/Step.vue | 39 ++++++++++++---- vue/src/utils/fractions.js | 63 ++++++++++++++++++++++++++ vue/src/utils/utils.js | 36 ++++++++++++++- vue/webpack-stats.json | 2 +- 8 files changed, 213 insertions(+), 35 deletions(-) create mode 100644 vue/src/utils/fractions.js diff --git a/cookbook/templates/test.html b/cookbook/templates/test.html index 8bf6d6e6b..26261a6e5 100644 --- a/cookbook/templates/test.html +++ b/cookbook/templates/test.html @@ -22,9 +22,13 @@ {% endif %} - {% render_bundle 'chunk-vendors' %} diff --git a/recipes/settings.py b/recipes/settings.py index 79929f4a5..a9fcfa748 100644 --- a/recipes/settings.py +++ b/recipes/settings.py @@ -24,9 +24,7 @@ SECRET_KEY = os.getenv('SECRET_KEY') if os.getenv('SECRET_KEY') else 'INSECURE_S DEBUG = bool(int(os.getenv('DEBUG', True))) -INTERNAL_IPS = [ - '127.0.0.1', -] +INTERNAL_IPS = os.getenv('INTERNAL_IPS').split(',') if os.getenv('INTERNAL_IPS') else ['127.0.0.1'] # allow djangos wsgi server to server mediafiles GUNICORN_MEDIA = bool(int(os.getenv('GUNICORN_MEDIA', True))) diff --git a/vue/src/apps/RecipeView/RecipeView.vue b/vue/src/apps/RecipeView/RecipeView.vue index 434ac6d8c..5186dedbf 100644 --- a/vue/src/apps/RecipeView/RecipeView.vue +++ b/vue/src/apps/RecipeView/RecipeView.vue @@ -23,10 +23,36 @@
-
-
-
- +
+ +
+
+
+
+

{{ _('Ingredients') }}

+
+
+
+ +
+ +
+
+
+
+
+
+
+ +
+
+ +
+
+
+
+
@@ -34,13 +60,16 @@
-
-
+ +
+ +
+
-
- +
+
@@ -77,7 +106,7 @@ export default { loading: true, recipe_id: window.RECIPE_ID, recipe: undefined, - has_ingredients: false, + ingredient_count: 0, servings: 1, } }, @@ -91,9 +120,8 @@ export default { this.loading = false for (let step of this.recipe.steps) { - if (step.ingredients.length > 0) { - this.has_ingredients = true - } + this.ingredient_count += step.ingredients.length + if (step.time !== 0) { this.has_times = true } diff --git a/vue/src/components/Ingredient.vue b/vue/src/components/Ingredient.vue index 68f50aaaf..c5f3251cb 100644 --- a/vue/src/components/Ingredient.vue +++ b/vue/src/components/Ingredient.vue @@ -1,20 +1,52 @@ diff --git a/vue/src/components/Step.vue b/vue/src/components/Step.vue index 867a547f8..658ea42cf 100644 --- a/vue/src/components/Step.vue +++ b/vue/src/components/Step.vue @@ -1,18 +1,32 @@ @@ -20,15 +34,20 @@ diff --git a/vue/src/utils/fractions.js b/vue/src/utils/fractions.js new file mode 100644 index 000000000..3b60f0485 --- /dev/null +++ b/vue/src/utils/fractions.js @@ -0,0 +1,63 @@ +/* frac.js (C) 2012-present SheetJS -- http://sheetjs.com */ + +/*https://developer.aliyun.com/mirror/npm/package/frac/v/0.3.0 Apache license*/ +export function frac(x, D, mixed) { + var n1 = Math.floor(x), d1 = 1; + var n2 = n1 + 1, d2 = 1; + if (x !== n1) while (d1 <= D && d2 <= D) { + var m = (n1 + n2) / (d1 + d2); + if (x === m) { + if (d1 + d2 <= D) { + d1 += d2; + n1 += n2; + d2 = D + 1; + } else if (d1 > d2) d2 = D + 1; + else d1 = D + 1; + break; + } else if (x < m) { + n2 = n1 + n2; + d2 = d1 + d2; + } else { + n1 = n1 + n2; + d1 = d1 + d2; + } + } + if (d1 > D) { + d1 = d2; + n1 = n2; + } + if (!mixed) return [0, n1, d1]; + var q = Math.floor(n1 / d1); + return [q, n1 - q * d1, d1]; +} + +export function cont(x, D, mixed) { + var sgn = x < 0 ? -1 : 1; + var B = x * sgn; + var P_2 = 0, P_1 = 1, P = 0; + var Q_2 = 1, Q_1 = 0, Q = 0; + var A = Math.floor(B); + while (Q_1 < D) { + A = Math.floor(B); + P = A * P_1 + P_2; + Q = A * Q_1 + Q_2; + if ((B - A) < 0.00000005) break; + B = 1 / (B - A); + P_2 = P_1; + P_1 = P; + Q_2 = Q_1; + Q_1 = Q; + } + if (Q > D) { + if (Q_1 > D) { + Q = Q_2; + P = P_2; + } else { + Q = Q_1; + P = P_1; + } + } + if (!mixed) return [0, sgn * P, Q]; + var q = Math.floor(sgn * P / Q); + return [q, sgn * P - q * Q, Q]; +} \ No newline at end of file diff --git a/vue/src/utils/utils.js b/vue/src/utils/utils.js index 493e8f745..44bd91aec 100644 --- a/vue/src/utils/utils.js +++ b/vue/src/utils/utils.js @@ -61,4 +61,38 @@ export const ResolveUrlMixin = { export function resolveDjangoUrl(url, params) { return window.Urls[url](params) -} \ No newline at end of file +} + +/* +* other utilities +* */ + +export function getUserPreference(pref) { + return window.USER_PREF[pref] +} + +import {frac} from "@/utils/fractions"; + +export function calculateAmount(amount, factor) { + if (getUserPreference('user_fractions')) { + let return_string = '' + let fraction = frac.cont((amount * factor), 9, true) + + if (fraction[0] > 0) { + return_string += fraction[0] + } + + if (fraction[1] > 0) { + return_string += ` ${(fraction[1])}${(fraction[2])}` + } + + return return_string + } else { + return roundDecimals(amount * factor) + } +} + +export function roundDecimals(num) { + let decimals = ((getUserPreference('user_fractions')) ? getUserPreference('user_fractions') : 2); + return +(Math.round(num + `e+${decimals}`) + `e-${decimals}`); +} diff --git a/vue/webpack-stats.json b/vue/webpack-stats.json index 90f3bb864..00784bbf1 100644 --- a/vue/webpack-stats.json +++ b/vue/webpack-stats.json @@ -1 +1 @@ -{"status":"done","publicPath":"http://localhost:8080/","chunks":{"chunk-vendors":[{"name":"js/chunk-vendors.js","publicPath":"http://localhost:8080/js/chunk-vendors.js","path":"F:\\Developement\\Django\\recipes\\cookbook\\static\\vue\\js\\chunk-vendors.js"}],"recipe_view":[{"name":"js/recipe_view.js","publicPath":"http://localhost:8080/js/recipe_view.js","path":"F:\\Developement\\Django\\recipes\\cookbook\\static\\vue\\js\\recipe_view.js"},{"name":"recipe_view.7d030f252e9720efc1eb.hot-update.js","publicPath":"http://localhost:8080/recipe_view.7d030f252e9720efc1eb.hot-update.js","path":"F:\\Developement\\Django\\recipes\\cookbook\\static\\vue\\recipe_view.7d030f252e9720efc1eb.hot-update.js"}]},"error":"ModuleBuildError","message":"Module build failed (from ./node_modules/eslint-loader/index.js):\nError: ENOENT: no such file or directory, open 'F:\\Developement\\Django\\recipes\\vue\\src\\apps\\RecipeView\\main.js'"} \ No newline at end of file +{"status":"done","publicPath":"http://localhost:8080/","chunks":{"chunk-vendors":[{"name":"js/chunk-vendors.js","publicPath":"http://localhost:8080/js/chunk-vendors.js","path":"F:\\Developement\\Django\\recipes\\cookbook\\static\\vue\\js\\chunk-vendors.js"}],"recipe_view":[{"name":"js/recipe_view.js","publicPath":"http://localhost:8080/js/recipe_view.js","path":"F:\\Developement\\Django\\recipes\\cookbook\\static\\vue\\js\\recipe_view.js"},{"name":"recipe_view.e165d99965cf62182119.hot-update.js","publicPath":"http://localhost:8080/recipe_view.e165d99965cf62182119.hot-update.js","path":"F:\\Developement\\Django\\recipes\\cookbook\\static\\vue\\recipe_view.e165d99965cf62182119.hot-update.js"}]},"error":"ModuleError","message":"Module Error (from ./node_modules/eslint-loader/index.js):\n\nF:\\Developement\\Django\\recipes\\vue\\src\\components\\Step.vue\n 22:37 error Parsing error: duplicate-attribute vue/no-parsing-error\n 22:37 error Duplicate attribute 'class' vue/no-duplicate-attributes\n\n✖ 2 problems (2 errors, 0 warnings)\n"} \ No newline at end of file