示例#1
0
def edit_language_set_root_action(user_id, topic_id, root_id):
    """Add/Modify an Enlish topic as language root;No root can be added for English topic"""
    topic = Topic.objects.get(pk=topic_id)
    root = Topic.objects.get(pk=root_id)
    if topic.language == 'en' or root.language != 'en':
        return '只能由非英语话题指向英语话题'
    else:
        previous_root = topic.language_set_root
        topic.language_set_root = root
        topic.save()
        
        reference = None #Initialize
        if previous_root:
            refs = Topic_Revision.objects.filter(topic__id=topic_id, operation='ml').order_by('-pk')
            if refs:
                reference = refs[0]
                if reference.user_id != user_id:
                    notification = Notification(operation='ml')
                    notification.user_id = reference.user_id
                    notification.related_user_id = user_id
                    notification.topic_id = topic_id
                    notification.incr_count_in_profile()
                    notification.save()
                    update_user_topic_fame(topic_id, user_id, reference.user_id)
            else:
                reference = None
                update_user_topic_fame(topic_id, user_id)
            
        topic_revision = Topic_Revision(operation='ml', reference=reference)
        topic_revision.topic_id = topic_id
        if previous_root:
            topic_revision.previous_root = previous_root
        topic_revision.user_id = user_id
        topic_revision.save()
        return topic.pk
示例#2
0
def add_topic_alias_action(user_id, topic_id, topic_name, alias):
    """return topic_alias.id(long) if succeed, otherwise return error message"""
    if topic_name == alias:
        return '别名不能与话题名相同'
    if alias and SensitiveWord.objects.filter(name=alias, disabled=False):
        return '根据当地法律,此别名不能被添加'
    old_aliases = Topic_Alias.objects.filter(topic__id=topic_id, alias=alias)
    if old_aliases:
        return '此别名已经添加到此话题'
    else:
        topic_alias = Topic_Alias(alias=alias)
        topic_alias.topic_id = topic_id
        topic_alias.save()
    
        refs = Topic_Revision.objects.filter(topic__id=topic_id, operation='da', alias=topic_alias.alias).order_by('-pk')
        if refs:
            reference = refs[0]
            if reference.user_id != user_id:
                notification = Notification(operation='aa')
                notification.user_id = reference.user_id
                notification.related_user_id = user_id
                notification.topic_id = topic_id
                notification.incr_count_in_profile()
                notification.save()
                update_user_topic_fame(topic_id, user_id, reference.user_id)
        else:
            reference = None
            update_user_topic_fame(topic_id, user_id)
            
        topic_revision = Topic_Revision(operation='aa', reference=reference)
        topic_revision.topic_id = topic_id
        topic_revision.alias = topic_alias.alias
        topic_revision.user_id = user_id
        topic_revision.save()
        return topic_alias.id
示例#3
0
def delete_topic_alias_action(user_id, topic_id, alias):
    """ return topic_id(int) if succeed, otherwise return error message"""
    old_alias_set = Topic_Alias.objects.filter(topic__id=topic_id, alias=alias)
    if old_alias_set:
        old_alias = old_alias_set[0]
 
        refs = Topic_Revision.objects.filter(topic__id=topic_id, operation='aa', alias=old_alias.alias).order_by('-pk')
        if refs:
            reference = refs[0]
            if reference.user_id != user_id:
                notification = Notification(operation='da')
                notification.user_id = reference.user_id
                notification.related_user_id = user_id
                notification.topic_id = topic_id
                notification.incr_count_in_profile()
                notification.save()
                update_user_topic_fame(topic_id, user_id, reference.user_id)
        else:
            reference = None
            update_user_topic_fame(topic_id, user_id)
            
        topic_revision = Topic_Revision(operation='da', reference=reference)
        topic_revision.topic_id = topic_id
        topic_revision.alias = old_alias.alias
        topic_revision.user_id = user_id
        topic_revision.save()
        
        old_alias.delete()
        return topic_id
    else:
        return '此别名不存在' 
示例#4
0
def delete_topic_parent_action(user_id, topic_id, parent_id):
    """return 0(int) if succeeded, otherwise return error message"""
    topic_parent = Topic_Parent.objects.filter(topic__id=topic_id, parent__id=parent_id)
    topic_parent.delete()
    refs = Topic_Revision.objects.filter(topic__id=topic_id, parent__id=parent_id, operation='ap').order_by('-pk')
    if refs:
        reference = refs[0]
        if reference.user_id != user_id:
            notification = Notification(operation='dp')
            notification.user_id = reference.user_id
            notification.related_user_id = user_id
            notification.topic_id = topic_id
            notification.incr_count_in_profile()
            notification.save()
            update_user_topic_fame(topic_id, user_id, reference.user_id) 
    else:
        reference = None
        update_user_topic_fame(topic_id, user_id)
        
    topic_revision = Topic_Revision(operation='dp', reference=reference)
    topic_revision.topic_id = topic_id
    topic_revision.parent_id = parent_id
    topic_revision.user_id = user_id
    topic_revision.save()
    return 0
示例#5
0
def add_topic_parent_action(user_id, topic_id, parent_id=0, parent_name=''):
    """
    Return parent_id(long) if succeed, otherwise return error message
    @attention: Haoliang modified the return value
    """
    #if paernt_id is passed, we use it directly later on; Otherwise we need to add topic and get parent_id
    if not parent_id and not parent_name:
        return '上级话题不能为空'
    
    if parent_name:
        parent_set = Topic.objects.filter(name=parent_name)
        if parent_set and not parent_set[0].deleted:
            parent_id = parent_set[0].pk
        else:
            return_value = add_topic_action(user_id, topic_name=parent_name)
            if  return_value['error_message']:
                return return_value['error_message']
            else:
                parent_id = int(return_value['topic_id'])
        
    if topic_id == parent_id:
        return '不能将自身话题作为上级话题'
    
    topic_childs = Topic_Parent.objects.filter(parent__id=topic_id)
    for tc in topic_childs:
        if parent_id == tc.topic_id:
            return '不能将此话题下层话题作为上层话题'
    
    topic_parent = Topic_Parent.objects.filter(topic__id=topic_id, parent__id=parent_id)
    if topic_parent:
        return '请勿输入重复的父母话题'
    
    topic_parent = Topic_Parent()
    topic_parent.topic_id = topic_id
    topic_parent.parent_id = parent_id
    topic_parent.save()
    
    refs = Topic_Revision.objects.filter(topic__id=topic_id, parent__id=parent_id, operation='dp').order_by('-pk')
    if refs:
        reference = refs[0]
        if reference.user_id != user_id:
            notification = Notification(operation='ap')
            notification.user_id = reference.user_id
            notification.related_user_id = user_id
            notification.topic_id = topic_id
            notification.incr_count_in_profile()
            notification.save()
            update_user_topic_fame(topic_id, user_id, reference.user_id) 
    else:
        reference = None
        update_user_topic_fame(topic_id, user_id)
        
    topic_revision = Topic_Revision(operation='ap', reference=reference)
    topic_revision.topic_id = topic_id
    topic_revision.parent_id = parent_id
    topic_revision.user_id = user_id
    topic_revision.save()
    return parent_id
示例#6
0
def delete_topic_action(user_id, topic_id, comment=''):
    """"
    Args: 
       topic_id: integer
       comment: string
    Return:
       Success: 0(int)
       Failed: error_message(string)
    TODO: limit the privilege of deleting large topics to only admin or user with good reputation.
    """
    topic = Topic.objects.get(pk=topic_id)
    if topic:
        if topic.locked:
            return '此话题已被锁定,只有管理员可以更改'
        elif not topic.deleted:
            topic.deleted = True 
            topic.save()
            
            item_topic_set = Item_Topic.objects.filter(topic__id=topic.pk)
            if item_topic_set:
                # TODO how to improve efficiency of bulk updating
                for item_topic in item_topic_set:
                    item_topic.topic_deleted = True
                    item_topic.save()
            
            topic_parent_set = Topic_Parent.objects.filter(parent__id=topic.pk)
            if topic_parent_set:
                for topic_parent in topic_parent_set:
                    topic_parent.parent_deleted = True
                    topic_parent.save()
            
            refs = Topic_Revision.objects.filter(topic__id=topic.pk, operation='a').order_by('-pk')
            if refs:
                reference = refs[0]
                if reference.user_id != user_id:
                    notification = Notification(operation='d')
                    notification.user_id = reference.user_id
                    notification.related_user_id = user_id
                    notification.topic_id = topic_id
                    notification.incr_count_in_profile()
                    notification.save()
                    update_user_topic_fame(topic_id, user_id, reference.user_id)  
            else:
                reference = None
                update_user_topic_fame(topic_id, user_id)
                 
            topic_revision = Topic_Revision(operation='d', reference=reference, comment=comment)
            topic_revision.topic_id = topic.pk
            topic_revision.user_id = user_id
            topic_revision.save()
            return 0
        else:
            return '此话题已被删除'
    else:
        return '话题不存在'
示例#7
0
def unmerge_topic_action(user_id, topic_id, comment=''):
    """ return topic_id(int) if succeed, return error message if failed"""
    topic = Topic.objects.get(pk=topic_id)
    merged_to_id = topic.merged_to_id
    if topic:
        if topic.locked or topic.deleted:
            return '此话题已被锁定或被删除'
        elif topic.merged_to_id:
            topic.merged_to_id = None
            topic.save()
            
            #Note: If topic A is merged to me, me is merged to topic B, then A is merged to B
            #Now me unmerges from B, but let A stay merged to B
            item_topic_set = Item_Topic.objects.filter(topic_merged_from__id=topic_id)
            if item_topic_set:
                for item_topic in item_topic_set:
                    item_topic.topic_id = item_topic.topic_merged_from_id
                    item_topic.topic_merged_from_id = None
                    item_topic.save()
            
            topic_parent_set = Topic_Parent.objects.filter(parent_merged_from__id=topic_id)
            if topic_parent_set:
                for topic_parent in topic_parent_set:
                    topic_parent.parent_id = topic_parent.parent_merged_from_id
                    topic_parent.parent_merged_from_id = None
                    topic_parent.save()
            
            refs = Topic_Revision.objects.filter(topic__id=topic_id, operation='m').order_by('-pk')
            if refs:
                reference = refs[0]
                if reference.user_id != user_id:
                    notification = Notification(operation='um')
                    notification.user_id = reference.user_id
                    notification.related_user_id = user_id
                    notification.topic_id = topic_id
                    notification.incr_count_in_profile()
                    notification.save()
                    update_user_topic_fame(topic_id, user_id, reference.user_id) 
            else:
                reference = None
                update_user_topic_fame(topic_id, user_id)
                
            topic_revision = Topic_Revision(operation='um', reference=reference, comment=comment)
            topic_revision.topic_id = topic_id
            topic_revision.user_id = user_id
            topic_revision.merged_to_id = merged_to_id
            topic_revision.save()
            return topic_id       
        else:
            return '此话题没被合并'
    else:
        return '话题不存在'
示例#8
0
def add_topic_action(user_id, topic_id=0, topic_name='', comment=''):
    """
    Used by add_topic(indirecly used by add_item_topic) and add_topic_parent; either topic_id or topic_name is passed.
    if topic_id is passed, revert deleting this topic; \
    if topic_name is passed, add this topic.
    
    Return:
       #. Success: return_value['topic_id']   (long type)
       #. Failed: return_value['error_message']  (string)
    """
    return_value = {}
    return_value['error_message'] = ''
    
    if not topic_id and not topic_name.strip():
        return '话题名不能为空'
    
    if topic_name:
        #Get the topic by name since 1,the topic is deleted and not shown in query suggestion; 
        #2,Ajax is too slow to show it
        topic_set = Topic.objects.filter(name=topic_name)
        if topic_set:
            topic = topic_set[0]
        else:
            topic = None
    else:
        topic = Topic.objects.get(pk=topic_id)
        
    if topic:
        if topic.locked:
            return_value['error_message'] = '此话题已被锁定,只有管理员可以更改'
            return return_value
        elif topic.deleted: #undelete the topic
            topic.deleted = False
            topic.save()
            
            item_topic_set = Item_Topic.objects.filter(topic__id=topic.pk)
            if item_topic_set:            
                for item_topic in item_topic_set:
                    item_topic.topic_deleted = False
                    item_topic.save()
            
            topic_parent_set = Topic_Parent.objects.filter(parent__id=topic.pk)
            if topic_parent_set:
                for topic_parent in topic_parent_set:
                    topic_parent.parent_deleted = False
                    topic_parent.save()
            
            refs = Topic_Revision.objects.filter(topic__id=topic.pk, operation='d').order_by('-pk')
            if refs:
                reference = refs[0]
                if reference.user_id != user_id:
                    notification = Notification(operation='a')
                    notification.user_id = reference.user_id
                    notification.related_user_id = user_id
                    notification.topic_id = topic.pk
                    notification.incr_count_in_profile()
                    notification.save()
                    update_user_topic_fame(topic_id, user_id, reference.user_id) 
            else:
                reference = None
                update_user_topic_fame(topic_id, user_id) 
            
            topic_revision = Topic_Revision(operation='a', reference=reference, comment=comment)
            topic_revision.topic_id = topic.pk
            topic_revision.user_id = user_id
            topic_revision.save()
            return_value['topic_id'] = topic.pk
            return return_value
        else:
            return_value['error_message'] = '此话题已存在'
            return return_value
    elif topic_name: #The topic name does not exist, add brand new topic
        if len(topic_name) > 25: #Each Chinese character only counts 1 here
            return_value['error_message'] = '话题名字过长'
            return return_value
        topic_with_alias_set = Topic_Alias.objects.filter(alias=topic_name)
        if topic_with_alias_set:
            topic_with_alias = topic_with_alias_set[0]
            return_value['error_message'] = '此话题已作为话题' + topic_with_alias.topic.name.encode('utf-8') + '的别名存在'
            return return_value
        if SensitiveWord.objects.filter(name=topic_name, disabled=False):
            return_value['error_message'] = '根据当地法律,此话题不能被添加'
            return return_value
        topic = Topic(name=topic_name)
        #Automatically detects whether the language is in ASCII, if not, set to be Chinese
        #TODO:Need to add ability of detecting more languages
        is_English = True
        for char in topic_name:
            if ord(char) >= 128:
                is_English = False
                break
        if is_English:
            topic.language = 'en'
        else:
            topic.language = 'zh'
        topic.creator_id = user_id
        topic.follower_count = 1 #creator automatically follows this topic
        topic.save()
        user_topic = User_Topic()
        user_topic.user_id = user_id
        user_topic.topic_id = topic.pk
        user_topic.save()
        update_user_topic_fame(topic.pk, user_id) 
        topic_revision = Topic_Revision(operation='a')
        topic_revision.topic_id = topic.pk
        topic_revision.user_id = user_id
        topic_revision.save()
        return_value['topic_id'] = topic.pk
        return return_value
    else: #The passed in value is topic_id but it does not exist
        return_value['error_message'] = '此话题不存在,不能重新添加'
        return return_value
示例#9
0
def merge_topic_action(user_id, topic_id, merged_to_id, comment=''):
    """All args are integer; return topic_id(int) if succeded, otherwise return error message"""
    if topic_id == merged_to_id:
        return  '不能合并到自身话题'
    topic = Topic.objects.get(pk=topic_id)
    if topic:
        if topic.locked or topic.deleted:
            return '此话题已被锁定或被删除'
        elif not topic.merged_to_id:
            merge_to_topic = Topic.objects.get(pk=merged_to_id)
            if not merge_to_topic or merge_to_topic.deleted:
                return '要合并到的话题不存在或已被删除'          
            #Find the topic that should really be merged to;
            #Should not be normal flow since merged topic won't show up in query suggestion, just in case
            if merge_to_topic.merged_to_id: 
                merged_to_id = merge_to_topic.merged_to_id
            
            topic.merged_to_id = merged_to_id
            topic.save()
            
            #Modify the topics that are merged to me
            merge_to_me_topics = Topic.objects.filter(merged_to__id=topic_id)
            if merge_to_me_topics:
                for mt in merge_to_me_topics:
                    mt.merged_to_id = merged_to_id
                    mt.save()
            
            item_topic_set = Item_Topic.objects.filter(topic__id=topic_id)
            if item_topic_set:          
                for item_topic in item_topic_set:
                    existing_item_topic = Item_Topic.objects.filter(item__id=item_topic.item_id, topic__id=merged_to_id)
                    if not existing_item_topic:
                        item_topic.topic_merged_from_id = item_topic.topic_id
                        item_topic.topic_id = merged_to_id
                        item_topic.save()
                      
            topic_parent_set = Topic_Parent.objects.filter(parent__id=topic_id)
            if topic_parent_set:
                for topic_parent in topic_parent_set:
                    topic_parent.parent_merged_from_id = topic_parent.parent_id
                    topic_parent.parent_id = merged_to_id
                    topic_parent.save()
            
            refs = Topic_Revision.objects.filter(topic__id=topic_id, operation='um').order_by('-pk')
            if refs:
                reference = refs[0]
                if reference.user_id != user_id:
                    notification = Notification(operation='m')
                    notification.user_id = reference.user_id
                    notification.related_user_id = user_id
                    notification.topic_id = topic_id
                    notification.incr_count_in_profile()
                    notification.save()
                    update_user_topic_fame(topic_id, user_id, reference.user_id) 
            else:
                reference = None
                update_user_topic_fame(topic_id, user_id)
                
            topic_revision = Topic_Revision(operation='m', reference=reference, comment=comment)
            topic_revision.topic_id = topic_id
            topic_revision.user_id = user_id
            topic_revision.merged_to_id = merged_to_id
            topic_revision.save()
            return topic_id     
        else:
            return '此话题已被合并,不能重复操作'
    else:
        return '话题不存在'