Facebook Connect: capturing user data with django-profiles and django-socialregistration - django

Either my google searching has completely left me or there's hardly any documentation/tutorials for django-socialregistration. Too bad, because it seems like a nice enough app. Through some trial-and-error, I have managed to get it mostly running on my site.
My question, using django-socialregistration how do I request permission for the facebook user's full name, current city and date of birth and store it in my UserProfile table (which is my AUTH_PROFILE_MODULE for django-profiles) in Django upon registration? Also, how do I post to the user's wall from Django once the connection is made?
Currently, when I click the "Connect with Facebook" button the facebook connection is made, a new Django user is created and the user is logged in with that Django account. However, no UserProfile is created and no facebook profile data is saved.
Any facebook connect gurus out there want to help the Django pony fly to Facebookland?
Setup:
- Django 1.2.1
- Python 2.5.2
- django-socialregistration 0.4.2
- django-registration 0.7
- django-profiles 0.2
"Kind sir, can you please help me find the magical Facebookland?"

In facebook_js.html you need to adjust the following line, by uncommenting items that you need to get from FB:
FB.login(handleResponse/*,{perms:'publish_stream,sms,offline_access,email,read_stream,status_update,etc'}*/);
Then, in FacebookMiddleware you can extract that data from fb_user, like this:
facebook.GraphAPI(fb_user['access_token']).get_object('me')

FWIW, I just found this moderately helpful nugget from the app author buried in the "Issues" section on github:
question from "tolano":
I have a profile model associated with the users, and everytime the user is created the profile should be created also. Should we create a new custom setup view for this purpose?
I'm finding several problems because the documentation is poor. Thank you very much.
answer from "flashingpumpkin":
Yes. Ideally you'll overwrite the setup view with your own. An easier method to adjust what is done on user creation is to pass a custom form into the setup view. You'll do that by overriding the standard url.

Here's another relevant nugget (source: http://github.com/flashingpumpkin/django-socialregistration/issues/closed#issue/7) Enough of these and this page will become the de facto django-socialregistration documentation ;)
question from "girasquid":
Maybe I'm just missing something, but I'm stuck here - is there a way to 'connect' accounts on other sites to an already-existing user?
For example, I've already signed up on Really Awesome Website, so I don't need to sign up again - but I'd like to connect my Facebook and Twitter accounts so that I can sign in with those as well.
Is there a way to do this already? If there isn't...how would I do it?
answer from "flashingpumpkin":
Yes there is. Just use the same template tags for Facebook Connect as you would for registration. Depending on if the user is already logged in or not it will create just the FacebookProfile object and link it to the existing user - or create both, the User object and the FacebookProfile object.
Have a look here:
http://github.com/flashingpumpkin/django-socialregistration/blob/master/socialregistration/templates/socialregistration/facebook_button.html
and
http://github.com/flashingpumpkin/django-socialregistration/blob/master/socialregistration/templatetags/facebook_tags.py

Related

Django Questionnaire app

I guys I am very new to Django and app dev and I am having trouble to structure my app.
I am creating an app to send team questionaire.So wokflow is the following:
1) I create a team_project (Team_name)
2) Send Invitations to team members using Emails
3) Based on that invitation Team_member signIn (creating a new user) and are directly assigned to that team created.
I have no idea how to handle that and especially part 3
If you could give me a direction how to do it I will really appreciate
Thx you very much
What you want is a common requirement, so perhaps there is already a library or solution for it.
to write the code from scratch, which is not recommended,
You can define a custom url , like example.com/join_team/some_random_looking_unique_string/
create a model which keeps a random string, email , and maybe some kind of expiration policy. Read the unique string in your view, and retrieve the record associated with it. send a form to get more details like password and etc and save the user in database.
I also found this repo that I think does what you want:
https://github.com/bee-keeper/django-invitations
explain more in your question, and you can get more detailed answers!

Django - User Model for Dating site - Admin -Staff/Agency+ other users

I'm trying to make my first django app (a dating site) that consist of varying user models.
Users need to have fields like location,language,religion, height, preferences, family details horoscope etc.
Staff/Agency - users added by Admin from the panel - some contact details like address,phone etc would be enough. No self registration required.
I prefer to have email-id as the USERNAME field.
Can someone please guide me how do I proceed to make User models in this case? I have been struggling to follow the docs and various thread on forums to get some light.
Any help would be highly appreciated.
Thanks.
You should 'extend' the django user class by creating a one to one model, generally called a profile that contains the rest of the information you need to gather on a person.
It is considered bad practise and difficult to extend djangos user class directly.
Have a look at this youtube video, it's a bit out of date so don't copy it word for word, but it gets the general concept across.
https://www.youtube.com/watch?v=qLRxkStiaUg

How to get django model relations using JSON?

I had no idea how to structure an accurate title for this question, but I did my best so please bear with me.
I am working on a app for my hockey team that consists of a django app and an mobile app that communicates with the django app using JSON (django-rest-framework). However, one problem I am struggeling with figuring out how to solve is as follows:
You create a user (using Token Authentication), and then you create a player and/or a manager.
However, what I am struggling with is what to do when an existing user logs in. How do I check whether or not there is a player or manager associated with that user? When I log in, all I get in return from the rest framework is that user's authentication token, so from a programming perspective, I have no clue what user it actually is since I dont have the user's Id. Even if I did, how can I look up players by anything other than their Id? Currently the only idea I have is to grab all players and loop through them to find one with the same email address as the user currently signed in has.
Hope this made some degree of sense!
Thanks
This isn't really a question about JSON.
Surely the token is associated with a user ID? I don't use django-rest-framework, but the documentation for TokenAuthentication is pretty clear that once the user logs in with their token, you'll get a normal auth.User instance in request.user just like you would with a standard web-based login.
Your second question is probably made irrelevant by that, but even so, you can always query by an email address, without needing to loop through:
Manager.objects.get(email=my_email_address)
Again this is standard Django querying - if you're not familiar with that syntax, you should do the Django tutorial.
Of course, since you have a User already, you can do a more efficient foreign key lookup:
Manager.objects.get(user=request.user)
or even
request.user.manager
(assuming you have a one-to-one relationship from User to Manager - it would have been helpful to see your models).

How do I dynamically create new pages in a Django wiki?

I am creating a wiki in Django where users should be able to register, login, create pages and other users who also login should be able to see all created pages and then either create new pages or edit existing ones.
I have already created registration and login pages and I am fine with creating and editing content on pages. My question is - do any of you (who know Django) know how I can implement the create new pages into my site? I think it will be in a "form" form where you then specify the URL, title, and content of the new page, but how do you actually create the new pages then and be able to view all created pages to any user?
I am stuck at this wall and any help would be appreciated!
At the time of page create with the form that you mentioned, if the user is logged in, you will want to store the user ID as a foreign key in your page Model. Then when you go to display created pages for a specific user, you just follow the relationship.
This was posted a long time ago, so you will probably just want to use this Django App as it solves all of this for you, in one beautiful package:
https://github.com/benjaoming/django-wiki
Have you seen the tutorial here? http://showmedo.com/videotutorials/video?name=1100000
I built a wiki with that tutorial - I think even if it doesn't answer your question may still point you in the right direction. Hope it helps!

Django Facebook Connect App Recommendation

I want to implement Facebook connect login for my Django site and I've checked the already existing apps.
So far, I have found Django-Socialauth, django-socialregistration, and django-facebookconnect for this purpose.
The question is which one to choose, and I would like to hear from other developers who have experience with any of these apps.
It is important for me that the Facebook Connect login app plays nicely with #login_required, with the default auth system, and with django-registration.
Please share your experience :)
Update (11/26/2013): I'm updating my recommendation. Since a sufficient amount of time has passed since I wrote this answer, I would recommend python-social-auth or django-allauth as the best tools for the job. They are active projects with good documentation and support for a lot more than just Facebook. I've had success using both.
I have had the most luck with adapting django-socialregistration with django-registration (0.8). Since you're working with django-registration, you're going to have to do a little bit of work, since all three of those packages assume the role of both the creation and the authentication of the user.
I was just going to explain what needed to be done, but you inspired me to finally get my version out: hello-social-registration.
Like I alluded to, it separates gives the registration functions to a django-registration backend and handles all the authorization itself. I've been using this on my near-beta application for a while now with no problems (I also handed it to a friend to use a few months ago and he got it to work without much modification).
It's definitely not ready to be a plug-and-play reusable application, yet, but hopefully it'll provide you with some insight. :)
By far the most commonly used package for Facebook authentication in Django is Django Facebook:
https://github.com/tschellenbach/Django-facebook
It also gives you access to the facebook APIs using the included Open Facebook api client.
I wanted to implement a basic "Login using Facebook" functionality in my Django app. I didn't want to show the user a form to fill or have her choose a password. I preferred to make it seamless.
Based on my requirements, django_facebook_oauth was the best app for me. It simply allows the user to login using facebook, and gets the user info my Facebook app requests from her (based on my Facebook Auth Dialog). It creates a new user in Django with the user's facebook email, a username and a blank password.
I highly recommend it.
Hi Take a look at fbconnect app that we (actually, Hernani, a guy on our team) put together for osqa (a clone of CNPROG).
You will have to, probably, tinker a bit to adapt that to your needs. It does work with #login_required decorator and the standard django.contrib.auth system, but we do not use django-registration.
Our app also works with openid and password login, but the openid part is tightly coupled with the Q&A component at present.
We may separate it though some time in the future, if anyone might be interested in "anything-signin" django pluggable app or has something better already - pls let us know.
I've used django-allauth and django-facebook on two different projects.
django-allauth was great and provided very good support for logging in and creating user profiles. It could also work with other auth providers, which I didn't implement.
django-facebook worked out of the box, but it's only compatible with Facebook. It also provided simple APIs for fetching users' likes and friends from Facebook directly into the db, which I liked very much!
facebook.get_and_store_likes(user)
facebook.get_and_store_friends(user)
I played with .NET based libraries and found them to be frustratingly out of date. Facebook seems to change their APIs frequently, so if you cannot find a library that is routinely maintained, you will find that you will get halfway through your implementation before you realize that there are serious problems.
I had some success with the javascript API that Facebook publishes and maintains. While the documentation may not be always up to date, I found that I was always within striking distance of the correct implementation (one or two changes needed).