python - Django Admin, custom permission checkbox -



python - Django Admin, custom permission checkbox -

please help me. i'm stuck in 1 place in django project generate questionnaires teachers. i've implemented own useradmin model, , despite is_staff, is_superuser fields, have own boolean field "is_teacher". when creating new user, if "is_teacher" box ticked, want give user permissions manage whole questionnaire model, remove permissions managing myuser model(creating, changing , deleting users)

this i've implemented:

in models.py

class myusermanager(baseusermanager): def create_user(self, username, email=none, password=none): if not username: raise valueerror('the given username must set') email = myusermanager.normalize_email(email) user = self.model(username=username, email=email, is_staff=true, is_active=true, is_superuser=false, is_teacher=true) user.set_password(password) user.save(using=self._db) homecoming user def create_superuser(self, username, email, password): u = self.create_user(username, email, password) u.is_staff = true u.is_active = true u.is_superuser = true u.is_teacher = true u.save(using=self._db) homecoming u class myuser(abstractbaseuser): username = models.charfield(max_length=30, unique=true) email = models.emailfield(blank=true) is_active = models.booleanfield(default=true) is_staff = models.booleanfield(default=true) is_superuser = models.booleanfield(default=false) is_teacher = models.booleanfield(default=true) objects = myusermanager() username_field = 'username' required_fields = ['email'] def get_full_name(self): homecoming self.username def get_short_name(self): homecoming self.username def __unicode__(self): homecoming self.username def has_perm(self, perm, obj=none): homecoming true def has_module_perms(self, module): homecoming true

in admin.py

class usercreationform(forms.modelform): """ form creates user, no privileges, given username , password. """ error_messages = { 'password_mismatch': "the 2 password fields didn't match.", } password1 = forms.charfield(label="password", widget=forms.passwordinput) password2 = forms.charfield(label="password confirmation", widget=forms.passwordinput) class meta: model = myuser fields = ("username",) def clean_password2(self): password1 = self.cleaned_data.get("password1") password2 = self.cleaned_data.get("password2") if password1 , password2 , password1 != password2: raise forms.validationerror( self.error_messages['password_mismatch'], code='password_mismatch', ) homecoming password2 def save(self, commit=true): user = super(usercreationform, self).save(commit=false) user.set_password(self.cleaned_data["password1"]) if commit: user.save() homecoming user class userchangeform(forms.modelform): password = readonlypasswordhashfield(label="password") class meta: model = myuser fields = ('username', 'password', 'email', 'is_active', 'is_staff', 'is_superuser', 'is_teacher') def clean_password(self): # regardless of user provides, homecoming initial value. # done here, rather on field, because # field not have access initial value homecoming self.initial["password"] class myuseradmin(useradmin): form = userchangeform add_form = usercreationform list_display = ('username', 'email', 'is_teacher') list_filter = ('is_staff', 'is_superuser', 'is_active') fieldsets = ( (none, {'fields': ('username', 'password')}), ('personal info', {'fields': ('email',)}), ('permissions', {'fields': ('is_superuser', 'is_teacher',)}), ) add_fieldsets = ( (none, { 'classes': ('wide',), 'fields': ('username', 'email', 'password1', 'password2')} ), ) search_fields = ('username', 'email') ordering = ('username',) filter_horizontal = ()

there 2 ways can this. 1 manually adding permissions. should in myuseradmin class, overriding default save method. this:

def save_model(self, request, obj, form, change): # add together permissions here so: obj.save() if obj.is_teacher: # illustration of permission can add together obj.user_permissions.add('questionaires_questionire_change', '......') else: # remove permissions in case user demoted teacher status obj.user_permissions.remove('questionaires_questionire_change', '......')

now, other way of doing this, think improve (because doesn't depend on saving user models, , can alter in future without need of doing run through users updating permissions):

you can override has_change_permission, has_add_permission , has_delete_permission questionnaire , other models need, in modeladmins. illustration questionnaire model (only showing alter permission here):

https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.modeladmin.has_change_permission

class questionnaireadmin(admin.modeladmin): def has_change_permission(self, request, obj=none): if request.user.is_teacher: homecoming true # if user not teacher, allow django evaluate specific permissions (a superusuer have permission if way) homecoming super(questionnaireadmin, self).has_change_permission(request, obj=none)

python django permissions

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -