예제 #1
0
def TestRecipe(
    title=None,
    slug=None,
    short_description=None,
    serving_size=ServingSize.FIVE_TO_SIX,
    image=None,
    ingredients=None,
    instructions=None,
    featured=False,
    is_public=True,
    added_by=None,
    popularity=None,
):
    # generating a unique title
    if not title:
        title_base = lorem_ipsum(3)
        suffix = 1
        not_unique = True
        while not_unique:
            title = "%s %s" % (title_base, suffix)
            not_unique = Recipe.objects.filter(title=title)
            suffix += 1

    return Recipe.objects.create(
        title=title,
        slug=slug,
        short_description=short_description or lorem_ipsum(3),
        serving_size=serving_size,
        image=image,
        ingredients=ingredients or lorem_ipsum(7),
        instructions=instructions or lorem_ipsum(7),
        featured=featured,
        is_public=is_public,
        added_by=added_by or TestUser(),
    )
예제 #2
0
def TestRecipeTag(name=None, type=TagType.MISCELLANEOUS, is_public=True, added_by=None):
    # generating unique name
    if not name:
        name_base = lorem_ipsum(1)
        suffix = 1
        not_unique = True
        while not_unique:
            name = "%s%s" % (name_base, suffix)
            not_unique = RecipeTag.objects.filter(name=name)
            suffix += 1

    return RecipeTag.objects.create(name=name, type=type, is_public=is_public, added_by=added_by)
예제 #3
0
def generate_test_data():
    def make_ingredient(category):
        ingredient = random.choice(category)
        
        if category in (MEASURED_INGREDIENTS, LIQUID_INGREDIENTS):
            units = random.choice(UNITS)
        elif category == WEIGHED_INGREDIENTS:
            units = random.choice(WEIGHTS)
        else:
            units = None
        
        quantity = random.choice(QUANTITIES)
        if quantity not in ('1', '1/2', '1/4', '1/8'):
            if units:
                if units in ('cup', 'lb'):
                    units += "s"
            else:
                ingredient += "s"
        
        if units:
            return " ".join([quantity, units, ingredient])
        return " ".join([quantity, ingredient])
    
    def make_ingredients_list():
        ingredients = []
        for i in range(random.randint(2, 4)):
            ingredients.append(make_ingredient(MEASURED_INGREDIENTS))
        for i in range(random.randint(1, 2)):
            ingredients.append(make_ingredient(SINGLE_INGREDIENTS))
        for i in range(random.randint(1, 2)):
            ingredients.append(make_ingredient(WEIGHED_INGREDIENTS))
        for i in range(random.randint(0, 1)):
            ingredients.append(make_ingredient(LIQUID_INGREDIENTS))
        
        random.shuffle(ingredients)
        return "<br>".join(ingredients) 
    
    def make_title():
        words = []
        for word_group in (NAMES, ADJECTIVES, DAYS_OF_THE_WEEK, COOKING_METHOD,
                           SINGLE_INGREDIENTS, DISH):
            use = random.randint(0, 1)
            if use or word_group in (SINGLE_INGREDIENTS, DISH):
                title_word = random.choice(word_group)
                
                # we have to pull this one out individually because title-
                # casing words like "Amy's" does strange things with the
                # capitalization
                if word_group == SINGLE_INGREDIENTS:
                    title_word = title_word.title()
                words.append(title_word)

        return " ".join(words)
    
    def make_instructions():
        instructions = []
        for inst in (OPENING_INSTRUCTIONS, SECOND_INSTRUCTIONS, 
                     MIDDLE_INSTRUCTIONS, ENDING_INSTRUCTIONS):
            number_to_choose = random.randint(1, 2)
            for i in range(number_to_choose):
                instructions.append(random.choice(inst))
        
        return " ".join(instructions)

    print "Making users..."
    users = [TestUser() for i in range(30)]\
    
    print "Creating tags..."
    call_command('loaddata', 'recipe_tags')
    for i in range(10):
        TestRecipeTag(is_public=bool(random.randint(0, 1)),
                      added_by=random.choice(users))
    
    print "Adding recipes and ratings..."
    for i in range(250):        
        title = make_title()
        while Recipe.objects.filter(title=title):
            title = make_title()
        
        featured = random.randint(1, 20) == 1
        is_public = random.randint(1, 30) != 1
        recipe = TestRecipe(title=title, 
                            short_description=lorem_ipsum(random.randint(5, 8)),
                            serving_size=random.randint(0, 9),
                            ingredients=make_ingredients_list(),
                            instructions=make_instructions(), 
                            featured=featured, is_public=is_public,
                            added_by=random.choice(users))
        
        selected_tags = []
        num_tags = random.randint(1, 4)
        tags = RecipeTag.objects.all()
        while len(selected_tags) < num_tags:
            tag = random.choice(tags)
            if tag not in selected_tags:
                selected_tags.append(tag)
        for tag in selected_tags:
            recipe.tags.add(tag)
        
        rated_users = []
        num_ratings = random.randint(0, 20)
        while len(rated_users) < num_ratings:
            rated_by = random.choice(users)
            if rated_by not in rated_users:
                rated_users.append(rated_by)
        for user in rated_users:
            vote = random.randint(1, 10) != 10
            TestRating(recipe=recipe, rated_by=user, vote=vote)
    
    print "30 users and 250 recipes created. Done!"