Esempio n. 1
0
def update_group_info():
    """更新设备组信息
     支持记录 置顶 settop = 1
    """
    user = g.user
    group_id = request.values.get('group_id')
    name = request.values.get("name")
    comment = request.values.get('comment')
    settop = request.values.get('set_top',0,type=int)
    group = model.Group.get(user_id=user.id,_id=ObjectId(group_id))
    if not group:
        return ErrorReturn(ErrorDefs.ObjectNotExist).response
    if name:
        group.name = name
    if comment:
        group.comment = comment
    if settop:
        group.order = -timestamp_current()

    if name or comment or settop:
        group.update_time = timestamp_current()
        group.save()

    return CR(result=group.id).response
Esempio n. 2
0
def get_product():
    """
    """
    # user = g.user
    sku = request.values.get('sku')

    product = model.Product.get(sku=sku)
    if not product:
        return ErrorReturn(ErrorDefs.ObjectNotExist,u'产品对象不存在').response

    main = instance.serviceManager.get('main')
    static_url = main.getConfig().get('static_root_url')

    product.url = static_url + '/' + product.url
    product.image_url = static_url + '/' + product.image_url
    product.slide_image_url = static_url + '/' + product.slide_image_url

    data = product.dict()
    images = product.content.split(',')
    data['content_urls'] = []
    for image in images:
        url = static_url+'/product/'+product.sku+'/'+image
        data['content_urls'].append(url)
    return CR(result=data).response
Esempio n. 3
0
def get_share_device_follower_list():
    """获取关注位置设备的好友
        GET /share-device/follower/list
    """
    user = g.user
    share_id = request.values.get('share_id')
    share = model.SharedDeviceLink.get(_id=ObjectId(share_id))
    if not share:
        return ErrorReturn(ErrorDefs.ObjectNotExist).response

    followers = model.ShareDeviceFollower.find(share_id=share_id)
    result = {}
    result['share'] = share.dict()
    result['followers'] = []
    for follower in followers:
        wxuser = model.WxUser.get(open_id=follower.open_id)
        if wxuser:
            follower.avatar_url = wxuser.avatarUrl
            follower.nick_name = wxuser.nickName

        data = follower.dict()
        result['followers'].append(data)

    return CR(result=result).response
Esempio n. 4
0
def query_ticks():
    """
    列举所有已订阅的合约代码
    :param symbol
    :param product_class
    :param start :  timestamp 70's
    :param end  : timestamp 70'
    :param limit 查询返回记录最大个数,默认值零表示返回记录不限。
    :return:
    """

    symbol = request.values.get('symbol', '')
    if not symbol:
        return ErrorReturn(ErrorDefs.ParameterInvalid).response
    product_class = request.values.get('product_class', ProductClass.Future)
    start = request.values.get('start', 0)
    end = request.values.get('end', 0)
    limit = request.values.get('limit', 500)

    service = instance.serviceManager.get('main')
    dbconn = service.datasourceManager.get('mongodb').conn
    dbname = DatabaseDefs.get(product_class, '').get('ticks', '')  # Ctp_Tick
    coll = dbconn[dbname][symbol]

    sortkey = 'datetime'
    dtstart = datetime.datetime.fromtimestamp(int(start))
    dtend = datetime.datetime.fromtimestamp(int(end))
    case = {"datetime": {"$gte": dtstart, "$lte": dtend}}
    if sortkey:
        cursor = coll.find(case).sort(sortkey, ASCENDING)
    else:
        cursor = coll.find(case)
    result = list(cursor.limit(limit))
    for r in result:
        r['datetime'] = time.mktime(r['datetime'].timetuple())
    return CR().assign(result).response
Esempio n. 5
0
def take_share_device():
    """用户接收共享设备,添加成为自己的设备
        如果设置了密码,且用户未上传密码,则提示密码输入
        @:return 返回新设备id
    """
    user = g.user
    auth = g.auth

    share_id = request.values.get('share_id')
    password = request.values.get('password')
    link = model.SharedDeviceLink.get(_id=ObjectId(share_id))
    if not link:
        return ErrorReturn(ErrorDefs.ObjectNotExist, u'共享资源对象不存在').response

    # 共享设备是否是自己的设备(自己注册的或者之前分享来自他人的)
    rel = model.DeviceUserRelation.get(user_id=user.id,
                                       device_id=link.device_id)
    if rel:
        return ErrorReturn(ErrorDefs.ObjectHasExist, u'分享设备已存在').response

    expire_time = parse(link.expire_time) + relativedelta(days=+1)
    expire_time = datetime_to_timestamp(expire_time)
    # 共享设备是否过期
    if expire_time and expire_time < timestamp_current():
        return ErrorReturn(ErrorDefs.ResExpired, u'设备分享已过期').response

    # 访问上限
    if link.user_limit:
        num = model.DeviceUserRelation.collection().find({
            'share_device_link':
            link.id
        }).count()
        if num >= link.user_limit:
            return ErrorReturn(ErrorDefs.ReachLimit, u'分享已达到人数上限').response

    # 检测当前用户是否被阻止
    follower = model.ShareDeviceFollower.get(share_id=share_id,
                                             user_id=user.id)
    if follower and follower.denied:
        return ErrorReturn(ErrorDefs.AccessDenied, u'当前用户访问共享资源受限').response

    #是否要提供密码
    if link.password:
        if password != link.password:
            return ErrorReturn(ErrorDefs.NeedPassword, u'需要提供分享访问密码').response
    #创建设备
    device = model.DeviceUserRelation()
    device.user_id = user.id
    device.device_id = link.device_id
    device.device_name = link.name
    device.update_time = timestamp_current()
    device.is_share_device = True
    device.share_user_id = link.user_id
    device.share_device_link = link.id
    device.save()
    #添加到分享者列表
    if not follower:
        follower = model.ShareDeviceFollower()
        follower.user_id = user.id
        follower.open_id = auth.open_id
        follower.share_id = link.id
        follower.device_id = device.id
        follower.create_time = timestamp_current()
        wxuser = model.WxUser.get(open_id=auth.open_id)
        if wxuser:
            follower.avatar_url = wxuser.avatarUrl
            follower.nick_name = wxuser.nickName
        follower.save()

    return CR(result=str(device.id)).response