0

Okay so I am very new to Django and relatively new to Python as well. In the website I am building, I am using some middleware that someone else made to keep track of "online" users using the cache to do so. Here is the middleware I'm referring to import datetime from django.core.cache import cache from django.conf import settings

class ActiveUserMiddleware:

    def process_request(self, request):
        current_user = request.user
        if request.user.is_authenticated():
            now = datetime.datetime.now()
            cache.set('seen_%s' % (current_user.username), now, 
                       settings.USER_LASTSEEN_TIMEOUT)

I want to take all of the online users, then divide them based on whether they are in highschool or college (which is an attributed I gave users via foreign key to a profile), and then return a random user from the list of online users who meet those certain requirements. I am at a loss on how to do this because the django structure is still confusing me. Would I implement this in a view or in a model? After looking at the code for the active_users app I have figured out that I can import the active_users, but I'm not sure if that is a list, an array, or an object. Also how do I determing the number of online_users? Does something like online_users.length work? Here is the code I have come up with so far: (I have omitted some other imports and views for the sake of brevity). I'm sorry I haven't come up with a lot of code on my own, I am just very stuck/frustrated. Any help is greatly appreciated.

from online_status.status import CACHE_USERS
from online_status.utils import encode_json
from django.contrib.auth.models import User
from django.core.cache import cache
from django.template.context import RequestContext

 def send_to(request):
  sender = request.user
  sender_level = sender.username
  online_users = cache.get(CACHE_USERS)
  match_users=[]
  for User in online_users:
      if User.username == sender_level:
          match_users.append(user)

  random_user = choice(match_users)
  html = "<html> <body> <p> User: %s % random_user </p></body></html>"  % random_user 
  return render_to_response(html)
John Mathews
  • 231
  • 3
  • 6

1 Answers1

0

With a cache based seen info you need to extract the user name from the db to be able to get online users. So the only way would be to filter UserProfile.objects.all() based on the value of the cache. Then you can do

from random import choice
random_user = choice(online_users)

But it would be more efficient for those kinds of requests to store the seen info in your model. This way you could directly do a request like :

online_users = UserProfile.objects.filter(seen__lte=now-timeout)

In a typical use you would have a lot of users but a few online users. So the first version would be really slower.

deufeufeu
  • 998
  • 5
  • 11
  • How would I go about extracting a list of the online users from the CACHE_USERS? Isn't the CACHE_USERS a list of online users? I updated what my view looks like now but I'm getting the error "'OnlineStatus' object has no attribute 'username'" – John Mathews May 24 '13 at 06:16
  • Ok, in the mean time I did too, so for completeness: `for status in online_users: if status.user.username == ...` – deufeufeu May 24 '13 at 06:52
  • As a side note, you should not name you variable `User` as it is the name of a class. You code might work, but everyone thinks of `User` as the user class in django. – deufeufeu May 24 '13 at 06:54