Django user.is_authenticated reversed - django

In my template if user is anonymous,it would show Register button. So I use
{% if user.is_authenticated %}
Log out
{% else %}
Log in
But when I test it. It reversed.

I am sorry I login into admin site and I forgot to logout.

Related

Django - Validate search query within template

I need the search result to be validated together with the statement that it is a query inside the template.
So if the user is searching for another user and this user does not exist, it returns nothing. otherwise it returns the username of the user.
If request.user just checks the user that is logged in.
something like this
{% if request.GET.q and request.GET.q.is_authenticated %}
but this does not work :D Thank you
Can I use is_authenticated or is there a better way?
<li class="dropdown-hover">
<form class="form-inline">
{% include "tweets/search_form.html" %}
</form>
{% if user in request.GET.q.is_authenticated %}
<div class="dropdown-content x-card-4 x-bar-block" style="width:300px">
<a href='{{ request.GET.q }}'>{{ request.GET.q }}</a><br/>
{% else %}
<div class="dropdown-content x-card-4 x-bar-block" style="width:300px">
<a href='#'>No users found</a><br/>
{% endif %}
</li>
Thank you for any help
There is a better way, just do {% if request.user.is_authenticated %}.
you need to add loginRequiredMixin in your class so it allow to perform tasks only if user is logged in otherwise it redirect to login page.
#andre If you want to show/hide html tags in a template to the guest/logged-in user, you can use this {% if user.is_authenticated %} or you want to show some pages to the logged-in user #tabish-manzoor's solution is great.

Django check if the user that is logged in is the user that created a record

I have a Django model that records the user who created a record. I want to display a button on a template only if the user logged in is the same as the user on the record.
I want to do something similar to:
{% if user.is_authenticated and (request.user.is_superuser or request.user == task.user) %}
where task is the record.
How can I do this?
You can't use parentheses in the {% if %} template tag. You can use the following check which is equivalent:
{% if user.is_authenticated and request.user.is_superuser or user.is_authenticated and request.user == task.user %}
You can then drop the first user.is_authenticated check, because only an authenticated user will be a superuser.
{% if user.is_authenticated and request.user.is_superuser or user.is_authenticated and request.user == task.user %}
You may also be able to drop the second user.is_authenticated check if all tasks have a user (since an anonymous user is never equal to a real user).
{% if request.user.is_superuser or request.user == task.user %}
Django discourages putting complicated logic in the template. In this case, you might be able to put the logic in a filter, and then your template would simplify to:
{% if task|display_button:request.user %}...{% endif %}

Flask Admin Custom View

I am pretty new to Flask/Flask-Admin.
I have followed the tutorial on flask admin and managed to get the admin panel working but slightly lost on how to get the below things implemented.
https://github.com/flask-admin/flask-admin/tree/master/examples/auth
When logged in as a normal user I can only see "home" page.
How can I expose other views to "normal user" and restrict actions such as read only etc.
I have created a "baseview" which is not associated with any other models as below:
class SitesView(MyBaseView):
#expose('/')
def index(self):
return self.render('views/testviews.html')
admin.add_view(SitesView(name='Test views', endpoint='test views'))
and html as below:
{% extends 'admin/master.html' %}
{% block body %}
{{ super() }}
{% if current_user.has_role('view1') %}
Site1
{% endif %}
{% if current_user.has_role('view2') %}
<a>Site2</a>
{% endif %}
{% if current_user.has_role('view3') %}
<a>Site3</a>
{% endif %}
{% if current_user.has_role('view4') %}
<a>Site4</a>
{% endif %}
{% endblock %}
This gives me a new tab with different views with works as expected.
What I am trying to achieve here is when user click the Site1 link they go to Site1 page within flask-admin interface but I am not sure how to do that. I could create a new route for this but the problem is I can't(don't know how to) extend flask admin template.
For example this works but it redirect the page outside flask-admin template:
#app.route('/views/')
def views():
return render_template('views/views1.html')
and modified the templates>admin>index.html page with below:
<ul class="lead text-center list-group">
{% if current_user.has_role('view1') %}
<li class="list-group-item">View1</li>
{% endif %}
{% if current_user.has_role('view2') %}
<li class="list-group-item">View2</li>
{% endif %}
{% if current_user.has_role('view3') %}
<li class="list-group-item">View3</li>
{% endif %}
{% if current_user.has_role('view4') %}
<li class="list-group-item">View4</li>
{% endif %}
</ul
I want to build the whole web site using flask admin so that I can keep user experience consistence. Am I doing this the wrong way?
Thanks for your time.
Please do let me know if you want me to provide more information on this issue.
Kind Regards.
So after going through documentations and tutorials I have found the solution to my issue.
For my first question:
When logged in as a normal user I can only see "home" page. How can I
expose other views to "normal user" and restrict actions such as read
only etc.
We can do this by overwriting our view functions is_accessible method as below:
def is_accessible(self):
if not current_user.is_active or not current_user.is_authenticated:
return False
if current_user.has_role('superuser') or current_user.has_role('user') or current_user.has_role('view1'):
return True
return False
For my second question we just need to give the endpoint as for our BaseView as below:
class MyView(BaseView):
#expose('/')
def index(self):
return self.render('views.html')
admin.add_view(MyView(name='Custom Views', endpoint='customviews'))
And then in your jinja template you need to call it:
href="{{ url_for('customviews.index') }}
Just one thing to note, doing this:
current_user.has_role('superuser') or current_user.has_role('user') or current_user.has_role('view1')
could get quite messy if we have so many roles, not sure how we would approach this but hoping this will help someone.
Thanks all.
I know this is an old question, but for the following code
current_user.has_role('superuser') or current_user.has_role('user') or current_user.has_role('view1')
What I like to do is having a hybrid_property (available on both Peewee and SQLAlchemy) inside my User class that consolidates these properties. So it'd look something like this:
#hybrid_property
def user_has_administrative_rights(self):
return self.has_role('superuser') or self.has_role('user')

how to pass account information to django template in django-registration?

I want to pass the account information to the template so that when an user account is activated, there is a message saying "your account is activated; please log in now" with a link below. if the activation days have expired, it must say "activation days expired". I have url and template here, but I do not know how to pass the account information to the template.
url.py
urlpatterns = patterns('',
url(r'^activate/complete/$',
TemplateView.as_view(template_name='registration/activate.html'),
name='registration_activation_complete'),
......)
registration/activate.html
{% extends "registration/base.html" %}
{% block title %}Account activated{% endblock %}
{% block content %}
<h1>Account activated.</h1>
{% load humanize %}
{% if account %}
<p>Thanks for signing up! Now you can log in.</p>
{% else %}
<p>Sorry, it didn't work. Either your activation link was incorrect, or
the activation key for your account has expired; activation keys are
only valid for {{ expiration_days|apnumber }} days after
registration.</p>
{% endif %}
{% endblock %}
basically, I want to pass account and expiration_days to the above template. I just don't know how. help plz!
I'm afraid it won't be enough just to use TemplateView like you did; you'll have to write a custom view which will contain the logic for activation and pass the data to the template.

2 problems with registration

I use the obvious powerful registration 0.8 alpha version. I do have to questions.
1.) I want to add the checkbox RegistrationTermsOfService at the registration form html file. I just don't know how to do it. Please don't give me an link on uebernstorm docs. Just tell me please how to do it. I have read and tried a lot and it actually doesn't work. I am dispairing!
2.) The activation email is send with the activation key. When I click on activation the account is getting activated in the database but the template says something different. It says:"Sorry, it didn't work. Either..."
I use the standard activate.html below:
{% extends "base.html" %}
{% block title %}Account activated{% endblock %}
{% block content %}
Account activated
{% load humanize %}
{% if account %}
Thanks for signing up! Now you can "href"
{% else %}
Sorry, it didn't work. Either your activation link was incorrect, or
the activation key for your account has expired; activation keys are
only valid for {{ expiration_days|apnumber }} days after
registration.
{% endif %}
{% endblock %}
PLEASE, PLEASE help me!!! I am dispairing!!!
Craphunter
For 1) you need to use a different form. Look in registration.forms for the one you need and pass it as an additional argument in the backend's urls.py
For 2) we'll need more info - is the URL correct? Is there a RegistrationProfile object created for the user account in question? What is the value of the key?