cuizin/cuizin/db.py

101 lines
2.9 KiB
Python
Raw Normal View History

2018-03-01 17:57:45 +01:00
"""
Database definition
"""
import base64
2018-02-28 23:11:22 +01:00
import json
2018-02-28 22:06:21 +01:00
import magic
import requests
from peewee import (
Model, SqliteDatabase,
BlobField, CharField, IntegerField, TextField
)
from playhouse.shortcuts import model_to_dict
2018-02-28 22:06:21 +01:00
database = SqliteDatabase('recipes.db')
2018-02-27 17:47:32 +01:00
database.connect()
2018-02-28 23:11:22 +01:00
class JSONField(TextField):
2018-03-01 17:57:45 +01:00
"""
A Peewee database field with transparent JSON dump/load.
"""
2018-02-28 23:11:22 +01:00
def db_value(self, value):
return json.dumps(value)
def python_value(self, value):
if value is not None:
return json.loads(value)
class Recipe(Model):
2018-03-01 17:57:45 +01:00
"""
Our base model for a recipe.
"""
# Name of the recipe
title = CharField(null=True)
# URL, should be unique
2018-02-27 17:47:32 +01:00
url = CharField(null=True, unique=True)
# Author
author = CharField(null=True)
# Picture as a binary blob
2018-03-11 16:49:05 +01:00
picture = TextField(null=True)
# Short description
short_description = TextField(null=True)
# Number of persons as text, as it can be either "N persons" or "N parts"
2018-02-28 23:11:22 +01:00
nb_person = TextField(null=True)
# Preparation and cooking times
preparation_time = IntegerField(null=True) # In minutes
cooking_time = IntegerField(null=True) # In minutes
# List of ingredients
2018-02-28 23:11:22 +01:00
ingredients = JSONField(null=True)
# Instructions
instructions = TextField(null=True)
class Meta:
database = database
2018-03-10 11:43:14 +01:00
def update_from_dict(self, d):
"""
2018-03-10 11:43:14 +01:00
Update field taking values from a dict of values.
"""
2018-03-01 17:57:45 +01:00
# Set fields
2018-02-28 23:11:22 +01:00
for field in ['title', 'url', 'author', 'picture_url',
'short_description', 'preparation_time', 'cooking_time',
2018-03-10 11:43:14 +01:00
'ingredients', 'instructions', 'nb_person']:
value = d.get(field, None)
if value:
setattr(self, field, value)
2018-03-01 17:57:45 +01:00
# Download picture and save it as a blob
2018-03-10 11:43:14 +01:00
if d.get('picture_url', None):
2018-03-11 16:49:05 +01:00
try:
picture = requests.get(d['picture_url']).content
picture_mime = (
'data:%s;base64' % magic.from_buffer(picture,
mime=True)
)
self.picture = '%s,%s' % (
picture_mime,
base64.b64encode(picture).decode('utf-8')
)
except requests.exceptions.InvalidSchema:
self.picture = d['picture_url']
2018-03-10 11:43:14 +01:00
def update_from_weboob(self, weboob_obj):
"""
Update fields taking values from the Weboob object.
"""
weboob_dict = dict(weboob_obj.iter_fields())
if weboob_dict.get('nb_person', None):
weboob_dict['nb_person'] = '-'.join(
str(num) for num in weboob_dict['nb_person']
)
self.update_from_dict(weboob_dict)
def to_dict(self):
2018-03-01 17:57:45 +01:00
"""
Dict conversion function, for serialization in the API.
"""
2018-03-11 16:49:05 +01:00
return model_to_dict(self)