示例#1
0
文件: views.py 项目: woostersoz/3m
def _get_tweets_for_company(company, offset, items_per_page):
    tweets = Tweet.objects(company=company.id).skip(offset).limit(items_per_page)
    serializedList = TweetSerializer(tweets, many=True)
    totalCount = Tweet.objects(company=company.id).count()
    firstDate = Tweet.objects(company=company.id).order_by('updated_date').first()
    firstDateCreated = firstDate['updated_date']
    lastDate = Tweet.objects(company=company.id).order_by('-updated_date').first()
    lastDateCreated = lastDate['updated_date']
    return {'results' : serializedList.data, 'totalCount': totalCount, 'firstDateCreated': firstDateCreated, 'lastDateCreated': lastDateCreated}
示例#2
0
文件: views.py 项目: woostersoz/3m
 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 
示例#3
0
文件: views.py 项目: woostersoz/3m
 def delete(self, request, id=None, tweet_id=None): 
     try:
         company = Company.objects.filter(company_id=id).first()
         deleted = Tweet.objects(Q(company=company.id) & Q(id=tweet_id)).delete()
         if deleted == 1:
             return HttpResponse("Tweet deleted", status=status.HTTP_200_OK)
         else:
             return HttpResponse("Tweet  could not be deleted", status=status.HTTP_400_BAD_REQUEST)
     except Exception as e:
         return Response(str(e))
示例#4
0
文件: views.py 项目: woostersoz/3m
def get_tw_category_size(request, id):
    category_id = request.GET.get('category_id', None)
    if category_id is None:
        return JsonResponse({'Error': 'No category provided'})
    company_id = request.user.company_id
    company = Company.objects.filter(company_id=company_id).first()
    category = ObjectId(category_id)
    print 'categ is '+ str(category_id) + ' and company is ' + str(company_id)
    tweetCount = Tweet.objects(Q(company=company.id) & Q(category=category)).count()
    print 'tw count is ' + str(tweetCount)
    return JsonResponse({'category_count': tweetCount})
示例#5
0
文件: views.py 项目: woostersoz/3m
 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))
示例#6
0
文件: views.py 项目: woostersoz/3m
 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))
示例#7
0
文件: views.py 项目: woostersoz/3m
    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))