Skip to content
This repository has been archived by the owner on Nov 22, 2017. It is now read-only.

Commit

Permalink
add social login and user profile for storing extra info
Browse files Browse the repository at this point in the history
  • Loading branch information
swmerko authored and sils committed Nov 22, 2017
1 parent b341e2f commit 4e080ed
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 9 deletions.
79 changes: 77 additions & 2 deletions CONFER/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# libs
'rest_framework',
'conferences',
'corsheaders',
'social_django',
# apps
'accounts',
'conferences',
]

MIDDLEWARE = [
Expand All @@ -72,6 +76,8 @@
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'social_django.context_processors.backends',
'social_django.context_processors.login_redirect',
],
},
},
Expand Down Expand Up @@ -134,7 +140,76 @@
'localhost:8080'
)

AUTHENTICATION_BACKENDS = (
'social_core.backends.github.GithubOAuth2',
'django.contrib.auth.backends.ModelBackend'
)

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication'
)
}

# GOOGLE API

GOOGLE_API_BASE_URL = 'http://maps.google.com'
GOOGLE_GEOCODE_ENDPOINT = '{base_url}/maps/api/geocode/json'.format(base_url=GOOGLE_API_BASE_URL)
GOOGLE_GEOCODE_ENDPOINT = '{base_url}/maps/api/geocode/json'.format(base_url=GOOGLE_API_BASE_URL)

# python-social-auth settings
SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/'
SOCIAL_AUTH_LOGIN_URL = '/login'
SOCIAL_AUTH_PIPELINE = (
# Get the information we can about the user and return it in a simple
# format to create the user instance later. On some cases the details are
# already part of the auth response from the provider, but sometimes this
# could hit a provider API.
'social_core.pipeline.social_auth.social_details',

# Get the social uid from whichever service we're authing thru. The uid is
# the unique identifier of the given user in the provider.
'social_core.pipeline.social_auth.social_uid',

# Verifies that the current auth process is valid within the current
# project, this is where emails and domains whitelists are applied (if
# defined).
'social_core.pipeline.social_auth.auth_allowed',

# Checks if the current social-account is already associated in the site.
'social_core.pipeline.social_auth.social_user',

# Make up a username for this person, appends a random string at the end if
# there's any collision.
'social_core.pipeline.user.get_username',

# Send a validation email to the user to verify its email address.
# Disabled by default.
# 'social_core.pipeline.mail.mail_validation',

# Associates the current social details with another user account with
# a similar email address. Disabled by default.
'social_core.pipeline.social_auth.associate_by_email',

# Create a user account if we haven't found one yet.
'social_core.pipeline.user.create_user',

# Create the record that associates the social account with the user.
'social_core.pipeline.social_auth.associate_user',

# Populate the extra_data field in the social record with the values
# specified by settings (and the default ones like access_token, etc).
'social_core.pipeline.social_auth.load_extra_data',

# Update the user record with any changed info from the auth service.
'social_core.pipeline.user.user_details',
)


SOCIAL_AUTH_GITHUB_KEY = os.environ.get('SOCIAL_AUTH_GITHUB_KEY')
SOCIAL_AUTH_GITHUB_SECRET = os.environ.get('SOCIAL_AUTH_GITHUB_SECRET')

SOCIAL_AUTH_GITHUB_SCOPE = [
'user',
'user:email',
]
3 changes: 2 additions & 1 deletion CONFER/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'', include('api.urls')),
url(r'^conferences/', include('conferences.urls')),
url('', include('social_django.urls', namespace='social')),
url(r'', include('api.urls')),
]
Empty file added accounts/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions accounts/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib import admin

from .models import Profile

admin.site.register(Profile)
8 changes: 8 additions & 0 deletions accounts/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.apps import AppConfig


class AccountsConfig(AppConfig):
name = 'accounts'
26 changes: 26 additions & 0 deletions accounts/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2017-04-13 18:56
from __future__ import unicode_literals

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='Profile',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
Empty file added accounts/migrations/__init__.py
Empty file.
23 changes: 23 additions & 0 deletions accounts/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
from django.contrib.auth.models import User
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver


class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)

def __str__(self):
return self.user


@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)


@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
6 changes: 6 additions & 0 deletions accounts/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.test import TestCase

# Create your tests here.
6 changes: 6 additions & 0 deletions accounts/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render

# Create your views here.
13 changes: 7 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
django
djangorestframework
markdown
django-filter
requests
django-cors-headers
django==1.11
djangorestframework==3.6.2
markdown==2.6.8
django-filter==1.0.2
requests==2.13.0
django-cors-headers==2.0.2
social-auth-app-django==1.1.0

0 comments on commit 4e080ed

Please sign in to comment.