Example #1
0
 def get_recipes(self, start, num):
     start = str_to_int(start, 0)
     num = str_to_int(num, 5)
     if num < 1:
         num = 5
     if start < 0:
         start = 0
     return self.recipes.all()[start:num+start]
Example #2
0
    def get_recipe_list(cls, meat_labels, effect_labels, time_labels, order, desc, start, num):
        '''
        根据条件获取菜谱列表

        meat_labels: 4,5 (string) - label ID 使用逗号连接
        effect_labels: 1,2 (string) - 同上
        time_labels: 3 (string) - 同上
        order: calories (string) - 排序方式,默认是 calories,其他可选值:duration(烹饪时间),收藏数(暂不支持)
        desc: false (string) - 是否倒序,默认是字符串 false,还可以是字符串 true
        start: 0 (string) - 偏移,默认是0。都是字符串,函数里会做转换,转成数字
        num: 10 (string) - 返回数量,默认是10。字符串
        '''
        recipes = cls.objects.all()
        query = {'status__gt': 0}
        # 逗号里面的是或 是并集 出现一个就好
        if meat_labels is not None:
            # 对 食材 进行筛选
            query['meat_labels__id__in']=split_labels_into_list(meat_labels)
        if effect_labels is not None:
            query['effect_labels__id__in']=split_labels_into_list(effect_labels)
        if time_labels is not None:
            query['time_labels__id__in']=split_labels_into_list(time_labels)
        if query:
            print query
            recipes = recipes.filter(**query);
            print recipes
        if order == 'duration':
            recipes = recipes.order_by('duration')
        else:
            # 还有按照收藏数的排序,不过现在还没做
            # 默认按照卡路里
            recipes = recipes.order_by('calories')
        if desc == 'true':
            recipes = recipes.reverse()
        start = str_to_int(start, 0)
        num = str_to_int(num, 10)
        # bug in django 1.8.3, filter duplicate
        recipes = recipes.distinct()
        if num < 1:
            # 如果请求num数量小于 0 是不对的,改成 10
            num = 10
        if start < 0:
            # 如果 start 是负数,改成 0
            start = 0
        return recipes[start:num + start]
Example #3
0
    def get_recipe_list(cls, labels, order, desc, start, num):
        '''
        根据条件获取菜谱列表

        labels: 4,5 (string) - label ID 使用逗号连接
        order: calories (string) - 排序方式,默认是 calories,其他可选值:duration(烹饪时间),收藏数(暂不支持)
        desc: false (string) - 是否倒序,默认是字符串 false,还可以是字符串 true
        start: 0 (string) - 偏移,默认是0。都是字符串,函数里会做转换,转成数字
        num: 10 (string) - 返回数量,默认是10。字符串
        '''
        recipes = cls.objects.all().filter(status__gt=0)
        # 逗号里面的是或 是并集 出现一个就好
        if labels is not None:
            # 对 食材 进行筛选
            recipes = recipes.filter(labels__id__in=split_labels_into_list(labels))
        if order == 'duration':
            recipes = recipes.order_by('duration')
        elif order == 'collection':
            recipes = recipes.order_by('collection_count')
        else:
            # 还有按照收藏数的排序,不过现在还没做
            # 默认按照卡路里
            recipes = recipes.order_by('calories')
        if desc == 'true':
            recipes = recipes.reverse()
        start = str_to_int(start, 0)
        num = str_to_int(num, 10)
        # bug in django 1.8.3, filter duplicate
        recipes = recipes.distinct()
        if num < 1:
            # 如果请求num数量小于 0 是不对的,改成 10
            num = 10
        if start < 0:
            # 如果 start 是负数,改成 0
            start = 0
        return recipes[start:num + start]