def handle(self, *args, **options): #items = Item.objects.filter(deleted=False, is_tagged=False) #Some items were not tagged the last time, so should not be tagged this time unless the amount #of topics has grown a significant amount. if len(args) > 0 and args[0] == 'video': #extend the mmseg dict by adding features in train_video_channel extend_dict_by_video_features() return recent_item_set = Item.objects.filter(deleted=False, is_tagged=True).order_by('-id') if recent_item_set: recent_item_id = recent_item_set[0].pk items = Item.objects.filter(pk__gt=recent_item_id, deleted=False, is_tagged=False) else: items = Item.objects.filter(deleted=False, is_tagged=False) tag_count = 0 for item in items: file = settings.MEDIA_ROOT + item.file sorted_terms = tag_html(file) if len(sorted_terms) > 10: range_len = 10 else: range_len = len(sorted_terms) is_tagged = False for i in xrange(range_len): topic_name = sorted_terms[i][0] if SensitiveWord.objects.filter(name=topic_name, disabled=False): continue topic_set = Topic.objects.filter(name=topic_name) if topic_set: try: is_tagged = True item_topic = Item_Topic(item=item, topic=topic_set[0]) #might duplicate with user generated one item_topic.save() tag_count += 1 except: continue else: #place in candidate topic candidate_topic_set = CandidateTopic.objects.filter(name=topic_name) if candidate_topic_set: candidate_topic = candidate_topic_set[0] else: language = 'en' for char in topic_name: if ord(char) >= 128: language = 'zh' break candidate_topic = CandidateTopic(name=topic_name, language=language) candidate_topic.count = candidate_topic.count + 1 candidate_topic.save() if is_tagged: item.is_tagged = True item.save() self.stdout.write('Tagged ' + str(len(items)) + ' items with ' + str(tag_count) + ' tag(s)\n')
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