def deduplicate(cls, main_sent, ids, post_cmnt=False, dry=False): cls.dry = dry # merge cls.merge_comments(main_sent.id, ids) cls.update_merge('TagsSentences', main_sent.id, ids) cls.merge_links(main_sent.id, ids) cls.update_merge('SentencesSentencesLists', main_sent.id, ids) cls.update_merge('FavoritesUsers', main_sent.id, ids, 'favorite_id') cls.update_merge('SentenceAnnotations', main_sent.id, ids) cls.update_merge('SentenceAnnotations', main_sent.id, ids, 'meaning_id') cls.update_merge('UsersSentences', main_sent.id, ids) cls.update_merge('Transcriptions', main_sent.id, ids) # delete and log duplicates cls.delete_sents(main_sent.id, ids) # fix correctness if needed if cls.not_approved: if not cls.dry: main_sent.correctness = -1 main_sent.save() cls.log_entry(main_sent.id, [], 'update Sentences', 'update', 'correctness', [main_sent]) # post comment on duplicate sentences if needed if post_cmnt and not dry: comments = [] for id in ids: comments.append( SentenceComments( sentence_id=id, text= 'Please go to #{0}.\nThis sentence has been deleted because it was a duplicate.' .format(main_sent.id), user_id=cls.bot.id, created=now(), hidden=0, )) comments.append( SentenceComments( sentence_id=main_sent.id, text='Duplicates of this sentence have been deleted:\n' + \ '\n'.join(['x #%s' % (id) for id in ids]), user_id=cls.bot.id, created=now(), hidden=0 ) ) SentenceComments.objects.bulk_create(comments)
def sents(db, request): # no owner, no audio, no correctness 1-4 Sentences(text='Normal, not duplicated.', lang='eng', modified=datetime(2014, 1, 1)).save() for i in xrange(3): Sentences(text='Normal, duplicated.', lang='eng', modified=datetime(2014, 1, 1)).save() # has owner 5-8 Sentences(text='Has owner, not duplicated.', lang='eng', user_id=1, modified=datetime(2014, 1, 2)).save() for i in xrange(2): Sentences(text='Has owner, duplicated.', lang='eng', modified=datetime(2014, 1, 2)).save() Sentences(text='Has owner, duplicated.', lang='eng', user_id=1, modified=datetime(2014, 1, 2)).save() # has audio 9-12 Sentences(text='Has audio, not duplicated.', lang='eng', hasaudio='shtooka', modified=datetime(2014, 1, 3)).save() for i in xrange(2): Sentences(text='Has audio, duplicated.', lang='eng', modified=datetime(2014, 1, 3)).save() Sentences(text='Has audio, duplicated.', lang='eng', hasaudio='shtooka', modified=datetime(2014, 1, 3)).save() # correctness -1 13-16 Sentences(text='Correctness -1, not duplicated.', lang='eng', correctness=-1, modified=datetime(2014, 1, 4)).save() for i in xrange(2): Sentences(text='Correctness -1, duplicated.', lang='eng', modified=datetime(2014, 1, 4)).save() Sentences(text='Correctness -1, duplicated.', lang='eng', correctness=-1, modified=datetime(2014, 1, 4)).save() # has owner, has audio, correctness -1 17-21 Sentences(text='Has owner, Has audio, Correctness -1, not duplicated.', lang='eng', user_id=1, hasaudio='shtooka', correctness=-1, modified=datetime(2014, 1, 5)).save() Sentences(text='Has owner, Has audio, Correctness -1 duplicated.', lang='eng', modified=datetime(2014, 1, 5)).save() Sentences(text='Has owner, Has audio, Correctness -1 duplicated.', lang='eng', user_id=1, modified=datetime(2014, 1, 5)).save() Sentences(text='Has owner, Has audio, Correctness -1 duplicated.', lang='eng', hasaudio='shtooka', modified=datetime(2014, 1, 5)).save() Sentences(text='Has owner, Has audio, Correctness -1 duplicated.', lang='eng', correctness=-1, modified=datetime(2014, 1, 5)).save() for i in xrange(6, 8 + 1): SentenceComments(sentence_id=i, text='Comment on ' + str(i), user_id=1, created=datetime.now(), hidden=0).save() SentencesTranslations(sentence_id=6, translation_id=9, distance=1).save() SentencesTranslations(sentence_id=9, translation_id=6, distance=1).save() SentencesTranslations(sentence_id=7, translation_id=10, distance=1).save() SentencesTranslations(sentence_id=10, translation_id=7, distance=1).save() TagsSentences(tag_id=1, sentence_id=6, user_id=1, added_time=datetime.now()).save() TagsSentences(tag_id=2, sentence_id=7, user_id=1, added_time=datetime.now()).save() TagsSentences(tag_id=3, sentence_id=8, user_id=1, added_time=datetime.now()).save() SentencesSentencesLists(sentences_list_id=1, sentence_id=6).save() SentencesSentencesLists(sentences_list_id=2, sentence_id=7).save() SentencesSentencesLists(sentences_list_id=3, sentence_id=8).save() FavoritesUsers(user_id=1, favorite_id=6).save() FavoritesUsers(user_id=2, favorite_id=7).save() FavoritesUsers(user_id=3, favorite_id=8).save() SentenceAnnotations(meaning_id=1, text='', modified=datetime.now(), user_id=1, sentence_id=6).save() SentenceAnnotations(meaning_id=2, text='', modified=datetime.now(), user_id=1, sentence_id=7).save() SentenceAnnotations(meaning_id=3, text='', modified=datetime.now(), user_id=1, sentence_id=8).save() SentenceAnnotations(meaning_id=10, text='', modified=datetime.now(), user_id=1, sentence_id=13).save() SentenceAnnotations(meaning_id=11, text='', modified=datetime.now(), user_id=1, sentence_id=14).save() SentenceAnnotations(meaning_id=12, text='', modified=datetime.now(), user_id=1, sentence_id=15).save() Contributions(text='Logs for 6', action='update', user_id=1, datetime=datetime.now(), type='sentence', sentence_id=6).save() Contributions(text='Logs for 6', action='insert', user_id=1, datetime=datetime.now(), type='link', sentence_id=6, translation_id=9).save() Contributions(text='Logs for 7', action='insert', user_id=1, datetime=datetime.now(), type='sentence', sentence_id=7).save() Contributions(text='', action='insert', user_id=1, datetime=datetime.now(), type='sentence', sentence_id=8).save() Contributions(text='Unknown datetime record', action='update', user_id=1, datetime=None, type='sentence', sentence_id=8).save() Wall(owner=1, content='test post', date=datetime.utcnow(), title='', hidden=0, lft=1, rght=2).save() if request.config.option.mysql: def fin(): conn = connections['default'] def clean_up(model): Model = get_model('tatoeba2.' + model) Model.objects.all().delete() conn.cursor().execute('TRUNCATE TABLE ' + Model._meta.db_table + ';') conn.cursor().execute('ALTER TABLE ' + Model._meta.db_table + ' AUTO_INCREMENT = 1;') clean_up('Sentences') clean_up('SentencesTranslations') clean_up('SentenceComments') clean_up('TagsSentences') clean_up('SentencesSentencesLists') clean_up('FavoritesUsers') clean_up('Contributions') clean_up('Users') clean_up('Wall') clean_up('SentenceAnnotations') request.addfinalizer(fin)