Allauth Facebook profile image not loading - python

I know this is a frequent issue, so pardon me for asking it again (it's really not a lack of research), I just can't seem to find a way to make Facebook Profile Picture load using Allauth.
I have used http://www.sarahhagstrom.com/2013/09/the-missing-django-allauth-tutorial/ for some tips, they all worked fine, but for Facebook Profile Picture seems to be missing something, the code on models.py responsible to pass the URL for Facebook Profile Picture can't get Facebook User Id
On the code below...
http://graph.facebook.com/{}/picture?width=40&height=40
... I've tried to use fb_uid and user_id but still doesn't load.
The profile URL is being called on the template as:
{% if request.user.is_authenticated %}
<img src="{{ request.user.profile.profile_image_url }}"/>
{% endif %}
Suggested here How can I get the user's facebook id with django-allauth? by Pennersr also doesn't give me access to users ID.
Thank you

The facebook api does not allow direct access to the image,You must write code to download the response from facebook profile url to your server and use your copy of the image.
The above code gives you URL which facebook redirects to their storage,so use
url = "http://graph.facebook.com/%s/picture?type=large" % response['id']
avatar = urlopen(url)
profile = user.get_profile()
profile.profile_photo.save(slugify(user.username + " social") + '.jpg',
ContentFile(avatar.read()))
profile.save()
But this approach is not recommended as it will cause delay in response,Some recommend celery background task to download the image,so look into that too.

Related

Django - social_auth - How could I separate social_auth_facebook_scope to request different scope

I am a new web developer using the Django framework and I try to set multiple scopes for social_auth_facebook_scope in the Django framework.
What I have done is ref from this link: How can I ask for different permissions from facebook at different times?
But I didn't know how to do it so
First,I put the FACEBOOK_SCOPE and FACEBOOK_CUSTOM_SCOPE in the setting.py file
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email', 'user_link']
SOCIAL_AUTH_FACEBOOK_CUSTOM_SCOPE = ['instragram_basic']
Second, I don't know where to put this code in.
class CustomFacebookOAuth2(FacebookOAuth2) :
name = 'facebook-custom'
Is it have to use in views.py or apps.py, I don't have any ideas.
Next, I want my 2 buttons to separate each request
First button is to log in with Facebook only
Second button is to request permission for an Instagram business account
so how do I config in my HTML file
Now what I am config is set the URL to call Facebook-custom to request different scope but it doesn't work and return Error pages not found
http://localhost:8000/social-auth/login/facebook-custom/
Instagram business Account HTML
class="nav-link" href="{% url 'social:begin' 'facebook-custom' %}">Instragram business account
How can I fix it, Anyone has an idea.

Link not working in anchor tag in django Template

I have a django project with custom user model. When the user registers, he also gives url of his company's website. I access and display it in the template using the USER. But it won't open as a separate link when I write it in an anchor tag as follows:
<li><b>Company Website</b><br>{{ detail.company_site }}</li>
Instead it takes me to this link
http://localhost:8000/detail/ahftech.com
Well, I figured it out myself. I simply added https:// as follows:
Before:
<li><b>Company Website</b><br>{{ detail.company_site }}</li
After:
<li><b>Company Website</b><br>{{ detail.company_site }}</li>

Skipping Initial Login Page with Django Allauth

I am using django-allauth for a website. I have integrated Google login through it. Currently, when I access the login, I get the standard sign-in page, on which is a link to sign in through Google. What is the best way to skip the standard sign-in page and go straight to the Google one (as in the one on accounts.google.com...)? All login is done through Google, so I don't need to see the initial page, just the Google one.
Should I override the provided template to just redirect? Or is there a better way to configure it?
I know it is a old topic, but I had the same issue and could not find a Solution on the web because of that I want to share my best solution. First of all I created a new socialaccounts template, so that this one will be taken instead of the original allauth template. For that you need to create a new folder and html file as followed:
"template->socialaccount-> login.html"
Thats the page you want to skip. To do that I simply inserted a javascript which, submits the form as soon the page is loaded:
{%load socialaccount%}
<body onload="document.forms['google_login'].submit()">
<p>You should be redirect to the Google login page in some seconds otherwise use the button below.</p>
<form method="post" name= "google_login">
{% csrf_token %}
<button type="submit">Continue</button>
</form>
</body>
Maybe this helps someone else.
Best regards
I just copied the address to which the existing Django-template hyperlink pointed, and then created a view to redirect all logins to that link with the correct return page. My URL pattern:
url(r'^login/$', views.redirectToGoogle, name="rediect_to_Google")
My view was:
GOOGLE_LOGIN_URL_PREFIX = '/accounts/google/login/?'
def redirectToGoogle(request):
coming_from = request.GET.get("next", "/manage")
url_params = {
"process": "login",
"next": coming_from
}
suffix = urllib.parse.urlencode(url_params)
return redirect(GOOGLE_LOGIN_URL_PREFIX + suffix)

django - render a file of a model to template

I have a model called Advertisement. This model has text field, title field, and file field.
I am successfully saving all 3 of these fields into the model.
Now I need to show them in template. My vision is:
ads = Advertisement.objects.all()
return render_to_response('page.html', {'ads':ads},context_instance=RequestContext(request))
and in my page.html:
{% for each_ad in ads %}
<p>{{each_ad.title}}</p>
<p>{{each_ad.text}}</p>
<p><a href="/ads/{{each_ad.file_pdf}}>{{each_ad.file_pdf.name}}</a></p>
{% endfor %}
Does this seem right? If not, please show me the way so I can learn. Thanks!
Whether the PDF link downloads depends on the browser of the user.
Adding target="_blank" will open the pdf in a new window/tab though:
<p>{{each_ad.file_pdf.name}}</p>
To ensure that it always downloads in every environment is more difficult, you would need to set your .htaccess (if you're using apache, per something like this: Force a file or image to download using .htaccess) or force it as an attachment in producing the PDF from Django.

Uploading files in django admin, download through public site

I am running a Django website and I want to be able to upload a file through my admin panel and then have visitors to the main site be able to download it. I am running my site using Django-nonrel, Django FileTransfers and Google App Engine. I believe the upload functionality is working correctly as I am able to see the file in my App Engine Blob Storage. What I can't seem to figure out is how to present a download link to the specified file on the public website. I have pasted the relevant classes below:
I have an app called Calendar, that has the following model:
class CalendarEvent (models.Model):
start = models.DateTimeField(auto_now=False, auto_now_add=False)
end = models.DateTimeField(auto_now=False, auto_now_add=False)
title = models.CharField(max_length=500)
description = models.TextField()
file = models.FileField(upload_to='uploads/%Y/%m/%d/%H/%M/%S/')
Here is the view:
def calendar(request):
events = CalendarEvent.objects.exclude(start__lt=datetime.datetime.now()).order_by('start')
return render_to_response('home/calendar.html',{'events': events},context_instance=RequestContext(request))
def download_handler(request, pk):
upload = get_object_or_404(CalendarEvent, pk=pk)
return serve_file(request, upload.file, save_as=True)
Here is my admin:
class calendarAdmin(admin.ModelAdmin):
list_display = ('title','start','end')
admin.site.register(CalendarEvent, calendarAdmin)
Finally, here is the relevant part of my template:
{% for e in events %}
{% url Calendar.views.download_handler pk=e.pk as fallback_url %}
Download
{% endfor %}
{% firstof e.file|public_download_url fallback_url %} is just returning blank, i'm not sure where I am going wrong.
The GAE blob store does not support public download according to the documentation here, so if you use the default backend for public download urls, it returns None. So my guess is that e.file|public_download_url always return None. You could verify that.
Then I think your template is wrong. You're trying to access e.views.download_handler where it should be Calendar.views.download_handler if your app is named Calendar.
I think the sample on the django-filetransfers page is error prone because the variable used in the template loop has the same name as the sample app: "upload".
If this doesn't fix it, could you post your urls.py from Calendar app. It could be that the template's url method is not able to resolve the url for Calendar.views.download_handler if there is no mapping in urlpatterns.
You should have something like
urlpatterns = patterns('Calendar.views',
...
(r'^download/(?P<pk>.+)$', 'download_handler'),
...
)
in this file.
I don't see anything special, eg
Download File
should work, or just use e.file.url directly?
I haven't deployed on Google App Engine myself, but this appears to be what django-filetransfers was designed for:
http://www.allbuttonspressed.com/projects/django-filetransfers#handling-downloads
edit: I believe I've answered this in the other question you posted, then: Trouble downlaoding file using Django FileTransfers
I think easiest way is to write a view since this file blob cannot be retrieved directly to write a function which is such:
def file_transfer(req, calendar_event_id):
try:
ce = CalendarEvent.objects.get(calendar_event_id)
except CalendarEvent.DoesNotExist:
raise Http404()
file = ce.file (Write Google appengine specfic routine to pull file)
return HttpResponse(file, media_type='file content type')
Hook it up on urls.py

Categories