Example #1
0
 def update(self, request, id=None, category_id=None): 
     try:
         company = Company.objects.filter(company_id=id).first()
         category = CompanyTweetCategory.objects(Q(company=company.id) & Q(id=category_id)).first()
         if category is not None:
             data = json.loads(request.body)
             category.category_name = data.get('category_name', None)
             category.description = data.get('description', None)
             category.weight = data.get('weight', None)
             category.save()
             categories = CompanyTweetCategory.objects(company=company.id)
             serializedList = CompanyTweetCategorySerializer(categories, many=True)
             return Response(serializedList.data)
         else:
             return HttpResponse("Category could not be updated", status=status.HTTP_400_BAD_REQUEST)
     except Exception as e:
         return Response(str(e))
Example #2
0
 def list(self, request, id=None): 
     try:
         company = Company.objects.filter(company_id=id).first()
         categories = CompanyTweetCategory.objects(company=company.id)
         serializedList = CompanyTweetCategorySerializer(categories, many=True)
         return Response(serializedList.data)
     except Exception as e:
         return Response(str(e))
Example #3
0
 def delete(self, request, id=None, category_id=None): 
     try:
         company = Company.objects.filter(company_id=id).first()
         tweets = Tweet.objects(Q(company=company.id) & Q(category=category_id)).all()
         print 'twe are ' + str(list(tweets))
         if len(tweets) > 0:
             return HttpResponse("Category has dependent tweets", status=status.HTTP_400_BAD_REQUEST)
         deleted = CompanyTweetCategory.objects(Q(company=company.id) & Q(id=category_id)).delete()
         if deleted == 1:
             return HttpResponse("Category deleted", status=status.HTTP_200_OK)
         else:
             return HttpResponse("Category could not be deleted", status=status.HTTP_400_BAD_REQUEST)
     except Exception as e:
         return Response(str(e))
Example #4
0
 def create(self, request, id=None, tweet_id=None):
     page_number = int(request.GET.get('page_number'))
     items_per_page = int(request.GET.get('per_page'))
     offset = (page_number - 1) * items_per_page
     try:
         company = Company.objects.filter(company_id=id).first()
         data = json.loads(request.body)
         category = data.get('category', None)
         category_id = category['id']
         category = CompanyTweetCategory.objects(Q(company=company.id) & Q(id=category_id)).first()
         tweet = Tweet()
         tweet.company = company
         tweet.category = category
         tweet.text1 = data.get('text1', None)
         tweet.text2 = data.get('text2', None)
         tweet.text3 = data.get('text3', None)
         tweet.save()
         return JsonResponse(_get_tweets_for_company(company, offset, items_per_page))
     except Exception as e:
         return Response(str(e))
Example #5
0
 def update(self, request, id=None, tweet_id=None): 
     page_number = int(request.GET.get('page_number'))
     items_per_page = int(request.GET.get('per_page'))
     offset = (page_number - 1) * items_per_page
     try:
         company = Company.objects.filter(company_id=id).first()
         tweet = Tweet.objects(Q(company=company.id) & Q(id=tweet_id)).first()
         if tweet is not None:
             data = json.loads(request.body)
             category = data.get('category', None)
             category_id = category['id']
             category = CompanyTweetCategory.objects(Q(company=company.id) & Q(id=category_id)).first()
             tweet.category = category
             tweet.text1 = data.get('text1', None)
             tweet.text2 = data.get('text2', None)
             tweet.text3 = data.get('text3', None)
             tweet.save()
             return JsonResponse(_get_tweets_for_company(company, offset, items_per_page))
         else:
             return HttpResponse("Tweet could not be updated", status=status.HTTP_400_BAD_REQUEST)
     except Exception as e:
         return Response(str(e))
Example #6
0
    def list(self, request, id=None, masterlist_id=None):
        
        def _get_tweets(tweets_by_category, sorted_tweets_by_category):
            for category in sorted_tweets_by_category: # for each category in the list
                tweets = Tweet.objects(Q(company=company.id) & Q(category=category)) # get all the tweets
                tweets_list = list(tweets)
                tweets_by_category[category]['tweets'] = []
                remaining_tweets = tweets_by_category[category]['first_number']
                max_tweets = tweets_by_category[category]['max_number']
                for i in range(remaining_tweets):
                    if max_tweets == 0: # no more tweets left in this category so move to the next one
                        break
                    elif max_tweets == 1:
                        random_index = 0
                    else:
                        random_index = randint(0, max_tweets - 1)
                    random_text_index = randint(1,3) # to pick from one othe three options of the text
                    selected_tweet = tweets_list.pop(random_index)
                    #for j in range(1, 4):
                    fieldname = 'text' + str(random_text_index)
                        #if j != random_text_index:
                            #tweet[fieldname] = ''
                    tweet = {}
                    tweet['tweet_id'] = str(selected_tweet.id)
                    tweet['text'] = selected_tweet[fieldname]
                    tweet['version'] = random_text_index
                    tweet['category_id'] = str(category)
                    tweet['category_name'] = tweets_by_category[category]['name']
                    tweets_by_category[category]['tweets'].append(tweet)
                    remaining_tweets -= 1
                    max_tweets -= 1
            #print str(tweets_by_category)
            return tweets_by_category 
            
        try:
            select_by_category = False
            selected_category = request.GET.get('category', None)
            selected_count = request.GET.get('count', None) #how many tweets from selected category
            if selected_category is not None: #the request is to select tweets of a specific category
                select_by_category = True
             
            company = Company.objects.filter(company_id=id).first()
            categories = CompanyTweetCategory.objects(company=company.id)
            if select_by_category: 
                if selected_category == 'Undefined': #If category is "Undefined", it's the first call so pick a random category
                    random_index = randint(0, len(categories) - 1) #pick a random category
                    print 'rand us ' + str(random_index)
                    categories = CompanyTweetCategory.objects(company=company.id).limit(-1).skip(random_index)
                    print 'categ' + str(categories)
                else:
                    categories = CompanyTweetCategory.objects(Q(company=company.id) & Q(id=ObjectId(selected_category)))
                for category in list(categories):
                    print 'category is ' + str(category)
                    category.weight = 100
            tweets_by_category = {}
            total_weight = 0
            master_list_tweets = {}
            master_list_tweets_number = 20 # change to variable later

            for category in categories:
                total_weight += category.weight
                tweets_by_category[str(category.id)] = {}
                tweets_by_category[str(category.id)]['weight'] = category.weight / 100 #self._round_down(category.weight, 10) / 100 # round down the weight to the nearest 10
                tweets_by_category[str(category.id)]['name'] = category.category_name
                
            if total_weight != 100:
                return HttpResponse("Category weights need to total up to 100", status=status.HTTP_400_BAD_REQUEST) 
            
            possible_tweets = 0 #how many tweets can we get in the first pass thru?
            #first pass based on inputs provided 
            for category in tweets_by_category.keys(): #loop through each category and select the tweets
                if selected_count == 'Undefined' or selected_count is None:
                    tweets_by_category[category]['max_number'] = Tweet.objects(Q(company=company.id) & Q(category=category)).count()
                else:
                    tweets_by_category[category]['max_number'] = int(selected_count)
                print 'got tweets ' + str(tweets_by_category[category]['max_number'])
                tweets_by_category[category]['first_number'] = math.trunc( tweets_by_category[category]['weight'] * tweets_by_category[category]['max_number'] )
                possible_tweets += tweets_by_category[category]['first_number'] 
                print ' for category ' + str(category) + ' we have ' + str(tweets_by_category[category]['first_number'])
                #starting_point = random.randrange(0, )
            
            #first pass is completed - see if we have enough tweets else pick randomly
            print 'total tw ' + str(possible_tweets)
            sorted_tweets_by_category = sorted(tweets_by_category, key=lambda category: tweets_by_category[category]['weight'],  reverse=True)
            if possible_tweets >= master_list_tweets_number or select_by_category: # we have hit our target, so pick the tweets and off we go
                tweets_by_category = _get_tweets(tweets_by_category, sorted_tweets_by_category)
            else: #we have not yet hit the number so backfill
                for category in sorted_tweets_by_category:  
                    this_category_possible = tweets_by_category[category]['max_number'] - tweets_by_category[category]['first_number']
                    possible_tweets += this_category_possible
                    if possible_tweets >= master_list_tweets_number:
                        tweets_by_category[category]['first_number'] += this_category_possible - ( possible_tweets - master_list_tweets_number )# we may have gone above the max so bring back to max
                        print 'final 1 ' + str(tweets_by_category[category]['first_number'])
                        tweets_by_category = _get_tweets(tweets_by_category, sorted_tweets_by_category)
                        break # we are done
                    else:
                        tweets_by_category[category]['first_number'] = tweets_by_category[category]['max_number'] # we havent hit the max yet so just swap the numbers here 
                        print 'final 2 ' + str(tweets_by_category[category]['first_number'])
                if possible_tweets < master_list_tweets_number:
                    return HttpResponse("Insufficient number of tweets", status=status.HTTP_400_BAD_REQUEST)
            
            results = []
            for category in tweets_by_category.keys():
                for obj in tweets_by_category[category]['tweets']:
                    results.append(obj)
            #print ' results ' + str(results)
            
            return Response(results) 
        except Exception as e:
            return Response(str(e))