-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_data.py
61 lines (54 loc) · 1.65 KB
/
export_data.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
# export data form datea v2 to import it later in v3
import os
from django.contrib.auth.models import User
from datea.datea_action.models import DateaAction
import json
from django.core import serializers
def export_data():
os.system('./manage.py dumpdata > data.json')
f = open('data.json')
data = f.read()
data = json.loads(data)
f.close()
# get user passwords
pws = {}
for u in User.objects.all():
pws[u.id] = u.password
actions = {}
for a in DateaAction.objects.all():
actions[a.pk] = a
for i in range(len(data)):
if 'auth.user' == data[i]['model']:
data[i]['fields']['password'] = pws[int(data[i]['pk'])]
if 'datea_mapping.dateamapping' == data[i]['model']:
action = actions[int(data[i]['pk'])]
extra_fields = {
'user': {'id': action.user.id},
'name': action.name,
'slug': action.slug,
'published': action.published,
'created': action.created.isoformat(),
'modified': action.created.isoformat(),
'short_description': action.short_description,
'hashtag': action.hashtag,
'category': {'id': action.category.id},
'featured': action.featured,
'action_type': action.action_type,
'item_count': action.item_count,
'user_count': action.user_count,
'comment_count': action.comment_count,
'follow_count': action.follow_count,
}
if action.image:
extra_fields['image'] = {'id': action.image.id}
else:
extra_fields['image'] = None
if action.end_date:
extra_fields['end_date'] = action.end_date.isoformat()
else:
extra_fields['end_date'] = None
data[i]['fields'].update(extra_fields)
output = json.dumps(data)
f = open('data.json', 'w')
f.write(output)
f.close()