Skip to content

Commit

Permalink
added oauth with google, github and some minor edits
Browse files Browse the repository at this point in the history
  • Loading branch information
rammanoj committed Sep 10, 2018
1 parent e5b6ce1 commit c19eeb4
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 33 deletions.
8 changes: 6 additions & 2 deletions Home/templates/base/side-nav.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<li><a href="{% url 'achievements' %}">achievements</a></li>
</ul>
</li>
<li><a href="{% url 'workshop_list' %}">Events</a></li>
<li><a href="{% url 'workshop_list' %}">Workshops</a></li>
<li><a href="{% url 'project' %}">Projects</a></li>
{% if not request.user.is_authenticated %}
<li><a href="{% url 'join' %}">Join us</a></li>
Expand All @@ -54,7 +54,11 @@
{% else %} {% ifequal request.path '/accounts/login/' %}
<li><a href="{% url 'signup' %}">Sign Up</a></li>
{% else %}
<li><a href="{% url 'login' %}?next={{ request.path }}">Login</a></li>
{% ifequal request.path '/accounts/logout/' %}
<li><a href="{% url 'login' %}?next={{ "/" }}">Login</a></li>
{% else %}
<li><a href="{% url 'login' %}?next={{ request.path }}">Login</a></li>
{% endifequal %}
{% endifequal %}
</ul>
</li>
Expand Down
25 changes: 25 additions & 0 deletions fosswebsite/SampleLogin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
from django.shortcuts import redirect, render
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.auth.models import User

class CustomLogin(DefaultSocialAccountAdapter):

def is_auto_signup_allowed(self, request, sociallogin):
return False

def is_open_for_signup(self, request, sociallogin):
return True

def pre_social_login(self, request, sociallogin):
user = sociallogin.user
if user.id:
return
try:
existing_user = User.objects.get(email=sociallogin.account.extra_data['email'])
if existing_user.is_active:
sociallogin.connect(request, existing_user)
else:
return
except ObjectDoesNotExist:
pass
22 changes: 22 additions & 0 deletions fosswebsite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.github',
'allauth.socialaccount.providers.google',
'bootstrap3_datetime',
'achievements',
'clubManagement',
Expand Down Expand Up @@ -117,6 +118,27 @@
},
]

#social login with allauth

ACCOUNT_AUTHENTICATION_METHOD = 'username_email'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = True
SOCIALACCOUNT_QUERY_EMAIL = True
SOCIALACCOUNT_PROVIDERS = \
{'github': {
'SCOPE': ['user:email',],
},
'google': {
'SCOPE': ['https://www.googleapis.com/auth/userinfo.profile',
'email'],
'AUTH_PARAMS': {'access_type': 'online'}
},
}
SOCIALACCOUNT_ADAPTER = "fosswebsite.SampleLogin.CustomLogin"
SOCIALACCOUNT_AUTO_SIGNUP = False
SOCIALACCOUNT_FORMS = {
'signup': 'registration.forms.SocialSignup'
}
# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/

Expand Down
34 changes: 21 additions & 13 deletions projects/templates/projects/project_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,27 @@ <h1 class="hero-title font-alt">Projects</h1>
</div>
<div class="row">
{% for object in object_list %}
<div class="col-md-6">
<div class="card">
{% if object.image %}
<img class="card-img-top img-fluid" src="/static/media/{{ object.image }}">
{% else %}
<img class="card-img-top img-fluid"
src="{% static 'projects/images/default.png' %}">
{% endif %}
<div class="card-block">
<h3 class="card-title">{{ object.title }}</h3>
<h5 class="card-text">{{ object.description }}</h5>
<center><a href="{% url 'project_detail' object.id %}"
class="btn btn-dark anim-scroll">More info</a></center>
<div class="col-md-4">
<div class="card_display">
<div class="front">
{% if object.image %}
<img class="card-image img-fluid" src="/static/media/{{ object.image }}"
onerror=this.src="{% static 'projects/images/default.png' %}">
{% else %}
<img class="card-image img-fluid"
src="{% static 'projects/images/default.png' %}">
{% endif %}
<div class="card-block">
<center> <h3 style="padding: 4px;">{{ object.title }}</h3></center>
</div>
</div>
<div class="back">
<div class="card-block" style="padding:2%;">
<h3 class="card-title">{{ object.title }}</h3>
<h5 class="card-text">{{ object.description | truncatechars:60 }}</h5>
<center><a href="{% url 'project_detail' object.id %}"
class="btn btn-dark anim-scroll">More info</a></center>
</div>
</div>
</div>
</div>
Expand Down
15 changes: 15 additions & 0 deletions registration/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
from django.contrib.auth.models import User
from django.db.models.functions import datetime
from django.utils.translation import ugettext_lazy as _
from allauth.socialaccount.forms import SignupForm

from registration.models import UserInfo


class UserSignUpForm(forms.ModelForm):

"""
A form that creates a user, with no privileges, from the given username and
password.
Expand Down Expand Up @@ -160,3 +162,16 @@ class Meta:
fields = ['first_name', 'last_name', 'email', 'profile_pic', 'small_intro', 'intro', 'interests', 'expertise',
'gitHub', 'blog', 'linkedIn', 'googlePlus', 'facebook', 'twitter', 'year', 'resume', 'typing_speed',
'system_number']

class SocialSignup(SignupForm):

password1 = forms.CharField(label=_("Password"),
widget=forms.PasswordInput)
password2 = forms.CharField(label=_("Password confirmation"),
widget=forms.PasswordInput,
help_text=_("Enter the same password as above, for verification."))
year = forms.IntegerField(label=_("Year of admission"))

class Meta:
model = User
fields = ["first_name", "last_name", "email", "username"]
2 changes: 1 addition & 1 deletion registration/templates/registration/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@
<span>Sign in with <strong>GitHub</strong></span>
</div>
</a>
<a href="#" class="connect googleplus">
<a href="{% provider_login_url "google" process="login" %}" class="connect googleplus">
<div class="connect__icon">
<i class="fa fa-google-plus"></i>
</div>
Expand Down
191 changes: 191 additions & 0 deletions registration/templates/socialaccount/signup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
<!DOCTYPE html>
<html lang="en">
{% load static %}
{% load socialaccount %}

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">

<title>amFOSS. Code | Share | Grow</title>

<!-- Favicons -->
<link rel="shortcut icon" href="{% static 'home/assets/images/favicon.png' %}">
<link rel="apple-touch-icon" href="{% static 'home/assets/images/apple-touch-icon.png' %}">
<link rel="apple-touch-icon" sizes="72x72" href="{% static 'home/assets/images/apple-touch-icon-72x72.png' %}">
<link rel="apple-touch-icon" sizes="114x114" href="{% static 'home/assets/images/apple-touch-icon-114x114.png' %}">
<!-- Fonts -->
<link href='https://fonts.googleapis.com/css?family=Roboto+Condensed:400,300,400italic,700' rel='stylesheet'
type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,400italic,700' rel='stylesheet' type='text/css'>

<!-- Fonts -->
<link href='https://fonts.googleapis.com/css?family=Roboto+Condensed:400,300,400italic,700' rel='stylesheet'
type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,400italic,700' rel='stylesheet' type='text/css'>

<!-- Bootstrap core CSS -->
<link href="{% static 'home/assets/bootstrap/css/bootstrap.min.css' %}" rel="stylesheet">

<!-- Icon Fonts -->
<link href="{% static 'home/assets/css/font-awesome.min.css' %}" rel="stylesheet">
<link href="{% static 'home/assets/css/et-line-font.min.css' %}" rel="stylesheet">

<!-- Font awesome CDN -->
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"
integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">

<!-- Plugins -->
<link href="{% static 'home/assets/css/magnific-popup.css' %}" rel="stylesheet">
<link href="{% static 'home/assets/css/owl.carousel.css' %}" rel="stylesheet">
<link href="{% static 'home/assets/css/superslides.css' %}" rel="stylesheet">
<link href="{% static 'home/assets/css/vertical.min.css' %}" rel="stylesheet">

<!-- Template core CSS -->
<link href="{% static 'home/assets/css/template.css' %}" rel="stylesheet">
<link href="{% static 'home/css/login.css' %}" rel="stylesheet">
</head>

<body>

{#<!-- PRELOADER -->#}
{#<div class="page-loader">#}
{# <div class="loader">Loading...</div>#}
{#</div>#}
{#<!-- /PRELOADER -->#}

<!-- Incudes navigation bar. -->
{% include 'home/../../../Home/templates/base/side-nav.html' %}

<div class="notifications" style="padding-left: 80%;">
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div class="alert alert-danger alert-dismissable">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
{{ field.label }}: {{ error|escape }}
</div>
{% endfor %}
{% endfor %}
{% for error in form.non_field_errors %}
<div class="alert alert-danger alert-dismissable">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
{{ error|escape }}
</div>
{% endfor %}
{% endif %}
</div>


<div class="logmod">
<div class="logmod__wrapper">
<span class="logmod__close">Close</span>
<div class="logmod__container">
<ul class="logmod__tabs">
<li data-tabtar="lgm-1"><a href="#">Sign Up</a></li>
</ul>
<div class="logmod__tab-wrapper">
<div class="logmod__tab lgm-1">
<div class="logmod__heading">
<span class="logmod__heading-subtitle">Enter your personal details <strong>to create an account</strong></span>
</div>
<div class="logmod__form">
<form accept-charset="utf-8" action="{% url 'signup' %}" class="simform" method="post">
{% csrf_token %}
<div class="sminputs">
<div class="input string optional">
<label class="string optional" for="user-name">First Name*</label>
<input class="string optional" maxlength="255" id="user-name"
placeholder="First Name" name="first_name" type="text" size="50"
value="{{ form.first_name.value|default_if_none:"" }}"/>
</div>
<div class="input string optional">
<label class="string optional" for="user-name">Last Name*</label>
<input class="string optional" maxlength="255" id="user-name"
placeholder="Last Name" name="last_name" type="text" size="50"
value="{{ form.last_name.value|default_if_none:"" }}"/>
</div>
</div>
<div class="sminputs">
<div class="input full">
<label class="string optional" for="user-name">Email*</label>
<input class="string optional" maxlength="255" id="user-email" name="email"
placeholder="Email" type="email" size="50"
value="{{ form.email.value|default_if_none:"" }}"/>
</div>
</div>
<div class="sminputs">
<div class="input string optional">
<label class="string optional" for="user-name">UserName*</label>
<input class="string optional" maxlength="255" id="user-name" name="username"
placeholder="Username" type="text" size="50"
value="{{ form.username.value|default_if_none:"" }}"/>
</div>
<div class="input string optional">
<label class="string optional" for="user-name">Batch*</label>
<input type="number" min="2006" class="form-control" name="year" id="year"
value="{{ form.year.value|default_if_none:"" }}" placeholder="YYYY">
</div>
</div>
<div class="sminputs">
<div class="input string optional">
<label class="string optional" for="user-pw">Password *</label>
<input class="string optional" maxlength="255" name="password1" id="user-pw"
placeholder="Password" type="password" size="50"/>
</div>
<div class="input string optional">
<label class="string optional" for="user-pw-repeat">Repeat password *</label>
<input class="string optional" maxlength="255" name="password2" id="user-pw-repeat"
placeholder="Repeat password" type="password" size="50"
value="{{ object.email }}"/>
</div>
</div>
<div class="simform__actions">
<input class="sumbit" name="commit" type="submit" value="Create Account"/>
<span class="simform__actions-sidetext">By creating an account you agree to our <a
class="special" href="#" target="_blank" role="link">Terms & Privacy</a></span>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>

<hr class="divider">

{##}
{# <!-- Footer section -->#}
{# {% include 'home/footer.html' %}#}

<!-- /WRAPPER -->

<!-- JAVASCRIPT FILES -->
<script src="{% static 'home/assets/js/jquery-2.1.4.min.js' %}"></script>
<script src="{% static 'home/assets/bootstrap/js/bootstrap.min.js' %}"></script>
<script src="{% static 'home/assets/js/jquery.superslides.min.js' %}"></script>
<script src="{% static 'home/assets/js/jquery.mb.YTPlayer.min.js' %}"></script>
<script src="{% static 'home/assets/js/imagesloaded.pkgd.js' %}"></script>
<script src="{% static 'home/assets/js/isotope.pkgd.min.js' %}"></script>
<script src="{% static 'home/assets/js/jquery.magnific-popup.min.js' %}"></script>
<script src="{% static 'home/assets/js/owl.carousel.min.js' %}"></script>
<script src="{% static 'home/assets/js/jquery.fitvids.js' %}"></script>
<script src="{% static 'home/assets/js/jqBootstrapValidation.js' %}"></script>
<script src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script src="{% static 'home/assets/js/gmap3.min.js' %}"></script>
<script src="{% static 'home/assets/js/appear.js' %}"></script>
<script src="{% static 'home/assets/js/smoothscroll.js' %}"></script>
<script src="{% static 'home/assets/js/submenu-fix.js' %}"></script>
<script src="{% static 'home/assets/js/contact.js' %}"></script>
<script src="{% static 'home/assets/js/custom.js' %}"></script>

{# Login JavaScript Src#}

<script src="{% static 'home/js/login.js' %}"></script>

</body>

</html>
4 changes: 3 additions & 1 deletion registration/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.urls import reverse_lazy
from django.views.generic import CreateView, UpdateView, DetailView, ListView, View
import xlrd, datetime

from allauth.socialaccount.models import SocialAccount
from achievements.models import Contribution
from clubManagement.models import Team
from projects.models import Project
Expand Down Expand Up @@ -74,6 +74,8 @@ def get(self, request, *args, **kwargs):
def post(self, request, *args, **kwargs):
if request.user != self.get_object().user:
return redirect('permission_denied')
user = self.get_object().user
social_account = SocialAccount.objects.filter(user=user).delete()
return super(UserUpdateView, self).post(request, *args, **kwargs)


Expand Down
Loading

0 comments on commit c19eeb4

Please sign in to comment.