Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_media_model():
from django.conf import settings
from django.apps import apps
try:
app_label, model_name = settings.WAGTAILMEDIA_MEDIA_MODEL.split('.')
except AttributeError:
return Media
except ValueError:
raise ImproperlyConfigured("WAGTAILMEDIA_MEDIA_MODEL must be of the form 'app_label.model_name'")
media_model = apps.get_model(app_label, model_name)
if media_model is None:
raise ImproperlyConfigured(
"WAGTAILMEDIA_MEDIA_MODEL refers to model '%s' that has not been installed" %
settings.WAGTAILMEDIA_MEDIA_MODEL
)
return media_model
# and when only one collection exists, it will get hidden anyway.
fields = list(fields) + ['collection']
return modelform_factory(
model,
form=BaseMediaForm,
fields=fields,
widgets={
'tags': widgets.AdminTagWidget,
'file': forms.FileInput(),
'thumbnail': forms.ClearableFileInput(),
})
GroupMediaPermissionFormSet = collection_member_permission_formset_factory(
Media,
[
('add_media', _("Add"), _("Add/edit media you own")),
('change_media', _("Edit"), _("Edit any media")),
],
'wagtailmedia/permissions/includes/media_permissions_formset.html'
)
@receiver(pre_delete, sender=Media)
def media_delete(sender, instance, **kwargs):
# Pass false so FileField doesn't save the model.
instance.file.delete(False)
instance.thumbnail.delete(False)
from django.conf import settings
from django.contrib import admin
from wagtailmedia.models import Media
if hasattr(settings, 'WAGTAILMEDIA_MEDIA_MODEL') and settings.WAGTAILMEDIA_MEDIA_MODEL != 'wagtailmedia.Media':
# This installation provides its own custom media class;
# to avoid confusion, we won't expose the unused wagtailmedia.Media class
# in the admin.
pass
else:
admin.site.register(Media)
from wagtail.core.permission_policies.collections import CollectionOwnershipPermissionPolicy
from wagtailmedia.models import Media, get_media_model
permission_policy = CollectionOwnershipPermissionPolicy(
get_media_model(),
auth_model=Media,
owner_field_name='uploaded_by_user'
)