Example #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