Exemplo n.º 1
0
def daily_record(request, template_name="kinger/revision/axis_daily_record.html"):
    """日常记录详情页 """
    if not is_vip(request.user):  
        return render(request, "403.html")
    date = request.POST.get('date')
    type = int(request.POST.get('ty','0'))
    tiles = Tile.objects.get_tiles_baby(request.user)
    category = NewTileCategory.objects.filter(is_tips=0, parent__pk=1130)
    date = datetime.datetime.strptime(date,"%Y-%m-%d")
    dates = get_group_date(request)
    group_date = [d for d in dates if d[0].date() <= date.date()]
    
    content_type = ContentType.objects.get_for_model(Tile)
    new_visitor = DailyRecordVisitor()
    new_visitor.visitor = request.user
    new_visitor.target_content_type = content_type
    new_visitor.save() 
    
    effective_date = [str(x[0].date()) for x in dates]
    current_date = datetime.datetime.now().date()
    effective_date.append(str(current_date))
    
    page = int(request.POST.get("page", '1'))
    start = (page - 1) * 5
    end = page * 5 
    is_last_page = False 
    if end >= len(group_date):
        is_last_page = True 
    
    is_first = {"year":0,"month":0}
    record_list = []
    for day in group_date[start:end]:
        record_dict = {}
        record_dict['day'] = day[0]
        record_dict['is_first'] = False
        #判断当年当月的首条记录
        if is_first['year'] != day[0].year or is_first['month'] != day[0].month:
            record_dict['is_first'] = True
            is_first['year'] = day[0].year
            is_first['month'] = day[0].month 

        daily_record = get_daily_category_tiles(tiles, category, day[0])
        if daily_record:
            record_dict['data'] = daily_record
            record_list.append(record_dict)

    ctx = {"channel":"baby", "record_list":record_list,"group_date":group_date,\
           'effective_date':effective_date,'type':type,'page':page,'date':date,'is_last_page':is_last_page}
    data = render(request, template_name,ctx)
    con=data.content
    return ajax_ok('成功',con)
Exemplo n.º 2
0
def daily_cookbook(request, template_name="kinger/revision/axis_cookbook.html"):
    """明日食谱详情页"""
    
    date = request.POST.get('date','')
    id = request.POST.get('id',0)
    
    user = request.user
    if not is_vip(user):  
        return render(request, "403.html")
    try:
        group = user.student.group
    except:
        return render(request, "404.html")
    school = group.school
    q = Q(group=group) | Q(school=school)
    
    if not id:
        tommory = datetime.datetime.strptime(date,"%Y-%m-%d") + datetime.timedelta(days = 1)
        q_date = Q(date__startswith=tommory.date())
        cookbooks = Cookbook.objects.filter(q & q_date).order_by('group','-date')
        id = cookbooks[0].id if cookbooks else None
    
    tommorrow = datetime.datetime.now() + datetime.timedelta(days = 1)
    
    current_day = (datetime.datetime.strptime(date,"%Y-%m-%d") + datetime.timedelta(days = 1)).date() if date else tommorrow.date()
    if not id:
        cookbook =None
        today = current_day
    else:
        cookbook = get_object_or_404(Cookbook, pk=id)
        today = cookbook.date
    today_book = cookbook

    mentors = Mentor.objects.all() 
    q = Q(group=group) | Q(school=school)       
    cookbooks = Cookbook.objects.filter(q).exclude(breakfast='',light_breakfast='',
                lunch='',light_lunch='',dinner='',light_dinner='').order_by('-date')
    #禁止访问其他用户数据
    if today_book:
        try:
            cookbooks.get(pk=id)
        except ObjectDoesNotExist:
            return render(request, "403.html")
    
        helpers.mark_cookbook_as_read(request,today_book)#标记当前用户食谱数据为已读
        helpers.add_daily_record_visitor(user,today_book)#增加用户访问记录
        
    effective_date = [str(x.date + datetime.timedelta(days = -1) ) for x in cookbooks if check_user_cookbook_by_date(user,x.date)]
    current_date = current_day
    effective_date.append(str(current_date))
    
    try:
        lastday_book =cookbooks.filter(date__lt=today)[0]
    except:
        lastday_book = None
        
    tommory = datetime.datetime.now() + datetime.timedelta(days = 1)
    tommory_date = tommory.date()
    try:
        nextday_book =cookbooks.filter(date__gt=today,date__lte=tommory_date).reverse()[0]
    except:
        nextday_book = None
        
    current_book = get_daily_cook_books(user,datetime.datetime.now())
    
    book_item = cook_book_item(today_book)
    book_item_pre = cook_book_item(lastday_book)
    book_item_next = cook_book_item(nextday_book)
    
    if not nextday_book and not current_book and today_book:
        nextday_book = {"id":0}

    today = today + datetime.timedelta(days = -1)
    ctx = {}
    ctx.update({"effective_date":effective_date,"book_item":book_item,"cookbooks": cookbooks,\
                 "today_book": today_book,"tommorrow":tommorrow, "yesterday": lastday_book,\
                  "next_day": nextday_book,"mentors":mentors, "ty":"cookbook","today":today,\
                  "current_date":current_date,'book_item_pre':book_item_pre,'book_item_next':book_item_next})
    data = render(request, template_name,ctx)
    con=data.content
    return ajax_ok('成功',con)
Exemplo n.º 3
0
def daily_activity(request, template_name="kinger/revision/axis_daily_activity.html"):
    """日常活动详情页"""
    date = request.POST.get('date','')
    id = request.POST.get('id',0)
    user = request.user
    if not is_vip(request.user):  
        return render(request, "403.html")
    
    try:
        group = user.student.group
        q = Q(group=group) | Q(user=user)
    except:
        q = Q(user=user)
    actives = Activity.objects.filter(q)
    
    if id:
        active_id = id
    else:
        date_active = actives.filter(start_time__startswith=date)
        active_id = date_active[0].id if date_active else 0
        
    if active_id == 0:
        active =None
    else:
        active = get_object_or_404(Activity, pk=active_id)
        is_empty = is_empty_active(active.description)
        if is_empty:
            return render(request, "404.html")
        
    q = get_q_user(user)
    actives = Activity.objects.filter(q)
    
    mentors = Mentor.objects.all()         
    # 禁止访问其它用户的记录
    if active:
        try:
            actives.get(pk=active_id)
        except ObjectDoesNotExist:
            return render(request, "403.html")
        
        helpers.add_daily_record_visitor(user,active)#添加访问记录
    
    current_date = datetime.datetime.strptime(date,"%Y-%m-%d") if date else datetime.datetime.now()
    today = active.start_time if active else current_date
    effective_date = [str(x.start_time.date()) for x in actives]
    
    effective_date.append(str(current_date))
 
    try:
        active_list = actives.filter(microsecond__gt=active.microsecond).exclude(id=active.id) if active else actives
        next_day = active_list.filter(start_time__gte=today).order_by("microsecond")[0]
    except:
        next_day = None
    try:
        active_list = actives.filter(microsecond__lt=active.microsecond).exclude(id=active.id) if active else actives
        yesterday = active_list.filter(start_time__lte=today).order_by("-microsecond")[0]
    except:
        yesterday = None
        
    today_active = get_daily_activitie_tiles(user)
    if not next_day and not today_active and active:
        next_day = {"id":0}
    
    today = today.date()    
    ctx = {}
    ctx.update({"tile": active, "effective_date":effective_date,"yesterday": yesterday,\
                "next_day": next_day,"mentors":mentors,"ty":"events","today":today,"current_date":current_date})
    data = render(request, template_name,ctx)
    con=data.content
    return ajax_ok('成功',con)