Completed
Push — master ( 8e9dbb...d1603a )
by Andrew
24s
created

Command.find_old_entry()   A

Complexity

Conditions 4

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
dl 0
loc 6
rs 9.2
1
from django.core.management.base import BaseCommand
2
3
from chat.models import Message
4
from chat.settings import WEBRTC_CONNECTION
5
6
7
class Command(BaseCommand):
8
9
	def __init__(self):
10
		super(Command, self).__init__()
11
	help = 'Migrates info.json'
12
13
	def handle(self, *args, **options):
14
		import json
15
16
		with open('/home/andrew/python/djangochat/trash/a.json') as old_file:
17
			data_old = json.load(old_file)
18
19
		with open('/home/andrew/python/djangochat/chat/static/smileys/info.json') as new_file:
20
			data_new = json.load(new_file)
21
22
		def find_old_entry(alt):
23
			for k in data_old:
24
				for smile in data_old[k]:
25
					if data_old[k][smile]['text_alt'] == alt:
26
						return smile
27
			raise Exception("Not {} found")
28
29
		output = {}
30
31
		for k in data_new:
32
			for smile in data_new[k]:
33
				entry = find_old_entry(smile['alt'])
34
				output[str(entry.encode('utf8'))] = smile['code']
35
		messages = Message.objects.all()
36
		for mess in messages:
37
			output_content = ""
38
			for char in mess.content:
39
				sdfsd = str(char.encode('utf8'))
40
				get = output.get(sdfsd)
41
				if get is not None:
42
					output_content += get
43
				else:
44
					output_content += char
45
			if mess.content != output_content:
46
				mess.content = output_content
47
				mess.save(update_fields=["content"])
48
				print("Updating " + mess.id)
49