Handle deletion of recipes

This commit is contained in:
Lucas Verney 2018-02-28 22:06:21 +01:00
parent 746f683cab
commit 8d57245ccf
9 changed files with 60 additions and 12 deletions

1
.gitignore vendored
View File

@ -3,6 +3,7 @@
package-lock.json
yarn.lock
*.db
__pycache__
.DS_Store
node_modules/

Binary file not shown.

View File

@ -1,6 +1,6 @@
import base64
import mimetypes
import magic
import requests
from peewee import (
Model, SqliteDatabase,
@ -9,7 +9,7 @@ from peewee import (
from playhouse.shortcuts import model_to_dict
database = SqliteDatabase('recipes.db', threadlocals=True)
database = SqliteDatabase('recipes.db')
database.connect()
@ -43,7 +43,8 @@ class Recipe(Model):
def to_dict(self):
serialized = model_to_dict(self)
prepend_info = (
'data:%s;base64' % mimetypes.guess_type(serialized['picture'])[0]
'data:%s;base64' % magic.from_buffer(serialized['picture'],
mime=True)
)
serialized['picture'] = '%s,%s' % (
prepend_info,

View File

@ -19,7 +19,7 @@
</v-form>
</v-flex>
</v-layout>
<v-layout row wrap mt-5>
<v-layout row wrap mt-5 v-if="featureAddManually">
<v-flex xs12>
<h2>Add manually</h2>
<v-form v-model="validAdd">
@ -116,6 +116,7 @@ export default {
}
},
],
featureAddManually: false,
};
},
methods: {

View File

@ -1,12 +1,28 @@
<template>
<v-container grid-list-md>
<v-layout row wrap>
<v-flex xs12 v-if="this.recipe">
<h1>{{ this.recipe.title }}</h1>
<p><img :src="this.recipe.picture" height="300px" /></p>
<v-flex xs6 offset-xs3>
<h1 class="text-xs-center mt-3 mb-3">
{{ this.recipe.title }}
<v-btn @click="handleDelete">Delete</v-btn>
</h1>
<p class="text-xs-center">
<img :src="this.recipe.picture" />
</p>
<v-layout row text-xs-center>
<v-flex xs6>
<p><v-icon>timelapse</v-icon> {{ recipe.preparation_time }} mins</p>
</v-flex>
<v-flex xs6>
<p><v-icon>whatshot</v-icon> {{ recipe.cooking_time }} mins</p>
</v-flex>
</v-layout>
<p>{{ this.recipe.short_description }}</p>
<p>{{ this.recipe.ingredients }}</p>
<p>{{ this.recipe.instructions }}</p>
<p v-if="this.recipe.url">
<a :href="this.recipe.url">Original link</a>
</p>
</v-flex>
</v-layout>
</v-container>
@ -40,6 +56,18 @@ export default {
this.isLoading = false;
});
},
handleDelete() {
fetch(`${constants.API_URL}/api/v1/recipe/${this.$route.params.recipeId}`, {
method: 'DELETE',
})
.then(() => this.$router.replace('/'));
},
},
};
</script>
<style scoped>
img {
width: 75%;
}
</style>

View File

@ -1,7 +1,6 @@
import json
import bottle
import peewee
from cuizin import add_recipe
from cuizin import db
@ -55,11 +54,13 @@ def api_v1_recipes_post():
recipes = []
try:
recipes = [add_recipe(data['url']).to_dict()]
except peewee.IntegrityError:
recipes = [db.Recipe.select().where(
recipe = db.Recipe.select().where(
db.Recipe.url == data['url']
).first().to_dict()]
).first()
assert recipe
recipes = [recipe.to_dict()]
except AssertionError:
recipes = [add_recipe(data['url']).to_dict()]
return {
'recipes': recipes
@ -81,6 +82,21 @@ def api_v1_recipe(id):
}
@app.delete('/api/v1/recipe/:id', ['DELETE', 'OPTIONS'])
def api_v1_recipe_delete(id):
# CORS
if bottle.request.method == 'OPTIONS':
return ''
db.Recipe.delete().where(
db.Recipe.id == id
).execute()
return {
'status': 'OK'
}
@app.get('/static/<filename:path>')
def get_static_files(filename):
"""Get Static files"""

View File

@ -1,5 +1,6 @@
bottle
peewee
python-magic
requests
https://git.weboob.org/weboob/devel/repository/archive.zip?ref=master
https://git.weboob.org/weboob/modules/repository/archive.zip?ref=master