コード例 #1
0
ファイル: views.py プロジェクト: seekplum/hmbd
def ajax_run(request):
    try:
        # 从mongodb数据库中获取数据
        db = get_db()
        system_time = []
        run_time = []
        weight = []
        for u in db.ebf_weight.find().sort([("system_time", -1)])[:31]:
            system_time.append(
                utc2local(u['system_time']).strftime("%Y-%m-%d %H:%M:%S"))
            run_time.append(float(u['run_time']))
            weight.append(float(u['weight']))
        time = json.dumps(system_time[::-1])
        number_run = json.dumps(run_time[::-1])
        number_wei = json.dumps(weight[::-1])

        if not isinstance(time, str) or not isinstance(
                number_run, str) or not isinstance(number_wei, str):
            time = time.decode("utf-8")
            number_run = number_run.decode("utf-8")
            number_wei = number_wei.decode("utf-8")
        name_dict = {
            'system_time': time,
            'number_run': number_run,
            'number_wei': number_wei
        }
        return JsonResponse(name_dict)
    except Exception as e:
        logging.error(e)
        return HttpResponse(
            '<html><script type="text/javascript">alert("从mongodb获取数据失败"); '
            'window.location="/main"</script></html>')
コード例 #2
0
def ad_content_edit(request):
    """
    数据管理添加,编辑
    :param request:
    :return:
    """
    action = getattr(request, request.method).get('action', None)
    if action is None:
        return HttpResponseRedirect(reverse("404"))
    if request.method == 'GET':
        if action == 'add':
            title = "添加"
            return render(request, 'table/ad-content-edit.html', locals())
        if action == 'edit':
            title = "编辑"
            _id = ObjectId(request.GET.get('_id'))

            db = get_db()
            content = db.ebf_qq.find_one({'_id': _id})
            system_time = utc2local(
                content['system_time']).strftime("%Y-%m-%d %H:%M:%S")
            content = {
                key.strip('_'): str(value)
                for key, value in content.items()
            }
            return render(request, 'table/ad-content-edit.html', locals())

    elif request.method == 'POST':
        try:
            if action == 'add':
                system_time = request.POST.get('system_time', None)
                online_number = request.POST.get('online_number', None)
                content = {
                    'online_number': online_number,
                    'system_time': system_time
                }
                db = get_db()
                posts = db.ebf_qq
                posts.insert(content)
                return HttpResponse(json.dumps({"status": 0}))
            elif action == 'edit':
                _id = request.POST.get('_id', None)
                online_number = request.POST.get('online_number', None)
                content = {
                    'online_number': online_number,
                    'system_time': datetime.datetime.utcnow()
                }
                db = get_db()
                posts = db.ebf_qq
                _id = ObjectId(_id)
                posts.update({"_id": _id}, content)
                return HttpResponse(json.dumps({"status": 0}))
        except Exception as e:
            print(e)
            return HttpResponse(json.dumps({"status": -1}))
    else:
        return HttpResponseRedirect(reverse("404"))
コード例 #3
0
ファイル: views.py プロジェクト: seekplum/hmbd
def query_query(request, o_id):
    db = get_db()
    if not o_id:
        m = db.ebf_weight.find()
        u = []
        for x in m:
            # 把'_id'改为'id'
            x = {key.strip('_'): value for key, value in x.items()}
            x['system_time'] = utc2local(
                x['system_time']).strftime("%Y-%m-%d %H:%M:%S")
            u.append(x)
    else:
        try:
            _id = ObjectId(o_id)
            l = db.ebf_weight.find_one({'_id': _id})
            u = []
            l = {key.strip('_'): value for key, value in l.items()}
            l['system_time'] = utc2local(
                l['system_time']).strftime("%Y-%m-%d %H:%M:%S")
            u.append(l)
        except Exception as e:
            print('查询异常:', e)
            u = None
    return u
コード例 #4
0
def content_detail(request, num_id):
    """
    数据详情
    :param request:
    :param num_id:
    :return:
    """
    try:
        db = get_db()
        results = db.ebf_qq.find_one({'_id': ObjectId(num_id)})
        if results:
            results['system_time'] = utc2local(
                results['system_time']).strftime("%Y-%m-%d %H:%M:%S")
            message = None
        else:
            message = '没有数据'
    except Exception as e:
        logging.error(e)
    return render(request, 'table/ad-content-detail.html', locals())
コード例 #5
0
def ajax_dict(request):
    try:
        # 缓存
        import redis

        r = redis.Redis(host='localhost', port=6379, db=0)
        h = r.dbsize()
        try:
            number = r.get('online_number')
            time = r.get('system_time')
        except:
            number = ''
            time = ''
        if not number or not time:
            # 缓存中没有数据,从mongodb数据库中获取
            db = get_db()
            system_time = []
            online_number = []
            for u in db.ebf_qq.find().sort([("system_time", -1)])[:51]:
                system_time.append(
                    utc2local(u['system_time']).strftime("%Y-%m-%d %H:%M:%S"))
                online_number.append(int(u['online_number']))
            system_time = system_time[::-1]
            online_number = online_number[::-1]
            time = json.dumps(system_time)
            number = json.dumps(online_number)
            r.set('online_number', number)
            r.set('system_time', time)
            # 缓存中数据30秒过期
            r.expire('online_number', 30)
            r.expire('system_time', 30)
        if not isinstance(time, str) or not isinstance(number, str):
            time = time.decode("utf-8")
            number = number.decode("utf-8")
        name_dict = {'system_time': time, 'online_number': number}
        return JsonResponse(name_dict)
    except Exception as e:
        logging.getLogger('').info("读取缓存出错:" + str(e))
        return HttpResponse(
            '<html><script type="text/javascript">alert("redis服务未开启"); '
            'window.location="/main"</script></html>')