Exemple #1
0
def act_addtopic(name, topic_id, status, user_id):
    '''add a Topic'''
    topic = Topic(
        name=name,
        category=Topic.objects.get(id=topic_id),
        status=status,
        creator=User.objects.get(id=user_id))
    topic.save()
    result = "OK, Topic:" + name + " added!"
    return result
    def test_search_feeds_and_topics(cls):
        """Topics exist - the search should search Topics and Feeds, and return a list of Feeds"""
        #init "videogames" Topic, with f1 and f2
        webcomics = Topic(name="webcomics", user=cls.user)
        webcomics.save()

        # Add NYT's Golf RSS to webcomics
        webcomics.feeds.add(cls.f5_m)

        #Topic and f2 and f4 fields both contain word "webcomic"
        #NYT, by virtue of being under "webcomic" is returned
        response = cls.client.post("/search/", {"searchString": "webcomic"})
        cls.assertEqual(response.status_code, 200)
        #cls.assertItemsEqual(response.data, [cls.f3, cls.f4, cls.f5])
        cls.assertTrue(cls.f3 in response.data)
        cls.assertTrue(cls.f4 in response.data)
        cls.assertTrue(cls.f5 in response.data)
    def setUpClass(cls):
        # Create User
        cls.user = User.objects.create_user(username="******",
                                            password="******")
        cls.user.save()
        cls.u_id = cls.user.id

        # Grab Uncategorized Topic
        cls.user_uncat = cls.user.topics.get(name="Uncategorized")

        # Create other topics
        cls.t1_m = Topic(name="sonnets", user=cls.user)
        cls.t2_m = Topic(name="tragedies", user=cls.user)
        cls.evil_t1_m = Topic(name="tragedies", user=cls.user)

        # Turn topics into JSON objects
        cls.t1_data = model_to_dict(cls.t1_m)
        cls.t2_data = model_to_dict(cls.t2_m)
        cls.evil_t1_data = model_to_dict(cls.evil_t1_m)
 def assign_topic(document, user, topic_name, topic_type=''):
     topic, topic_created = Topic.objects.get_or_create(
         slug=Topic.make_slug(topic_name),
         defaults={ 'preferred_name': topic_name,
                    'creator': user, 'last_updater': user })
     topic.type = topic_type
     topic.save()
     TopicAssignment.objects.create(
         content_object=document, topic=topic, creator=user)
     if topic_created:
         return 1
     else:
         return 0
    def setUpClass(cls):
        # Create User
        cls.user = User.objects.create_user(username="******",
                                            password="******")
        cls.user.save()
        cls.u_id = cls.user.id

        # Grab Uncategorized Topic
        cls.user_uncat = cls.user.topics.get(name="Uncategorized")

        # Create other topics
        cls.t1_m = Topic(name="sonnets", user=cls.user)
        cls.t1_m.save()
        cls.t1_id = cls.t1_m.id

        cls.t2_m = Topic(name="tragedies", user=cls.user)
        cls.t2_m.save()
        cls.t2_id = cls.t2_m.id

        cls.evil_t1_m = Topic(name="tragedies", user=cls.user)
        cls.evil_t1_id = 154  # shakespeare wrote this many sonnets! <- Be more subtle Lucia, let the reader figure it out

        # Turn topics into JSON objects
        cls.evil_t1_data = model_to_dict(cls.evil_t1_m)
Exemple #6
0
 def assign_topic(document, user, topic_name, topic_type=''):
     topic, topic_created = Topic.objects.get_or_create(
         slug=Topic.make_slug(topic_name),
         defaults={
             'preferred_name': topic_name,
             'creator': user,
             'last_updater': user
         })
     topic.type = topic_type
     topic.save()
     TopicAssignment.objects.create(content_object=document,
                                    topic=topic,
                                    creator=user)
     if topic_created:
         return 1
     else:
         return 0
Exemple #7
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