forked from proyectosdeley/proyectos_de_ley
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate_db.py
88 lines (70 loc) · 2.27 KB
/
migrate_db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import dataset
import datetime
import os
import unicodedata
def convert_name_to_slug(name):
"""Takes a congresista name and returns its slug."""
name = name.replace(",", "").lower()
name = name.split(" ")
if len(name) > 2:
i = 0
slug = ""
while i < 3:
slug += name[i]
if i < 2:
slug += "_"
i += 1
slug = unicodedata.normalize('NFKD', slug).encode('ascii', 'ignore')
slug = str(slug, encoding="utf-8")
return slug + "/"
old_db = os.path.join("..", "leyes.db")
new_db = "leyes_sqlite3.db"
db = dataset.connect("sqlite:///" + old_db)
res = db.query("select * from proyectos")
new_items = []
slugs = [] # translation table between name an URL
for i in res:
timestamp = datetime.datetime.fromtimestamp(i['timestamp'])
i['time_created'] = timestamp
i['time_edited'] = timestamp
try:
fecha_presentacion = datetime.datetime.strptime(
i['fecha_presentacion'],
'%d/%m/%Y',
)
except ValueError:
fecha_presentacion = datetime.datetime.strptime(
i['fecha_presentacion'],
'%d/%m/%y',
)
fecha_presentacion = datetime.datetime.date(fecha_presentacion)
i['fecha_presentacion'] = fecha_presentacion
i['expediente'] = i['link_to_pdf']
if i['pdf_url'] is None:
i['pdf_url'] = ''
if i['seguimiento_page'] is None:
i['seguimiento_page'] = ''
del i['link_to_pdf']
del i['timestamp']
del i['id']
del i['link']
congresistas = i['congresistas'].split(';')
for congre in congresistas:
congre = congre.strip()
obj = dict(nombre=congre)
if congre is not None and congre.strip() != '':
congre_slug = convert_name_to_slug(congre)
obj['slug'] = congre_slug
if obj not in slugs and congre_slug is not None:
slugs.append(obj)
new_items.append(i)
db = dataset.connect("sqlite:///" + new_db)
table = db['pdl_proyecto']
table.insert_many(new_items)
table = db['pdl_slug']
table.insert_many(slugs)
# fix domain from example.com to proyectosdeley.pe
table = db['django_site']
table.update(dict(id=1, domain='proyectosdeley.pe', name='proyectosdeley.pe'),
['id']
)