Skip to content

Commit

Permalink
Added Facebook authorization and base profile logged users.
Browse files Browse the repository at this point in the history
  • Loading branch information
miletskiy committed Oct 13, 2015
1 parent 06c0aae commit 82fc6a2
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 7 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ MySQL-python==1.2.5
django-crispy-forms==1.5.2
python-dateutil==2.4.2
django-registration-redux==1.2
six==1.9.0
six==1.9.0
python-social-auth==0.2.12
14 changes: 9 additions & 5 deletions students/templates/students/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,14 @@
<!--
<script src="https://github.com/moment/moment/blob/develop/locale/ru.js"></script>
<link rel="stylesheet" href="/static/css/main.css">
<script type="" src={% static "js/ru.js" %}></script>
-->


<!-- <style type="text/css">.col-xs-12, .col-xs-4, .col-xs-8, .col-xs-6,.col-xs-2,.col-xs-3, .col-xs-9 {border:1px solid red;}</style>-->

{% block extra_css %}{% endblock extra_css %}
</head>


<body>
<!-- <h1>Вітаю вас на нашій сторінці!</h1> -->
<!-- <p>Поки тут порожньо, але незабаром ми матимемо список студентів</p> -->
Expand Down Expand Up @@ -122,13 +118,21 @@ <h1>{% trans "Students Accounting Service" %}</h1>

<!-- User's toolbar for authenticated users -->
<div class="col-xs-4" id="user-toolbar">
<span>{{ user.username }}</span> |
{# {% if user.first_name or user.last_name %} #}
<a href="{% url "profile" %}">{{ user.get_full_name|default:user.username }} |</a>
<!-- <span>{{ user.first_name }} {{ user.last_name }}</span> | -->

{# {% else %} #}
<!-- <span>{{ user.username }}</span> | -->
{# {% endif %} #}
<a href="{% url "users:auth_logout" %}">{% trans "Logout" %}</a>
</div>

{% else %}
<!-- User's toolbar for anonymous users -->
<div class="col-xs-4" id="user-toolbar">
<a href="{% url 'social:begin' 'facebook' %}?next= {{ request.path }} ">{% trans "via Facebook" %}</a> |

<a href="{% url "users:auth_login" %}">{% trans "Login" %}</a> |
<a href="{% url "users:registration_register" %}">{% trans "Register" %}</a>
</div>
Expand Down
19 changes: 19 additions & 0 deletions studentsdb/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

from django.contrib import admin
from django.contrib.auth import admin as auth_admin
from django.contrib.auth.models import User

from .models import StProfile

class StProfileInline(admin.StackedInline):
# class StProfileInline(admin.TabularInline):
model = StProfile

class UserAdmin(auth_admin.UserAdmin):
inlines = (
StProfileInline,
)

# replace existing User admin form
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
24 changes: 24 additions & 0 deletions studentsdb/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _


class StProfile(models.Model):
"""To keep extra user data"""
# user mapping
user = models.OneToOneField(User)

class Meta(object):
verbose_name = _(u"User Profile")

# extra user data
mobile_phone = models.CharField(
max_length=12,
blank=True,
verbose_name=_(u"Mobile Phone"),
default='')

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

20 changes: 20 additions & 0 deletions studentsdb/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@
'django.contrib.sites.models',
'crispy_forms',
'registration',
'social.apps.django_app.default',
'students',
'studentsdb',
)

MIDDLEWARE_CLASSES = (
Expand Down Expand Up @@ -77,6 +79,8 @@
'django.template.context_processors.i18n',
'django.template.context_processors.request',
'django.contrib.messages.context_processors.messages',
'social.apps.django_app.context_processors.backends',
'social.apps.django_app.context_processors.login_redirect',
"students.context_processors.groups_processor",
# "students.context_processors.lang_processor",
"studentsdb.context_processors.students_proc",
Expand Down Expand Up @@ -248,7 +252,23 @@

REGISTRATION_OPEN = True

ACCOUNT_ACTIVATION_DAYS = 3

LOGIN_URL = 'users:auth_login'
LOGOUT_URL = 'users:auth_logout'

AUTHENTICATION_BACKENDS = (
# 'social.backends.facebook.FacebookOAuth2',
'social.backends.facebook.Facebook2OAuth2',
'django.contrib.auth.backends.ModelBackend',
)
# from social.backends.facebook import Facebook2AppOAuth2
from social.backends.facebook import Facebook2OAuth2

SOCIAL_AUTH_FACEBOOK_KEY = '571465946336791'
SOCIAL_AUTH_FACEBOOK_SECRET = 'c3460c81d831b0e181b8eec60e9f8c6a'





15 changes: 14 additions & 1 deletion studentsdb/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,27 @@

url(r'^users/logout/$', auth_views.logout, kwargs={'next_page': 'home'},
name='auth_logout'),

url(r'^register/complete/$', RedirectView.as_view(pattern_name='home'),
name='registration_complete'),
#
# url(r'^users/profile/$', login_required(TemplateView.as_view(
# template_name='registration/profile.html')), name='profile'),

url(r'^accounts/profile/$', login_required(TemplateView.as_view(
template_name='registration/profile.html')), name='profile'),


url(r'^users/', include('registration.backends.simple.urls',
namespace='users')),
# url(r'^accounts/', include('registration.backends.default.urls',
# namespace='users')),


url('^social/', include('social.apps.django_app.urls', namespace='social')),
# social.apps.django_app.urls
# from social.apps.django_app.urls import extra
# social.apps.django_app.views
# from social.apps.django_app.views import auth
#Default admin url
url(r'^admin/', include(admin.site.urls)),

Expand Down
52 changes: 52 additions & 0 deletions templates/registration/profile.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{% extends "students/base.html" %}

{% load i18n %}

{% block meta_title %}{{ user.get_full_name|default:user.username }}{% endblock meta_title %}

{% block title %}{{ user.get_full_name|default:user.username }}{% endblock title %}

{#
{% block status_message %}
{% if form.errors %}
<div class="alert alert-warning" role="alert">
{% if form.non_field_errors %}
{{ form.non_field_errors.as_text }}
{% else %}
{% trans "Please, correct the following errors." %}
{% endif %}
</div>
{% endif %}
{% endblock %} #}

{% block content %}

<table class="table table-striped">
{% if user.email %}
<tr>
<td>{% trans "Email" %}:</td>
<td>{{ user.email }}</td>
</tr>
{% endif %}
{% if user.username %}
<tr>
<td>{% trans "Username" %}:</td>
<td>{{ user.get_full_name }}</td>
</tr>
{% endif %}
{% if user.date_joined %}
<tr>
<td>{% trans "Joined" %}:</td>
<td>{{ user.date_joined }}</td>
</tr>
{% endif %}
{% if user.stprofile.mobile_phone %}
<tr>
<td>{% trans "Mobile Phone" %}:</td>
<td>{{ user.stprofile.mobile_phone }}</td>
</tr>
{% endif %}
</table>

{% endblock content %}

0 comments on commit 82fc6a2

Please sign in to comment.