mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-03 21:43:21 +00:00 
			
		
		
		
	message: Add migrations to correct has_* fields.
This follows up on changes to correctly set has_link, has_attachment, and has_image when rendering messages. Fixes #12912.
This commit is contained in:
		
							
								
								
									
										53
									
								
								zerver/migrations/0257_fix_has_link_attribute.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								zerver/migrations/0257_fix_has_link_attribute.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,53 @@
 | 
			
		||||
# -*- coding: utf-8 -*-
 | 
			
		||||
# Generated by Django 1.11.24 on 2019-10-07 05:25
 | 
			
		||||
from __future__ import unicode_literals
 | 
			
		||||
 | 
			
		||||
from django.db import migrations
 | 
			
		||||
from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
 | 
			
		||||
from django.db.migrations.state import StateApps
 | 
			
		||||
 | 
			
		||||
import lxml
 | 
			
		||||
 | 
			
		||||
def fix_has_link(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
 | 
			
		||||
    Message = apps.get_model('zerver', 'Message')
 | 
			
		||||
    for message in Message.objects.all():
 | 
			
		||||
        # Because we maintain the Attachment table, this should be as
 | 
			
		||||
        # simple as just just checking if there's any Attachment
 | 
			
		||||
        # objects associated with this message.
 | 
			
		||||
        has_attachment = message.attachment_set.exists()
 | 
			
		||||
 | 
			
		||||
        # For has_link and has_image, we need to parse the messages.
 | 
			
		||||
        # Links are simple -- look for a link in the message.
 | 
			
		||||
        lxml_obj = lxml.html.fromstring(message.rendered_content)
 | 
			
		||||
        has_link = False
 | 
			
		||||
        for link in lxml_obj.xpath("//a"):
 | 
			
		||||
            has_link = True
 | 
			
		||||
            break
 | 
			
		||||
 | 
			
		||||
        # has_image refers to inline image previews, so we just check
 | 
			
		||||
        # for the relevant CSS class.
 | 
			
		||||
        has_image = False
 | 
			
		||||
        for img in lxml_obj.find_class("message_inline_image"):
 | 
			
		||||
            has_image = True
 | 
			
		||||
            break
 | 
			
		||||
 | 
			
		||||
        if (message.has_link == has_link and
 | 
			
		||||
                message.has_attachment == has_attachment and
 | 
			
		||||
                message.has_image == has_image):
 | 
			
		||||
            # No need to spend time with the database if there aren't changes.
 | 
			
		||||
            continue
 | 
			
		||||
        message.has_image = has_image
 | 
			
		||||
        message.has_link = has_link
 | 
			
		||||
        message.has_attachment = has_attachment
 | 
			
		||||
        message.save(update_fields=['has_link', 'has_attachment', 'has_image'])
 | 
			
		||||
 | 
			
		||||
class Migration(migrations.Migration):
 | 
			
		||||
 | 
			
		||||
    dependencies = [
 | 
			
		||||
        ('zerver', '0256_userprofile_stream_set_recipient_column_values'),
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    operations = [
 | 
			
		||||
        migrations.RunPython(fix_has_link,
 | 
			
		||||
                             reverse_code=migrations.RunPython.noop),
 | 
			
		||||
    ]
 | 
			
		||||
		Reference in New Issue
	
	Block a user