def delete(request, downloadable_id, template_name = "downloads/delete.html", extra_context = None, **kwargs): """ Arguments: - `request`: - `downloadable_id`: - `template_name`: - `extra_context`: - `**kwargs`: """ dnldbl = get_object_or_404(Downloadable, pk = downloadable_id) if not request.user.has_perm('downloads.delete_downloadable', dnldbl) and \ request.user != dnldbl.owner: return HttpResponseForbidden(_("You do not have permission to delete this downloadable item.")) if request.method == "POST": # Delete the underlying file, and then delete the downloadable. # name = dnldbl.blurb dnldbl.content.delete() dnldbl.delete() msg_user(request.user, _("Downloadable %s deleted.") % name) return HttpResponseRedirect(reverse('downloads_list')) return asrender_to_response(request, template_name, { 'downloadable' : dnldbl }, extra_context, **kwargs)
def create(request, extra_context = None, form_class = DownloadableForm, template_name = "downloads/create.html", **kwargs): """ Arguments: - `request`: - `template_name`: """ # Does this user have permission to create downloadloadable objects? # if not request.user.has_perm("downloads.add_downloadable"): return HttpResponseForbidden(_("You do not have permission to create a downloadable item.")) # On POST we create the uploadable.. # if request.method == 'POST' and 'content' in request.FILES: if 'content' in request.FILES: file_name = request.FILES['content'].name dnldbl = Downloadable(owner = request.user, content = file_name) form = form_class(request.POST, request.FILES, instance = dnldbl) else: dnldbl = Downloadable(owner = request.user, content = file_name) form = form_class(request.POST, instance = dnldbl) if form.is_valid(): dnldbl = form.save(commit = False) dnldbl.owner = request.user dnldbl.save() # The owner gets permissions to his own objects. # assign('downloads.change_downloadable', request.user, dnldbl) assign('downloads.delete_downloadable', request.user, dnldbl) assign('downloads.view_downloadable', request.user, dnldbl) assign('downloads.download_downloadable', request.user, dnldbl) msg_user(request.user, _('File "%s" successfully uploaded') % \ os.path.basename(dnldbl.content.name)) return HttpResponseRedirect(dnldbl.get_absolute_url()) else: form = form_class() return asrender_to_response(request, template_name, { 'form' : form }, extra_context, **kwargs)
def edit(request, downloadable_id, template_name = "downloads/edit.html", extra_context = None, form_class = DownloadableForm, **kwargs): """ Arguments: - `request`: - `downloadable_id`: - `template_name`: - `extra_context`: - `**kwargs`: """ dnldbl = get_object_or_404(Downloadable, pk = downloadable_id) if not request.user.has_perm('downloads.change_downloadable', dnldbl) and \ request.user != dnldbl.owner: return HttpResponseForbidden(_("You do not have permission to change this downloadable item.")) if request.method == "POST": # are we also uploading a new file with this edit? # if 'content' in request.FILES: file_name = request.FILES['content'].name dnldbl.content = file_name form = form_class(request.POST, request.FILES, instance = dnldbl) else: form = form_class(request.POST, instance = dnldbl) if form.is_valid(): dnldbl = form.save() msg_user(request.user, "Downloadable updated.") return HttpResponseRedirect(dnldbl.get_absolute_url()) else: form = form_class(instance = dnldbl) return asrender_to_response(request, template_name, { 'downloadable' :dnldbl, 'form' : form }, extra_context, **kwargs)