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 package-lock.json
yarn.lock yarn.lock
*.db *.db
__pycache__
.DS_Store .DS_Store
node_modules/ node_modules/

Binary file not shown.

View File

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

View File

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

View File

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

View File

@ -1,7 +1,6 @@
import json import json
import bottle import bottle
import peewee
from cuizin import add_recipe from cuizin import add_recipe
from cuizin import db from cuizin import db
@ -55,11 +54,13 @@ def api_v1_recipes_post():
recipes = [] recipes = []
try: try:
recipes = [add_recipe(data['url']).to_dict()] recipe = db.Recipe.select().where(
except peewee.IntegrityError:
recipes = [db.Recipe.select().where(
db.Recipe.url == data['url'] 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 { return {
'recipes': recipes '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>') @app.get('/static/<filename:path>')
def get_static_files(filename): def get_static_files(filename):
"""Get Static files""" """Get Static files"""

View File

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