def get_queryset(self): status = self.kwargs['status'] company_id = self.request.user.get_company() print 'co id is ' + str(company_id) record_id = CompanyIntegration.objects(company_id = company_id ).only('id').first() existingIntegration = {} existingIntegration['integrations'] = {} map = Code("function () {" " for (var key in this.integrations) emit(key, this.integrations[key]['access_token']); } ") 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] = {'access_token': result.value} # for key in existingIntegration.integrations.keys(): # if 'metadata' in existingIntegration.integrations[key]: # existingIntegration.integrations[key]['metadata'] = {} #empty the metadata object so that excess data is not sent back # if 'metadata' in existingIntegration.integrations[key]: # existingIntegration.integrations[key]['metadata'] = {} #empty the metadata object so that excess data is not sent back if existingIntegration['integrations'] != {}: #assume there's onlg one entry if status == 'new': queryset = SuperIntegration.objects(code__nin=existingIntegration['integrations'].keys()) for obj in queryset: #company_info = CompanyGenericIntegrationSerializer() obj.company_info = {} elif status == 'existing': queryset = SuperIntegration.objects(code__in=existingIntegration['integrations'].keys()) for obj in queryset: #company_info = CompanyGenericIntegrationSerializer(existingIntegration.integrations[obj.code]) # print "code is " + str(existingIntegration.integrations[obj.code]) obj.company_info = existingIntegration['integrations'][obj.code] obj.company_info["code"] = obj.code obj.company_info['record_id'] = json.dumps(record_id['id'], default=json_util.default) return queryset # if we are here, there are no records for the company if status == 'new': print 'new sttatus' queryset_new = SuperIntegration.objects.all() elif status == 'existing': queryset_new = None return queryset_new
def filterTwInteractions(request, id): user_id = request.user.id company_id = request.user.company_id start_date = request.GET.get('start_date') end_date = request.GET.get('end_date') interaction_type = request.GET.get('interaction_type') series_type = request.GET.get('series_type') query_type = request.GET.get('query_type') page_number = int(request.GET.get('page_number')) items_per_page = int(request.GET.get('per_page')) system_type = request.GET.get('system_type') chart_name = request.GET.get('chart_name') offset = (page_number - 1) * items_per_page user_id = request.user.id company_id = request.user.company_id existingIntegration = CompanyIntegration.objects(company_id = company_id ).first() 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") elif code == 'bufr': result = filtertwInteractionsBufr(user_id=user_id, company_id=company_id, start_date=start_date, end_date=end_date, interaction_type=interaction_type, series_type=series_type, query_type=query_type, page_number=page_number, items_per_page=items_per_page, system_type=system_type, offset=offset, code=code, chart_name=chart_name) else: result = 'Nothing to report' return result except Exception as e: return JsonResponse({'Error' : str(e)})
def filterCampaignEmailEventsByType(request, id): user_id = request.user.id company_id = request.user.company_id start_date = request.GET.get('start_date') end_date = request.GET.get('end_date') event_type = request.GET.get('event_type') campaign_guid = request.GET.get('campaign_guid') email_id = request.GET.get('email_id') query_type = request.GET.get('query_type') page_number = int(request.GET.get('page_number')) items_per_page = int(request.GET.get('per_page')) system_type = request.GET.get('system_type') chart_name = request.GET.get('chart_name') export_type = request.GET.get('export_type') offset = (page_number - 1) * items_per_page print ' in filter 22' user_id = request.user.id company_id = request.user.company_id existingIntegration = CompanyIntegration.objects(company_id = company_id ).first() 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 client_secret = existingIntegration['integrations'][code]['client_secret'] print 'found code' + str(code) if code is None: raise ValueError("No integrations defined") elif code == 'mkto': pass elif code == 'sfdc': pass #result = filterLeadsSfdc(user_id=user_id, company_id=company_id, start_date=start_date, end_date=end_date, lead_type=lead_type, query_type=query_type, page_number=page_number, items_per_page=items_per_page, system_type=system_type, offset=offset, code=code) elif code == 'hspt': result = filterCampaignEmailEventsByTypeHspt(user_id=user_id, company_id=company_id, start_date=start_date, end_date=end_date, event_type=event_type, email_id=email_id, query_type=query_type, page_number=page_number, items_per_page=items_per_page, system_type=system_type, offset=offset, code=code, campaign_guid=campaign_guid, export_type=export_type) result['portal_id'] = client_secret else: result = 'Nothing to report' return JsonResponse(result, safe=False) except Exception as e: return JsonResponse({'Error' : str(e)})
def _get_system(company_id=None, system_type=None): '''Find the appropriate system code e.g. mkto or sfdc for a given system type e.g. ma or crm ''' if company_id is None or system_type is None: return None map = Code("function () {" " for (var key in this.integrations) emit(key, null); } ") reduce = Code("function (key, values) { return null; } ") results = CompanyIntegration.objects(company_id=company_id).map_reduce(map, reduce, "inline") results = list(results) systems = SuperIntegration.objects(system_type=system_type).only('code') systems = list(systems) for system in systems: for result in results: if result.key == system['code']: return system['code'] return None
def retrieveViews(request, company_id): start_date = request.GET.get('start_date') end_date = request.GET.get('end_date') view_name = request.GET.get('view_name') system_type = request.GET.get('system_type') user_id = request.user.id company_id = request.user.company_id existingIntegration = CompanyIntegration.objects(company_id = company_id ).first() try: #first see if generic view i.e. not dependent on a specific system generic_views = {'contacts': getLeads, 'campaigns': getCampaigns, 'accounts': getAccounts, 'opps': getOpportunities} if view_name in generic_views: result = generic_views[view_name](request, company_id) return result #assume that the view will convert to JSONResponse else: #if view is dependent on type of system 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") elif code == 'hspt': pass #result = retrieveHsptDashboards(user_id=user_id, company_id=company_id, start_date=start_date, end_date=end_date, dashboard_name=dashboard_name) elif code == 'mkto': pass #result = retrieveMktoDashboards(user_id=user_id, company_id=company_id, start_date=start_date, end_date=end_date, dashboard_name=dashboard_name) else: result = {'Error': 'No view found'} return JsonResponse(result, safe=False) except Exception as e: return JsonResponse({'Error' : str(e)})
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)})