mirror of
https://gitlab.crans.org/mediatek/med.git
synced 2025-02-24 21:41:21 +00:00
Compare commits
3 Commits
6789b9e3ac
...
ae0d1a080e
Author | SHA1 | Date | |
---|---|---|---|
ae0d1a080e | |||
1e6e033cdd | |||
d0805ebe8a |
@ -23,7 +23,11 @@ class AuthorAdmin(VersionAdmin):
|
||||
class BorrowableAdmin(PolymorphicParentModelAdmin):
|
||||
search_fields = ('title',)
|
||||
child_models = (CD, Comic, Manga, Novel, Review, Vinyl,)
|
||||
show_in_index = False
|
||||
|
||||
def get_model_perms(self, request):
|
||||
# We don't want that the borrowable items appear directly in
|
||||
# main menu, but we still want search borrowable items.
|
||||
return {}
|
||||
|
||||
|
||||
class MediumAdmin(VersionAdmin, PolymorphicChildModelAdmin):
|
||||
|
128
media/management/commands/migrate_to_new_format.py
Normal file
128
media/management/commands/migrate_to_new_format.py
Normal file
@ -0,0 +1,128 @@
|
||||
from django.core.management import BaseCommand
|
||||
from django.db import transaction
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from tqdm import tqdm
|
||||
|
||||
from media.models import CD, Comic, Game, Manga, Novel, Review, Vinyl, \
|
||||
OldCD, OldComic, OldGame, OldManga, OldNovel, OldReview, OldVinyl
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
"""
|
||||
Convert old format into new format
|
||||
"""
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--doit', action='store_true',
|
||||
help="Actually do the mogration.")
|
||||
|
||||
@transaction.atomic
|
||||
def handle(self, *args, **options):
|
||||
# Migrate books
|
||||
for old_book_class, book_class in [(OldComic, Comic),
|
||||
(OldManga, Manga),
|
||||
(OldNovel, Novel)]:
|
||||
name = book_class._meta.verbose_name
|
||||
name_plural = book_class._meta.verbose_name_plural
|
||||
for book in tqdm(old_book_class.objects.all(),
|
||||
desc=name_plural, unit=str(name)):
|
||||
try:
|
||||
new_book = book_class.objects.create(
|
||||
isbn=book.isbn,
|
||||
title=book.title,
|
||||
subtitle=book.subtitle,
|
||||
external_url=book.external_url,
|
||||
side_identifier=book.side_identifier,
|
||||
number_of_pages=book.number_of_pages,
|
||||
publish_date=book.publish_date,
|
||||
present=book.present,
|
||||
)
|
||||
new_book.authors.set(book.authors.all())
|
||||
new_book.save()
|
||||
except:
|
||||
self.stderr.write(f"There was an error with {name} "
|
||||
f"{book} ({book.pk})")
|
||||
raise
|
||||
|
||||
self.stdout.write(f"{book_class.objects.count()} {name_plural} "
|
||||
"migrated")
|
||||
|
||||
# Migrate CDs
|
||||
for cd in tqdm(OldCD.objects.all(),
|
||||
desc=_("CDs"), unit=str(_("CD"))):
|
||||
try:
|
||||
new_cd = CD.objects.create(
|
||||
title=cd.title,
|
||||
present=cd.present,
|
||||
)
|
||||
new_cd.authors.set(cd.authors.all())
|
||||
new_cd.save()
|
||||
except:
|
||||
self.stderr.write(f"There was an error with {cd} ({cd.pk})")
|
||||
raise
|
||||
|
||||
self.stdout.write(f"{CD.objects.count()} {_('CDs')} migrated")
|
||||
|
||||
# Migrate vinyls
|
||||
for vinyl in tqdm(OldVinyl.objects.all(),
|
||||
desc=_("vinyls"), unit=str(_("vinyl"))):
|
||||
try:
|
||||
new_vinyl = Vinyl.objects.create(
|
||||
title=vinyl.title,
|
||||
present=vinyl.present,
|
||||
rpm=vinyl.rpm,
|
||||
)
|
||||
new_vinyl.authors.set(vinyl.authors.all())
|
||||
new_vinyl.save()
|
||||
except:
|
||||
self.stderr.write(f"There was an error with {vinyl} "
|
||||
f"({vinyl.pk})")
|
||||
raise
|
||||
|
||||
self.stdout.write(f"{Vinyl.objects.count()} {_('vinyls')} migrated")
|
||||
|
||||
# Migrate reviews
|
||||
for review in tqdm(OldReview.objects.all(),
|
||||
desc=_("reviews"), unit=str(_("review"))):
|
||||
try:
|
||||
Review.objects.create(
|
||||
title=review.title,
|
||||
number=review.number,
|
||||
year=review.year,
|
||||
month=review.month,
|
||||
day=review.day,
|
||||
double=review.double,
|
||||
present=review.present,
|
||||
)
|
||||
except:
|
||||
self.stderr.write(f"There was an error with {review} "
|
||||
f"({review.pk})")
|
||||
raise
|
||||
|
||||
self.stdout.write(f"{Review.objects.count()} {_('reviews')} migrated")
|
||||
|
||||
# Migrate games
|
||||
for game in tqdm(OldGame.objects.all(),
|
||||
desc=_("games"), unit=str(_("game"))):
|
||||
try:
|
||||
Game.objects.create(
|
||||
title=game.title,
|
||||
owner=game.owner,
|
||||
duration=game.duration,
|
||||
players_min=game.players_min,
|
||||
players_max=game.players_max,
|
||||
comment=game.comment,
|
||||
)
|
||||
except:
|
||||
self.stderr.write(f"There was an error with {game} "
|
||||
f"({game.pk})")
|
||||
raise
|
||||
|
||||
self.stdout.write(f"{Game.objects.count()} {_('games')} migrated")
|
||||
|
||||
if not options['doit']:
|
||||
self.stdout.write(self.style.WARNING(
|
||||
"Warning: Data were't saved. Please use --doit option "
|
||||
"to really perform the migration."
|
||||
))
|
||||
exit(1)
|
@ -44,7 +44,17 @@ class Borrowable(PolymorphicModel):
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
return self.title
|
||||
obj = self
|
||||
if obj.__class__ == Borrowable:
|
||||
# Get true object instance, useful for autocompletion
|
||||
obj = Borrowable.objects.get(pk=obj.pk)
|
||||
|
||||
title = obj.title
|
||||
if hasattr(obj, 'subtitle'):
|
||||
subtitle = obj.subtitle
|
||||
if subtitle:
|
||||
title = f"{title} : {subtitle}"
|
||||
return title
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('borrowable')
|
||||
@ -99,12 +109,6 @@ class Book(Medium):
|
||||
null=True,
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
if self.subtitle:
|
||||
return "{} : {}".format(self.title, self.subtitle)
|
||||
else:
|
||||
return self.title
|
||||
|
||||
class Meta:
|
||||
verbose_name = _("book")
|
||||
verbose_name_plural = _("books")
|
||||
|
Loading…
x
Reference in New Issue
Block a user