<file_sep>import redis
from bestrecipe.models import Recipe
r = redis.StrictRedis(host='localhost', port=6379, db=0)
jsonList = []
for key in r.keys():
    jsonList.append(key)

<file_sep>from django.db import models
from jsonfield import JSONField

class Recipe(models.Model):
    title = models.CharField(max_length=300)
    summary = JSONField()
    cooktime = models.IntegerField()
    nutrition = JSONField()
    img = models.URLField()
    URL = models.URLField()
    structIngredients = JSONField()
    ingredients = JSONField()
    instruction = JSONField()
    description = JSONField()

<file_sep>from django.conf.urls import patterns, include, url
from django.conf import settings
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'mysite.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^$', 'bestrecipe.views.index'),
                       # url(r'^blog/', include('blog.urls')),
    (r'^static/(?P<path>.*)$','django.views.static.serve',{'document_root':settings.STATIC_PATH}),
    url(r'^recipe$', 'bestrecipe.views.recipe'),
    url(r'^results$', 'bestrecipe.views.results'),
    
)
<file_sep>from django.shortcuts import render_to_response
from django.template import RequestContext, Context
from bestrecipe.models import Recipe
import redis
import re
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

def index(request):
    return render_to_response('index.html')

def results(request):
    r = redis.StrictRedis(host='localhost', port=6379, db=0)
    ingredients= request.GET.getlist('ingredients', '')
    conj=set()
    results=[]
    for i in ingredients:
        if conj==set():
            conj = set(r.lrange(i,0,-1))
        else:
            conj=conj&set(r.lrange(i,0,-1))
    for index in conj:
        result = Recipe.objects.get(id=index)
        results.append(result)
        
    paginator = Paginator(results, 9)
    page = request.GET.get('page')
    try:
        group = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        group = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        group = paginator.page(paginator.num_pages)

    return render_to_response('results.html',{'results':group})

def recipe(request):
    index = request.GET.get('index', '')
    recipe = Recipe.objects.get(id=index)
    cooktime=recipe.cooktime
    if cooktime > 59:
        recipe.cooktime /= 60
        timeUnit='hrs'
    elif cooktime > 3599:
        recipe.cooktime /= 3600
        timeUnit='days'
    else:
        timeUnit='mins'

    pdv={"sodium":2400,"total_fat":65,"saturated_fat":20,"cholesterol":300,"total_carbohydrate":300,"dietary_fiber":25,"sugars":1,"protein":1,"calories":1,"calories_from_fat":1}
    recipe.nutrition={item:int(recipe.nutrition[item]) for item in recipe.nutrition}
    percent ={item:int(float(recipe.nutrition[item])/float(pdv[item])*100) for item in recipe.nutrition}
    #matches = re.search(r'\.(\w+\.com)',  recipe.URL)
    temp = re.findall("http://w*\.*([^/]*\.\w+)(/|$)",recipe.URL,re.I)
    if temp:
        domain=temp[0][0]
    else:
        domain="None"
    #domain = matches.group(1);

    return render_to_response('recipe.html',{'recipe':recipe,'timeunit':timeUnit,'percent':percent,'domain':domain})



<file_sep>import pickle
import redis
from bestrecipe.models import Recipe

def add2DB(self,fileName):
    with open(fileName,"rb") as infile:
        cookbook=pickle.load(infile)

    for i in cookbook:
        dict={}
        for key, value in i.items():
            dict[key] = value
        p = Recipe(title=dict['title'],summary=dict['summary'],cooktime=dict['cooktime'],img=dict['img'],URL=dict['url'],nutrition=dict['nutrient'],structIngredients=dict['structIngs'],ingredients=dict['ingredient'],instruction=dict['instruction'],description=dict['description'])
        p.save()

def add2Redis(self):
    r = redis.StrictRedis(host='localhost', port=6379, db=0)
    for i in Recipe.objects.all():
        ingres=i.structIngredients
        for ingName,trival in ingres.items():
            r.lpush(ingName,i.id)

def clearRedis(self):
    r = redis.StrictRedis(host='localhost', port=6379, db=0)
    for key in r.keys():
        r.delete(key)

def showRedis(self):
    r = redis.StrictRedis(host='localhost', port=6379, db=0)
    for key in r.keys():
        r.lrange(key,0,-1)
