Created
November 7, 2014 16:42
-
-
Save christianplazas-wf/171a7aaff20cf48835ea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
from elements.element import ElementChanges | |
from elements.change_set import ElementChangeSet | |
# Step 1 | |
# Get ElementChanges entities for the | |
membership_key_str = '' | |
membership = db.get(membership_key_str) | |
ecs = ElementChanges.query(ancestor=ndb.Key('DraftGroup', membership.key().id())).fetch(100) | |
for ec in ecs: | |
print 'TemplateID: %s | Changes: %s' % (ec.key.pairs()[1][1], len(ec.changes)) | |
# Step 2 | |
# Get a list of TVE ElementChangeSets for the membership | |
ecss = base.fetch_entities(ElementChangeSet.gql('where membership=:1', membership)) | |
ecss_with_template = [] | |
for cs in ecss: | |
if '~' in cs.element_keyname: | |
ecss_with_template.append(cs) | |
print len(ecss_with_template) | |
# Step 3 | |
# Get the unique list of template ids represented in the TVE ElementChangeSets list | |
cs_elems_by_template = defaultdict(list) | |
for cs in ecss_with_template: | |
cs_elems_by_template[cs.element_keyname.split('~')[1]].append(cs.element_keyname) | |
print cs_elems_by_template.keys() | |
# Step 4 | |
# Remove any ElementChanges entities for template ids not in the | |
# TVE ElementChangeSets list | |
ecs_to_delete = [] | |
for ec in ecs: | |
if ec.key.pairs()[1][1] not in cs_elems_by_template.keys(): | |
ecs_to_delete.append(ec.key) | |
if ecs_to_delete: | |
print ecs_to_delete | |
ndb.delete_multi(ecs_to_delete) | |
# Step 5 Rebuild any remaining ElementChanges using the TVE ElementChangeSest list | |
ecs_to_save = [] | |
for ec in ecs: | |
templateid = ec.key.pairs()[1][1] | |
if templateid in cs_elems_by_template.keys() and len(ec.changes) != len(cs_elems_by_template[templateid]): | |
ec.changes = cs_elems_by_template[templateid] | |
ecs_to_save.append(ec) | |
if ecs_to_save: | |
print ecs_to_save | |
ndb.put_multi(ecs_to_save) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment