def inquire(request):
    isin = False
    if request.user.is_authenticated():
        isin = True
        name = request.user.username
    else:
        name = u'登录'
    if request.POST:
        ainqure = InqureForm(request.POST)
        try:
            rooname = request.POST['mroom']            
            roos = Room.objects.filter(name__icontains = rooname)
        except:
            roos = []
        if roos:
            days = []
            for i in range(7):
                d1 = datetime.datetime.now()
                d2 = d1 + datetime.timedelta(days = i)
                day = str(d2).split()[0]
                days.append(day)
            tabs = []
            n = len(roos)
            for i in range(n):
                tabs.append([])
                tabs[i].append(roos[i])
                for j in range(7):
                    for k in range(5):
                        datet = Datetime.objects.get(date = days[j], period = k)
                        user = Useroom.objects.get(datetime = datet, room = roos[i]).user
                        tabs[i].append(user)
        else:
            tabs = None
            days = None
        if ainqure.is_valid():
            rooms = None
            rooms = Useroom.inqureresults(ainqure)
            dat = ainqure['day'].value()
            peri = ainqure['period'].value()
            datetim = Datetime.objects.get(date = dat, period = peri).id
            return render_to_response('inquire.html',
                    {
                        'name': name,
                        'rooms': rooms,
                        'inqure': ainqure,
                        'isin': isin,
                        'datetime': datetim,
                        'tabs': tabs,
                        'days': days,
                    })
        else:
            ainqure = InqureForm(request.POST)
            return render_to_response('inquire.html', {'name': name, 'inqure': ainqure, 'isin': isin,\
            'msg': u"查询无结果", 'tabs': tabs, 'days': days})
    else:
        ainqure = InqureForm()
        return render_to_response("inquire.html", {'name': name, 'inqure': ainqure, 'isin': isin})
def task(request):
    try:
        for i in range(7):
            d1 = datetime.datetime.now()
            d2 = d1 + datetime.timedelta(days = i)
            day = str(d2).split()[0]
            dates = Datetime.objects.filter(date = day)
            if not dates:
                for j in range(5):
                    thedate = Datetime(date = day, period = j)
                    thedate.save()
                    rooms = Room.objects.all()
                    for room in rooms:
                        tab = Useroom()
                        tab.room = room
                        tab.datetime = thedate
                        tab.save()
        return render_to_response("task.html", {'msg': u'添加成功'})
    except:
        return render_to_response("task.html", {'msg': u'添加失败,请重试'})