Last active
August 29, 2015 14:18
-
-
Save robcmills/09ee79d18f126006216d to your computer and use it in GitHub Desktop.
Mezzanine site tree multi-tenancy data migration
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
# execfile('/Users/gogamedesign/newsite/website-bravo/misc/09ee79d18f126006216d/multi-tenancy-migration.py') | |
from gogame_app.models import * | |
from gogame_gallery.models import * | |
from mezzanine.blog.models import * | |
from django.contrib.sites.models import Site | |
from django.conf import settings | |
from django.contrib.redirects.models import Redirect | |
from mezzanine.pages.models import Page | |
from copy import copy | |
def copy_model(model, attrs={}): | |
model.pk = None | |
model.id = None | |
model.save() | |
for attr in attrs: | |
setattr(model, attr, attrs[attr]) | |
model.save() | |
return model | |
def copy_model_with_relations(model, foreign_keys={}): | |
# todo: implement | |
pass | |
def copy_page(page, parent=None, attrs={}): | |
'''copy page, children, and all related models recursively | |
and set provided attributes | |
''' | |
content_model = page.get_content_model() | |
children = page.children.all() | |
all_related = content_model._meta.get_all_related_objects() | |
# method is recursive, handle children separately | |
related_objects = [ | |
r for r in all_related if r.get_accessor_name() != 'children'] | |
related_models = [] | |
for related_object in related_objects: | |
related_manager = getattr( | |
content_model, related_object.get_accessor_name()) | |
for related_model in related_manager.all(): | |
related_models.append({ | |
'field_name': related_object.field.name, | |
'model': related_model | |
}) | |
page_copy = copy_model(content_model, attrs) | |
page_copy.set_parent(parent) | |
# hack to fix urls | |
if page_copy.slug[0] == '/': | |
page_copy.set_slug(page_copy.slug[1:]) | |
page_copy.save() | |
for r in related_models: | |
attrs_copy = copy(attrs) | |
attrs_copy.update({r['field_name']: page_copy}) | |
copy_model(r['model'], attrs_copy) | |
for child in children: | |
copy_page(child, page_copy, attrs) | |
## create eu site object | |
eu_site = Site(domain='thegogame.eu', name='The Go Game Europe') | |
eu_site.save() | |
# eu_site = Site.objects.get(domain='thegogame.eu') | |
# ensure current site is us | |
us_site = Site.objects.get(domain='thegogame.com') | |
settings.SITE_ID = us_site.id | |
## copy all root pages recursively and assign to eu site | |
root_pages = Page.objects.filter(parent=None) | |
for page in root_pages: | |
copy_page(page, None, {'site': eu_site}) | |
## Prune irrelevant pages and update misc routing | |
# switch to eu site | |
settings.SITE_ID = eu_site.id | |
# change home page to eu | |
home_page = Page.objects.get(slug='team-building-san-francisco') | |
home_page.set_slug('team-building-europe') | |
home_page.title = 'Team Building Europe | The Go Game' | |
home_page.save() | |
convention = Page.objects.get(slug='convention-team-building') | |
convention.delete() | |
# create new root redirect | |
root_redirect = Redirect(site=eu_site, old_path='/', | |
new_path='team-building') | |
root_redirect.save() | |
# create admin redirect | |
admin_redirect = Redirect(site=eu_site, old_path='/admin', | |
new_path='/admin/') | |
admin_redirect.save() | |
# BlogPosts are not connected to the gogame blog page | |
# by any relational field, so copy them separately here. | |
# Note that though relational fields exist on the BlogPost model, | |
# currently all BlogPost objects have no related models | |
# (ie no categories nor related_posts) so we can safely copy without. | |
# for post in BlogPost.objects.all(): | |
# copy_model(post, {'site': eu_site}) | |
### Locations Migration | |
# delete states | |
states = Area.objects.filter(is_domestic=True) | |
for state in states: | |
state.delete() | |
# create America page and make url link to us site | |
america = Area( | |
title='America', | |
name='America', | |
abbrev='us', | |
is_domestic=True, | |
icon='', | |
css_id='usa', | |
intro='Team-Building America', | |
slug='http://thegogame.com' | |
) | |
locations = Locations.objects.get(title='Locations') | |
america.set_parent(locations) | |
america.site = eu_site | |
america.save() | |
### Games Migration | |
# delete unsupported game types | |
''' | |
supported game types: | |
OUTDOOR | |
Classic Go Game | |
Spygame | |
Movie Game | |
Lawn Games | |
INDOOR | |
Convention Games | |
Music Video games | |
Game Show | |
Classic Go Game | |
''' | |
games = Game.objects.filter(title__icontains='lip dub') | |
for game in games: | |
game.delete() | |
games = Game.objects.filter(title__icontains='disaster preparedness') | |
for game in games: | |
game.delete() | |
games = Game.objects.filter(title__icontains='slow game') | |
for game in games: | |
game.delete() | |
games = Game.objects.filter(title__icontains='marketing') | |
for game in games: | |
game.delete() | |
games = Game.objects.filter(title__icontains='recess') | |
for game in games: | |
game.delete() | |
games = Game.objects.filter(title__icontains='festival') | |
for game in games: | |
game.delete() | |
games = Game.objects.filter(title__icontains='holiday') | |
for game in games: | |
game.delete() | |
training_games = Page.objects.get(title='Training Games') | |
training_games.delete() | |
# remove game producer job | |
jobs = Jobs.objects.first() | |
job = jobs.job_set.last() | |
jobs.job_set.remove(job) | |
jobs.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment