Completed
Push — master ( 0834f0...423d59 )
by Andrew
26s
created

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
# -*- coding: utf-8 -*-
2
# Generated by Django 1.9.12 on 2017-07-01 13:37
3
from __future__ import unicode_literals
4
5
from django.db import migrations
6
import json
7
8
def forwards(apps, schema_editor):
9
    ch_m = apps.all_models['chat']
10
    Message = ch_m['message']
11
12
    with open('old_smileys_info.json') as old_file:
13
        data_old = json.load(old_file)
14
15
    with open('chat/static/smileys/info.json') as new_file:
16
        data_new = json.load(new_file)
17
18
    def find_old_entry(alt):
19
        for k in data_old:
20
            for smile in data_old[k]:
21
                if data_old[k][smile]['text_alt'] == alt:
22
                    return smile
23
        raise Exception("Not {} found")
24
25
    output = {}
26
27
    for k in data_new:
28
        for smile in data_new[k]:
29
            entry = find_old_entry(smile['alt'])
30
            output[json.dumps(entry)] = smile['code']
31
    messages = Message.objects.all()
32
    updated_count = 0
33
    updated_smileys = 0
34
    for mess in messages:
35
        output_content = ""
36
        for char in mess.content:
37
            get = output.get(json.dumps(char))
38
            if get is not None:
39
                updated_smileys+=1
40
                output_content += get
41
            else:
42
                output_content += char
43
        if mess.content != output_content:
44
            mess.content = output_content
45
            mess.save(update_fields=["content"])
46
            updated_count += 1
47
    print("Updated " + str(updated_count) + " , total smielys: " + str(updated_smileys))
48
49
50
class Migration(migrations.Migration):
51
52
    dependencies = [('chat', '0005_add_symbol_20170707_1213'), ]
53
54
    operations = [
55
        migrations.RunPython(forwards, hints={'target_db': 'default'}),
56
    ]
57