# Path: entity/category.py
# class Category:
#     def __init__(self, id=None, name=''):
#     def __str__(self):
#     def delete(self, db):
#     def find_all(db):
#     def find_name(db, name):
#     def find_pk(db, id):
#     def find_recipe(db, recipe):
#     def from_row(row):
#     def is_new(self):
#     def name_exists(db, name):
#     def save(self, db):
#     def __generic_find(db, query, params):
# 
# Path: entity/image.py
# class Image:
#     def __init__(self, id=None, recipe_id=None, path=''):
#     def __str__(self):
#     def delete(self, db):
#     def find_all(db):
#     def find_pk(db, id):
#     def find_recipe(db, recipe):
#     def from_row(row):
#     def is_new(self):
#     def save(self, db):
#     def __generic_find(db, query, params):
# 
# Path: entity/synonym.py
# class Synonym:
#     def __init__(self, id=None, recipe_id=None, name=''):
#     def __str__(self):
#     def delete(self, db):
#     def find_all(db):
#     def find_pk(db, id):
#     def find_recipe(db, recipe):
#     def from_row(row):
#     def is_new(self):
#     def save(self, db):
#     def __generic_find(db, query, params):
# 
# Path: entity/tag.py
# class Tag:
#     def __init__(self, id=None, name='', synonym_of=None, synonyms=[]):
#     def __str__(self):
#     def delete(self, db):
#     def find_all(db):
#     def find_name(db, name):
#     def find_pk(db, id):
#     def find_recipe(db, recipe):
#     def find_synonyms(self, db):
#     def from_row(db, row, find_synonyms=True):
#     def is_new(self):
#     def name_exists(db, name):
#     def save(self, db):
#     def __generic_find(db, query, params):
# 
# Path: entity/url.py
# class Url:
#     def __init__(self, id=None, recipe_id=None, name='', url=''):
#     def __str__(self):
#     def delete(self, db):
#     def find_all(db):
#     def find_pk(db, id):
#     def find_recipe(db, recipe):
#     def from_row(row):
#     def is_new(self):
#     def save(self, db):
#     def __generic_find(db, query, params):
# 
# Path: entity/user.py
# class User:
#     def __init__(self, id=None, name=None, pw_hash=None, salt=None, session=None):
#     def __str__(self):
#     def count_all(db):
#     def delete(self, db):
#     def find_all(db):
#     def find_name(db, name):
#     def find_pk(db, id):
#     def from_row(db, row):
#     def is_new(self):
#     def save(self, db):
#     def __generic_find(db, query, params):


from entity import category as category_entity
from entity import image as image_entity
from entity import synonym as synonym_entity
from entity import tag as tag_entity
from entity import url as url_entity
from entity import user as user_entity
import os

    @staticmethod
    def find_random(db, num):
        """ Find random entities in database. Returns list of found
        entities ordered by name ascending."""
        query = 'SELECT description, id, info, ingredients, ' \
                'rating, serving_size, title, user_id ' \
                'FROM recipes ' \
                'ORDER BY RANDOM() ' \
                'LIMIT ?'
        params = [num]
        cursor = db.cursor()
        cursor.execute(query, params)
        result = []
        for row in cursor.fetchall():
            result.append(Recipe.from_row(db, row))
        return result

    @staticmethod
    def from_row(db, row):
        """ Create entity from given row. """
        recipe = Recipe(description=row[0], id=row[1], info=row[2],
                        ingredients=row[3], rating=row[4], serving_size=row[5],
                        title=row[6])
        recipe.categories = category_entity.Category.find_recipe(db, recipe)
        recipe.images = image_entity.Image.find_recipe(db, recipe)
        recipe.synonyms = synonym_entity.Synonym.find_recipe(db, recipe)
        recipe.tags = tag_entity.Tag.find_recipe(db, recipe)
        recipe.urls = url_entity.Url.find_recipe(db, recipe)
        if row[7] is not None:

# Path: entity/category.py
# class Category:
#     def __init__(self, id=None, name=''):
#     def __str__(self):
#     def delete(self, db):
#     def find_all(db):
#     def find_name(db, name):
#     def find_pk(db, id):
#     def find_recipe(db, recipe):
#     def from_row(row):
#     def is_new(self):
#     def name_exists(db, name):
#     def save(self, db):
#     def __generic_find(db, query, params):
# 
# Path: entity/image.py
# class Image:
#     def __init__(self, id=None, recipe_id=None, path=''):
#     def __str__(self):
#     def delete(self, db):
#     def find_all(db):
#     def find_pk(db, id):
#     def find_recipe(db, recipe):
#     def from_row(row):
#     def is_new(self):
#     def save(self, db):
#     def __generic_find(db, query, params):
# 
# Path: entity/synonym.py
# class Synonym:
#     def __init__(self, id=None, recipe_id=None, name=''):
#     def __str__(self):
#     def delete(self, db):
#     def find_all(db):
#     def find_pk(db, id):
#     def find_recipe(db, recipe):
#     def from_row(row):
#     def is_new(self):
#     def save(self, db):
#     def __generic_find(db, query, params):
# 
# Path: entity/tag.py
# class Tag:
#     def __init__(self, id=None, name='', synonym_of=None, synonyms=[]):
#     def __str__(self):
#     def delete(self, db):
#     def find_all(db):
#     def find_name(db, name):
#     def find_pk(db, id):
#     def find_recipe(db, recipe):
#     def find_synonyms(self, db):
#     def from_row(db, row, find_synonyms=True):
#     def is_new(self):
#     def name_exists(db, name):
#     def save(self, db):
#     def __generic_find(db, query, params):
# 
# Path: entity/url.py
# class Url:
#     def __init__(self, id=None, recipe_id=None, name='', url=''):
#     def __str__(self):
#     def delete(self, db):
#     def find_all(db):
#     def find_pk(db, id):
#     def find_recipe(db, recipe):
#     def from_row(row):
#     def is_new(self):
#     def save(self, db):
#     def __generic_find(db, query, params):
# 
# Path: entity/user.py
# class User:
#     def __init__(self, id=None, name=None, pw_hash=None, salt=None, session=None):
#     def __str__(self):
#     def count_all(db):
#     def delete(self, db):
#     def find_all(db):
#     def find_name(db, name):
#     def find_pk(db, id):
#     def from_row(db, row):
#     def is_new(self):
#     def save(self, db):
#     def __generic_find(db, query, params):


# Path: entity/recipe.py
from entity import category as category_entity
from entity import image as image_entity
from entity import synonym as synonym_entity
from entity import tag as tag_entity
from entity import url as url_entity
from entity import user as user_entity
import os


    @staticmethod
    def find_random(db, num):
        """ Find random entities in database. Returns list of found
        entities ordered by name ascending."""
        query = 'SELECT description, id, info, ingredients, ' \
                'rating, serving_size, title, user_id ' \
                'FROM recipes ' \
                'ORDER BY RANDOM() ' \
                'LIMIT ?'
        params = [num]
        cursor = db.cursor()
        cursor.execute(query, params)
        result = []
        for row in cursor.fetchall():
            result.append(Recipe.from_row(db, row))
        return result

    @staticmethod
    def from_row(db, row):
        """ Create entity from given row. """
        recipe = Recipe(description=row[0], id=row[1], info=row[2],
                        ingredients=row[3], rating=row[4], serving_size=row[5],
                        title=row[6])
        recipe.categories = category_entity.Category.find_recipe(db, recipe)
        recipe.images = image_entity.Image.find_recipe(db, recipe)
        recipe.synonyms = synonym_entity.Synonym.find_recipe(db, recipe)
        recipe.tags = tag_entity.Tag.find_recipe(db, recipe)
        recipe.urls = url_entity.Url.find_recipe(db, recipe)
        if row[7] is not None:

            recipe.author = user_entity.User.find_pk(db, row[7])