我们从Python开源项目中,提取了以下35个代码示例,用于说明如何使用django.contrib.admin.utils.unquote()。
def resend_view(self, request, object_id, extra_context=None): # pylint: disable=W0613 """ View that re-sends the notification """ obj = self.get_object(request, unquote(object_id)) success = obj.resend() if success: self.message_user(request, 'The notification was resent successfully.', messages.SUCCESS) else: self.message_user(request, 'The notification failed to resend.', messages.ERROR) return self.response_post_save_change(request, obj)
def changeform_view(self, request, object_id=None, form_url='', extra_context=None): extra_context = extra_context or {} comment = self.get_object(request, unquote(object_id)) if comment is not None: extra_context['user_agent'] = get_user_agent(comment.user_agent) or truncatechars(comment.user_agent, 60) comment.requires_attention = '' comment.save() changeform = super(CommentAdmin, self).changeform_view(request, object_id=object_id, form_url=form_url, extra_context=extra_context) return changeform
def change_view(self, request, object_id, *args, **kwargs): if object_id is None: entity = None else: entity = self.get_object(request, unquote(object_id)) extra_context = kwargs.pop('extra_context', None) or {} kwargs['extra_context'] = dict(extra_context, **{ 'entity': entity, }) return super().change_view(request, object_id, *args, **kwargs)
def test_switch_moderator_on(self): with force_language("en"): pages_root = unquote(reverse("pages-root")) page1 = create_page('page', 'nav_playground.html', 'en', published=True) with disable_logger(log): call_command('cms', 'moderator', 'on') with force_language("en"): path = page1.get_absolute_url()[len(pages_root):].strip('/') page2 = get_page_from_path(path) self.assertEqual(page1.get_absolute_url(), page2.get_absolute_url())
def test_switch_moderator_off(self): with force_language("en"): pages_root = unquote(reverse("pages-root")) page1 = create_page('page', 'nav_playground.html', 'en', published=True) path = page1.get_absolute_url()[len(pages_root):].strip('/') page2 = get_page_from_path(path) self.assertIsNotNone(page2) self.assertEqual(page1.get_absolute_url(), page2.get_absolute_url())
def change_view(self, request, object_id, form_url='', extra_context=None): """ Override this function to hide the sumbit row from the user who has view only permission """ to_field = request.POST.get( TO_FIELD_VAR, request.GET.get(TO_FIELD_VAR) ) model = self.model opts = model._meta # TODO: Overriding the change_view costs 1 query more (one from us # and another from the super) obj = self.get_object(request, unquote(object_id), to_field) if self.has_view_permission(request, obj) and \ not self._has_change_only_permission(request, obj): extra_context = extra_context or {} extra_context['title'] = _('View %s') % force_text( opts.verbose_name) extra_context['show_save'] = False extra_context['show_save_and_continue'] = False extra_context['show_save_and_add_another'] = False extra_context['show_save_as_new'] = False inlines = self.get_inline_instances(request, obj) for inline in inlines: if (inline._has_change_only_permission(request, obj) or inline.has_add_permission(request)): extra_context['show_save'] = True extra_context['show_save_and_continue'] = True break return super(AdminViewPermissionModelAdmin, self).change_view( request, object_id, form_url, extra_context)
def user_change_password(self, request, id, form_url=''): if not self.has_change_permission(request): raise PermissionDenied user = self.get_object(request, unquote(id)) if user is None: raise Http404(_('%(name)s object with primary key %(key)r does not exist.') % { 'name': force_text(self.model._meta.verbose_name), 'key': escape(id), }) if request.method == 'POST': form = self.change_password_form(user, request.POST) if form.is_valid(): form.save() change_message = self.construct_change_message(request, form, None) self.log_change(request, user, change_message) msg = ugettext('Password changed successfully.') messages.success(request, msg) update_session_auth_hash(request, form.user) return HttpResponseRedirect( reverse( '%s:%s_%s_change' % ( self.admin_site.name, user._meta.app_label, user._meta.model_name, ), args=(user.pk,), ) ) else: form = self.change_password_form(user) fieldsets = [(None, {'fields': list(form.base_fields)})] adminForm = admin.helpers.AdminForm(form, fieldsets, {}) context = { 'title': _('Change password: %s') % escape(user.get_username()), 'adminForm': adminForm, 'form_url': form_url, 'form': form, 'is_popup': (IS_POPUP_VAR in request.POST or IS_POPUP_VAR in request.GET), 'add': True, 'change': False, 'has_delete_permission': False, 'has_change_permission': True, 'has_absolute_url': False, 'opts': self.model._meta, 'original': user, 'save_as': False, 'show_save': True, } context.update(self.admin_site.each_context(request)) request.current_app = self.admin_site.name return TemplateResponse(request, self.change_user_password_template or 'admin/auth/user/change_password.html', context)
def user_change_password(self, request, id, form_url=''): if not self.has_change_permission(request): raise PermissionDenied user = self.get_object(request, unquote(id)) if user is None: raise Http404(_('%(name)s object with primary key %(key)r does not exist.') % { 'name': force_text(self.model._meta.verbose_name), 'key': escape(id), }) if request.method == 'POST': form = self.change_password_form(user, request.POST) if form.is_valid(): form.save() change_message = self.construct_change_message(request, form, None) self.log_change(request, user, change_message) msg = ugettext('Password changed successfully.') messages.success(request, msg) update_session_auth_hash(request, form.user) return HttpResponseRedirect( reverse( '%s:%s_%s_change' % ( self.admin_site.name, user._meta.app_label, user._meta.model_name, ), args=(user.pk,), ) ) else: form = self.change_password_form(user) fieldsets = [(None, {'fields': list(form.base_fields)})] adminForm = admin.helpers.AdminForm(form, fieldsets, {}) context = { 'title': _('Change password: %s') % escape(user.get_username()), 'adminForm': adminForm, 'form_url': form_url, 'form': form, 'is_popup': (IS_POPUP_VAR in request.POST or IS_POPUP_VAR in request.GET), 'add': True, 'change': False, 'has_delete_permission': False, 'has_change_permission': True, 'has_absolute_url': False, 'opts': self.model._meta, 'original': user, 'save_as': False, 'show_save': True, } context.update(self.admin_site.each_context(request)) request.current_app = self.admin_site.name return TemplateResponse( request, self.change_user_password_template or 'admin/auth/user/change_password.html', context, )
def user_change_password(self, request, id, form_url=''): if not self.has_change_permission(request): raise PermissionDenied user = self.get_object(request, unquote(id)) if user is None: raise Http404(_('%(name)s object with primary key %(key)r does not exist.') % { 'name': force_text(self.model._meta.verbose_name), 'key': escape(id), }) if request.method == 'POST': form = self.change_password_form(user, request.POST) if form.is_valid(): form.save() change_message = self.construct_change_message(request, form, None) self.log_change(request, user, change_message) msg = ugettext('Password changed successfully.') messages.success(request, msg) update_session_auth_hash(request, form.user) return HttpResponseRedirect('..') else: form = self.change_password_form(user) fieldsets = [(None, {'fields': list(form.base_fields)})] adminForm = admin.helpers.AdminForm(form, fieldsets, {}) context = { 'title': _('Change password: %s') % escape(user.get_username()), 'adminForm': adminForm, 'form_url': form_url, 'form': form, 'is_popup': (IS_POPUP_VAR in request.POST or IS_POPUP_VAR in request.GET), 'add': True, 'change': False, 'has_delete_permission': False, 'has_change_permission': True, 'has_absolute_url': False, 'opts': self.model._meta, 'original': user, 'save_as': False, 'show_save': True, } context.update(admin.site.each_context(request)) request.current_app = self.admin_site.name return TemplateResponse(request, self.change_user_password_template or 'admin/auth/user/change_password.html', context)
def user_change_password(self, request, id, form_url=''): if not self.has_change_permission(request): raise PermissionDenied user = self.get_object(request, unquote(id)) if user is None: raise Http404(_('%(name)s object with primary key %(key)r does not exist.') % { 'name': force_text(self.model._meta.verbose_name), 'key': escape(id), }) if request.method == 'POST': form = self.change_password_form(user, request.POST) if form.is_valid(): form.save() change_message = self.construct_change_message(request, form, None) self.log_change(request, user, change_message) msg = ugettext('Password changed successfully.') messages.success(request, msg) update_session_auth_hash(request, form.user) return HttpResponseRedirect( reverse( '%s:%s_%s_change' % ( self.admin_site.name, user._meta.app_label, user._meta.model_name, ), args=(user.pk,), ) ) else: form = self.change_password_form(user) fieldsets = [(None, {'fields': list(form.base_fields)})] adminForm = admin.helpers.AdminForm(form, fieldsets, {}) context = { 'title': _('Change password: %s') % escape(user.get_email()), 'adminForm': adminForm, 'form_url': form_url, 'form': form, 'is_popup': (IS_POPUP_VAR in request.POST or IS_POPUP_VAR in request.GET), 'add': True, 'change': False, 'has_delete_permission': False, 'has_change_permission': True, 'has_absolute_url': False, 'opts': self.model._meta, 'original': user, 'save_as': False, 'show_save': True, } context.update(self.admin_site.each_context(request)) request.current_app = self.admin_site.name return TemplateResponse(request, self.change_user_password_template or 'admin/auth/user/change_password.html', context)
def add_member(self, request, id): if not self.has_change_permission(request) or not request.user.has_perm('people.view_person'): raise PermissionDenied group = self.get_object(request, unquote(id)) if group is None: raise Http404(_("Pas de groupe avec cet identifiant.")) if request.method == "POST": form = AddMemberForm(group, request.POST) if form.is_valid(): membership = form.save() messages.success(request, _("{email} a bien été ajouté au groupe").format(email=membership.person.email)) return HttpResponseRedirect( reverse( '%s:%s_%s_change' % ( self.admin_site.name, group._meta.app_label, group._meta.model_name, ), args=(group.pk,), ) ) else: form = AddMemberForm(group) fieldsets = [(None, {'fields': ['person']})] admin_form = admin.helpers.AdminForm(form, fieldsets, {}) context = { 'title': _('Ajouter un membre au groupe: %s') % escape(group.name), 'adminform': admin_form, 'form': form, 'is_popup': (IS_POPUP_VAR in request.POST or IS_POPUP_VAR in request.GET), 'opts': self.model._meta, 'original': group, 'change': False, 'add': False, 'save_as': True, 'show_save': False, 'has_delete_permission': False, 'has_add_permission': False, 'has_change_permission': True, 'media': self.media + admin_form.media } context.update(self.admin_site.each_context(request)) request.current_app = self.admin_site.name return TemplateResponse( request, 'admin/supportgroups/add_member.html', context, )