本文整理汇总了Python中accounts.models.Account.objects方法的典型用法代码示例。如果您正苦于以下问题:Python Account.objects方法的具体用法?Python Account.objects怎么用?Python Account.objects使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类accounts.models.Account
的用法示例。
在下文中一共展示了Account.objects方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_accounts
# 需要导入模块: from accounts.models import Account [as 别名]
# 或者: from accounts.models.Account import objects [as 别名]
def test_accounts(self):
from django.core.urlresolvers import reverse
# Can't access when we are not logged in.
response = self.client.get(reverse('accounts'))
self.assertEqual(response.status_code, 200)
response = self.client.get(
reverse('accounts_detail', kwargs={'object_id': self.bob.id}))
self.assertEqual(response.status_code, 200)
response = self.client.get(
reverse('accounts_edit', kwargs={'object_id': self.bob.id}))
self.assertEqual(response.status_code, 302)
self.client.login(username='bob', password='password')
response = self.client.get(
reverse('accounts_edit', kwargs={'object_id': self.bob.id}))
self.assertEqual(response.status_code, 200)
self.assertContains(response, "%s" % self.bob.name)
response = self.client.get(
reverse('accounts_remove', kwargs={'object_id': self.alice.id}))
self.assertEqual(response.status_code, 302)
self.client.logout()
self.user_bob.is_staff = True
self.user_bob.is_superuser = True
self.user_bob.save()
self.assertTrue(self.user_bob.is_superuser)
self.client.login(username='bob', password='password')
response = self.client.get(
reverse('accounts_remove', kwargs={'object_id': self.alice.id}))
self.assertEqual(response.status_code, 200)
name = self.alice.name
local_id = self.alice.local_id
response = self.client.post(
reverse('accounts_remove',
kwargs={'object_id': self.alice.id}),
data={'object_name': name, 'result': 'Cancel'})
self.assertEqual(response.status_code, 302)
self.assertEqual(Account.objects(name=name).count(), 1)
# no Resources in setup so delete works
response = self.client.post(
reverse('accounts_remove',
kwargs={'object_id': self.alice.id}),
data={'object_name': name, 'result': 'Delete'})
self.assertEqual(response.status_code, 302)
self.assertEqual(Account.objects(name=name).count(), 0)
self.assertEqual(User.objects.filter(pk=local_id).count(), 0)
示例2: saveSfdcAccountsToMaster
# 需要导入模块: from accounts.models import Account [as 别名]
# 或者: from accounts.models.Account import objects [as 别名]
def saveSfdcAccountsToMaster(user_id=None, company_id=None, job_id=None, run_type=None):
#delete later
#job_id = ObjectId("569fd4078afb002426ef2fd3")
if run_type == 'initial':
accounts = TempData.objects(Q(company_id=company_id) & Q(record_type='account') & Q(source_system='sfdc') & Q(job_id=job_id) ).only('source_record') #& Q(job_id=job_id)
else:
accounts = TempDataDelta.objects(Q(company_id=company_id) & Q(record_type='account') & Q(source_system='sfdc') & Q(job_id=job_id) ).only('source_record') #& Q(job_id=job_id)
accountListTemp = list(accounts)
accountList = [i['source_record'] for i in accountListTemp]
try:
for newAccount in accountList:
sfdc_id = str(newAccount['Id'])
#find all leads that have this account ID
relatedLeadList = []
relatedLeadListTemp = None
relatedLeads = Lead.objects(Q(company_id=company_id) & Q(sfdc_account_id=sfdc_id)).only('sfdc_contact_id') #if SFDC Account, then matching lead must have a SFDC Contact ID
if relatedLeads is not None:
relatedLeadListTemp = [lead.to_mongo().to_dict() for lead in relatedLeads]
#print 'rll is ' + str(relatedLeadListTemp)
for i in relatedLeadListTemp:
if 'sfdc_contact_id' in i:
relatedLeadList.append({'sfdc_contact_id': i['sfdc_contact_id']})
#print 'related leads are ' + str(relatedLeadList)
# if relatedLeads is not None:
# #leadListTemp = list(relatedLeads)
# #relatedLeadList = [i.id for i in leadListTemp]
# for lead in relatedLeads:
# relatedLeadList.append(lead)
print 'account id is ' + sfdc_id
# sfdc_mkto_id = str(newLead['sfdcLeadId']) #check if there is a corresponding lead from MKTO
existingAccount = None
existingAccount = Account.objects(Q(company_id=company_id) & Q(sfdc_id=sfdc_id)).first()
if existingAccount is not None: # we found this contact already in the DB
print 'found existing account for id ' + str(sfdc_id)
if 'sfdc' in existingAccount.accounts:
existingAccount.source_name = newAccount['Name']
existingAccount.source_source = newAccount['AccountSource']
existingAccount.source_industry = newAccount['Industry']
existingAccount.source_created_date = newAccount['CreatedDate']
existingAccount.accounts["sfdc"] = newAccount
if relatedLeadList is not None:
existingAccount.leads = relatedLeadList
existingAccount.save()
#Lead.objects(Q(company_id=company_id) & Q(sfdc_contact_id=sfdc_contact_Id)).update(contacts__sfdc=newContact)
else:
existingAccount.accounts['sfdc'] = {}
existingAccount.accounts['sfdc'] = newAccount
if relatedLeadList is not None:
existingAccount.leads = relatedLeadList
existingAccount.save()
elif existingAccount is None: # this account does not exist
account = _saveSfdcNewAccount(sfdc_id, newAccount, relatedLeadList, company_id)
except Exception as e:
print 'exception while saving accounts ' + str(e)
send_notification(dict(type='error', success=False, message=str(e)))
示例3: saveSfdcOppStageHistoryToMaster
# 需要导入模块: from accounts.models import Account [as 别名]
# 或者: from accounts.models.Account import objects [as 别名]
def saveSfdcOppStageHistoryToMaster(user_id=None, company_id=None, job_id=None, run_type=None):
#job_id = ObjectId("56b2b92c8afb0070a795c4b2")
if run_type == 'initial':
#activities = TempData.objects(Q(company_id=company_id) & Q(record_type='activity') & Q(source_system='mkto') & Q(job_id=job_id) ).only('source_record')
collection = TempData._get_collection()
activities = collection.find({'company_id': int(company_id), 'record_type': 'opp_stage_history', 'source_system': 'sfdc', 'job_id': job_id}, projection={'source_record': True}, batch_size=1000)
else:
collection = TempDataDelta._get_collection()
activities = collection.find({'company_id': int(company_id), 'record_type': 'opp_stage_history', 'source_system': 'sfdc', 'job_id': job_id}, projection={'source_record': True}, batch_size=1000)
try:
print 'got history ' + str(activities.count())
for activity in activities:
skipThis = False
newActivity = activity['source_record']
addThisActivity = True
print 'act is ' + str(newActivity)
newOppId = newActivity["OpportunityId"]
if newOppId is not None:
print 'trying to get opp'
existingAccount = Account.objects(Q(company_id = company_id) & Q(opportunities__sfdc__Id = newOppId)).first()
print 'account is ' + str(existingAccount)
else:
continue
if existingAccount is None:
continue
for existingOpp in existingAccount['opportunities']['sfdc']: #loop through each opp to find the right one
if existingOpp['Id'] == newOppId:
if 'stage_activities' not in existingOpp: #if no prior activities
existingOpp['stage_activities'] = [] #create list
existingOpp['stage_activities'].append(newActivity) #save activity
print 'saved virgin stage activity for opp'
else:
for existingActivity in existingOpp['stage_activities']: #check if this activity already exists
if existingActivity['Id'] == newActivity['Id']: #it exists, so skip the activity
skipThis = True
print 'skipping opp stage activity'
break
if skipThis:
break # get out of this for loop
else:
existingOpp['stage_activities'].append(newActivity)
print 'saved stage activity for opp'
existingAccount.save()
except Exception as e:
print 'exception while saving SFDC Opp stage history to master' + str(e)
send_notification(dict(
type='error',
success=False,
message=str(e)
))
示例4: locations_remove
# 需要导入模块: from accounts.models import Account [as 别名]
# 或者: from accounts.models.Account import objects [as 别名]
def locations_remove(request, object_id):
"""docstring for location_remove"""
object = get_one_or_404(Location, id=object_id, user=request.user, perm='can_delete')
resources = Resource.objects(locations=object)
accounts = Account.objects(locations=object)
if resources or accounts:
res_str = ','.join([res.name for res in resources])
acct_str = ','.join(['<a href="%s">%s</a>' % (reverse('accounts_edit', args=[acct.id]), acct.name) for acct in accounts])
messages.error(
request,
'Resources/Accounts using this location:<br>%s<br>%s.' % (res_str, acct_str))
return HttpResponseRedirect(reverse('cab_locations_detail', args=[object.id]))
object.delete()
messages.success(request, 'Location removed')
return HttpResponseRedirect(reverse('cab_locations'))
示例5: clean_email
# 需要导入模块: from accounts.models import Account [as 别名]
# 或者: from accounts.models.Account import objects [as 别名]
def clean_email(self):
if "email" in self.cleaned_data:
addr = self.data["email"].strip()
accounts = Account.objects(email=addr).count()
if accounts > 0:
raise forms.ValidationError("'%s' already has an account." % addr)
exists = Invitation.objects(email=addr).count()
if exists > 0:
raise forms.ValidationError("'%s' has already been invited." % addr)
# mongo stores stripped string anyway :)
return self.cleaned_data["email"]
示例6: tags_process
# 需要导入模块: from accounts.models import Account [as 别名]
# 或者: from accounts.models.Account import objects [as 别名]
def tags_process(request, options):
results = []
if options['split'] or options['lower_case']:
setting, _ = Setting.objects.get_or_create(key=UPPERCASE)
exceptions = setting.value.get('data', [])
for obj in Curation.objects():
tp = TagProcessor(obj.tags)
new_tags = tp.split(options['split']).lower(options['lower_case'], exceptions).tags
if new_tags != obj.tags:
obj.tags = new_tags
obj.save()
for obj in Account.objects():
tp = TagProcessor(obj.tags)
new_tags = tp.split(options['split']).lower(options['lower_case'], exceptions).tags
if new_tags != obj.tags:
obj.tags = new_tags
obj.save()
results.append('done')
messages.success(request, '<br>'.join(results))
示例7: matchAccountName
# 需要导入模块: from accounts.models import Account [as 别名]
# 或者: from accounts.models.Account import objects [as 别名]
def matchAccountName(request, id, accountSearchName):
try:
if accountSearchName is None:
return JsonResponse({'count' : 0, 'results': None})
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
accounts = Account.objects(company_id=company_id)
accounts_list = list(accounts)
#accounts_list.sort(key=lambda x:len(x.source_name), reverse=False)
results_temp = matchingAlgo(request, search_name=accountSearchName, entries=accounts_list, object_type='account')
results = results_temp[offset:offset+items_per_page]
serializer = AccountSerializer(results, many=True)
return JsonResponse({'count' : len(results_temp), 'results': serializer.data})
except Exception as e:
return JsonResponse({'Error' : str(e)})
示例8: getOpportunities
# 需要导入模块: from accounts.models import Account [as 别名]
# 或者: from accounts.models.Account import objects [as 别名]
def getOpportunities(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')
filters = request.GET.get('filters')
filters = json.loads(filters)
superfilters = request.GET.get('superfilters')
super_filters = json.loads(superfilters)
#print 'super filters are ' + str(super_filters)
date_field = None
querydict_filters = {}
#match_filters = {}
company_field_qry = 'company_id'
opp_field_qry = 'opportunities__sfdc__exists'
subview_field_qry = ''
original_date_field = ''
projection = {'$project': {'_id': '$opportunities.sfdc.Id', 'created_date': '$opportunities.sfdc.CreatedDate', 'close_date': '$opportunities.sfdc.CloseDate', 'account_name': '$source_name', 'name': '$opportunities.sfdc.Name', 'amount': '$opportunities.sfdc.Amount', 'account_id': '$sfdc_id', 'closed': '$opportunities.sfdc.IsClosed', 'won': '$opportunities.sfdc.IsWon', 'owner_id': '$opportunities.sfdc.OwnerId', 'stage': '$opportunities.sfdc.StageName' } }
match = {'$match' : { }}
if super_filters is not None:
#print 'sf ' + str(super_filters)
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:
start_date = datetime.fromtimestamp(float(start_date) / 1000)
#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:
end_date = datetime.fromtimestamp(float(end_date) / 1000)
#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')
local_start_date = get_current_timezone().localize(start_date, is_dst=None)
utc_day_start = local_start_date.astimezone(pytz.timezone('UTC'))
utc_day_start_string = datetime.strftime(utc_day_start, '%Y-%m-%dT%H-%M-%S.000+0000')
local_end_date = get_current_timezone().localize(end_date, is_dst=None)
utc_day_end = local_end_date.astimezone(pytz.timezone('UTC'))
utc_day_end_string = datetime.strftime(utc_day_end, '%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)
#remove the date_types item
#super_filters.pop('date_types')
if filters is not None:
for key, value in filters.items():
if value is not None and value != '':
querydict_filters['opportunities__sfdc__' + key] = value #creates an additional querydict that can be added to the main qd
match['$match']['opportunities.sfdc.' + key] = value
if date_field is None: #if there's no date filter
querydict = {opp_field_qry: True, company_field_qry: company_id}
querydict.update(querydict_filters)
opps = Account.objects(**querydict).aggregate({'$unwind': '$opportunities.sfdc'}, match, projection) #, {'$match': {'opportunities.sfdc.Id' : {'$ne': None}}} #
else: #if date filter is used
if date_field == 'opportunities.sfdc.CloseDate': #change to Last Modified Date because CloseDate in Opp may not be correctly updated by user
date_field = 'opportunities.sfdc.LastModifiedDate'
original_date_field = date_field
date_field = date_field.replace('.', '__') # needed to change embedded field format for querydict
date_field_start_qry = date_field + '__gte'
date_field_end_qry = date_field + '__lte'
match['$match'][original_date_field] = {'$gte': utc_day_start_string, '$lte': utc_day_end_string}
if original_date_field == 'opportunities.sfdc.LastModifiedDate': #if close add, add a filter for 'IsClosed'
isclosed_field_qry = 'opportunities__sfdc__IsClosed'
querydict = {company_field_qry: company_id, date_field_start_qry: utc_day_start_string, date_field_end_qry: utc_day_end_string, isclosed_field_qry: True}
querydict.update(querydict_filters)
match['$match']['opportunities.sfdc.IsClosed'] = True
opps = Account.objects(**querydict).aggregate({'$unwind': '$opportunities.sfdc'}, match, projection) #, {'$match': {'opportunities.sfdc.Id' : {'$ne': None}}} #
else:
querydict = {company_field_qry: company_id, date_field_start_qry: utc_day_start_string, date_field_end_qry: utc_day_end_string}
querydict.update(querydict_filters)
opps = Account.objects(**querydict).aggregate({'$unwind': '$opportunities.sfdc'}, match, projection) #, {'$match': {'opportunities.sfdc.Id' : {'$ne': None}}} #
#print 'qd is ' + str(querydict)
#print 'start time was ' + str(time.time())
#total = Account.objects(**querydict).count()
#print 'start time2 was ' + str(time.time())
opps_list = list(opps)
#see if there's a subview
#.........这里部分代码省略.........
示例9: getSuperFilters
# 需要导入模块: from accounts.models import Account [as 别名]
# 或者: from accounts.models.Account import objects [as 别名]
def getSuperFilters(request, company_id):
user_id = request.user.id
company_id = request.user.company_id
object_type = request.GET.get('object_type')
system_type = request.GET.get('system_type')
#existingIntegration = CompanyIntegration.objects(company_id = company_id ).first()
existingIntegration = {}
existingIntegration['integrations'] = {}
map = Code("function () {"
" for (var key in this.integrations) emit(key, this.integrations[key]['filters']); } ")
reduce = Code("function (key, values) { return null; } ")
results = CompanyIntegration.objects(company_id=company_id).map_reduce(map, reduce, "inline")
results = list(results)
for result in results:
existingIntegration['integrations'][result.key] = {'filters': result.value}
try:
code = None
if existingIntegration is not None:
for source in existingIntegration['integrations'].keys():
defined_system_type = SuperIntegration.objects(Q(code = source) & Q(system_type = system_type)).first()
if defined_system_type is not None:
code = source
print 'found code' + str(code)
if code is None:
raise ValueError("No integrations defined")
else:
super_filters = SuperFilters.objects(source_system = code).first()
if super_filters is None:
result = []
else:
if object_type not in super_filters['filters']:
result = []
else:
temp = super_filters['filters'].get(object_type, None)
filters = existingIntegration['integrations'][code].get('filters', None)
if filters is not None:
filter_obj = filters.get(object_type, None)
if filter_obj is None:
return JsonResponse({'results': temp}, safe=False)
for filter, values in filter_obj.items():
if filter in temp:
if filter == 'OwnerId': #reduce the users list to only those with opportunities
temp_values = {}
opp_field_qry = 'opportunities__sfdc__exists'
company_field_qry = 'company_id'
projection = {'$project': {'owner_id': '$opportunities.sfdc.OwnerId' } }
querydict = {opp_field_qry: True, company_field_qry: company_id}
opps = Account.objects(**querydict).aggregate({'$unwind': '$opportunities.sfdc'}, projection)
opps = list(opps)
opps_owner_ids = [opp['owner_id'] for opp in opps]
print 'opp owner ids ' + str(opps_owner_ids)
tempValues = [value for value in values['values'] if value['value'] in opps_owner_ids]
print 'temp values2 is ' + str(tempValues)
temp_values['values'] = tempValues
temp_values['label'] = values['label']
values = temp_values
values['values'].sort()
temp[filter] = values
result = {'results': temp}
#result = 'Nothing to report'
return JsonResponse(result, safe=False)
except Exception as e:
return JsonResponse({'Error' : str(e)})
示例10: getAccounts
# 需要导入模块: from accounts.models import Account [as 别名]
# 或者: from accounts.models.Account import objects [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)})
示例11: saveSfdcOpportunitiesToMaster
# 需要导入模块: from accounts.models import Account [as 别名]
# 或者: from accounts.models.Account import objects [as 别名]
def saveSfdcOpportunitiesToMaster(user_id=None, company_id=None, job_id=None, run_type=None):
#job_id = ObjectId("56a3f89f8afb003c13a59e26")
if run_type == 'initial':
opps = TempData.objects(Q(company_id=company_id) & Q(record_type='opportunity') & Q(source_system='sfdc') & Q(job_id=job_id) ).only('source_record') #& Q(job_id=job_id)
contacts = TempData.objects(Q(company_id=company_id) & Q(record_type='contact') & Q(source_system='sfdc') & Q(job_id=job_id) ).only('source_record') #& Q(job_id=job_id)
else:
opps = TempDataDelta.objects(Q(company_id=company_id) & Q(record_type='opportunity') & Q(source_system='sfdc') & Q(job_id=job_id) ).only('source_record') #& Q(job_id=job_id)
contacts = TempDataDelta.objects(Q(company_id=company_id) & Q(record_type='contact') & Q(source_system='sfdc') & Q(job_id=job_id) ).only('source_record') #& Q(job_id=job_id)
oppListTemp = list(opps)
oppList = [i['source_record'] for i in oppListTemp]
contactListTemp = list(contacts)
contactList = [i['source_record'] for i in contactListTemp]
try:
allOpps = oppList #['records']
# below code copied from contacts.tasks
for newContact in contactList: #['records']:
if 'OpportunityContactRoles' not in newContact or newContact['OpportunityContactRoles'] is None: # if this contact has no opportunities
continue # move to next contact
# company_id = request.user.company_id
sfdc_account_id = None
thisLeadsOppsIds = newContact['OpportunityContactRoles']['records']
thisLeadsOpps = []
for opp in thisLeadsOppsIds: #loop through all the Opp records in the Contact record
print 'trying for opp with id ' + str(opp['OpportunityId'])
thisOpp = next((x for x in allOpps if x['Id'] == opp['OpportunityId']), None) # if this opp is found in the list of opps retrieved separately
if thisOpp is not None: # if found
print 'found this opp'
sfdc_account_id = thisOpp['AccountId']
thisLeadsOpps.append(thisOpp) #add it
sfdc_contact_Id = str(newContact['Id'])
print 'contact id is ' + sfdc_contact_Id
# sfdc_mkto_id = str(newLead['sfdcLeadId']) #check if there is a corresponding lead from MKTO
existingLeadMkto = None
existingLeadSfdc = None
existingLeadHspt = None
existingContact = Lead.objects(Q(company_id=company_id) & Q(sfdc_contact_id=sfdc_contact_Id)).first()
if existingContact is not None: # we found this contact already in the DB
print ' eC is not none'
if 'sfdc' not in existingContact.opportunities:
opportunities = {}
opportunities['sfdc'] = []
opportunities['sfdc'].extend(thisLeadsOpps)
existingContact.update(opportunities__sfdc = opportunities['sfdc'])
existingContact.update(sfdc_account_id = sfdc_account_id)
print 'just updated acct id 1'
else:
for newOpp in thisLeadsOpps:
print ' nefre get'
if not any (e.get('Id', None) == newOpp['Id'] for e in existingContact.opportunities['sfdc']): # does an opportunity with this Id already exist
opportunities = existingContact.opportunities['sfdc']
opportunities.append(newOpp)
existingContact.sfdc_account_id = sfdc_account_id
# save this opportunity
existingContact.update(opportunities__sfdc = opportunities)
existingContact.update(sfdc_account_id = sfdc_account_id)
print 'just updated acct id 2'
else: #this opp already exists
for i in range(len(existingContact.opportunities['sfdc'])):
if existingContact.opportunities['sfdc'][i]['Id'] == newOpp['Id']:
existingContact.opportunities['sfdc'][i] = newOpp
existingContact.sfdc_account_id = sfdc_account_id
existingContact.save()
print 'just updated acct id 3'
elif existingContact is None: # this lead does not exist
print ' eC is much none'
existingLeadSfdc = Lead.objects(Q(company_id=company_id) & Q(leads__sfdc__ConvertedContactId=sfdc_contact_Id)).first()
if existingLeadSfdc is not None:
if 'sfdc' not in existingLeadSfdc.opportunities:
opportunities = {}
opportunities['sfdc'] = []
opportunities['sfdc'].extend(thisLeadsOpps)
existingLeadSfdc.update(opportunities__sfdc = opportunities['sfdc'])
existingLeadSfdc.update(sfdc_account_id = sfdc_account_id)
print 'just updated acct id 4'
else:
for newOpp in thisLeadsOpps:
if not any (e.get('Id', None) == newOpp['Id'] for e in existingLeadSfdc.opportunities['sfdc']): # does an opportunity with this Id already exist
opportunities = existingLeadSfdc.opportunities['sfdc']
opportunities.append(newOpp)
# save this opportunity
existingLeadSfdc.update(opportunities__sfdc = opportunities)
existingLeadSfdc.update(sfdc_account_id = sfdc_account_id)
print 'just updated acct id 5'
else: #this opp already exists
for i in range(len(existingLeadSfdc.opportunities['sfdc'])):
if existingLeadSfdc.opportunities['sfdc'][i]['Id'] == newOpp['Id']:
existingLeadSfdc.opportunities['sfdc'][i] = newOpp
existingLeadSfdc.sfdc_account_id = sfdc_account_id
existingLeadSfdc.save()
print 'just updated acct id 6'
else:
existingLeadMkto = Lead.objects(Q(company_id=company_id) & Q(leads__mkto__sfdcContactId=sfdc_contact_Id)).first()
if existingLeadMkto is not None: # we found a MKto lead record which is matched to this new Sfdc lead
#.........这里部分代码省略.........