Ejemplo n.º 1
0
def get_stories_from_story_id_list(story_id_list):
    """
    Get stories from story ID list
    Parameter: story ID list
    """
    if not isinstance(story_id_list, list):
        return None

    stories = []

    for story_id in story_id_list:
        story = cache.get("story:" + str(story_id))

        if story == None:
            story = redis.update_story_cache(story_id)

        story_hits = cache.get("story:" + str(story_id) + ":hits")

        if story_hits == None:
            story_hits = redis.update_hits_value_cache(story_id)

        story["hits"] += int(story_hits)

        stories.append(story)

    return stories
Ejemplo n.º 2
0
def close_inactive_story_for_few_days():
    """
    Close story if story is not updated for few days
    Period: once a day
    """
    stories = Stories.objects.filter(
        state='processing', 
        updated_at__lt=timezone.now() - timedelta(days=INACTIVATE_DAYS_TO_CLOSE_STORY)
    ).exclude(id=25)

    for story in stories:
        story.state = 'closed'
        story.save()
        
        redis.update_story_cache(story.id, 'state', 'closed')
        redis.update_story_cache(story.id, 'closing_vote_opened', False)
        
        # Push notification to story contributors and subscribers
        push_notification_to_user(story, 'closed', story.title, None, None, 0)

    return None
Ejemplo n.º 3
0
def check_closing_vote_for_story():
    """
    Check closing vote for story and close story if agreement ratio exceeds specific value
    Period: every hour on the hour
    """
    closing_votes = ClosingVotes.objects.filter(closed=False, due__lte=timezone.now())

    for closing_vote in closing_votes:
        story = closing_vote.story
        
        # Story already closed
        if story.state == 'closed':
            closing_vote.closed = True
            closing_vote.save()
            
            redis.update_story_cache(story.id, 'state', 'closed')
        # Succeed to close story
        elif utilities.get_percentage_value(closing_vote.agreement_count, 
            closing_vote.disagreement_count) >= MIN_AGREEMENT_RATIO_TO_CLOSE_STORY:
            story.state = 'closed'
            story.save()
        
            redis.update_story_cache(story.id, 'state', 'closed')
            
            # Push notification to story contributors and subscribers
            push_notification_to_user(story, 'closed', story.title, None, None, 0)
        # Failed to close story
        else:
            closing_vote.closed = True
            closing_vote.save()
            
        redis.update_story_cache(story.id, 'closing_vote_opened', False)

    return None
Ejemplo n.º 4
0
def update_nickname_of_user_wrote_contents_cache(user_obj):
    """
    Update nickname of stories, comments, replies cache that user wrote
    Parameter: user object
    """
    if not isinstance(user_obj, Users):
        return None

    story_id_list = api.get_id_list_of_user_wrote_stories(user_obj, None, None)

    for story_id in story_id_list:                               
        redis.update_story_cache(story_id, 'author_nickname', user_obj.nickname) 

    comment_id_list = api.get_id_list_of_user_wrote_comments(user_obj, None, None)

    for comment_id in comment_id_list:
        redis.update_comment_cache(comment_id, 'author_nickname', user_obj.nickname)

    reply_id_list = api.get_id_list_of_user_wrote_replies(user_obj, None, None)

    for reply_id in reply_id_list:
        redis.update_reply_cache(reply_id, 'author_nickname', user_obj.nickname)

    return None