Skip to content

Instantly share code, notes, and snippets.

@EntityReborn
Created November 8, 2011 21:39
Show Gist options
  • Save EntityReborn/1349335 to your computer and use it in GitHub Desktop.
Save EntityReborn/1349335 to your computer and use it in GitHub Desktop.
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from sqlalchemy import ForeignKey, DateTime, create_engine
from sqlalchemy.orm import sessionmaker, relationship, backref
from datetime import datetime
Base = declarative_base()
class Ingredient(Base):
__tablename__ = 'ingredients'
id = Column(Integer, primary_key=True)
amount = Column(String)
ingredient = Column(String)
notes = Column(String)
recipe_id = Column(Integer, ForeignKey('recipies.id'))
def __init__(self, amount, ingredient, notes=None):
self.amount = amount
self.ingredient = ingredient
self.notes = notes or ""
def __repr__(self):
return "<Ingredient('%s', '%s', '%s')>" % \
(self.amount, self.ingredient, self.notes)
class Step(Base):
__tablename__ = 'steps'
id = Column(Integer, primary_key=True)
stepnumber = Column(Integer)
steptext = Column(String)
notes = Column(String)
recipe_id = Column(Integer, ForeignKey('recipies.id'))
def __init__(self, number, text, notes):
self.stepnumber = number
self.text = text
self.notes = notes
def __repr__(self):
return "<Step(%d, '%s', '%s')>" % \
(self.number, self.text, self.notes)
class Recipe(Base):
__tablename__ = 'recipies'
id = Column(Integer, primary_key=True)
name = Column(String)
author = Column(String)
servings = Column(Integer)
preptime = Column(String)
cooktime = Column(String)
created = Column(DateTime)
notes = Column(String)
parent_id = Column(Integer, ForeignKey('recipies.id'))
subrecipes = relationship("Recipe", remote_side=[id], backref="parent")
ingredients = relationship("Ingredient", backref="recipe")
steps = relationship("Step", backref="recipe")
def __init__(self, name, author, servings, preptime, cooktime,
created, notes=None):
self.name = name
self.author = author
self.servings = servings
self.preptime = preptime
self.cooktime = cooktime
self.created = created
self.notes = notes or ""
def __repr__(self):
return "<Recipe('%s', '%s', %d, '%s', '%s', '%s', '%s')>" % \
(self.name, self.author, self.servings, self.preptime, self.cooktime,
self.created, self.notes)
class RecipeManager(object):
def __init__(self, db="/:memory:"):
self.engine = create_engine('sqlite://%s' % db)
Base.metadata.create_all(self.engine)
Session = sessionmaker(bind=self.engine)
self.session = Session()
def newIngredient(self, recipe, amount, ingredients, notes=None):
if not notes:
notes = ""
ingredient = Ingredient(amount, ingredients, notes)
self.session.add(ingredient)
recipe.ingredients.append(ingredient)
self.session.commit()
return ingredient
def newStep(self, recipe, number, text, notes):
step = Step(number, text, notes)
self.session.add(step)
recipe.steps.append(step)
self.session.commit()
return step
def addRecipe(self, name, author, servings, preptime, cooktime, notes=""):
recipe = Recipe(name, author, servings, preptime,
cooktime, datetime.now(), notes)
self.session.add(recipe)
self.session.commit()
return recipe
def commit(self):
self.session.commit()
if __name__ == "__main__":
m = RecipeManager()
r = m.addRecipe("Test", "Me", 0, "0", "0")
r2 = m.addRecipe("Test2", "Me", 0, "0", "0")
r.subrecipies.append(r2)
i = m.newIngredient(r, "1/2 cups", "Sugar")
r.ingredients.append(i)
m.commit()
print r2.parent
print r.name, r.ingredients[0].ingredient
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment