Example #1
0
def index(request, template_name="cookbook/index.html"):
    """
    食谱首页
    """
    # 初始化数据
    user = request.user
    schools = user.manageSchools.all()

    if schools.count() > 0:
        school = schools[0]
    else:
        messages.error(request, "你没有可管理的学校")
        return redirect("manage")

    groups = school.group_set.all()
    cur_date = datetime.date.today()

    cookbook_type = "school"  # 食谱类型,默认是学校的

    # 取得参数
    date_param = request.GET.get("date")
    school_param = request.GET.get("school")
    group_param = request.GET.get("group")

    # 日期初始化,空日期代表当前月份
    if date_param:
        try:
            the_date = datetime.datetime.strptime(date_param, "%Y-%m")
        except ValueError:
            the_date = cur_date
    else:
        the_date = cur_date

    #  只有学校为空,班级不为空,才是班级类型,其它都是学校
    if not school_param and group_param:
        cookbook_type = "group"

    days = calendar.monthrange(the_date.year, the_date.month)[1]

    # 当前日期
    cur_month_date = cur_date.strftime("%Y-%m")

    # 上下日期
    prev_month = helpers.move_month(the_date, "-")
    next_month = helpers.move_month(the_date, "+")

    school_cookbook = Cookbook.objects.filter(school=school, date__year=the_date.year, date__month=the_date.month)

    month_cal = calendar.monthcalendar(the_date.year, the_date.month)

    items = Cookbook.objects.get_items()
    cookbook_set = CookbookSet.objects.get_set(school=school)

    cookbook_data = {
        "cur_month_date": cur_month_date,
        "prev_month": prev_month,
        "next_month": next_month,
        "month_cal": month_cal,
        "type": "school",
        "date": {"year": the_date.year, "month": the_date.month},
        "group": {"id": 0, "name": ""},
        "groups": groups,
        "set": cookbook_set,
        "cookbook": [],
    }

    cur_day_con_demo = {
        "day": 1,
        "is_pub": False,
        "con": {
            "breakfast": {"con": "", "comefrom": "school"},
            "light_breakfast": {"con": "", "comefrom": "school"},
            "lunch": {"con": "", "comefrom": "school"},
            "light_lunch": {"con": "", "comefrom": "school"},
            "dinner": {"con": "", "comefrom": "school"},
            "light_dinner": {"con": "", "comefrom": "school"},
        },
        "items": [],
    }

    if cookbook_type == "school":
        cookbook_data["type"] = "school"

        # 取得学校食谱
        for d in range(1, days + 1):
            cur_day_con = copy.deepcopy(cur_day_con_demo)
            cur_day_con["day"] = d

            school_cookbook_day = None

            for s in school_cookbook:
                if s.date.day == d:
                    school_cookbook_day = s
                    break

            school_pub = True if school_cookbook_day else False

            # 发布过,默认为,从未发布过
            if school_pub:
                cur_day_con["is_pub"] = True

                for item in items:
                    school_con = getattr(school_cookbook_day, item) if school_pub else ""

                    cur_day_con["con"][item]["con"] = school_con
                    cur_day_con["con"][item]["comefrom"] = "school"

                    # 加入显示项
                    if cur_day_con["con"][item]["con"] != "" and cookbook_set[item]["is_show"]:
                        cur_day_con["items"].append(cookbook_set[item]["name"])

            cookbook_data["cookbook"].append(cur_day_con)

    elif cookbook_type == "group":
        cookbook_data["type"] = "group"

        group_pk = int(group_param)
        group = Group.objects.get(pk=group_pk)
        group_cookbook = Cookbook.objects.filter(group=group, date__year=the_date.year, date__month=the_date.month)

        cookbook_data["group"]["id"] = group.pk
        cookbook_data["group"]["name"] = group.name

        for d in range(1, days + 1):
            cur_day_con = copy.deepcopy(cur_day_con_demo)
            cur_day_con["day"] = d

            school_cookbook_day = None
            group_cookbook_day = None

            for s in school_cookbook:
                if s.date.day == d:
                    school_cookbook_day = s
                    break

            for g in group_cookbook:
                if g.date.day == d:
                    group_cookbook_day = g
                    break

            school_pub = True if school_cookbook_day else False
            group_pub = True if group_cookbook_day else False

            # 发布过,默认为,从未发布过
            if school_pub or group_pub:

                cur_day_con["is_pub"] = True

                for item in items:

                    group_con = getattr(group_cookbook_day, item) if group_pub else ""
                    school_con = getattr(school_cookbook_day, item) if school_pub else ""

                    # 读取班级
                    if group_con != "":
                        cur_day_con["con"][item]["con"] = group_con
                        cur_day_con["con"][item]["comefrom"] = "group"

                    # 读取学校
                    else:
                        cur_day_con["con"][item]["con"] = school_con
                        cur_day_con["con"][item]["comefrom"] = "school"

                    # 加入显示项
                    if cur_day_con["con"][item]["con"] != "" and cookbook_set[item]["is_show"]:
                        cur_day_con["items"].append(cookbook_set[item]["name"])

            cookbook_data["cookbook"].append(cur_day_con)

    ctx = cookbook_data

    return render(request, template_name, ctx)
Example #2
0
        当前月份.
    """
    try:
        request.session.pop('kinger_channel')
    except KeyError:
        pass
    cal = calendar
    cur_month_date = datetime.datetime.today()
    try:
        month = request.GET.get("month")
        if month:
            cur_month_date = datetime.datetime.strptime(month, "%Y-%m")
    except Exception, e:
        logger.error(e)

    prev_month = helpers.move_month(cur_month_date, "-")
    next_month = helpers.move_month(cur_month_date, "+")

    month_cal = cal.monthcalendar(cur_month_date.year, cur_month_date.month)


    tiles = Tile.objects.get_tiles_baby(request.user)
   
    tiles = tiles.filter(start_time__year=cur_month_date.year,start_time__month=cur_month_date.month)
    daily_category = get_daily_category()
    if daily_category:
        tiles = tiles.exclude(category__parent=daily_category)
        
    ctx = {"month_cal": month_cal, "cur_month_date": cur_month_date, "is_cal": True}
    ctx.update({"prev_month": prev_month, "next_month": next_month, 'tiles':tiles })
    return render(request, template_name, ctx)
Example #3
0
def index(request, template_name="oa/cookbook_list.html"):
    """
    食谱首页
    """
    # 初始化数据
    user = request.user
    schools = get_schools(request.user)
    school_pks = [s.id for s in schools if s.parent_id > 0]
    school_id = int(request.GET.get('sid',0))
    print school_id,'school_id-----------------'
    if school_id != 0:
        try:
            school =School.objects.get(id=school_id,id__in=school_pks)
        except:
            messages.error(request,'你没有可管理的学校')
            return redirect('oa_home')
    else:
        if schools:
            school = schools[0]
            school_id = school.id if school.parent_id > 0 else 0
        else:
            messages.error(request,'你没有可管理的学校')
            return redirect('oa_home')
    
    groups = school.group_set.all().exclude(type=3)
    cur_date = datetime.date.today()

    cookbook_type = 'school' # 食谱类型,默认是学校的


    # 取得参数
    date_param = request.GET.get('date')
    school_param = request.GET.get('school')
    group_param = request.GET.get('group')


    # 日期初始化,空日期代表当前月份
    if date_param:
        try:
            the_date = datetime.datetime.strptime(date_param, "%Y-%m")
        except ValueError:
            the_date = cur_date
    else:
        the_date = cur_date

    #  只有学校为空,班级不为空,才是班级类型,其它都是学校
    if group_param and group_param != '0':
        cookbook_type = 'group'


    days = calendar.monthrange(the_date.year,the_date.month)[1]

    #当前日期
    cur_month_date = cur_date.strftime("%Y-%m")

    #上下日期
    prev_month = helpers.move_month(the_date, "-")
    next_month = helpers.move_month(the_date, "+")

    school_cookbook = Cookbook.objects.filter(school=school,date__year=the_date.year,date__month=the_date.month)

    month_cal = calendar.monthcalendar(the_date.year, the_date.month)

    items = Cookbook.objects.get_items()
    cookbook_set = CookbookSet.objects.get_set(school=school)

    cookbook_data = {       
        'cur_month_date':cur_month_date,
        'prev_month':prev_month,
        'next_month':next_month,
        'month_cal':month_cal,
        'type':'school',
        'date':{
            'year':the_date.year,
            'month':the_date.month
        },
        'group':{
            'id':0,
            'name':''
        },
        'groups':groups,
        'set':cookbook_set,
        'cookbook':[]      
    }    

    cur_day_con_demo = {
        'day':1,
        'is_pub':False,
        'con':{
            'breakfast':{
                'con':'',
                'comefrom':'school'
            },
            'light_breakfast':{
                'con':'',
                'comefrom':'school'
            },
            'lunch':{
                'con':'',
                'comefrom':'school'
            },
            'light_lunch':{
                'con':'',
                'comefrom':'school'
            },
            'dinner':{
                'con':'',
                'comefrom':'school'
            },
            'light_dinner':{
                'con':'',
                'comefrom':'school'
            },
        },
        'items':[]
    }

    if cookbook_type == 'school':
        cookbook_data['type'] = 'school'

        # 取得学校食谱
        for d in range(1,days+1):
            cur_day_con = copy.deepcopy(cur_day_con_demo)
            cur_day_con['day'] = d

            school_cookbook_day = None

            for s in school_cookbook:
                if s.date.day == d:
                    school_cookbook_day = s
                    break                                    
            
            school_pub = True if school_cookbook_day else False

            # 发布过,默认为,从未发布过
            if school_pub:
                cur_day_con['is_pub'] = True               

                for item in items: 
                    school_con = getattr(school_cookbook_day, item) if school_pub else ''              

                    cur_day_con['con'][item]['con'] = school_con
                    cur_day_con['con'][item]['comefrom'] = 'school' 

                    # 加入显示项
                    if cur_day_con['con'][item]['con'] !='' and cookbook_set[item]['is_show']:
                        cur_day_con['items'].append(cookbook_set[item]['name'])

            cookbook_data['cookbook'].append(cur_day_con)

    elif cookbook_type == 'group':
        cookbook_data['type'] = 'group'

        group_pk = int(group_param)
        group = Group.objects.get(pk=group_pk)
        group_cookbook = Cookbook.objects.filter(group=group,date__year=the_date.year,date__month=the_date.month)

        cookbook_data['group']['id'] = group.pk
        cookbook_data['group']['name'] = group.name

        for d in range(1,days+1):
            cur_day_con = copy.deepcopy(cur_day_con_demo)
            cur_day_con['day'] = d

            school_cookbook_day = None
            group_cookbook_day = None

            for s in school_cookbook:
                if s.date.day == d:
                    school_cookbook_day = s
                    break

            for g in group_cookbook:
                if g.date.day == d:
                    group_cookbook_day = g
                    break

            school_pub = True if school_cookbook_day else False
            group_pub = True if group_cookbook_day else False

            

            # 发布过,默认为,从未发布过
            if school_pub or group_pub:
                
                cur_day_con['is_pub'] = True               

                for item in items:                 

                    group_con = getattr(group_cookbook_day, item) if group_pub else ''
                    school_con = getattr(school_cookbook_day, item) if school_pub else ''               

                    # 读取班级
                    if group_con != '':
                        cur_day_con['con'][item]['con'] = group_con
                        cur_day_con['con'][item]['comefrom'] = 'group'

                    # 读取学校
                    else:
                        cur_day_con['con'][item]['con'] = school_con
                        cur_day_con['con'][item]['comefrom'] = 'school' 

                    # 加入显示项
                    if cur_day_con['con'][item]['con'] !='' and cookbook_set[item]['is_show']:
                        cur_day_con['items'].append(cookbook_set[item]['name'])

            cookbook_data['cookbook'].append(cur_day_con)

    ctx = cookbook_data
    print school_id,'iiiiiiiiiiiiiiiiiiiiii'
    ctx.update({"schools":schools,"sid":school_id})

    return render(request, template_name, ctx)