mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	Fixes #2665. Regenerated by tabbott with `lint --fix` after a rebase and change in parameters. Note from tabbott: In a few cases, this converts technical debt in the form of unsorted imports into different technical debt in the form of our largest files having very long, ugly import sequences at the start. I expect this change will increase pressure for us to split those files, which isn't a bad thing. Signed-off-by: Anders Kaseorg <anders@zulip.com>
		
			
				
	
	
		
			51 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			51 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
from typing import Dict, List
 | 
						|
 | 
						|
import ujson
 | 
						|
from django.db import migrations
 | 
						|
from django.db.backends.postgresql.schema import DatabaseSchemaEditor
 | 
						|
from django.db.migrations.state import StateApps
 | 
						|
 | 
						|
 | 
						|
def move_to_seperate_table(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
 | 
						|
    UserProfile = apps.get_model('zerver', 'UserProfile')
 | 
						|
    AlertWord = apps.get_model('zerver', 'AlertWord')
 | 
						|
 | 
						|
    for user_profile in UserProfile.objects.all():
 | 
						|
        list_of_words = ujson.loads(user_profile.alert_words)
 | 
						|
 | 
						|
        # Remove duplicates with our case-insensitive model.
 | 
						|
        word_dict: Dict[str, str] = {}
 | 
						|
        for word in list_of_words:
 | 
						|
            word_dict[word.lower()] = word
 | 
						|
 | 
						|
        AlertWord.objects.bulk_create(
 | 
						|
            AlertWord(user_profile=user_profile, word=word, realm=user_profile.realm)
 | 
						|
            for word in word_dict.values()
 | 
						|
        )
 | 
						|
 | 
						|
def move_back_to_user_profile(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
 | 
						|
    AlertWord = apps.get_model('zerver', 'AlertWord')
 | 
						|
    UserProfile = apps.get_model('zerver', 'UserProfile')
 | 
						|
 | 
						|
    user_ids_and_words = AlertWord.objects.all().values("user_profile_id", "word")
 | 
						|
    user_ids_with_words: Dict[int, List[str]] = dict()
 | 
						|
 | 
						|
    for id_and_word in user_ids_and_words:
 | 
						|
        user_ids_with_words.setdefault(id_and_word["user_profile_id"], [])
 | 
						|
        user_ids_with_words[id_and_word["user_profile_id"]].append(id_and_word["word"])
 | 
						|
 | 
						|
    for (user_id, words) in user_ids_with_words.items():
 | 
						|
        user_profile = UserProfile.objects.get(id=user_id)
 | 
						|
        user_profile.alert_words = ujson.dumps(words)
 | 
						|
        user_profile.save(update_fields=['alert_words'])
 | 
						|
 | 
						|
class Migration(migrations.Migration):
 | 
						|
 | 
						|
    dependencies = [
 | 
						|
        ('zerver', '0276_alertword'),
 | 
						|
    ]
 | 
						|
 | 
						|
    operations = [
 | 
						|
        migrations.RunPython(move_to_seperate_table, move_back_to_user_profile, elidable=True),
 | 
						|
    ]
 |