-
Notifications
You must be signed in to change notification settings - Fork 5
Profiles
Abstract class Profile is used for every "auth service" that we support (Twitter, GitHub, etc.) as well as a default "AustinPython" (or something) if they just sign up via the site.
For example, if someone signs up via Twitter:
@profile:
class Twitter(Profile):
""" Extra data for Twitter Profile, in addition to the core profile. """
@property
def tweet_feed(self):
""" Return the URL to a persons tweets. """
return self.data.get("tweet_feed")
@classmethod
def populate_from_request(cls, request):
""" Return a populated instance of the profile """
data = self.get_default_data()
data["tweet_feed"] = request.POST.get("tweet_feed")
profile = cls(data=data, name=request.POST.get("name"), email=request.POST.get("email"))
# ... other stuff ...
return profile # never save, let the controller do that
def get_default_data(self):
""" Return a simple data dictionary for defaults. """
return { "tweet_feed": None } # or whatever
By registering it with the Profiles singleton (that's what the decorator does), this profile will be available attached to the user, and if it is the first profile, it will be used to determine profile images, the user's name and email, etc.
Use Case: New user signs up using the "built-in" registration.
- User fills out a form for our "internal" profile (AustinPython)
- new_user = User(...), new_user.save()
- new_profile = AustinPython.populate_from_request(REQUEST_INSTANCE)
- new_profile.default = True
- new_profile.user = new_user
- new_profile.save()
Use Case: New user signs up using Twitter
- User "authenticates" via Twitter
- On OAuth callback, new_user = User(...), new_user.save()
- new_profile = Twitter.populate_from_request(REQUEST_INSTANCE)
- new_profile.default = True
- new_profile.user = new_user
- new_profile.save()
Use Case: User "adds" GitHub to an existing account
- User clicks "Add GitHub" button on profile settings page
- On OAuth callback, user = request.user (raise 403 if not a "real" user)
- new_profile = GitHub.populate_from_request(REQUEST_INSTANCE)
- new_profile.user = user
- new_profile.save()
Retrieve the "default" user profile:
- request.user.profile # Your results may vary, check user for auth
Let the user "change" their default profile to GitHub:
- old_profile = request.user.profile
- old_profile.default = False
- old_profile.save()
- new_profile = GitHub(user=request.user)
- new_profile.default = True
- new_profile.save()
BIG FAT NOTICE: All profiles MUST support a few basic types for "generic" view purposes, i.e. email, username, etc.