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?
You can also use the
@user_passes_testdecorator to restrict views to particular subsets of users. Or create your own decorator:Now you can use it as:
The alternative is:
The advantage of the
@betaform is that it is a bit easier to re-use.