laravel blade check whether authenticated user exists in db - if-statement

I'm working on user request model. According to the below mentioned code , if the authenticated use id exists in database, he should see thumbs-up(font awesome), suppose if not he should see request button(font awesome). I'm using ajax and JQuery, Everything working fine but I'm not getting how to check it using if condition. someone help me Plz..
<div class="col-sm-1 follows" style="padding:0px">
#if($followCount->user_id != Auth::user()->id)
<h2 id="deligate" value="1">
<i class="fa fa-user-plus" data-toggle="tooltip" title="follow" ></i>
</h2>
#else(!empty($followCount->user_id))
<h2 id="unfollow">
<i class="fa fa-thumbs-up" data-toggle="tooltip" title="unfollow" style="display:none;color: #ff9933" id="test"></i>
</h2>
#endif
</div>

For this you have to use only following code in blade file
#if(Auth::user())
{{ code for authorise user. }}
</h2>
#else
{{ Code for unauthorised user.}}
#endif
This check user authenticated user or not you have not need to add extra code for this.
Only use Auth::user() this check and match with your users table in DB.
Thank You and hope this work for you.

Related

How can I get custom form field value from within Django Admin's response_change?

I've added a custom functionality to a model by overriding change_form.html. Basically, I'm letting users change the objects of a model if these changes were approved by the admin. I added two buttons, named accept-suggestion and decline-suggestion and I intend to handle the custom functionality through response_change method:
def response_change(self, request, obj):
if "decline-suggestion" in request.POST:
# do stuff...
if "accept-suggestion" in request.POST:
# do stuff...
Both buttons will send an e-mail to the user saying if the suggestion was declined or approaved. So far so good. The problem is that I want to add the possibility to the admin write a brief justification explaining why the suggestion was declined. So I changed change_form.html again.
<div class="submit-row">
<div class="float-left">
<a class="decline-button-outlined accordion" type="button" href="#">DECLINE SUGGESTION</a>
</div>
<div class="float-right">
<input class="accept-button" type="submit" name="accept-suggestion" value="ACEITAR SUGESTÃO">
</div>
</div>
<div class="additional-infos">
<fieldset class="module aligned">
<div class="form-row">
<label for="decline-reasons">Reasons for rejection:</label>
<textarea
placeholder="If you find necessary, provide information on the reasons that led to the rejection of the suggestion"
id="decline-reasons" class="vLargeTextField" rows="5"></textarea>
</div>
<div class="submit-row">
<div class="float-right">
<input class="decline-button" type="submit" name="decline-suggestion" value="DECLINE">
</div>
</div>
</fieldset>
</div>
Is this the best approach? If so, how can I get the value of the <textarea> above from within response_change? If not, what would you suggest?
Thank you very much!
If you add a name to your <textarea> you will be able to retrieve the contents on the server side. Without a name, the data is not being sent to the server (Django).
So something like this:
<textarea
placeholder="If you find necessary, provide information on the reasons that led to the rejection of the suggestion"
id="decline-reasons" name="decline-reasons" class="vLargeTextField" rows="5"></textarea>
Should allow you to retrieve the text on the Django side with request.POST["decline-reasons"].

How to implement a google-login for any google APIs in a Django wep-app?

I'm currently developing a Django web app that will allow users to sign-in with their google account and get access to their Google Calendar and Google Drive data.
I already succeeded in doing so on another project in a front-end context using an Angular library called ng-gapi (GoogleAuthService) but I can't seem to find an equivalent for Django! All I was able to do for the moment is to implement a simple google login, but can't communicate with the different google APIs.
I have tried to use a module called django-allauth but it only logs-in the user via google but without providing any useful information (like authorization tokens etc...) needed to grant the web app to communicate with the user's google data.
{% load socialaccount %}
{% providers_media_js %}
{% load static %}
<html>
<body>
{% if user.is_authenticated %}
<p>Welcome {{ user.username }}</p>
<a class="btn btn-warning" href="http://localhost:8000/accounts/logout/">Log out</a>
<a class="btn btn-secondary" href="http://localhost:8000/show/">My Gigs</a>
<p>You're logged in with {{ user.get_provider }} as {{ user }}.</p>
<img style="max-width: 80px; padding:10px; margin-bottom:10px" src="{{ user.socialaccount_set.all.0.get_avatar_url }}" />
<p>UID: {{ user.socialaccount_set.all.0.uid }}</p>
<p>Date Joined: {{ user.socialaccount_set.all.0.date_joined}}</p>
<p>Last Login: {{ user.socialaccount_set.all.0.last_login}}</p>
<p>{{ user.socialaccount_set.all.0.extra_data.name }}</p>
{% else %}
<a class="btn btn-primary" href="{% provider_login_url 'google' %}">Log in</a>
{% endif %}
</body>
</html>
When I try to log-in this way, I get the google sign-in page which works well, but I don't have the second window which would have to be the one concerning the user granting my web-app to have access to his Google Calendar and Google Drive data (even though I have already configured it in my google developer console).
I would be immensely grateful if you could help me, I know that it looks as if this question has been answered multiple times but I have read hundreds of posts on the subject and couldn't find a solution.
You can use Django social auth library
An example of the implementation is here
You should enable access to google calendar and disk API from GOOGLE API CONSOLE

Data base saving & Redirection issue using django

I have an issue, I do not know why it is happening and how to solve it;
My app ask a user to create a project and is redirect directly to the project detail page. On that detail page if a team_id is empty I ask the user to create a team and when the team is created the user is redirected again to the project detail page to now be able to populate his team.
I used the code {% if Project.team_id == None %} when the user is redirected after creating his team but it is not working .. could you please help ? It is like before the redirection the new team is not saved in the Db ..
my html:
{% extends 'base.html' %}
{% block body %}
<div class="container">
<div class="jumbotron">
<h2>Welcome to your Project {{ project.name }} Detail page</h2>
</div>
{% if Project.team_id == None %}
<div class="invite-team">
<div class="jumbotron">
<div class="jumbo-text">
<h3>It is time to link a team to your project now create a new team and add team members</h3>
</div>
<div class="jumbo-button">
<span class="glyphicon glyphicon-plus"></span> Create a new team
</div>
</div>
{% else %}
<div class="invite-teammembers">
<div class="jumbotron">
<div class="jumbo-text">
<h3>The team {{ project.team_id }} has beed created, we now need to add TeamMembers</h3>
</div>
<div class="jumbo-button">
<span class="glyphicon glyphicon-plus"></span> Create a new team
</div>
</div>
</div>
{% endif %}
</div>
</div>
{% endblock%}
Looking at your surrounding code, you are using project as the container of your project. However, in your statement you are using Project (first character uppercase). Changing Project to project might help.
Your comment question:
what do you mean "Looking at your surrounding code, you are using project as the container of your project". my model Project is with a capital letter why now it is without ?
With looking at the surrounding code I mean that I literally looked at your code how you are using the variables in the other parts of your code. I am not sure if you are using CBV (class based views) or FBV (function based views).
With CBV the object is added to the context with the name defined in:
DetailView:81
or ListView:104
You can override the context object name by using the context_object_name in the View class
If you are using FBV, you have added it to the context manually as something like:
return render(request, 'myapp/template.html', {
'project': <project_query_or_variable>,
})

How can I login again when I logout using Google Plus Auth

To login with Google Plus Auth, I use the example of Python Social Auth at https://github.com/omab/python-social-auth/tree/master/examples/django_example
And I have the scenario as follows:
Firstly, I login by google plus sign in button
<div id="signinButton">
<span class="g-signin" data-scope="{{ plus_scope }}"
data-clientid="{{ plus_id }}"
data-redirecturi="postmessage"
data-accesstype="offline"
data-cookiepolicy="single_host_origin"
data-callback="signInCallback">
</span>
</div>
Secondly, I click on the logout button to sign out
<a class="btn btn-primary" href="/logout/">
<i class="fa fa-sign-out"></i>
Logout
</a>
Finally, when I login again by Google Plus sign button, the Google popup can not display to enter the username and password. However, I can login again by the button of Django example
<a class="col-md-2 btn btn-default" name="{{ backend|backend_class }}" href="{% url "social:begin" backend=name %}">
<i class="fa fa-{{ name|icon_name }}"></i>
{{ backend|backend_name }}
</a>
But I don't like this method because this is not recommended from Google https://developers.google.com/+/web/signin/redirect-uri-flow
As I understand, Google popup cannot display because I logged in by Google. I just logout from my django example app. For that reason, the popup cannot open.
However, I think this is a common action of users. They can login/logout many times from our Django apps and they don't need to know about their Google status.
So, is there any approach to login again for my scenario?
Thank you so much
Notes: This is my screen shot

Django Caching - Not caching for certain part of the page

I have a problem with django caching for login area using Safari.
When I use memcache and have 'django.middleware.cache.FetchFromCacheMiddleware' on, the login part of the page is always cached, showing "Welcome username" even after I press Logout.
It should change back to "login/sign up" link after logout is clicked. This seems to happen only in safari. Is there any way to not cache a part of the HTML page? I used #never_cache when it comes to view functions, but this login information is part of the base.html which gets extended throughout other site template htmls.
Thank you.
Code
In base.html
{% if user.is_authenticated %}
<div class="login">
<a id="login_dropdown_link" title="Login_nav">Welcome {{ user.username }} ( <span id="total_count"> {% total_count %}</span> )</a>
</div>
{% else %}
<div class="login">
<span class="spaced">Log In | Sign Up</span>
</div>
{% endif %}
This is most likely due to a header issue where Safari is being instructed to cache the page for a certain amount of time.