Getting user from admin validation class

I'm trying to control a control entry where superuser accounts cannot save a ChannelStatus input file with a date attribute that is older than 2 days. I need to get the user so that I can check if the request is recursive or superuser, but was unable to achieve that.

I have already tried "request.user.is_superuser", "user.is_superuser", "self.user.is_superuser" and "self.request.user.is_superuser" but none works.

class ChannelStatusValidForm(forms.ModelForm):
    class Meta:
            model = ChannelStatus
    def clean(self):
        cleaned_data = self.cleaned_data
        mydate = cleaned_data.get("date")
        today = date.today()
        if request.user.is_superuser:## here is the problem
            return cleaned_data
        elif (today - timedelta(days=2)) > mydate:
            raise forms.ValidationError("Invalid date, maximum 2 days allowed.")
        else:
            return cleaned_data

      

+2


source to share


2 answers


Adding (and adjusting) Daniel Roseman's answer from another question :

class ChannelStatusValidForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request', None)
        super(MyForm, self).__init__(*args, **kwargs)


    def clean(self):
        cleaned_data = self.cleaned_data
        mydate = cleaned_data.get("date")
        today = date.today()
        if self.request.user.is_superuser:
            return cleaned_data
        elif (today - timedelta(days=2)) > mydate:
            raise forms.ValidationError("Invalid date, maximum 2 days allowed.")
        else:
            return cleaned_data

      



and in your opinion:

myform = ChannelStatusValidForm(request.POST, request=request)

      

+1


source


There is a way to achieve this without creating additional admin views: use the format metaclass in get_form()

:



class ChannelStatusValidForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request', None) # Now you can access request anywhere in your form methods by using self.request.            
        super(ChannelStatusValidForm, self).__init__(*args, **kwargs)

    def clean(self):
        cleaned_data = self.cleaned_data
        mydate = cleaned_data.get("date")
        today = date.today()
            request = self.request
        if request.user.is_superuser:
            return cleaned_data
        elif (today - timedelta(days=2)) > mydate:
            raise forms.ValidationError("Invalid date, maximum 2 days allowed.")
        else:
            return cleaned_data
    class Meta:
            model = ChannelStatus

class ChannelStatusAdmin(admin.ModelAdmin):
    form = ChannelStatusValidForm    
    def get_form(self, request, obj=None, **kwargs):
        AdminForm = super(ChannelStatusAdmin, self).get_form(request, obj, **kwargs)
        class ModelFormMetaClass(AdminForm):
            def __new__(cls, *args, **kwargs):
                kwargs['request'] = request
                return AdminForm(*args, **kwargs)
        return ModelFormMetaClass

      

0


source







All Articles