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)
media = form.save()
def add(request, media_type):
Media = get_media_model()
MediaForm = get_media_form(Media)
if request.POST:
media = Media(uploaded_by_user=request.user, type=media_type)
form = MediaForm(request.POST, request.FILES, instance=media, user=request.user)
if form.is_valid():
form.save()
# Reindex the media entry to make sure all tags are indexed
for backend in get_search_backends():
backend.add(media)
messages.success(request, _("Media file '{0}' added.").format(media.title), buttons=[
messages.button(reverse('wagtailmedia:edit', args=(media.id,)), _('Edit'))
])
return redirect('wagtailmedia:index')
else: