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 '话题不存在'
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 '话题不存在'