# Path: feincms/module/medialibrary/models.py
# class Category(models.Model):
#     """
#     These categories are meant primarily for organizing media files in the
#     library.
#     """
# 
#     title = models.CharField(_("title"), max_length=200)
#     parent = models.ForeignKey(
#         "self",
#         blank=True,
#         null=True,
#         on_delete=models.CASCADE,
#         related_name="children",
#         limit_choices_to={"parent__isnull": True},
#         verbose_name=_("parent"),
#     )
# 
#     slug = models.SlugField(_("slug"), max_length=150)
# 
#     class Meta:
#         ordering = ["parent__title", "title"]
#         verbose_name = _("category")
#         verbose_name_plural = _("categories")
#         app_label = "medialibrary"
# 
#     objects = CategoryManager()
# 
#     def __str__(self):
#         if self.parent_id:
#             return f"{self.parent.title} - {self.title}"
# 
#         return self.title
# 
#     def save(self, *args, **kwargs):
#         if not self.slug:
#             self.slug = slugify(self.title)
# 
#         super().save(*args, **kwargs)
# 
#     save.alters_data = True
# 
#     def path_list(self):
#         if self.parent is None:
#             return [self]
#         p = self.parent.path_list()
#         p.append(self)
#         return p
# 
#     def path(self):
#         return " - ".join(f.title for f in self.path_list())
# 
# class MediaFile(MediaFileBase):
#     class Meta:
#         app_label = "medialibrary"
# 
# class MediaFileTranslation(Translation(MediaFile)):
#     """
#     Translated media file caption and description.
#     """
# 
#     caption = models.CharField(_("caption"), max_length=1000)
#     description = models.TextField(_("description"), blank=True)
# 
#     class Meta:
#         verbose_name = _("media file translation")
#         verbose_name_plural = _("media file translations")
#         unique_together = ("parent", "language_code")
#         app_label = "medialibrary"
# 
#     def __str__(self):
#         return self.caption


import json
import os
import time
import zipfile
from django.conf import settings as django_settings
from django.core.files.base import ContentFile
from django.template.defaultfilters import slugify
from django.utils import timezone
from .models import Category, MediaFile, MediaFileTranslation
    Import a collection of media files from a zip file.

    category_id: if set, the pk of a Category that all uploaded
        files will have added (eg. cathegory "newly uploaded files")
    overwrite: attempt to overwrite existing files. This might
        not work with non-trivial storage handlers
    """
    category = None
    if category_id:
        category = Category.objects.get(pk=int(category_id))

    z = zipfile.ZipFile(data)

    # Peek into zip file to find out whether it contains meta information
    is_export_file = False
    info = {}
    try:
        info = json.loads(z.comment)
        if info["export_magic"] == export_magic:
            is_export_file = True
    except Exception:
        pass

    # If meta information, do we need to create any categories?
    # Also build translation map for category ids.
    category_id_map = {}
    if is_export_file:
        for cat in sorted(
            info.get("categories", []), key=lambda k: k.get("level", 999)
        ):

# Path: feincms/module/medialibrary/models.py
# class Category(models.Model):
#     """
#     These categories are meant primarily for organizing media files in the
#     library.
#     """
# 
#     title = models.CharField(_("title"), max_length=200)
#     parent = models.ForeignKey(
#         "self",
#         blank=True,
#         null=True,
#         on_delete=models.CASCADE,
#         related_name="children",
#         limit_choices_to={"parent__isnull": True},
#         verbose_name=_("parent"),
#     )
# 
#     slug = models.SlugField(_("slug"), max_length=150)
# 
#     class Meta:
#         ordering = ["parent__title", "title"]
#         verbose_name = _("category")
#         verbose_name_plural = _("categories")
#         app_label = "medialibrary"
# 
#     objects = CategoryManager()
# 
#     def __str__(self):
#         if self.parent_id:
#             return f"{self.parent.title} - {self.title}"
# 
#         return self.title
# 
#     def save(self, *args, **kwargs):
#         if not self.slug:
#             self.slug = slugify(self.title)
# 
#         super().save(*args, **kwargs)
# 
#     save.alters_data = True
# 
#     def path_list(self):
#         if self.parent is None:
#             return [self]
#         p = self.parent.path_list()
#         p.append(self)
#         return p
# 
#     def path(self):
#         return " - ".join(f.title for f in self.path_list())
# 
# class MediaFile(MediaFileBase):
#     class Meta:
#         app_label = "medialibrary"
# 
# class MediaFileTranslation(Translation(MediaFile)):
#     """
#     Translated media file caption and description.
#     """
# 
#     caption = models.CharField(_("caption"), max_length=1000)
#     description = models.TextField(_("description"), blank=True)
# 
#     class Meta:
#         verbose_name = _("media file translation")
#         verbose_name_plural = _("media file translations")
#         unique_together = ("parent", "language_code")
#         app_label = "medialibrary"
# 
#     def __str__(self):
#         return self.caption


# Path: feincms/module/medialibrary/zip.py
import json
import os
import time
import zipfile
from django.conf import settings as django_settings
from django.core.files.base import ContentFile
from django.template.defaultfilters import slugify
from django.utils import timezone
from .models import Category, MediaFile, MediaFileTranslation

    Import a collection of media files from a zip file.

    category_id: if set, the pk of a Category that all uploaded
        files will have added (eg. cathegory "newly uploaded files")
    overwrite: attempt to overwrite existing files. This might
        not work with non-trivial storage handlers
    """
    category = None
    if category_id:
        category = Category.objects.get(pk=int(category_id))

    z = zipfile.ZipFile(data)

    # Peek into zip file to find out whether it contains meta information
    is_export_file = False
    info = {}
    try:
        info = json.loads(z.comment)
        if info["export_magic"] == export_magic:
            is_export_file = True
    except Exception:
        pass

    # If meta information, do we need to create any categories?
    # Also build translation map for category ids.
    category_id_map = {}
    if is_export_file:
        for cat in sorted(
            info.get("categories", []), key=lambda k: k.get("level", 999)
        ):

            new_cat, created = Category.objects.get_or_create(