XenoAura
2/13/2013 - 6:34 PM

Django-Social-Auth and Mezzanine/Cartridge integration from spring 2012. It looks like `mezzanine.accounts`, but the integration with that a

Django-Social-Auth and Mezzanine/Cartridge integration from spring 2012. It looks like mezzanine.accounts, but the integration with that app is likely incomplete.

from django.conf.urls.defaults import patterns, include, url
from django.contrib import admin

admin.autodiscover()

# Add the urlpatterns for any custom Django applications here.
# You can also change the ``home`` view to add your own functionality
# to the project's homepage.
urlpatterns = patterns("",
    url(r'', include('social_auth.urls')),
    ("^", include("mezzanine.urls")),
)
INSTALLED_APPS = (
    "django.contrib.admin",
    "django.contrib.admindocs",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.redirects",
    "django.contrib.sessions",
    "django.contrib.sites",
    "django.contrib.sitemaps",
    "django.contrib.staticfiles",
    "django.contrib.humanize",
    "gunicorn",
    "compressor",
    "social_auth",
    "cartridge.shop",
    "mezzanine.boot",
    "mezzanine.conf",
    "mezzanine.core",
    "mezzanine.accounts",
    "mezzanine.generic",
    "mezzanine.blog",
    "mezzanine.forms",
    "mezzanine.pages",
    "mezzanine.galleries",
    "mezzanine.twitter",
    "mezzanine.mobile",
) + PROJECT_APPS

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.contrib.messages.context_processors.messages",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.static",
    "django.core.context_processors.media",
    "django.core.context_processors.request",
    "mezzanine.conf.context_processors.settings",
    "social_auth.context_processors.social_auth_by_name_backends",
    "social_auth.context_processors.social_auth_backends",
    "social_auth.context_processors.social_auth_by_type_backends",
)

AUTHENTICATION_BACKENDS = (
    #"social_auth.backends.twitter.TwitterBackend",
    #"social_auth.backends.facebook.FacebookBackend",
    #"social_auth.backends.google.GoogleOAuthBackend",
    "social_auth.backends.google.GoogleOAuth2Backend",
    #"social_auth.backends.google.GoogleBackend",
    #"social_auth.backends.yahoo.YahooBackend",
    #"social_auth.backends.browserid.BrowserIDBackend",
    #"social_auth.backends.contrib.linkedin.LinkedinBackend",
    #"social_auth.backends.contrib.livejournal.LiveJournalBackend",
    #"social_auth.backends.contrib.orkut.OrkutBackend",
    #"social_auth.backends.contrib.foursquare.FoursquareBackend",
    #"social_auth.backends.contrib.github.GithubBackend",
    #"social_auth.backends.OpenIDBackend",
    "mezzanine.core.auth_backends.MezzanineBackend",
    #"django.contrib.auth.backends.ModelBackend",
)

###############
# SOCIAL AUTH #
###############

# URLs used for login/logout when ACCOUNTS_ENABLED is set to True.
LOGIN_URL = "/account/"
LOGOUT_URL = "/account/logout/"

SOCIAL_AUTH_LOGIN_URL = '/login/'
SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/'
SOCIAL_AUTH_LOGIN_ERROR_URL = '/error/'

SOCIAL_AUTH_COMPLETE_URL_NAME = 'socialauth_complete'
SOCIAL_AUTH_ASSOCIATE_URL_NAME = 'socialauth_associate_complete'


GOOGLE_OAUTH2_CLIENT_ID = ''
GOOGLE_OAUTH2_CLIENT_SECRET = ''

GOOGLE_OAUTH_EXTRA_SCOPE = ['https://gdata.youtube.com']
GOOGLE_OAUTH2_AUTH_EXTRA_ARGUMENTS = {'access_type': 'offline'}

SOCIAL_AUTH_RAISE_EXCEPTIONS = DEBUG
from django.contrib.auth.models import User
from django.db import models
from django.db.models.signals import post_save

from mezzanine.core.fields import RichTextField

from cartridge.shop.models import Product

from social_auth.signals import pre_update
from social_auth.backends.google import GoogleOAuth2Backend

from youtube.models import Video
from youtube.utils import WatchHistoryImport


class UserProfile(models.Model):
    user = models.OneToOneField(User)
    biography = RichTextField(blank=True)
    viewed_videos = models.ManyToManyField(Video, blank=True,
        related_name='video_viewers')
    products = models.ManyToManyField(Product, blank=True)

    def __unicode__(self):
        return self.user.username


def create_user_profile(sender, instance, created, **kwargs):
    """Create the UserProfile when a new User is saved"""
    if created:
        profile = UserProfile()
        profile.user = instance
        profile.save()

post_save.connect(create_user_profile, sender=User)
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])


def google_extra_values(sender, user, response, details, **kwargs):
    if user.user.social_auth.filter(provider='google-oauth2') == True:
        WatchHistoryImport(user)

pre_update.connect(google_extra_values, sender=GoogleOAuth2Backend)