Skip to content

Commit

Permalink
Merge pull request #311 from domino14/feature/308/new_lexica
Browse files Browse the repository at this point in the history
fix an auto_now issue with SavedList and the migration
  • Loading branch information
domino14 authored Feb 16, 2019
2 parents 6a6e901 + 70765c3 commit efc52a3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
43 changes: 43 additions & 0 deletions djAerolith/base/management/commands/fix_savelist_dates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
I made a mistake migrating all lists to new lexicon as it updates
the saved date on these. Bring them back to what they used to be.
"""
import pickle

from django.core.management.base import BaseCommand, CommandError
from base.models import WordList


def turn_off_auto_now(ModelClass, field_name):
def auto_now_off(field):
field.auto_now = False
do_to_model(ModelClass, field_name, auto_now_off)


def do_to_model(ModelClass, field_name, func):
field = ModelClass._meta.get_field(field_name)
func(field)


class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('pickle_file', type=str)

def handle(self, *args, **options):
if 'pickle_file' not in options:
raise CommandError('You must pass in pickle_file with old dates')

dates_obj = pickle.load(open(options['pickle_file'], 'rb'))

turn_off_auto_now(WordList, 'lastSaved')

for list_id, last_saved in dates_obj.items():
try:
wl = WordList.objects.get(pk=list_id)
except WordList.DoesNotExist:
print(f'word list with id {list_id} no longer exists')
continue

wl.lastSaved = last_saved
wl.save()
13 changes: 13 additions & 0 deletions djAerolith/base/management/commands/migrate_list_lexica.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@
from base.models import Lexicon, SavedList


def turn_off_auto_now(ModelClass, field_name):
def auto_now_off(field):
field.auto_now = False
do_to_model(ModelClass, field_name, auto_now_off)


def do_to_model(ModelClass, field_name, func):
field = ModelClass._meta.get_field(field_name)
func(field)


class Command(BaseCommand):

def add_arguments(self, parser):
Expand All @@ -23,6 +34,8 @@ def handle(self, *args, **options):
raise CommandError(e)

count = 0
turn_off_auto_now(SavedList, 'lastSaved')

for word_list in SavedList.objects.filter(lexicon=lex1,
is_temporary=False):
count += 1
Expand Down

0 comments on commit efc52a3

Please sign in to comment.