Created
October 22, 2016 09:54
-
-
Save slapec/566401c4fc1830616726eff1fd5aa98f to your computer and use it in GitHub Desktop.
dump and load script to migrate from the alpha version of wimm to the latest
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
# coding: utf-8 | |
import json | |
import argparse | |
import os | |
import re | |
from pathlib import Path | |
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "wimm.settings") | |
import django | |
django.setup() | |
from item.models import Item | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='Dumps data from the database of the alpha ' | |
'version wimm') | |
parser.add_argument('out', help='Output path', type=Path) | |
parser.add_argument('--meta-split', default=',', type=re.compile, | |
help='Split item meta by this regexp (default: %(default)s).' | |
'Trailing spaces are stripped.') | |
parser.add_argument('--name-split', default=',', type=re.compile, | |
help='Split item name by this regexp (default: %(default)s).' | |
'Trailing spaces are stripped.') | |
parser.add_argument('--keep-name', default=False, action='store_true', | |
help='Convert item name to a tag (default: %(default)s)') | |
args = parser.parse_args() | |
items = [] | |
for item in Item.objects.all().order_by('date'): | |
row = { | |
'date': item.date.isoformat(), | |
'price': str(item.price), | |
'tags': [_.strip() for _ in args.meta_split.split(item.meta) if _], | |
'created_at': item.created_at.isoformat() | |
} | |
if args.keep_name: | |
row['tags'] += [_.strip() for _ in args.name_split.split(item.name) if _] | |
items.append(row) | |
json.dump(items, args.out.open('w'), indent=4) | |
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
# coding: utf-8 | |
import json | |
import argparse | |
import os | |
from pathlib import Path | |
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "wimm.settings") | |
import django | |
django.setup() | |
from django.db import transaction | |
from item.models import Item | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='Loads data from the result json of the' | |
'dump.py script') | |
parser.add_argument('input', help='Input path', type=lambda x: Path(x).expanduser().resolve()) | |
args = parser.parse_args() | |
with transaction.atomic(): | |
Item._meta.get_field('created_at').auto_now_add = False | |
for i, item in enumerate(json.load(args.input.open())): | |
obj = Item.objects.create( | |
date=item['date'], | |
price=item['price'], | |
created_at=item['created_at'] | |
) | |
obj.tags.add(*item['tags']) | |
obj.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment