def cal_average_vote(): """ """ start_time = now() - datetime.timedelta(days=AVERAGE_TIME) for topic in Topic.objects.all(): topic.link_average_votes = ( topic.topic_links.filter(is_visible=True) .filter(time__gt=start_time) .all() .aggregate(Avg("n_supporter"))["n_supporter__avg"] ) topic.discuss_average_votes = ( topic.topic_discusses.filter(is_visible=True) .filter(time__gt=start_time) .all() .aggregate(Avg("n_supporter"))["n_supporter__avg"] ) topic.comment_average_votes = cal_comment_avg(topic, start_time) if topic.link_average_votes == None: topic.link_average_votes = 0 if topic.discuss_average_votes == None: topic.discuss_average_votes = 0 topic.save() g = get_averages() g.link_average_votes = ( Link.objects.filter(is_visible=True) .filter(time__gt=start_time) .all() .aggregate(Avg("n_supporter"))["n_supporter__avg"] ) g.discuss_average_votes = ( Discuss.objects.filter(is_visible=True) .filter(time__gt=start_time) .all() .aggregate(Avg("n_supporter"))["n_supporter__avg"] ) g.comment_average_votes = ( Comment.objects.filter(is_visible=True) .filter(time__gt=start_time) .all() .aggregate(Avg("n_supporter"))["n_supporter__avg"] ) if g.link_average_votes == None: g.link_average_votes = 0 if g.discuss_average_votes == None: g.discuss_average_votes = 0 if g.comment_average_votes == None: g.comment_average_votes = 0 g.save()
def cal_average_vote(): ''' ''' start_time = now() - datetime.timedelta(days=AVERAGE_TIME) for topic in Topic.objects.all(): topic.link_average_votes = topic.topic_links.filter( is_visible=True).filter( time__gt=start_time).all( ).aggregate(Avg('n_supporter') )['n_supporter__avg'] topic.discuss_average_votes = topic.topic_discusses.filter( is_visible=True).filter( time__gt=start_time).all( ).aggregate(Avg('n_supporter') )['n_supporter__avg'] topic.comment_average_votes = cal_comment_avg(topic, start_time) if topic.link_average_votes == None: topic.link_average_votes = 0 if topic.discuss_average_votes == None: topic.discuss_average_votes = 0 topic.save() g = get_averages() g.link_average_votes = Link.objects.filter( is_visible=True).filter( time__gt=start_time).all( ).aggregate(Avg('n_supporter') )['n_supporter__avg'] g.discuss_average_votes = Discuss.objects.filter( is_visible=True).filter( time__gt=start_time).all( ).aggregate(Avg('n_supporter') )['n_supporter__avg'] g.comment_average_votes = Comment.objects.filter( is_visible=True).filter( time__gt=start_time).all( ).aggregate(Avg('n_supporter') )['n_supporter__avg'] if g.link_average_votes == None: g.link_average_votes = 0 if g.discuss_average_votes == None: g.discuss_average_votes = 0 if g.comment_average_votes == None: g.comment_average_votes = 0 g.save()
def vote_obj(user, obj): ''' ''' v = Vote.objects.filter(user=user, object_id=obj.id) if v: return v[0] if not isinstance(obj, Link) and \ not isinstance(obj, Discuss) and \ not isinstance(obj, Comment): return False if user == obj.user: return True data = obj.user.userdata data.honor += 1 now = timezone.now() if is_this_month(now, data.this_month_start_time): data.this_month_vote += 1 elif is_last_month(now, data.this_month_start_time): if not is_last2_month(now, data.last_month_start_time): data.last_month_half_votes = 0 data.this_month_vote = (data.this_month_vote - data.last_month_half_votes) / 2 data.last_month_half_votes = data.this_month_vote data.last_month_start_time = data.this_month_start_time data.this_month_start_time = now data.this_month_vote += 1 else: data.this_month_vote = 1 data.this_month_start_time = now data.save() if isinstance(obj, Link): for topic in obj.topics.all(): topic_ship_update(topic, obj.user, domain=obj.domain, is_vote=True) elif isinstance(obj, Discuss): for topic in obj.topics.all(): topic_ship_update(topic, obj.user, is_vote=True) obj.n_supporter += 1 if isinstance(obj, Link): average_sum = 0 for topic in obj.topics.all(): if FILTER: hot_filter(obj, topic, topic.link_average_votes, WAY_LINK_TOPIC_POST) average_sum += topic.link_average_votes count = obj.topics.count() if count: average = average_sum * 1.0 / count else: average = get_averages().link_average_votes if obj.n_supporter >= int(BOUTIQUE_RATE * average) + 1: if average != 0: obj.is_boutique = True elif isinstance(obj, Discuss): average_sum = 0 for topic in obj.topics.all(): if filter: hot_filter(obj, topic, topic.discuss_average_votes, WAY_DISCUSS_TOPIC_POST) average_sum += topic.discuss_average_votes count = obj.topics.count() if count: average = average_sum * 1.0 / count else: average = get_averages().discuss_average_votes if obj.n_supporter >= int(BOUTIQUE_RATE * average) + 1: if average != 0: obj.is_boutique = True else: average_sum = 0 for topic in obj.content_object.topics.all(): average_sum += topic.comment_average_votes count = obj.content_object.topics.count() if count: average = average_sum * 1.0 / count else: average = get_averages().comment_average_votes if obj.n_supporter >= int(BOUTIQUE_RATE * average) + 1: if average != 0: obj.is_boutique = True UserData.objects.filter(user=user).update(n_supports=F('n_supports') + 1) obj.save() vote = Vote.objects.create(user=user, content_object=obj) return vote
def vote_obj(user, obj): """ """ v = Vote.objects.filter(user=user, object_id=obj.id) if v: return v[0] if not isinstance(obj, Link) and not isinstance(obj, Discuss) and not isinstance(obj, Comment): return False if user == obj.user: return True data = obj.user.userdata data.honor += 1 now = timezone.now() if is_this_month(now, data.this_month_start_time): data.this_month_vote += 1 elif is_last_month(now, data.this_month_start_time): if not is_last2_month(now, data.last_month_start_time): data.last_month_half_votes = 0 data.this_month_vote = (data.this_month_vote - data.last_month_half_votes) / 2 data.last_month_half_votes = data.this_month_vote data.last_month_start_time = data.this_month_start_time data.this_month_start_time = now data.this_month_vote += 1 else: data.this_month_vote = 1 data.this_month_start_time = now data.save() if isinstance(obj, Link): for topic in obj.topics.all(): topic_ship_update(topic, obj.user, domain=obj.domain, is_vote=True) elif isinstance(obj, Discuss): for topic in obj.topics.all(): topic_ship_update(topic, obj.user, is_vote=True) obj.n_supporter += 1 if isinstance(obj, Link): average_sum = 0 for topic in obj.topics.all(): if FILTER: hot_filter(obj, topic, topic.link_average_votes, WAY_LINK_TOPIC_POST) average_sum += topic.link_average_votes count = obj.topics.count() if count: average = average_sum * 1.0 / count else: average = get_averages().link_average_votes if obj.n_supporter >= int(BOUTIQUE_RATE * average) + 1: if average != 0: obj.is_boutique = True elif isinstance(obj, Discuss): average_sum = 0 for topic in obj.topics.all(): if filter: hot_filter(obj, topic, topic.discuss_average_votes, WAY_DISCUSS_TOPIC_POST) average_sum += topic.discuss_average_votes count = obj.topics.count() if count: average = average_sum * 1.0 / count else: average = get_averages().discuss_average_votes if obj.n_supporter >= int(BOUTIQUE_RATE * average) + 1: if average != 0: obj.is_boutique = True else: average_sum = 0 for topic in obj.content_object.topics.all(): average_sum += topic.comment_average_votes count = obj.content_object.topics.count() if count: average = average_sum * 1.0 / count else: average = get_averages().comment_average_votes if obj.n_supporter >= int(BOUTIQUE_RATE * average) + 1: if average != 0: obj.is_boutique = True UserData.objects.filter(user=user).update(n_supports=F("n_supports") + 1) obj.save() vote = Vote.objects.create(user=user, content_object=obj) return vote