Best Python code snippet using Kiwi_python
admin.py
Source:admin.py
...19 if qs.count():20 raise forms.ValidationError(_('This email address is already in use'))21 else:22 return self.cleaned_data['email']23def _modifying_myself(request, object_id):24 return request.user.pk == int(object_id)25class KiwiUserAdmin(UserAdmin):26 list_display = UserAdmin.list_display + ('is_superuser', 'date_joined', 'last_login')27 ordering = ['-pk'] # same as -date_joined28 # override standard form and make the email address unique29 # even when adding users via admin panel30 form = MyUserChangeForm31 def has_change_permission(self, request, obj=None):32 return request.user.is_superuser or obj is not None33 def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None):34 if obj and not (_modifying_myself(request, obj.pk) or request.user.is_superuser):35 context.update({36 'show_save': False,37 'show_save_and_continue': False,38 })39 return super().render_change_form(request, context, add, change, form_url, obj)40 def get_readonly_fields(self, request, obj=None):41 if request.user.is_superuser:42 return super().get_readonly_fields(request, obj)43 readonly_fields = ['username', 'is_staff', 'is_active',44 'is_superuser', 'last_login', 'date_joined',45 'groups', 'user_permissions']46 if not _modifying_myself(request, obj.pk):47 readonly_fields.extend(['first_name', 'last_name', 'email'])48 return readonly_fields49 def get_fieldsets(self, request, obj=None):50 if request.user.is_superuser:51 return super().get_fieldsets(request, obj)52 first_fieldset_fields = ('username',)53 if _modifying_myself(request, obj.pk):54 first_fieldset_fields = first_fieldset_fields + ('password',)55 return ((None, {'fields': first_fieldset_fields}),56 (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),57 (_('Permissions'), {'fields': ('is_active', 'groups')}))58 @sensitive_post_parameters_m59 def user_change_password(self, request, id, form_url=''):60 return HttpResponseRedirect(reverse('admin:password_change'))61 @admin.options.csrf_protect_m62 def delete_view(self, request, object_id, extra_context=None):63 if not _modifying_myself(request, object_id):64 return super().delete_view(request, object_id, extra_context)65 # allow deletion of the user own account66 permission = Permission.objects.get(content_type__app_label='auth',67 codename='delete_user')68 try:69 request.user.user_permissions.add(permission)70 return super().delete_view(request, object_id, extra_context)71 finally:72 request.user.user_permissions.remove(permission)73 def response_delete(self, request, obj_display, obj_id):74 result = super().response_delete(request, obj_display, obj_id)75 if not _modifying_myself(request, obj_id):76 return result77 # user doesn't exist anymore so go to the index page78 # instead of returning to the user admin page79 return HttpResponseRedirect(reverse('core-views-index'))80 def has_delete_permission(self, request, obj=None):81 # allow to delete yourself without having 'delete' permission82 # explicitly assigned83 if _modifying_myself(request, getattr(obj, 'pk', 0)):84 return True85 return super().has_delete_permission(request, obj)86# user admin extended functionality87admin.site.unregister(User)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!