def publishMl(request, id, masterlist_id): try: company = Company.objects.filter(company_id=id).first() ml = TweetMasterList.objects(Q(company=company.id) & Q(id=masterlist_id)).first() if ml is None: return HttpResponse("Could not find the Master List for publishing", status=status.HTTP_400_BAD_REQUEST) tweets = ml['tweets'] shuffle(tweets) #buffer_profile_id = '558f150b7409ab382f11a39e' #change to parameter later existingIntegration = CompanyIntegration.objects(company_id = id ).first() if 'bufr' in existingIntegration['integrations']: # if Buffer is present and configured client_id = existingIntegration['integrations']['bufr']['client_id'] client_secret = existingIntegration['integrations']['bufr']['client_secret'] access_token = existingIntegration['integrations']['bufr']['access_token'] buffer = Buffer() api = Buffer.get_api(buffer, client_id=client_id, client_secret=client_secret, access_token=access_token) #profile = Buffer.get_twitter_profile(buffer, api) profile = Buffer.get_twitter_profile_by_id(buffer, api, ml.buffer_profile_id) #print 'posting to profile ' + profile['id'] for tweet in tweets: try: post = quote_plus(tweet['text']) Buffer.post_to_twitter(buffer, api, post, profile) except Exception as e: continue TweetMasterList.objects(Q(company=company.id) & Q(id=masterlist_id)).update(published=True) TweetMasterList.objects(Q(company=company.id) & Q(id=masterlist_id)).update(published_date=datetime.utcnow()) return HttpResponse("Tweets posted", status=status.HTTP_200_OK) else: return HttpResponse("No publishing integration found", status=status.HTTP_400_BAD_REQUEST) except Exception as e: return JsonResponse({'Error' : str(e)})
def list(self, request, id=None): try: company = Company.objects.filter(company_id=id).first() tw_mls = TweetMasterList.objects(company=company.id) serializedList = TweetMasterListSerializer(tw_mls, many=True) totalCount = TweetMasterList.objects(company=company.id).count() numPublished = TweetMasterList.objects(Q(company=company.id) & Q(published=True)).count() tw_mls_list = list(tw_mls) totalTwCount = 0 for tw_ml in tw_mls_list: totalTwCount+= len(tw_ml.tweets) return JsonResponse({'results' : serializedList.data, 'totalCount': totalCount, 'numPublished': numPublished, 'totalTwCount' : totalTwCount}) except Exception as e: return Response(str(e))