Esempio n. 1
0
def retrieveHsptOpportunities(user_id=None, company_id=None, job_id=None, run_type=None, sinceDateTime=None):
    try:
        print 'retrieving Hspt deals'
        hspt = Hubspot(company_id)
        oppList = []
#         if run_type == 'initial':
#             oppList = hspt.get_deals(count=10000)
#         else:
#             oppList = hspt.get_deals(count=1000)
#         print 'got opps ' + str(len(oppList['results']))
#         saveHsptOpportunities(user_id=user_id, company_id=company_id, oppList=oppList, job_id=job_id, run_type=run_type)
        #print 'job id is ' + str(job_id)
        if run_type == 'initial':
            print 'initial run for opps'
            try:
                for opp in hspt.get_deals(count=10000)['results']:
                    oppList.append(opp)
                    if len(oppList) == 100:
                        #print 'going to save'
                        saveHsptOpportunities(user_id=user_id, company_id=company_id, oppList=oppList, job_id=job_id, run_type=run_type)
                        oppList = []
            except Exception as e:
                print 'exception: ' + str(e)
        else:
            try:
                for opp in hspt.get_deals(count=1000)['results']:
                    oppList.append(opp)
                    if len(oppList) == 100:
                        saveHsptOpportunities(user_id=user_id, company_id=company_id, oppList=oppList, job_id=job_id, run_type=run_type)
                        oppList = []
            except Exception as e:
                print 'exception: ' + str(e)
        
        
        try:
            message = 'Opportunities retrieved from Hubspot'
            notification = Notification()
            #notification.company_id = company_id
            notification.owner = user_id
            notification.module = 'Opportunities'
            notification.type = 'Background task' 
            notification.method = os.path.basename(__file__)
            notification.message = message
            notification.success = True
            notification.read = False
            notification.save()
        except Exception as e:
            send_notification(dict(
                 type='error',
                 success=False,
                 message=str(e)
                ))    
        return oppList
    except Exception as e:
        send_notification(dict(type='error', success=False, message=str(e)))