Exemplo n.º 1
0
def update_user_topic_fame(topic_id, current_user_id, added_score):
    """
    updates the *score* in User_Topic_Fame for the creator of an item that is tagged this topic
    """
    
    utf_current_set = User_Topic_Fame.objects.filter(user__pk=current_user_id, topic__pk=topic_id)
    if utf_current_set:
        utf_current = utf_current_set[0]
        utf_current.score = utf_current.score + added_score
        utf_current.save()
    else:
        utf_current = User_Topic_Fame()
        utf_current.topic_id = topic_id
        utf_current.user_id = current_user_id
        utf_current.score = added_score
        utf_current.save()
    
    return "success"
Exemplo n.º 2
0
def delete_item_topic_action(item_id, user_id, topic_id):
    """
    Delete topic from an item
    
    Args:
       #. item_id (long type)
       #. user_id (long type)
       #. topic_id (long type)
    Return:
       #. Success: item_topic_id (long type)
       #. Failed: error_message (string type)s
    
    """      
    item_topic = Item_Topic.objects.get(item__id=item_id, topic__id=topic_id)
    item_topic.deleted = True
    previous_user_id = item_topic.recent_user_id
    item_topic.recent_user_id = user_id
    item_topic.save()
    
    if previous_user_id: #Can be None if tagged by system    
        utf_previous = User_Topic_Fame.objects.get(user__pk=previous_user_id, topic__pk=topic_id)
        utf_previous.reverted_in_item_count = utf_previous.reverted_in_item_count + 1
        utf_previous.save()
    
    utf_current_set = User_Topic_Fame.objects.filter(user__pk=user_id, topic__pk=topic_id)
    if utf_current_set:
        utf_current = utf_current_set[0]
        utf_current.action_in_item_count = utf_current.action_in_item_count + 1
        utf_current.save()
    else:
        utf_current = User_Topic_Fame()
        utf_current.topic_id = topic_id  
        utf_current.user_id = user_id
        utf_current.action_in_item_count = 1
        utf_current.save()
    
    return item_topic.id
Exemplo n.º 3
0
def update_user_topic_fame(topic_id, current_user_id, previous_user_id=None):
    """
    Updates the topic_action_count and reverted_in_topic_count field in User_Topic_Fame
    for all the changes that are involved with topic management(not including add/delete item_topic)
    current_user is the one who did this operation, while previous user is the one whose operation gets reverted
    """
    if previous_user_id:        
        utf_previous = User_Topic_Fame.objects.get(user__pk=previous_user_id, topic__pk=topic_id)
        utf_previous.reverted_in_topic_count = utf_previous.reverted_in_topic_count + 1
        utf_previous.save()
    
    utf_current_set = User_Topic_Fame.objects.filter(user__pk=current_user_id, topic__pk=topic_id)
    if utf_current_set:
        utf_current = utf_current_set[0]
        utf_current. topic_action_count = utf_current.topic_action_count + 1
        utf_current.save()
    else:
        utf_current = User_Topic_Fame()
        utf_current.topic_id = topic_id
        utf_current.user_id = current_user_id
        utf_current.topic_action_count = 1
        utf_current.save()
    
    return "success"
Exemplo n.º 4
0
def add_item_topic_action(item_id, user_id, topic_id=0, topic_name=''):
    """
    Args:
       #. item_id (long type)
       #. user_id (long type)
       #. topic_id (long type, if it's null, then should use topic_name instead)
       #. topic_name (string type, if it's null, then should use topic_id instead)
    Return:
       #. Success: topic_id (long type) in case that topic id is required
       #. Failed: error_message (string type)s
    """
    #Topic name is only used if there is no topic found by query suggestion(or deleted)
    #A rare case could also occur if query suggestion is not quick enough
    if not topic_id and not topic_name:
        return '话题名字不能为空'

    if topic_name:
        #Ajax might not be able to respond in time, so user may enter a topic_name that already exists
        topic_set = Topic.objects.filter(name=topic_name)
        if topic_set:
            topic_id = topic_set[0].pk
        else:
            return_value = add_topic_action(user_id, topic_name=topic_name)
            if return_value.get('topic_id', None):
                topic_id = return_value.get('topic_id', None)
            else:
                return return_value['error_message']
    
    topic = Topic.objects.get(pk=topic_id)
    if topic.deleted or topic.merged_to_id:
        return '此话题已被删除或合并'    
    item_topic_set = Item_Topic.objects.filter(item__id=item_id, topic__id=topic_id)
    previous_user_id = None
    if item_topic_set:
        item_topic = item_topic_set[0]
        if item_topic.deleted == False:
            return '此话题已经添加这个网址'
        
        item_topic.deleted = False
        previous_user_id = item_topic.recent_user_id
    else:
        item_topic = Item_Topic()
        item_topic.item_id = item_id
        item_topic.topic_id = topic_id
    
    item_topic.recent_user_id = user_id
    item_topic.save()
    
    if previous_user_id:          
        utf_previous = User_Topic_Fame.objects.get(user__pk=previous_user_id, topic__pk=topic_id)
        utf_previous.reverted_in_item_count = utf_previous.reverted_in_item_count + 1
        utf_previous.save()
    
    utf_current_set = User_Topic_Fame.objects.filter(user__pk=user_id, topic__pk=topic_id)
    if utf_current_set:
        utf_current = utf_current_set[0]
        utf_current.action_in_item_count = utf_current.action_in_item_count + 1
        utf_current.save()
    else:
        utf_current = User_Topic_Fame()
        utf_current.topic_id = topic_id
        utf_current.user_id = user_id
        utf_current.action_in_item_count = 1
        utf_current.save()
        
    return topic_id #success