# Path: vodmanagement/my_storage.py
# class VodStorage(FileSystemStorage):
#     """
#     Returns same name for existing file and deletes existing file on save.
#     """
# 
#     def _save(self, name, content):
#         if self.exists(name):
#             self.delete(name)
#         return super(VodStorage, self)._save(name, content)
# 
#     def get_available_name(self, name, max_length=None):
#         return name
# 
# Path: admin_resumable/fields.py
# class ModelAdminResumableFileField(models.FileField):
#     save_model = False
# 
#     def __init__(self, verbose_name=None, name=None, upload_to='',
#                  storage=None, **kwargs):
#         self.orig_upload_to = upload_to
#         super(ModelAdminResumableFileField, self).__init__(
#             verbose_name, name, 'unused', **kwargs)
# 
#     def formfield(self, **kwargs):
#         content_type_id = ContentType.objects.get_for_model(self.model).id
#         defaults = {
#             'form_class': FormAdminResumableFileField,
#             'widget': AdminResumableWidget(attrs={
#                 'content_type_id': content_type_id,
#                 'field_name': self.name})
#         }
#         kwargs.update(defaults)
#         return super(ModelAdminResumableFileField, self).formfield(**kwargs)
# 
# class ModelAdminResumableImageField(models.ImageField):
#     # orig_upload_to = ''
#     save_model = False
# 
#     def __init__(self, verbose_name=None, name=None, width_field=None, height_field=None, **kwargs):
#         self.orig_upload_to = ''
#         super(ModelAdminResumableImageField, self).__init__(verbose_name, name, width_field, height_field, **kwargs)
# 
# 
#     def formfield(self, **kwargs):
#         content_type_id = ContentType.objects.get_for_model(self.model).id
#         defaults = {
#             'form_class': FormAdminResumableFileField,
#             'widget': AdminFileResumableWidget(attrs={
#                 'content_type_id': content_type_id,
#                 'field_name': self.name})
#         }
#         kwargs.update(defaults)
#         return super(ModelAdminResumableImageField, self).formfield(**kwargs)
# 
# class ModelAdminResumableMultiFileField(models.FileField):
#     save_model = True
# 
#     def __init__(self, verbose_name=None, name=None, upload_to='',
#                  storage=None, **kwargs):
#         self.orig_upload_to = upload_to
#         super(ModelAdminResumableMultiFileField, self).__init__(
#             verbose_name, name, 'unused', **kwargs)
# 
#     def formfield(self, **kwargs):
#         content_type_id = ContentType.objects.get_for_model(self.model).id
#         defaults = {
#             'form_class': FormAdminResumableFileField,
#             'widget': AdminFileResumableWidget(attrs={
#                 'content_type_id': content_type_id,
#                 'field_name': self.name})
#         }
#         kwargs.update(defaults)
#         return super(ModelAdminResumableMultiFileField, self).formfield(**kwargs)
# 
# class ModelAdminResumableRestoreFileField(models.FileField):
# 
#     def __init__(self, verbose_name=None, name=None, upload_to='',
#                  storage=None, **kwargs):
#         super(ModelAdminResumableRestoreFileField, self).__init__(
#             verbose_name, name, 'unused', **kwargs)
# 
#     def formfield(self, **kwargs):
#         content_type_id = ContentType.objects.get_for_model(self.model).id
#         defaults = {
#             'form_class': FormAdminResumableFileField,
#             'widget': AdminRestoreFileResumableWidget(attrs={
#                 'content_type_id': content_type_id,
#                 'field_name': self.name})
#         }
#         kwargs.update(defaults)
#         return super(ModelAdminResumableRestoreFileField, self).formfield(**kwargs)


import logging
import os
import datetime
import six
import humanfriendly
from pathlib import Path
from django.db import models
from django.utils.html import format_html
from django.utils.encoding import uri_to_iri
from django.core.management import call_command
from django.utils.safestring import mark_safe
from django.conf import settings
from django.contrib.auth.models import User
from django.db.models.signals import pre_save, post_init, post_save
from django.dispatch import receiver
from django.core.urlresolvers import reverse
from django.core.files import File
from sortedm2m.fields import SortedManyToManyField
from uuslug import uuslug
from .my_storage import VodStorage
from admin_resumable.fields import (
    ModelAdminResumableFileField, ModelAdminResumableImageField,
    ModelAdminResumableMultiFileField, ModelAdminResumableRestoreFileField
)
from xpinyin import Pinyin  # for pinyin search
    from django.utils.encoding import smart_str
    from django.utils.encoding import smart_unicode as smart_str
                                verbose_name='分类等级')
    subset = models.ManyToManyField('self', blank=True, verbose_name='分类关系')

    class Meta:
        verbose_name = '视频分类'
        verbose_name_plural = '视频分类管理'

    def __str__(self):
        base_name = self.name + str('  (level %d)' % (self.level))
        if self.subset.first() and self.level == 2:
            return '--'.join([self.subset.first().name, base_name])
        else:
            return base_name

    def save(self, *args, **kwargs):
        super(VideoCategory, self).save(*args, **kwargs)

    def colored_level(self):
        color_code = 'red' if self.level == 1 else 'green'
        return format_html(
            '<span style="color:{};">{}</span>',
            color_code,
            self.get_level_display()
        )

    colored_level.short_description = '分级'


# ---------------------------------------------------------------------
class MultipleUpload(models.Model):

# Path: vodmanagement/my_storage.py
# class VodStorage(FileSystemStorage):
#     """
#     Returns same name for existing file and deletes existing file on save.
#     """
# 
#     def _save(self, name, content):
#         if self.exists(name):
#             self.delete(name)
#         return super(VodStorage, self)._save(name, content)
# 
#     def get_available_name(self, name, max_length=None):
#         return name
# 
# Path: admin_resumable/fields.py
# class ModelAdminResumableFileField(models.FileField):
#     save_model = False
# 
#     def __init__(self, verbose_name=None, name=None, upload_to='',
#                  storage=None, **kwargs):
#         self.orig_upload_to = upload_to
#         super(ModelAdminResumableFileField, self).__init__(
#             verbose_name, name, 'unused', **kwargs)
# 
#     def formfield(self, **kwargs):
#         content_type_id = ContentType.objects.get_for_model(self.model).id
#         defaults = {
#             'form_class': FormAdminResumableFileField,
#             'widget': AdminResumableWidget(attrs={
#                 'content_type_id': content_type_id,
#                 'field_name': self.name})
#         }
#         kwargs.update(defaults)
#         return super(ModelAdminResumableFileField, self).formfield(**kwargs)
# 
# class ModelAdminResumableImageField(models.ImageField):
#     # orig_upload_to = ''
#     save_model = False
# 
#     def __init__(self, verbose_name=None, name=None, width_field=None, height_field=None, **kwargs):
#         self.orig_upload_to = ''
#         super(ModelAdminResumableImageField, self).__init__(verbose_name, name, width_field, height_field, **kwargs)
# 
# 
#     def formfield(self, **kwargs):
#         content_type_id = ContentType.objects.get_for_model(self.model).id
#         defaults = {
#             'form_class': FormAdminResumableFileField,
#             'widget': AdminFileResumableWidget(attrs={
#                 'content_type_id': content_type_id,
#                 'field_name': self.name})
#         }
#         kwargs.update(defaults)
#         return super(ModelAdminResumableImageField, self).formfield(**kwargs)
# 
# class ModelAdminResumableMultiFileField(models.FileField):
#     save_model = True
# 
#     def __init__(self, verbose_name=None, name=None, upload_to='',
#                  storage=None, **kwargs):
#         self.orig_upload_to = upload_to
#         super(ModelAdminResumableMultiFileField, self).__init__(
#             verbose_name, name, 'unused', **kwargs)
# 
#     def formfield(self, **kwargs):
#         content_type_id = ContentType.objects.get_for_model(self.model).id
#         defaults = {
#             'form_class': FormAdminResumableFileField,
#             'widget': AdminFileResumableWidget(attrs={
#                 'content_type_id': content_type_id,
#                 'field_name': self.name})
#         }
#         kwargs.update(defaults)
#         return super(ModelAdminResumableMultiFileField, self).formfield(**kwargs)
# 
# class ModelAdminResumableRestoreFileField(models.FileField):
# 
#     def __init__(self, verbose_name=None, name=None, upload_to='',
#                  storage=None, **kwargs):
#         super(ModelAdminResumableRestoreFileField, self).__init__(
#             verbose_name, name, 'unused', **kwargs)
# 
#     def formfield(self, **kwargs):
#         content_type_id = ContentType.objects.get_for_model(self.model).id
#         defaults = {
#             'form_class': FormAdminResumableFileField,
#             'widget': AdminRestoreFileResumableWidget(attrs={
#                 'content_type_id': content_type_id,
#                 'field_name': self.name})
#         }
#         kwargs.update(defaults)
#         return super(ModelAdminResumableRestoreFileField, self).formfield(**kwargs)


# Path: vodmanagement/models.py
import logging
import os
import datetime
import six
import humanfriendly
from pathlib import Path
from django.db import models
from django.utils.html import format_html
from django.utils.encoding import uri_to_iri
from django.core.management import call_command
from django.utils.safestring import mark_safe
from django.conf import settings
from django.contrib.auth.models import User
from django.db.models.signals import pre_save, post_init, post_save
from django.dispatch import receiver
from django.core.urlresolvers import reverse
from django.core.files import File
from sortedm2m.fields import SortedManyToManyField
from uuslug import uuslug
from .my_storage import VodStorage
from admin_resumable.fields import (
    ModelAdminResumableFileField, ModelAdminResumableImageField,
    ModelAdminResumableMultiFileField, ModelAdminResumableRestoreFileField
)
from xpinyin import Pinyin  # for pinyin search
    from django.utils.encoding import smart_str
    from django.utils.encoding import smart_unicode as smart_str

                                verbose_name='分类等级')
    subset = models.ManyToManyField('self', blank=True, verbose_name='分类关系')

    class Meta:
        verbose_name = '视频分类'
        verbose_name_plural = '视频分类管理'

    def __str__(self):
        base_name = self.name + str('  (level %d)' % (self.level))
        if self.subset.first() and self.level == 2:
            return '--'.join([self.subset.first().name, base_name])
        else:
            return base_name

    def save(self, *args, **kwargs):
        super(VideoCategory, self).save(*args, **kwargs)

    def colored_level(self):
        color_code = 'red' if self.level == 1 else 'green'
        return format_html(
            '<span style="color:{};">{}</span>',
            color_code,
            self.get_level_display()
        )

    colored_level.short_description = '分级'


# ---------------------------------------------------------------------
class MultipleUpload(models.Model):

    files = ModelAdminResumableMultiFileField(null=True, blank=True, storage=VodStorage(), verbose_name='文件')