示例#1
0
 def get(self, request):
     page = request.GET.get('page')
     try:
         query = Query(Card)
         #query.not_equal_to('publish',True)
         #query.not_equal_to('deleted',True)
         query.not_equal_to('publish', False)
         #query.does_not_exist('cid')
         count = query.count()
         query.include('user')
         if page is None:
             page = 0
         count = query.count()
         query.skip(int(page) * 100)
         query.limit(100)
         cards = query.descending('publishAt').find()
     except LeanCloudError as e:
         if e.code == 101:
             cards = []
         else:
             raise e
     return render(request, 'card_publish.html', {
         'cards': cards,
         'count': count
     })
def see_record_num(period_label_dic):
	valid_num=0
	
	valid_period=[] 
	####init
	connect_db()
	#####get all
	UserSensor = Object.extend('UserSensor')
	query = Query(UserSensor)
	#
	for label,periods in period_label_dic.items():
	
		for period in periods:
			stamp_range=generate_stamp(period)
			#
			query.equal_to("deviceId",device).not_equal_to("events",None).\
			equal_to("sensorType",sensor).\
			less_than("timestamp", stamp_range[1]).greater_than("timestamp",stamp_range[0])
			#equal_to("motion",class_type).\
			
			if query.count()>10:
				valid='valid'
				valid_num+=query.count()
				valid_period.append([label,period])
			else:valid='not valid'
			print label,period,'count',query.count(),valid
	print 'total valid record:',valid_num
def get_installationid_by_trackerid(tracker_id=None):
    print "tracker_id", tracker_id
    query = Query(Installation)
    user = {
        "__type": "Pointer",
        "className": "_User",
        "objectId": tracker_id
    }
    query.equal_to('user', user)
    query.descending('updatedAt')
    print "query.count", query.count()
    installation = query.find()[0] if query.count() else {}
    print "query Installaton", installation.id
    return installation.id, installation.get('deviceType')
示例#4
0
def dislike(**params):
    card_id = params['cid']
    user_id = params['uid']
    card = Card.create_without_data(card_id)
    user = User.create_without_data(user_id)
    query = Query(Like)
    query.equal_to('card', card)
    query.equal_to('user', user)
    count = query.count()

    if count > 0:
        try:
            likes = query.first()
            likes.destroy()

            card.increment('likes', -1)
            card.fetch_when_save = True
            card.save()
            return {'code': 200, 'message': 'ok'}
        except LeanCloudError as e:
            result = {'code': e.code, 'message': e.error}
            return result
    else:
        result = {'code': 400, 'message': '点赞记录不存在'}
        return result
示例#5
0
文件: userer.py 项目: heamon7/bbsUser
    def __init__(self):
        leancloud.init('mctfj249nwy7c1ymu3cps56lof26s17hevwq4jjqeqoloaey', master_key='ao6h5oezem93tumlalxggg039qehcbl3x3u8ofo7crw7atok')

        Users = Object.extend('Users')
        query = Query(Users)
        curTime = datetime.now()
        query.exists('userId')
        query.less_than('createdAt',curTime)
        userCount = query.count()


        print "userCounts: %s" %str(userCount)
        queryLimit = 500
        queryTimes = userCount/queryLimit + 1
        self.urls = []

        for index in range(queryTimes):
            query = Query(Users)
            query.exists('userId')
            query.less_than('createdAt',curTime)
            query.descending('createdAt')
            query.limit(queryLimit)
            query.skip(index*queryLimit)
            query.select('userId')
            userRet = query.find()
            for user in userRet:
                self.urls.append(self.baseUrl + user.get('userId') +".json")
        pass
示例#6
0
def user():
    page = force_int(request.args.get('page', 1), 1)
    page_size = 10

    q = Query("_User")

    search_form = SearchAccountForm()

    keyword = None
    if request.method == 'GET':
        search_form.keyword.data = keyword = request.args.get('keyword', '')

    if search_form.validate_on_submit():
        keyword = search_form.keyword.data.strip()

    if keyword:
        if "@" in keyword:
            q = q.startswith("email", keyword)
        else:
            q = q.startswith("username", keyword)

    users = q.skip((page - 1) * page_size).limit(page_size).find()
    count = q.count()
    pages = count / page_size + 1

    return render_template('admin/user.html',
                           users=users,
                           form=search_form,
                           count=count,
                           pages=pages,
                           page=page)
def get_tracker_data(table_name=None,tracker_list=None,field_name=None):

    DBTable = Object.extend(table_name)
    tracker_data_dict = {}
    for index,tracker in enumerate(tracker_list):
        #这样处理是因为可能一个user的记录超过了一次可以读取的数量(1K条)
        query = Query(DBTable)
        query.equal_to(field_name,tracker)
        query.less_than('createdAt',currentTime)
        total_count=query.count()
        # print 'TotalCount  %s' %str(total_count)

        query_times=(total_count+query_limit-1)/query_limit
        #如果想在这里按timestamp排序取出每个user的记录是不靠谱的,可能读取时还有插入,而且timestamp介于之间
        for index in range(query_times):
            # print 'querying index: %s' %str(index)
            query = Query(DBTable)
            query.equal_to(field_name,tracker)
            query.less_than('createdAt',currentTime)
            query.ascending('createdAt')
            query.limit(query_limit)
            query.skip(index*query_limit)
            if tracker in userDataDict.keys():
                tracker_data_dict.get(tracker).extend(query.find())
            else :
                tracker_data_dict[tracker]=query.find()
    return tracker_data_dict
示例#8
0
文件: api.py 项目: jasminexie/uberpku
def set_activity(uid, month, money, calories):
	activity = Activity()
	query = Query(Activity)
	query.equal_to("uid", uid)
	query.equal_to("start_time", month)
	if query.count() == 0:
		entry = Activity()
		entry.set("uid", uid)
		entry.set("start_time", month)
		entry.set("duration", 30)
		entry.set("budget_money", money)
		entry.set("budget_cal", calories)
		entry.set("curr_money", 0)
		entry.set("curr_cal",0)
		entry.set("curr_time",0)
		entry.set("curr_ubers",0)
		# todo: duration, time!
		entry.save()
	else:
		query.equal_to("uid", uid)
		query.equal_to("start_time", month)
		entry = query.first()
		entry.set("budget_money", money)
		entry.set("budget_cal", calories)
		entry.save()
def get_all_tracker(table_name=INSTALLATION_TABLE):
    DbTable = table_name
    query = Query(DbTable)
    query.less_than('createdAt',current_time)
    query.exists('objectId')
    total_count=query.count()
    query_times=(total_count+query_limit-1)/query_limit
    all_installation_list = []
    for index in range(query_times):
        query = Query(DbTable)
        query.exists('objectId')
        query.less_than('createdAt',current_time)
        query.select(USER_FIELD)
        query.ascending('createdAt')
        query.limit(query_limit)
        query.skip(index*query_limit)
        all_installation_list.extend(query.find())

    user_id_set = set()
    user_list = []
    for installation in all_installation_list:
        user = installation.get(USER_FIELD)
        user_id = user.id
        if user_id not in user_id_set:
            user_id_set.add(user_id)
            user_list.append(user)


        else:
            pass
    print "Have gotten all the trackers,total number is: %s" %(str(len(user_list)))
    return user_list
def get_all_behavior_prediction(table_name='UserBehavior',field_name='prediction'):
    DBTable = Object.extend(table_name)
    query = Query(DBTable)
    query.exists(field_name)
    query.less_than('updatedAt',currentTime)
    total_count=query.count()
    print 'TotalCount  %s' %str(total_count)

    query_times=(total_count+query_limit-1)/query_limit

    behavior_prediction_dict={}
    for index in range(query_times):
        print 'querying index: %s' %str(index)
        query = Query(DBTable)
        query.exists(field_name)
        query.less_than('updatedAt',currentTime)
        query.limit(query_limit)
        query.skip(index*query_limit)
        query.descending('updatedAt')
        result_list=query.find()
        for result in result_list:
            record_wrap ={}
            record_wrap['startTime'] = result.get('startTime')
            record_wrap['endTime'] = result.get('endTime')
            record_wrap['prediction'] = result.get(field_name)
            # record_wrap['UserBehavior'] = result   # 这里是为了绑定userBehavior
            behavior_prediction_dict[result]=record_wrap  #这里的键user由于是对象,所以即使是同一个user,他们也是不同的
    return behavior_prediction_dict
示例#11
0
def user():
    page = force_int(request.args.get('page', 1), 1)
    page_size = 10

    q = Query("_User")

    search_form = SearchAccountForm()

    keyword = None
    if request.method == 'GET':
        search_form.keyword.data = keyword = request.args.get('keyword', '')

    if search_form.validate_on_submit():
        keyword = search_form.keyword.data.strip()

    if keyword:
        if "@" in keyword:
            q = q.startswith("email", keyword)
        else:
            q = q.startswith("username", keyword)

    users = q.skip((page - 1) * page_size).limit(page_size).find()
    count = q.count()
    pages = count / page_size + 1

    return render_template('admin/user.html', users=users, form=search_form, count=count, pages=pages, page=page)
示例#12
0
文件: api.py 项目: brucx/uberpku
def set_activity(uid, money, calories, est_price):
    now = datetime.datetime.now()
    month = now.year*100 + now.month
    query = Query(Activity)
    query.equal_to("uid", uid)
    query.equal_to("start_time", month)
    if query.count() == 0:
        ubers = money/est_price
        entry = Activity()
        entry.set("uid", uid)
        entry.set("start_time", month)
        entry.set("duration", 30)
        entry.set("budget_money", money)
        entry.set("budget_ubers", ubers)
        entry.set("budget_cal", calories)
        entry.set("curr_money", 0)
        entry.set("curr_cal", 0)
        entry.set("curr_time", 0)
        entry.set("curr_ubers", 0)
        # todo: duration, time!
        entry.save()
    else:
        ubers = money/est_price
        entry = query.first()
        entry.set("budget_money", money)
        entry.set("budget_cal", calories)
        entry.set("budget_ubers", ubers)
        entry.save()
示例#13
0
    def __init__(self):
        leancloud.init(settings.APP_ID, master_key=settings.MASTER_KEY)
        QuestionFollowee = Object.extend('Followee' + self.userDataId)
        questionFollowee = QuestionFollowee()  #
        query = Query(QuestionFollowee)

        query.equal_to('followerLinkId',self.userLinkId)
        curTime = datetime.now()
        query.less_than('createdAt', curTime)
        followeeCount = query.count()
        print "followeeNumbers: %s" % str(questionNum)
        queryLimit = 500
        queryTimes = int(followeeCount +queryLimit -1) / queryLimit
        self.urls = []
        for index in range(queryTimes):
            query = Query(QuestionFollowee)
            query.less_than('createdAt', curTime)
            query.equal_to('followerLinkId',self.userLinkId)
            query.descending('createdAt')
            query.limit(queryLimit)
            query.skip(index * queryLimit)
            query.select('followeeDataId')
            quesRet = query.find()
            for ques in quesRet:
                self.followeeDataIdList.extend(ques.get('followeeDataId'))
示例#14
0
def create_group(args):
    group_id = args.get('id')
    query = Query(DashboardGroup)
    query.equal_to('objectId', group_id)
    group = query.first() if query.count() else DashboardGroup()
    group.clear()
    for k, v in args.items():
        group.set(k, v)
    group.save()
def updata_backend_info(parse_dict):
    print parse_dict
    user_id = parse_dict['user_id']
    # get user Object
    query = Query(Object.extend('_User'))
    query.equal_to('objectId', user_id)
    user = query.find()[0] if query.count() else None

    # get app Object
    query = Query(Object.extend('BindingInstallation'))
    query.equal_to('user', user)
    result_list = query.find()
    app_set = set()
    for result in result_list:
        app_set.add(result.attributes['application'].id)
    app_id_list = list(app_set)

    for app_id in app_id_list:
        query = Query(Object.extend('Application'))
        query.equal_to('objectId', app_id)
        app = query.find()[0]

        table_dash = Object.extend('DashboardSource')
        query = Query(table_dash)
        query.equal_to('app', app)
        query.equal_to('user', user)
        dst_table = query.find()
        if not dst_table:
            dst_table = table_dash()
        else:
            dst_table = dst_table[0]

        dst_table.set('app', app)
        for key, value in parse_dict.items():
            if key is 'user_id':
                dst_table.set('user', user)
            elif key is 'home_office_status':
                home_office_status = dst_table.get('home_office_status') or {}
                for k, v in parse_dict['home_office_status'].items():
                    home_office_status[k] = v
                dst_table.set('home_office_status', home_office_status)
            elif key is 'event':
                event = dst_table.get('event') or {}
                for k, v in parse_dict['event'].items():
                    event[k] = v
                dst_table.set('event', event)
            elif key is 'location':
                location = dst_table.get('location') or {}
                for k, v in parse_dict['location'].items():
                    location[k] = v
                dst_table.set('location', location)
            else:
                dst_table.set(key, value)

        dst_table.save()
        return True
def get_obj_list(obj_name):
    query = Query(Object.extend(obj_name))
    total_count = query.count()
    query_times = (total_count + query_limit - 1) / query_limit
    ret_list = []
    for index in range(query_times):
        query.limit(query_limit)
        query.skip(index * query_limit)
        ret_list.extend(query.find())
    return ret_list
示例#17
0
def get_query_list(app_id='', *field):
    app_id = app_id or '5621fb0f60b27457e863fabb'
    app = {
        "__type": "Pointer",
        "className": "Application",
        "objectId": app_id
    }
    try:
        query_limit = 100
        if app_id == u'5621fb0f60b27457e863fabb':
            query = Query(DashDataSource)
            query.equal_to('app_id', app_id)
        elif app_id == u'all':
            query = Query(DashboardSource)
        else:
            query = Query(DashboardSource)
            query.equal_to('app', app)
        for item in field:
            query.select(item)
        total_count = query.count()
        query_times = (total_count + query_limit - 1) / query_limit
        result_list = []
        for index in xrange(query_times):
            query.limit(query_limit)
            query.skip(index * query_limit)
            result_list.extend(query.find())
    except LeanCloudError:
        return {}

    ret_dict = {}
    for item in field:
        if app_id == '5621fb0f60b27457e863fabb':
            ret_dict[item] = map(lambda result: result.attributes.get(item), result_list)
        else:
            if item == 'event':
                events_list = filter(lambda x: x,
                                     map(lambda result: result.attributes.get(item), result_list))
                ret_dict[item] = events_list
            elif item == 'home_office_status':
                status = filter(lambda x: x is not None,
                                map(lambda result: result.attributes.get(item), result_list))
                ret_dict[item] = status
            elif item == 'province':
                locations = filter(lambda x: x is not None,
                                   map(lambda result: result.attributes.get('location'), result_list))
                ret_dict[item] = map(lambda x: x.get(item), locations)
            elif item == 'city':
                locations = filter(lambda x: x is not None,
                                   map(lambda result: result.attributes.get('location'), result_list))
                ret_dict[item] = map(lambda x: x.get(item), locations)
            else:
                ret_dict[item] = map(lambda result: result.attributes.get(item), result_list)
    return ret_dict
示例#18
0
def match_key_setup():
    q_a = Query(A)
    q_b = Query(B)

    if q_a.count() == 50 and q_b.count() == 50:
        pass
    else:
        old1 = q_a.find()
        old2 = q_b.find()
        for i in old1:
            i.destroy()
        for k in old2:
            k.destroy()

        for i in range(5):
            for k in range(10):
                a = A()
                a.set('age', k)
                a.save()
                b = B()
                b.set('work_year', k)
                b.save()
示例#19
0
 def get(self, request):
     try:
         query = Query(_User)
         query.exists('avatarUrl')
         count = query.count()
         query.limit(1000)
         users = query.descending('createdAt').find()
     except LeanCloudError as e:
         if e.code == 101:
             cards = []
         else:
             raise e
     return render(request,'users.html',{'users':users,'count':count})              
示例#20
0
def match_key_setup():
    q_a = Query(A)
    q_b = Query(B)

    if q_a.count() == 50 and q_b.count() == 50:
        pass
    else:
        old1 = q_a.find()
        old2 = q_b.find()
        for i in old1:
            i.destroy()
        for k in old2:
            k.destroy()

        for i in range(5):
            for k in range(10):
                a = A()
                a.set('age', k)
                a.save()
                b = B()
                b.set('work_year', k)
                b.save()
示例#21
0
def random_text(message):
    '''
    Deprecated
    '''
    Text = Object.extend('Text')
    _query = Query(Text)
    count = _query.count()
    skip = random.randint(0, count - 1)
    texts = _query.limit(1).skip(skip).find()
    if len(texts) == 1:
        text = texts[0]
    else:
        return
    bot.sendMessage(chat_id=message.chat.id, text=text)
示例#22
0
文件: api.py 项目: brucx/uberpku
def set_token(uid, token):
    query = Query(User)
    query.equal_to("uid", uid)
    number = query.count()
    if number == 0:
        temp_user = User()
        temp_user.set("uid", uid)
        temp_user.set("token", token)
        temp_user.save()
    else:
        query.equal_to("uid", uid)
        temp_user = query.first()
        temp_user.set("token", token)
        temp_user.save()
示例#23
0
文件: api.py 项目: brucx/uberpku
def update_nouber(uid, time, calories, money=0):
    now = datetime.datetime.now()
    month = now.year*100 + now.month
    query = Query(Activity)
    query.equal_to("uid", uid)
    query.equal_to("start_time", month)
    if query.count() == 0:
        return False
    else:
        entry = query.first()
        entry.increment("curr_money", money)
        entry.increment("curr_time", time)
        entry.increment("curr_cal", calories)
        return True
示例#24
0
文件: api.py 项目: brucx/uberpku
def set_profile(uid, profile):
    query = Query(Profile)
    query.equal_to("uid", uid)
    if query.count() == 0:
        entry = Profile()
        entry.set("uid", uid)
        obj = {}
    else:
        entry = query.first()
        obj = entry.get("profile")
    for key in profile:
        obj[key] = profile[key]
    entry.set("profile", obj)
    entry.save()
示例#25
0
def random_text(message):
    '''
    Deprecated
    '''
    Text = Object.extend('Text')
    _query = Query(Text)
    count = _query.count()
    skip = random.randint(0, count - 1)
    texts = _query.limit(1).skip(skip).find()
    if len(texts) == 1:
        text = texts[0]
    else:
        return
    bot.sendMessage(chat_id=message.chat.id, text=text)
def db_label_data(period):
    #########################3
    #db label.data
    #########################3
    ####init
    connect_db()
    stamp_range = generate_stamp(period)
    #####get all
    UserSensor = Object.extend('UserSensor')
    query_acc = Query(UserSensor)

    #stamp_range0=[time2stamp(duration[0]),time2stamp(duration[1])]
    #stamp_range=[t*1000 for t in stamp_range0]
    #query.not_equal_to("deviceId",None).not_equal_to("events",None).equal_to("motion",class_type)
    query_acc.equal_to("deviceId",device).not_equal_to("events",None).\
     equal_to("sensorType",'accelerometer').\
     less_than("timestamp", stamp_range[1]).greater_than("timestamp",stamp_range[0])

    #equal_to("motion",class_type).\

    print 'count acc', query_acc.count()
    acc_list = get_all(query_acc, 0, [])
    print 'acc', len(acc_list)  #,all_list[0]#instance object

    ######acc
    all_acc_list = []
    i = 0
    for obj in acc_list:
        obs = get_content_fromLabel(
            obj)  #data form: {timestamp:[o],...}  [o]=[motion,x,y,z]
        all_acc_list.append(obs)  #[{},...]
        i += obs.__len__()
    print 'total', i, all_acc_list.__len__()

    #####
    ###############combine{} sort by timestamp
    data_dic = {}
    acc_list = all_acc_list
    for dic in acc_list:
        for k, v in dic.items():
            if k not in data_dic:
                data_dic[k] = v
    ##sort acc
    ll = sorted(data_dic.items(), key=lambda f: f[0], reverse=False)
    # # DATA FORMATE  {timestamp:[motion x y z],...}->[ (timestamp,[motion,x,y,z]),...]
    xyz = np.array([obs[1][1:] for obs in ll])
    print 'xyz', xyz.shape  #[ [xyz],[]...] shape[n,3]
    #save2pickle(xyz,'xyz-watchphone-nov-'+class_type)
    return xyz
示例#27
0
 def get(self, request):
     try:
         query = Query(Card)
         query.not_equal_to('deleted',False)
         query.does_not_exist('cid')
         query.limit(1000)
         count = query.count()
         query.include('user')
         cards = query.descending('createdAt').find()
     except LeanCloudError as e:
         if e.code == 101:
             cards = []
         else:
             raise e
     return render(request,'card_deleted.html',{'cards':cards,'count':count})                 
示例#28
0
def already_exist(q_homepage):
    '''
    Check whether the item has already existed in the DB
    :param q_homepage:
    :return: Business object
    '''
    try:
        query = Query(Business)
        query.equal_to('homepage', q_homepage)
        # print("query.count = ", query.count())
        assert(query.count() <= 1)
        current_business = query.first()
        return current_business
    except leancloud.errors.LeanCloudError:
        return None
示例#29
0
 def __save(self, item):
     """
     保存抓取的数据
     :param item:
     :return:
     """
     query = Query(self.News)
     query.equal_to('newsId', item['newsId'])
     if query.count():
         log.msg('News has exist with same newsId {0}'.format(item['newsId']), 'INFO')
         return
     news = self.News()
     for key in item:
         news.set(key, item.get(key))
     news.save()
def get_userid_list():
    query = Query(Object.extend('User'))
    query.not_equal_to('type', 'developer')
    total_count = query.count()
    query_times = (total_count + query_limit - 1) / query_limit
    user_list = []
    for index in range(query_times):
        query.limit(query_limit)
        query.skip(index * query_limit)
        user_list.extend(query.find())

    tracker_id_set = set()
    for user in user_list:
        if user.id not in tracker_id_set:
            tracker_id_set.add(user.id)
    return list(tracker_id_set)
示例#31
0
def test_basic_query():
    # find
    q = Query(GameScore)
    results = q.find()
    eq_(len(results), 10)

    # first
    q = Query(GameScore)
    game_score = q.first()
    assert game_score

    # get
    q = Query(GameScore)
    local_game_score = game_scores[0]
    q.get(local_game_score.id)

    # count
    q = Query(GameScore)
    eq_(q.count(), 10)

    # descending
    q = Query(GameScore).descending('score')
    eq_([x.get('score') for x in q.find()], range(9, -1, -1))

    # greater_than
    q = Query(GameScore).greater_than('score', 5).ascending('score')
    eq_([x.get('score') for x in q.find()], range(6, 10))

    q = Query(GameScore).greater_than_or_equal_to('score',
                                                  5).ascending('score')
    eq_([x.get('score') for x in q.find()], range(5, 10))

    q = Query(GameScore).less_than('score', 5).ascending('score')
    eq_([x.get('score') for x in q.find()], range(0, 5))

    q = Query(GameScore).less_than_or_equal_to('score', 5).ascending('score')
    eq_([x.get('score') for x in q.find()], range(0, 6))

    q = Query(GameScore).contained_in('score', [1, 2, 3]).ascending('score')
    eq_([x.get('score') for x in q.find()], range(1, 4))

    q = Query(GameScore).not_contained_in('score',
                                          [0, 1, 2, 3]).ascending('score')
    eq_([x.get('score') for x in q.find()], range(4, 10))

    q = Query(GameScore).select('score')
    assert not q.find()[0].has('playerName')
示例#32
0
文件: site.py 项目: zicoapp/phoscc
def tag():
    """Tag Photo"""
    form = MultiTagForm()
    if form.validate_on_submit():
        tags = form.data['tags'].split(',')

        photo = Query(Photo).get(form.data['photoid'])
        relation = photo.relation('tags')

        for tag in tags:
            # Obtain existed tag by name
            results = Query(Tag).equal_to('name', tag).find()
            if len(results) != 0:
                avostag = results[0]
                avostag.increment('count', 1)
            else:
                avostag = Tag()
                avostag.set('name', tag)
                avostag.save()
            contributors = avostag.relation('contributors')
            contributors.add(g.user)
            avostag.save()
            # Add relation to photo
            relation.add(avostag)
        photo.save()

        query = Relation.reverse_query('Tag', 'contributors', g.user)
        count = query.count()
        return render_template('site/tag/done.html', user_tag_count=count)
    else:
        total = Query.do_cloud_query('select count(*) from Photo')
        try:
            # query = Query(Photo).descending('createdAt').skip(randint(0, total.count))
            # item = query.first()
            query = Query(Photo).descending('createdAt').skip(randint(0, total.count)).limit(5)
            results = query.find()

            jsonresult = json.dumps([o.dump() for o in results])

            query = Relation.reverse_query('PhotoTag', 'contributors', g.user)
            count = query.count()

            categories = Query(Category).find()
            return render_template('site/tag/tag.html', current_photo=results[0],
                coming_photos=jsonresult, utagcount=count, categories=categories, form=form)
        except LeanCloudError, e:
            return redirect(url_for('site.about'))
def get_all_tracker(table_name=None):
    DbTable = table_name
    query = Query(DbTable)
    query.less_than('createdAt',current_time)
    query.exists('objectId')
    total_count=query.count()
    query_times=(total_count+query_limit-1)/query_limit
    user_list = []
    for index in range(query_times):
        query = Query(DbTable)
        query.exists('objectId')
        query.less_than('createdAt',current_time)
        query.ascending('createdAt')
        query.limit(query_limit)
        query.skip(index*query_limit)
        user_list.extend(query.find())
    return user_list
示例#34
0
 def modify_app(self, app_id=None, app_name=None, app_type=None):
     try:
         user = self.become(session_token=self.session_token)
         query = Query(Application)
         query.equal_to('user', user)
         query.equal_to('app_id', app_id)
         app = query.find()[0] if query.count() else None
         if app:
             if app_name:
                 app.set('app_name', app_name)
             if app_type:
                 app.set('type', app_type)
             app.save()
             return True
         return False
     except LeanCloudError:
         return False
示例#35
0
 def __save(self, item):
     """
     保存抓取的数据
     :param item:
     :return:
     """
     query = Query(self.News)
     query.equal_to('newsId', item['newsId'])
     if query.count():
         log.msg(
             'News has exist with same newsId {0}'.format(item['newsId']),
             'INFO')
         return
     news = self.News()
     for key in item:
         news.set(key, item.get(key))
     news.save()
def get_all_real_applications(db_name='Application'):
    DbTable = db_name
    query = Query(DbTable)
    query.less_than('createdAt',current_time)
    query.exists('objectId')
    total_count=query.count()
    query_times=(total_count+query_limit-1)/query_limit
    result_list = []
    for index in range(query_times):
        query = Query(DbTable)
        query.exists('objectId')
        query.less_than('createdAt',current_time)
        query.ascending('createdAt')
        query.limit(query_limit)
        query.skip(index*query_limit)
        result_list.extend(query.find())
    return result_list
def db_label_data(period):
	#########################3
	#db label.data
	#########################3
	####init
	connect_db()
	stamp_range=generate_stamp(period) 
	#####get all
	UserSensor = Object.extend('UserSensor')
	query_acc = Query(UserSensor) 
	

	#stamp_range0=[time2stamp(duration[0]),time2stamp(duration[1])]
	#stamp_range=[t*1000 for t in stamp_range0]
	#query.not_equal_to("deviceId",None).not_equal_to("events",None).equal_to("motion",class_type)
	query_acc.equal_to("deviceId",device).not_equal_to("events",None).\
		equal_to("sensorType",'accelerometer').\
		less_than("timestamp", stamp_range[1]).greater_than("timestamp",stamp_range[0])
		
		#equal_to("motion",class_type).\
	 
	print 'count acc',query_acc.count() 
	acc_list=get_all(query_acc,0,[]);print 'acc',len(acc_list)#,all_list[0]#instance object
	 
	######acc  
	all_acc_list=[];i=0
	for obj in acc_list:
		obs=get_content_fromLabel(obj)#data form: {timestamp:[o],...}  [o]=[motion,x,y,z]
		all_acc_list.append(obs)#[{},...]
		i+=obs.__len__()
	print 'total',i,all_acc_list.__len__()
	 
	#####
	###############combine{} sort by timestamp
	data_dic={};acc_list=all_acc_list
    	for dic in acc_list:
		for k,v in dic.items():
			if k not in data_dic:
				data_dic[k]=v
	##sort acc
	ll=sorted(data_dic.items(),key=lambda f:f[0],reverse=False)
	# # DATA FORMATE  {timestamp:[motion x y z],...}->[ (timestamp,[motion,x,y,z]),...]
	xyz=np.array([obs[1][1:] for obs in ll]);print 'xyz',xyz.shape#[ [xyz],[]...] shape[n,3]
	#save2pickle(xyz,'xyz-watchphone-nov-'+class_type)
	return xyz
示例#38
0
def test_basic_query():
    # find
    q = Query(GameScore)
    results = q.find()
    eq_(len(results), 10)

    # first
    q = Query(GameScore)
    game_score = q.first()
    assert game_score

    # get
    q = Query(GameScore)
    local_game_score = game_scores[0]
    q.get(local_game_score.id)

    # count
    q = Query(GameScore)
    eq_(q.count(), 10)

    # descending
    q = Query(GameScore).descending('score')
    eq_([x.get('score') for x in q.find()], range(9, -1, -1))

    # greater_than
    q = Query(GameScore).greater_than('score', 5).ascending('score')
    eq_([x.get('score') for x in q.find()], range(6, 10))

    q = Query(GameScore).greater_than_or_equal_to('score', 5).ascending('score')
    eq_([x.get('score') for x in q.find()], range(5, 10))

    q = Query(GameScore).less_than('score', 5).ascending('score')
    eq_([x.get('score') for x in q.find()], range(0, 5))

    q = Query(GameScore).less_than_or_equal_to('score', 5).ascending('score')
    eq_([x.get('score') for x in q.find()], range(0, 6))

    q = Query(GameScore).contained_in('score', [1, 2, 3]).ascending('score')
    eq_([x.get('score') for x in q.find()], range(1, 4))

    q = Query(GameScore).not_contained_in('score', [0, 1, 2, 3]).ascending('score')
    eq_([x.get('score') for x in q.find()], range(4, 10))

    q = Query(GameScore).select('score')
    assert not q.find()[0].has('playerName')
def get_all_static_info(table_name='UserInfoLog',field_name='staticInfo'):
    DBTable = Object.extend(table_name)
    query = Query(DBTable)
    query.exists(field_name)
    query.less_than('updatedAt',current_time)
    total_count=query.count()
    print 'TotalCount %s' %str(total_count)
    query_times=(total_count+query_limit-1)/query_limit
    static_info_list=[]
    for index in range(query_times):
        print 'querying index: %s' %str(index)
        query = Query(DBTable)
        query.exists(field_name)
        query.less_than('updatedAt',current_time)
        query.limit(query_limit)
        query.skip(index*query_limit)
        query.descending('updatedAt')
        result_list=query.find()
        static_info_list.extend(result_list)
    return static_info_list
示例#40
0
文件: api.py 项目: brucx/uberpku
def add_schedule(uid, month, time, strategy, start_lat, start_long, end_lat, end_long, to_work):
    query = Query(Schedule)
    query.equal_to("uid", uid)
    query.equal_to("month", month)
    query.equal_to("to_work", to_work)
    if query.count() != 0:
        return False
    else:
        schedule = Schedule()
        schedule.set("uid", uid)
        schedule.set("month", month)
        schedule.set("time", time)
        schedule.set("strategy", strategy)
        schedule.set("start_lat", start_lat)
        schedule.set("start_long", start_long)
        schedule.set("end_lat", end_lat)
        schedule.set("end_long", end_long)
        query.equal_to("to_work", to_work)
        schedule.save()
        return True
示例#41
0
def like(**params):
    card_id = params['cid']
    user_id = params['uid']
    card = Card.create_without_data(card_id)
    user = User.create_without_data(user_id)
    query = Query(Like)
    query.equal_to('card', card)
    query.equal_to('user', user)
    count = query.count()
    if count == 0:
        like = Like(card=card, user=user)
        try:
            like.save()
            card.increment('likes')
            card.fetch_when_save = True
            card.save()
            return 'ok'
        except LeanCloudError as e:
            return HttpResponseServerError(e.error)
    else:
        return 'no'
示例#42
0
def update_rec_list(**params):
    qRec = Query('Recommend')
    num_of_user = qRec.count()
    results = qRec.limit(num_of_user).ascending('uId').find()
    list_of_rec = []
    for result in results:
        list_of_rec.append({
            'objectId': result.get('objectId'),
            'pIds': result.get('pIds'),
            'pTitles': result.get('pTitles'),
            'uId': result.get('uId'),
        })

    # delete all data in the database
    Recommend = Object.extend('Recommend')
    destroy_list = []
    for rec in list_of_rec:
        recommend = Recommend.create_without_data(rec.get('objectId'))
        destroy_list.append(recommend)
    Object.destroy_all(destroy_list)
    # update recommend data for each users
    cloudfunc.run.local('build_rec_list')
示例#43
0
def getAccountCount():
    Account = Object.extend('Account')
    query = Query(Account)
    query.equal_to('flag', '0')
    return query.count()
示例#44
0
class Card(Object):
    pass
class Photo(Object):
    pass  
class _User(Object):
    pass      
class _File(Object):
	pass


print('getting total count:')
query = Query(_File)

query.limit(100)
count = query.count()
print('Total:'+str(count))
filelist = query.find()
cardquery = Card.query
userquery = _User.query
for file in filelist:
    meta = file.get('metaData')
    username = meta['owner']
    url = file.get('url')
    
    cardquery.equal_to('img_url',url)
    count = cardquery.count()

    if(count>0):
        card = cardquery.first()
        data = Card.create_without_data(card.get('objectId'))