如何调用函数并形成返回对象以在我的设置文件中使用?

发布于 2024-10-10 23:27:53 字数 791 浏览 4 评论 0原文

我有一些中间件,它需要一个元组(用户名......它只允许元组中的用户名通过网站的某些区域)。

我有一个 UserProfile 模型,其中包含有关每个用户的信息,我想对其进行过滤,以便它返回与此中间件一起使用的用户名元组 - 换句话说,设置变量 BETA_USERS = (dynamically- generated-tuple)。

您对实现这一目标有什么建议吗?

编辑:

所以,元组确实不是一个重要的细节 - 这是一个例子:

通常,我只是将其硬编码到设置中:

BETA_USERS = ('username1', 'username2', 'username3', 'username4')

但是,我有一个包含 Beta 列的 UserProfile 模型,可以设置为 1。前 50 名注册测试版的人将设置为 1,其他人为 0。因此,我可以通过在模型对象上调用过滤器方法来轻松过滤此内容:

users = UserProfile.objects.filter(beta='1')

我可以做到这一点一个带有这个奇怪的小循环的漂亮元组:

for user in users:
    list.append((user.user.username).upper())
return tuple(list)

我想我真正的问题是,我在设置文件中调用它的最佳方法是什么?

或者,换句话说,在设置文件中分配动态创建的变量的最佳方法是什么?

I have some middleware which takes a tuple (of usernames... it only allows usernames in the tuple to pass through certain areas of the site).

I have a UserProfile model which contains information about each user, and I want to filter it so that it returns a tuple of usernames for use with this middleware -- in otherwords, set a variable BETA_USERS = (dynamically-generated-tuple).

Have you got any suggestions for accomplishing this?

Edit:

So, the tuple really isn't an important detail -- here's an example:

Generally, I would just hard-code this into settings:

BETA_USERS = ('username1', 'username2', 'username3', 'username4')

However, I have a UserProfile model which contains a Beta column, which can be set to 1. The first 50 people to sign up for the beta will be set to 1, everyone else 0. So, I can easily filter this by calling a filter method on a model object:

users = UserProfile.objects.filter(beta='1')

and I can make that a nice tuple with this strange little loop:

for user in users:
    list.append((user.user.username).upper())
return tuple(list)

I guess my real question is, what is the best way for me to call this in my settings file?

or, stated another way, what's the best way to assign dynamically created variables in the settings file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清风疏影 2024-10-17 23:27:53

您还可以使用 @user_passes_test 装饰器将视图限制为特定的用户子集。或者创建您自己的装饰器:

from django.utils.functional import wraps

def beta(view):
    @wraps(view)
    def inner(request, *args, **kwargs):
        if request.user.user_profile.beta:
            return view(request, *args, **kwargs)
        # Up to you how you return failure...
    return inner

现在您可以将其用作:

@beta
def my_view(request):
    # do something new here.

另一种方法是:

@user_passes_test(lambda u: u.profile.beta)
def my_view(request):
    # do something clever

@beta 形式的优点是它更容易重用。

You can also use the @user_passes_test decorator to restrict views to particular subsets of users. Or create your own decorator:

from django.utils.functional import wraps

def beta(view):
    @wraps(view)
    def inner(request, *args, **kwargs):
        if request.user.user_profile.beta:
            return view(request, *args, **kwargs)
        # Up to you how you return failure...
    return inner

Now you can use it as:

@beta
def my_view(request):
    # do something new here.

The alternative is:

@user_passes_test(lambda u: u.profile.beta)
def my_view(request):
    # do something clever

The advantage of the @beta form is that it is a bit easier to re-use.

圈圈圆圆圈圈 2024-10-17 23:27:53

我想到的第一件事是构建一个列表,然后调用 tuple(),或者使用生成每个用户名的生成器函数动态创建一个元组。无论哪种方式,您都需要为您的数据构建一个接口。我浏览过此网站,看来您的数据是最好的访问方式通过访问对象的全局对象的接口。只需迭代 django.model 对象并提取您的用户名,每次都会生成用于生成元组的生成器。

The first thing which comes to mind is to build a list then call tuple(), or make a tuple on the fly using a generator function yielding each user name. Either way you will need to build an interface for your data. I've skimmed this site and it seems your data is best accessed by an interface accessing an object's global object. Simply iterate through your django.model objects and extract your user name, yielding each time to a generator used to make a tuple.

栀梦 2024-10-17 23:27:53

如果你想继续按照现在的方式做,蒂姆有一个很好的答案。

但是,django 提供了一种好方法,通过 自定义权限

您应该实现它们并使用 @permission_required 装饰需要权限的视图

Tim has a good answer, if you want to continue to do how you are now doing it.

But, django provides a good way to let only some people into the areas of the site, via custom permissions.

You should implement them and decorate the views that require permission with the @permission_required

葬心 2024-10-17 23:27:53

我不确定我是否正确理解你的问题。是这样的吗?

>>> allowed_users = ("John", "Eric", "Graham", "Terry")
>>> current_users = ("Connie", "John", "Ian", "Terry")
>>> tuple(user for user in current_users if user in allowed_users)
('John', 'Terry')

更好的解决方案(感谢 sukhbir 提醒我!):

>>> set(allowed_users) & set(current_users)
{'John', 'Terry'}

或者,如果结果必须是一个元组:

>>> tuple(set(allowed_users) & set(current_users))
('John', 'Terry')

使用集合时的唯一缺点是不一定会保留顺序。

I'm not sure if I understand your problem correctly. Is it something like this?

>>> allowed_users = ("John", "Eric", "Graham", "Terry")
>>> current_users = ("Connie", "John", "Ian", "Terry")
>>> tuple(user for user in current_users if user in allowed_users)
('John', 'Terry')

A better solution (thanks to sukhbir for reminding me!):

>>> set(allowed_users) & set(current_users)
{'John', 'Terry'}

or, if the result has to be a tuple:

>>> tuple(set(allowed_users) & set(current_users))
('John', 'Terry')

The only drawback when using a set is that order will not necessarily be preserved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文