Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def edit(request, media_id):
Media = get_media_model()
MediaForm = get_media_form(Media)
media = get_object_or_404(Media, id=media_id)
if not permission_policy.user_has_permission_for_instance(request.user, 'change', media):
return permission_denied(request)
if request.POST:
original_file = media.file
form = MediaForm(request.POST, request.FILES, instance=media, user=request.user)
if form.is_valid():
if 'file' in form.changed_data:
# if providing a new media file, delete the old one.
# NB Doing this via original_file.delete() clears the file field,
# which definitely isn't what we want...
original_file.storage.delete(original_file.name)
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
def edit(request, media_id):
Media = get_media_model()
MediaForm = get_media_form(Media)
media = get_object_or_404(Media, id=media_id)
if not permission_policy.user_has_permission_for_instance(request.user, 'change', media):
return permission_denied(request)
if request.POST:
original_file = media.file
form = MediaForm(request.POST, request.FILES, instance=media, user=request.user)
if form.is_valid():
if 'file' in form.changed_data:
# if providing a new media file, delete the old one.
# NB Doing this via original_file.delete() clears the file field,
# which definitely isn't what we want...
original_file.storage.delete(original_file.name)
media = form.save()
# Reindex the media entry to make sure all tags are indexed
for backend in get_search_backends():
backend.add(media)
def edit(request, media_id):
Media = get_media_model()
MediaForm = get_media_form(Media)
media = get_object_or_404(Media, id=media_id)
if not permission_policy.user_has_permission_for_instance(request.user, 'change', media):
return permission_denied(request)
if request.POST:
original_file = media.file
form = MediaForm(request.POST, request.FILES, instance=media, user=request.user)
if form.is_valid():
if 'file' in form.changed_data:
# if providing a new media file, delete the old one.
# NB Doing this via original_file.delete() clears the file field,
# which definitely isn't what we want...
original_file.storage.delete(original_file.name)
media = form.save()
def index(request):
Media = get_media_model()
# Get media files (filtered by user permission)
media = permission_policy.instances_user_has_any_permission_for(
request.user, ['change', 'delete']
)
# Ordering
if 'ordering' in request.GET and request.GET['ordering'] in ['title', '-created_at']:
ordering = request.GET['ordering']
else:
ordering = '-created_at'
media = media.order_by(ordering)
# Filter by collection
current_collection = None
collection_id = request.GET.get('collection_id')
def usage(request, media_id):
Media = get_media_model()
media = get_object_or_404(Media, id=media_id)
paginator, used_by = paginate(request, media.get_usage())
return render(request, "wagtailmedia/media/usage.html", {
'media': media,
'used_by': used_by
})
def get_context(self):
return {
'total_media': get_media_model().objects.count(),
}
def describe_collection_media(collection):
media_count = get_media_model().objects.filter(collection=collection).count()
if media_count:
url = reverse('wagtailmedia:index') + ('?collection_id=%d' % collection.id)
return {
'count': media_count,
'count_text': ungettext(
"%(count)s media file",
"%(count)s media files",
media_count
) % {'count': media_count},
'url': url,
}
def __init__(self, **kwargs):
super(AdminMediaChooser, self).__init__(**kwargs)
self.media_model = get_media_model()
def delete(request, media_id):
Media = get_media_model()
media = get_object_or_404(Media, id=media_id)
if not permission_policy.user_has_permission_for_instance(request.user, 'delete', media):
return permission_denied(request)
if request.POST:
media.delete()
messages.success(request, _("Media file '{0}' deleted.").format(media.title))
return redirect('wagtailmedia:index')
return render(request, "wagtailmedia/media/confirm_delete.html", {
'media': media,
})