How to know if a user is facebook logged in? Django - django

As the title indicates, I would like to know how to do to distinguish a user that is logged in with facebook from a user that is logged in by mail. I'm using django-facebook.
It seems that
request.user.is_authenticated()
is for every kind of authentification.
Any help would be welcome,

I think you can check if request.user has a FacebookProfile:
{% if request.user.facebookprofile %}user has facebook profile{% endif %}
Or in python:
if request.user.facebookprofile_id:
print 'has facebookprofile'

Related

Check for online users in django(django 1.10 python 3.5)

I am developing an app where employees would log in and search for already uploaded data,
I want to create a page for admin users where they can see which employees are online/offline/onBreak
How can i achieve this ?
After a lot of searching and re-searching i finally got the answer!
In views.py
def get_current_users(request):
active_sessions = Session.objects.filter(expire_date__gte=timezone.now())
user_id_list = []
for session in active_sessions:
data = session.get_decoded()
user_id_list.append(data.get('_auth_user_id', None))
return User.objects.filter(id__in=user_id_list)
def userList(request):
users = User.objects.all()
online = get_current_users([])
context = {
"users_list": users,
"online_list": online,
}
return render(request, 'Index/users.html',context)
Create this two views.(as a beginner that the best i could do feel free to manipulate the code)
in urls.py
url(r'^users/$', views.userList, name="user"),
then in users.html
{% extends "Index/header.html" %} {% block content %}
{% for user in users_list %}
{{ user }}
{% if user in online_list %}
online
{%else%}
offline
{% endif %}
{% endfor%}
{% endblock %}
What its basically doing is :
First its collecting all the logged in users
then gathering all listed users the cross checking weather users in the user list with logged in user list. If true its printing online if not its printing offline beside the user name.
To detect whether a user is online or offline you can follow the below
strategy.
Create new DB table which will track the records of all the online
users.
Steps -
Create model let say online_user_tracking with the fields ( id, user_id, entry_timestamp )
When user logs in, add an entry to this model with mentioned fields
When user log out delete the entry from this model
Now it could be possible that user may not log out properly and go away, in that case, you have to use keep alive (Polling) mechanism as
mentioned below -
Polling ( Keep alive ) -
When user login to your site the entry will be created to the model "online_user_tracking". Then on every 5 second's you have to
update its timestamp field for the current logged in user_id. Now on
the backend we have to write a script wich will be executed in every 5
seconds ( May have to schedule a CRON job on the server ) and check if
any records have not updated it's timestamp in last 3 minutes those
entries should be deleted.
That's how we can keep the records of online and offline users of the site.
Hope this explanation will help...
You can use django-socketio and define it to give the user status to the server from the client HTML. This will keep polling to the server about the status change.

How to know request.user logged in by facebook account

I have created an application for e commerce. There is option for facebook authentication.I want to check if the user logged in by facebook.Is there any condition to check request.user logged in by facebook?
I have searched google and got one condition but its not working for me
{% if request.user.facebookprofile %}user has facebook profile{% endif %}
You can do this in python
def isFacebook(user_id):
fb_uid = SocialAccount.objects.filter(user_id=self.user.id, provider='facebook')
return len(fb_uid) != 0

Check if current user is logged in using any django social auth provider

I would like to check if a user is logged in via social authentication or using the django default authentication.
something like
if user.social_auth = true?
Ok after doing some research i came up with this solution to make sure if a user is authenticated using any social provider or just the default django auth. Check here for moreinfo..
{% if user.is_authenticated and not backends.associated %}
#Do or show something if user is not authenticated with social provider but default auth
{% elif user.is_authenticated and backends.associated %}
#Do or show something if user is authenticated with social provider
{% else %}
#Do or show something if none of both
{% endif %}
i was searching on that and the solution is user.social_auth.exists()
it will return True if the user exists in the database else it will return false.
from social_auth.models import UserSocialAuth
try:
UserSocialAuth.objects.get(user_id=user.id)
except UserSocialAuth.DoesNotExist:
print "user is logged in using the django default authentication"
else:
print "user is logged in via social authentication"
You may to add a method to User model.
From Python:
user.social_auth.exists()
will return True if user is social, False otherwise.
From template:
{% if not backends.associated %}
<code if user is NOT social>
{% else %}
<code if user is social>
{% endif %}
The backends context variable is automatically set in Django templates when you include the python-social-auth app in your Django project's config.
Currently, the django-social-auth is deprecated. You can use python-social-auth instead.
In that case, you should use:
user.social_auth.filter(provider='BACKEND_NAME')
For instance, if current user is authenticated by Google Account:
if user.is_authenticated:
if user.social_auth.filter(provider='google-oauth2'):
print 'user is using Google Account!'
else:
print 'user is using Django default authentication or another social provider'
you can query all the users that are using social authentication as follow, from UserSocialAuth app:
from social_django.models import UserSocialAuth
if user in [u.user for u in UserSocialAuth.objects.all()]:
#dosomething
I have same problem to recognize social logged-in users by python-social-auth and I should specify a separate tab for them in navigation bar to complete their profile page. Of course if a user has logged-in through social auth, no password has been set for him\her in DB. So I used has_usable_password() method (django.contrib.auth) for this propose. For exp:
{% if user.has_usable_password %}
<a class="dropdown-item" href="{% url 'password_change' %}">Change password</a>
{% elif not user.has_usable_password %}
<a class="dropdown-item" href="{% url 'set_pass' %}">Set password</a>
Obviously this tip will helpful as long as the user doesn't set password.

Multiple views for webpage depending on authentication status in Django

I am currently working on a developing a web application that people can use to browse through book reviews, but if they login they can post comments on the book reviews, or post their own reviews of books.
While I have implemented posting new reviews. I am do not understand how to modify the method in my views.py file that renders a book review such that if the user is logged in, in shows all previous comments, a form for the user to post a comment and a logout button, and if the user is not logged in it simply shows the book review and a log in button.
You can tell if your user is logged in or not in your views like this:
if request.user.is_authenticated():
# logged in logic
return render_to_response("loggedin_templates.html")
else:
# not logged in logic
return render_to_response("not_loggedin_templates.html")
Or you can tell them in the templates like this:
{% if user.is_authenticated %}
<span>{{ user.username }}</span>
{% else %}
<li>login
{% endif %}

admin panel recent actions

I was wondering if I can get django admin (logged as super user) to display in some kind of "recent actions box", changes other users (non super users) made?
Thanx,
Luka
LogEntry.objects.log_action()
http://www.djangosnippets.org/snippets/1052/
Tying in to Django Admin's Model History
it was my problem too, and it has a simple answer
Just go this address and do the following;
Address: your virtulenv/lib/site-packages/django/contrib/admin/templates/admin
And find base.html, inside of this file edit after {% load Log %} and simply add this :
{% if user.is_superuser %}
{% get _admin_log (numbers of actions you want) as admin_log %}
And for the else do the same but at the end don't forget to add for_user user 😊