Django Registration Number of users logged in - django

I have an application where I would like to display the number of users that are logged into the system. Im trying to determine the cleanest way to do this that will not create a large load on the system.
I know that the system keeps track of the "last logged in time" So I could do a query and aggreate the number of users whose last login is within a given time?
Are there better solutions??
Any thoughts are welcome!! Please.

Using the last logged in time doesn't work. Your sessions can last for days/weeks so you won't know the amount of active users that way.
One solution would be to create a table with login sessions where you store that session id in a session cookie. Session cookies expire once the user has closed his/her browser window so it should give you quite an accurate estimation of when people logged in. If you actually want to store the entire session duration, than you will also have to update the table with every page view to store the time that the user was last active. This would be slightly heavier for your database ofcourse.

Related

Django Multiple concurrent logged-in accounts

In the context of a e-health related service, the end-users (health operators, doctors and physiotherapists) working in the same physical environment and sharing just one client PC to interact with my backend, asked me to provide them with a mechanism to quickly switch among different accounts (security is not a concern most of the time because they are used to work within a LAN but, occasionally, they might work from remote clients, so I must keep an authentication/authorization backend in place). They would log in just once and then, before using the web application, would select their logged-in account from a combobox (sort-of).
The UI model might be the same as in GMail where it's possible to keep multiple logged-in user accounts and switch between them through the account selector in the top right corner.
I'm no django-authentication expert, so I am not able to even tell whether this is possible in the context of a django based app.
Does anyone know of any ready-made app/middleware to get this done? Or maybe point me in the right direction if I have to extend or modify existing code?
Thanks a lot.
I haven't looked for any existing solution for this, so this is how I would go about it from scratch.
You'd need to add storage to the user session to hold multiple users. Currently it looks much like:
{'_auth_user_backend': 'membership.auth_backends.MyCustomAuthenticationBackend',
'_auth_user_hash': 'e2c8ecf1e7ecdbd<snip>',
'_auth_user_id': '3806'}
and I would add an array to the session:
logged_in_users = [{'_auth_user_backend': ... }, {}, {}] # one auth dict per user
Then when you "switch users", edit the session object to move that user's auth details (id, hash and backend) into the top-level ones in the session.
You'll also have to write a custom login function that stores the login into the logged_in_users array, and takes out the bit in the current login function that flushes your session if the key is the same as the session of a different logged in user. Similarly, you'll need to think what happens when you logout.

Django: Force users to re-login

I would like to know how I can force users to re-login in django?
My situation is the following: I'm developing have a facebook app, where I store some of their data.
As sometimes, it's expensive (time consuming) to update all users info, I don't do it at every access, but only when they login.
Sometimes, I do want to re-update their data to store new available data or even data due to new permissions, so I would like a way to force them to re-login. This will be done eventually, so no need for automation.
Any ideas?
Thanks in advance
Just delete the Session table. Their sessions are stored there and once they return to site they'll have to login again.

Saving Sitecore DMS Visitor info to a user

With Sitecore DMS you can create Profiles and show specific pages based on the user's visiting habits. This information is stored using a cookie so whenever the user comes back they have the same visitor profile. But if they delete their cookies or switch browsers that information is gone. Now what I want to do is save this visitor information to a sitecore user, in that they can log in and their visitor profiles will be the same as when they left, no matter where they are or what browser they are using. I've been trying to figure this out for a while now but without success. Whenever I login with a user and create a specific profile, that profile is deleted as soon as i log out. Is this even possible for Sitecore, because it seems rather silly to only have profiles based on cookies when cookies are deleted constantly.
With Sitecore Analytics, you have two tracking cookies, one is for Session--the other for GlobalSession (which doesn't expire across normal asp.net sessions). Unfortunately, Sitecore doesn't track profile key scores based on GlobalSession, but by a single asp.net session. The entire system is based around this, and it's very disappointing. '
To get around this, I was using OMS and use Reflector to disasssemble and rewrite some of the analytics code to record by global session. It also required a couple of schema changes as well as new rules... pretty extensive work. At the end of the day, because of the size of the database and our need for a very limited number of features, I ended up creating a single table and recording profile key values in that by globalsession.
From what I understand, the schema has changed from OMS to DMS, but the single most important factor, that data is recorded by session, has not changed.
With regard to storing the User's id, you can associate this with a 'tag' which is stored, I believe, with the globalsessionid, at least in OMS. However, Sitecore doesn't update the tag records, so you'll end up with multiple records per global session. If you're storing this value on every request, that table will bloat quickly.
There are a couple of reasons for this... not the least of which is that in many cases, you don't have a logged in user to correlate this information with. The profile data isn't stored in cookies... it's stored in the Analytics table. But it's associated with a cookie that has a unique ID and once that has been deleted, the ID (hopefully!) won't be used again.
A suggestion for how to get around this here in this StackOverflow answer.

Sitecore: tracking of the last visited page

On the site that I'm developing we need to track the last visited page for each user (users login to the site). What's the best way to do this? We are already using a custom profile so adding a new field is easy. The site will not have a lot of traffic so updating this field wont be an issue, i think. Are there better ideas? Does sitecore already offers something that we can possible use?
OMS has a "Top Exit Pages" report by default... but that is tracked across sessions, not users.
A good IIS log parser should also be able to give you this information... again, that would be by session (or IP) and not logged in user.
If you really want to get every exit page AND filter by logged in user... what I would do is add a new pipeline processor to httpRequestBegin, and place it after the ItemResolver. Then save the Item.Paths.Path. I would advise against writing this data to the user Profile if you are using the default ASP.NET Profile handler and you have a decent amount of traffic, because it is highly inefficient. Roll your own simple storage solution here, or just dump the data to a log.
My first question would be: Why do you need only the last visited page of the user? What are you trying to determine?
In a lot of cases, you are probably starting down an analytics route, or perhaps even trying to drive some marketing.
If the analytics is what you are going for, you can probably just pop an event out to your google analytics account with the current username as an event variable to allow you to look at analytics by user and by page. Alternatively, you could use the Sitecore OMS/DMS features for tracking all that data and looking at the analytics there.
If you are looking at driving marketing, you probably want to use OMS/DMS, especially if you want to start getting into personalization or engagement plans. OMS/DMS will track user activity, and all the pages they visit, though not by user account. With some customizations, you can probably add that data in, but it will depend on what you are trying to use the information for. The username may not be what is important to you.
If, however, you just want to know what page to send the user back to after logging them in, it would be better to just store that in session or pass as a post parameter if session is not a viable option for you.

Track page time spent with cookie

I have an asp.net web app that has Session state stored in sql server. I need to keep track of the time spent by a user on each page. How can i do that using cookies?
Couldn't you use a cookie to store the time when the user navigated to the page and then when they navigate to another page calculate the time spent on the previous page, enter that into the database or something and then repeat the process?
If you can get away with tracking time for all users collectively rather than individual users, I would install Google Analytics.
http://www.google.com/analytics/
Tracking user time across a site isn't ideally done in the cookies, which are more for providng things back to the user.
That said, you'd need to store the intermediate data in the cookie if you're going to store the true 'time per page' per user rather than faking it at the server by counting the gap between page requests per session. You could do this by setting the load time at the page load, then comparing that to the current time in the onUnload event hander. Save that to the cookie and it should be available to the server in the request body of the next page it sends.