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 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,
})
def chooser(request):
Media = get_media_model()
media_files = []
q = None
is_searching = False
if 'q' in request.GET or 'p' in request.GET or 'collection_id' in request.GET:
media_files = Media.objects.all()
collection_id = request.GET.get('collection_id')
if collection_id:
media_files = media_files.filter(collection=collection_id)
searchform = SearchForm(request.GET)
if searchform.is_valid():
q = searchform.cleaned_data['q']
def media_chosen(request, media_id):
media = get_object_or_404(get_media_model(), id=media_id)
return render_modal_workflow(
request, None, None,
None,
json_data={'step': 'media_chosen', 'result': get_media_json(media)}
)
def target_model(self):
from wagtailmedia.models import get_media_model
return get_media_model()