我正在使用braces.views中的GroupRequiredMixin尝试将某些视图的访问权限限制为特定用户组。
我的观点如下:
class InviteCompanyAdminView(LoginRequiredMixin, GroupRequiredMixin, CreateView):
model = CompanyAdminInvitation
group_required = u"staff" # For the GroupRequiredMixin
success_url = '/'
def form_valid(self, form):
# This method is called when valid form data has been POSTed.
# It should return an HttpResponse
form.instance.send_invite(self.request)
return super(InviteCompanyAdminView, self).form_valid(form)
我正在访问该视图的用户不属于任何组,但视图已呈现。知道为什么吗?
我的模型看起来像这样:
class CompanyAdminInvitation(models.Model):
user_first_name = models.CharField(max_length=128)
user_last_name = models.CharField(max_length=128)
user_title = models.CharField(max_length=128)
user_email = models.EmailField()
company_name = models.CharField(max_length=128)
company_country = CountryField()
invitation_uid = models.CharField(max_length=100, null=True, blank=True, unique=True, editable=False)
has_been_used = models.BooleanField(default=False, editable=False)
invitation_date = models.DateTimeField(auto_now_add=True)
def __init__(self, *args, **kwargs):
super(CompanyAdminInvitation, self).__init__(*args, **kwargs)
self.invitation_uid = str(uuid4())
def send_invite(self, request):
# TODO use a template instead of hardcoding the email
subject = "Get started with your account!"
message = "Hi, your link to start is %s" % \
(request.build_absolute_uri(reverse("users:admin_signup",
kwargs={"invite_id": str(self.invitation_uid)})))
from_email = "no-reply@example.com"
recipient_list = [self.user_email,]
send_mail(subject=subject, message=message, from_email=from_email, recipient_list=recipient_list)
def get_absolute_url(self):
return "/"
答案 0 :(得分:1)
我不确定它是否是原始海报问题的原因,但如果使用超级用户进行测试会导致这种情况,这会忽略大括号中的组权限(请参阅issue #105了解django-braces)< / p>
尝试使用非超级用户进行测试以解决此问题。