import os
import glob
from django.core.management.base import BaseCommand
from django.core.files import File
from memopolis.models import Content, User
[docs]
class Command(BaseCommand):
help = 'Import/update buildin content.'
[docs]
def handle(self, *args, **options):
self.stdout.write(self.style.MIGRATE_LABEL("\nImporting buildin content..."))
user = User.objects.filter(is_superuser=True)[0]
for path in glob.glob("/buildin-content/**/*.png", recursive=True):
name = path.replace("/buildin-content/", "").replace(".png", "")
special = "buildin:" + name
print(special)
content, created = Content.objects.get_or_create(user=user, special=special)
if created:
local_file = open(path, "rb")
content.image.save('%s.png' % name.replace("/", "_"), File(local_file))
for t in ["item", "background", "character", "portal", "object", "flag"]:
if t in path:
content.tags.add(t)
content.save()
local_file.close()