當前位置: 首頁>>代碼示例>>Python>>正文


Python Account._get_collection方法代碼示例

本文整理匯總了Python中accounts.models.Account._get_collection方法的典型用法代碼示例。如果您正苦於以下問題:Python Account._get_collection方法的具體用法?Python Account._get_collection怎麽用?Python Account._get_collection使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在accounts.models.Account的用法示例。


在下文中一共展示了Account._get_collection方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: getAccounts

# 需要導入模塊: from accounts.models import Account [as 別名]
# 或者: from accounts.models.Account import _get_collection [as 別名]
def getAccounts(request, id):
    try:
        company_id = request.user.company_id
        page_number = int(request.GET.get('page_number'))
        items_per_page = int(request.GET.get('per_page'))
        offset = (page_number - 1) * items_per_page
        start_date = int(request.GET.get('start_date'))
        end_date = int(request.GET.get('end_date'))
        sub_view = request.GET.get('subview')
        superfilters = request.GET.get('superfilters')
        super_filters = json.loads(superfilters)
        #print 'super filters are ' + str(super_filters)
        date_field = None
        if super_filters is not None:
            if 'date_types' in super_filters: # need to filter by a certain type of date
                date_field = super_filters['date_types']
                if start_date is not None:
                    utc_day_start_epoch =  datetime.fromtimestamp(float(start_date / 1000))
                    #utc_day_start_epoch = str('{0:f}'.format(utc_day_start_epoch).rstrip('0').rstrip('.'))
                    print 'utc start epoch is ' + str(utc_day_start_epoch)
       
                    #local_start_date = get_current_timezone().localize(local_start_date_naive, is_dst=None)
                #print 'start2 is ' + str(time.time())
                if end_date is not None:
                    utc_day_end_epoch = datetime.fromtimestamp(float(end_date / 1000))
                    #utc_day_end_epoch = str('{0:f}'.format(utc_day_end_epoch).rstrip('0').rstrip('.'))
                    print 'utc end epoch is ' + str(utc_day_end_epoch)
                utc_day_start_string = datetime.strftime(utc_day_start_epoch, '%Y-%m-%dT%H-%M-%S.000+0000')
                utc_day_end_string = datetime.strftime(utc_day_end_epoch, '%Y-%m-%dT%H-%M-%S.000+0000')
                print 'utc start string is ' + str(utc_day_start_string)
                print 'utc end string is ' + str(utc_day_end_string)
                
        result = []
        company_field_qry = 'company_id'
        #print 'start time was '  + str(time.time())
        collection = Account._get_collection()
        if date_field is None:
            total = collection.find({'company_id': int(company_id)}).count() #.hint('company_id_1')
        else:
            total = collection.find({'company_id': int(company_id), date_field: {'$gte':utc_day_start_string, '$lte':utc_day_end_string}}).count() #.hint('company_id_1')
        
        if date_field is None:
            queryset = Account.objects(company_id=company_id).skip(offset).limit(items_per_page)
        else:
            date_field_start_qry = date_field + '__gte'
            date_field_end_qry = date_field + '__lte'
            company_field_qry = 'company_id'
            querydict = {company_field_qry: company_id, date_field_start_qry: utc_day_start_string, date_field_end_qry: utc_day_end_string}
            queryset = Account.objects(**querydict).skip(offset).limit(items_per_page)
        
        #qlist = list(queryset)
        #print 'start time3 was '  + str(time.time())
        #total = len(qlist)
        #result = qlist[offset:offset+items_per_page]
        #print 'start time4 was '  + str(time.time())
        for account in queryset:
            leadsTemp = []
            leads = account['leads']
            for lead in leads: # each 'lead' here is an object of type {lead_id_type: lead_id} e.g. {'sfdc_contact_id': 1234}
                for k, v in lead.iteritems():
                    lead_field_qry = k
                    querydict = {lead_field_qry: v, company_field_qry: company_id}
                    qset = Lead.objects(**querydict).only('source_first_name').only('source_last_name').only('id').first()
                    #print 'qset ' + str(qset)
                    #qset_actual_lead_list_temp = [qset_lead.to_mongo().to_dict() for qset_lead in qset]
                    #for qset_actual_lead in qset_actual_lead_list_temp:
                    leadsTemp.append(qset)
            account['leads'] = leadsTemp
            result.append(account)
        
        #result.sort(key=lambda account:len(account.leads))
        #print 'qset is ' + str(qlist)
        #print 'start time5 was '  + str(time.time())
        serializer = AccountSerializer(result, many=True)  
        #print 'start time6 was '  + str(time.time()) 
        type = 'accounts'
        return JsonResponse({'count' : total, 'results': serializer.data, 'type': type})    
    except Exception as e:
        print 'exception while getting all accounts ' + str(e)
        return JsonResponse({'Error' : str(e)})
開發者ID:woostersoz,項目名稱:3m,代碼行數:82,代碼來源:views.py


注:本文中的accounts.models.Account._get_collection方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。