This repository has been archived by the owner on Oct 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfix_data.py
114 lines (86 loc) · 2.61 KB
/
fix_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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from datetime import datetime, timedelta
from functools import wraps
from itertools import islice
import makerbase
from makerbase.models import *
def for_class(*classes):
def do_that(fn):
@wraps(fn)
def do_for_class():
for cls in classes:
keys = cls.get_bucket().get_keys()
for key in keys:
obj = cls.get(key)
fn(obj)
return do_for_class
return do_that
@for_class(Maker)
def fix_maker_history(maker):
for histitem in maker.history:
# Fix the actions.
if histitem.action == 'create':
histitem.action = 'addmaker'
elif histitem.action == 'edit':
histitem.action = 'editmaker'
# Make sure the maker is tagged.
if histitem.maker is None:
histitem.add_link(maker, tag='maker')
histitem.save()
@for_class(Project)
def fix_project_history(project):
for histitem in project.history:
# Fix the actions.
if histitem.action == 'create':
histitem.action = 'addproject'
elif histitem.action == 'edit':
histitem.action = 'editproject'
# Make sure the project is tagged.
if histitem.project is None:
histitem.add_link(project, tag='project')
histitem.save()
@for_class(Project)
def save_all_projects(project):
project.save()
@for_class(Maker)
def save_all_makers(maker):
maker.save()
@for_class(Participation)
def link_party_history_to_party(party):
for histitem in party.history:
histitem.add_link(party, tag='participation')
histitem.save()
@for_class(Project, Maker)
def add_data_to_history(obj):
history = sorted(obj.history, key=lambda h: h.when, reverse=True)
histitems = list(islice(history, 1))
if not histitems:
return
newest_item = histitems[0]
if newest_item.action.endswith('party'):
new_data = newest_item.participation.get_entity_data()
else:
new_data = obj.get_entity_data()
newest_item.old_data = {}
newest_item.new_data = new_data
newest_item.save()
@for_class(Participation)
def remove_party_reasons(party):
try:
del party.reason
except AttributeError:
pass
else:
party.save()
@for_class(History)
def remove_party_reasons_from_history(histitem):
if not histitem.action.endswith('party'):
return
try:
del histitem.old_data['reason']
except (AttributeError, KeyError):
pass
try:
del histitem.new_data['reason']
except (AttributeError, KeyError):
pass
histitem.save()