def appointment(request):
    isin = False
    if request.user.is_authenticated():
        isin = True
        name = request.user.username
        neworder = Order(user= request.user)
        try:
            two = request.GET['q'].split()
            roomid = int(two[0])
            did = int(two[1])
            neworder.datetime = Datetime.objects.get(id=did)
            theroom = Room.objects.get(id=roomid)
        except:
            theroom = None
        if request.POST:
            if not theroom:
                 return render_to_response('appointment.html', {'name': name, \
                'msg': u'您尚未选择任何教室,请先查询再预约', 'isin': isin})
            message = request.POST['message']
            if len(message) > 10:
                neworder.message = message
                neworder.save()
                neworder.room.add(theroom)
                subject = request.user.username + u'用户预约教室'
                send_mail(subject, message, '*****@*****.**', ['*****@*****.**'], fail_silently=True)
                return render_to_response('appointment.html', {'name': name, \
                'msg': u'提交申请成功', 'isin': isin})
            else:
                return render_to_response('appointment.html', {'name': name, \
                'msg': u'提交申请失败,简述字数不应少于10个字', 'isin': isin})
        return render_to_response('appointment.html', {'name': name, 'isin': isin })
            
    else:
        return render_to_response("appointment.html", {'name': u'登录', \
        'msg': u'您尚未登录,请登录后预约教室', 'isin': isin})
def submit(request):
    rooms = request.GET['rooms']
    day = request.GET['day']
    per = int(request.GET['period'])
    message = request.GET['message']
    datet = Datetime.objects.get(date = day, period = per)
    try:
        order = Order(user = request.user, datetime = datet, message = message)
        order.save()
        rs = rooms.split()
        for rm in rs:
            order.room.add(Room.objects.get(name = rm))
        is_ok = True
    except:
        is_ok = False
    subject = request.user.username + u'用户预约教室'
    send_mail(subject, message, '*****@*****.**', ['*****@*****.**'], fail_silently=True)
    return HttpResponse(json.dumps(is_ok), content_type='application/json')