Skip first step of django two-factor auth - django

The first step of Django two-factor auth is just a page where user is asked to click on "activate two factor authentication".
How can I skip that step wizard.steps.current == 'welcome' and get the user directly to the actual page where he chooses his authentication method. wizard.steps.current == 'method'.
{% if wizard.steps.current == 'welcome' %}
...
{% elif wizard.steps.current == 'method' %}
...
{% elif ... %}
...
{% endif %}
A possible solution is to override the wizard.step.current by setting it up to 'method' before user hit the setup.html page but I don't know to achieve this.
Thanks

The solution was quite easy.
In the core.py of the two-factor package find the class SetupView() and comment or remove the line ('welcome', Form)
in the view that preceed the two factor setup insert the following code:
return redirect('/account/two_factor/setup/').
This will redirect directly the user to setup page and skip two-factor explanatory page

Related

Django Template strange behavior of layout

I have a function which generate a pdf.file and send it by email. And it works perfect. And I have a Table on my frontend
like above.
In my Django Model - Point 1 set by default as False, by condition if Point 1 is False - Cell 2 is empty, else - marked as Done. When I changing Table via Django form it works fine as well (frontend marked as Done). The problem is when I trying to change this via function which generate a pdf. I have added below lines of code in my pdf.generate Function:
def generatePdf(request, pk):
point = get_object_or_404(MyObj.objects.select_related('related'), pk=pk)
...
email.send(fail_silently=False)
point.one = True
print(point.one)
messages.success(request, 'Success')
return HttpResponseRedirect....
at the terminal I got the message that value changed properly from False to True
but for some reason Cell 2 in my Table on the frontend still is empty...
Part of frontend code:
{% for item in object_list %}
...
<td>
{% if item.one %}
<span><i class="fa fa-solid fa-check"></i></span>
{% else %}
<span></span>
{% endif %}
</td>
...
{% endfor %}
Summarizing the above said - why frontend condition working properly only if I change via Django form (function) and not if I trying to change via generatePdf function? Cell value in database changed properly in both ways!
I just forgot to add:
point.save()
in order to save to database changes.
So simple...

how to make get user specific posts in django

I am creating a web app where users are allowed to post stuffs and i have a profile section where i want users to see the stuffs they have posted so i am using IF loop in django.
{% if user_post.author == "request.user.username" %}
hello there {{user_post.title}}
{% else %}
againnn
{{user_post.author}}
{{request.user.username}}
{% endif%}
here in the if loop i am checking for all the authors and comparing them with the user logged in.
but it is not executing, in the else part i am confirming names of the user . but the else part is executing.
i am not sure if it can be done this way..if not please suggest me a way
thanks
how about change the codes.
{% if user_post.author == "request.user.username" %}
to
{% if user_post.author == request.user.username %}

How do i check user authentication on application level Django

How do check the user is authenticated or not when some one try to access a page through url.
ex: http://127.0.0.1/test
when some one types the url i want to check in the page that url redirects, that user is authenticated or not.?
If you're doing it inside view, you can check request.user.is_authenticated(). In some methods of Class-based views, this is not provided as parameter, so you can access it using self.request.
If you're trying to do it inside template:
{% if request.user.is_authenticated %}
You're authenticated!
{% else %}
You're not authenticated :(
{% endif %}
but request must be passed into context, by context processor or by hand, inside your view function.

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.

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 😊