Skip to content

Commit

Permalink
python 3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
jmg committed Aug 11, 2022
1 parent d04a171 commit ef7abdf
Show file tree
Hide file tree
Showing 21 changed files with 363 additions and 37 deletions.
4 changes: 2 additions & 2 deletions examples/djangoapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@
ROOT_URLCONF = 'djangoapp.urls'

# Facebook related Settings
FACEBOOK_APP_ID = '178358228892649'
FACEBOOK_SECRET_KEY = 'cc2fbfac64784491fd84fc275b700496'
FACEBOOK_APP_ID = '535394336554651'
FACEBOOK_SECRET_KEY = 'c26350ed86d9d98de5e9e5461e5ac492'
FACEBOOK_REDIRECT_URL = 'http://localhost:8000/facebook_login_success'


Expand Down
Empty file added examples/djangoapp3/db.sqlite3
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions examples/djangoapp3/django_pyfb/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions examples/djangoapp3/django_pyfb/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class DjangoPyfbConfig(AppConfig):
name = 'django_pyfb'
Empty file.
3 changes: 3 additions & 0 deletions examples/djangoapp3/django_pyfb/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions examples/djangoapp3/django_pyfb/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
49 changes: 49 additions & 0 deletions examples/djangoapp3/django_pyfb/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from pyfb import Pyfb
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response

from django.conf import settings

def index(request):
return render_to_response("index.html", {"FACEBOOK_APP_ID": settings.FACEBOOK_APP_ID})


#This view redirects the user to facebook in order to get the code that allows
#pyfb to obtain the access_token in the facebook_login_success view
def facebook_login(request):

facebook = Pyfb(settings.FACEBOOK_APP_ID)
return HttpResponseRedirect(facebook.get_auth_code_url(redirect_uri=settings.FACEBOOK_REDIRECT_URL))


#This view must be refered in your FACEBOOK_REDIRECT_URL. For example: http://www.mywebsite.com/facebook_login_success/
def facebook_login_success(request):

code = request.GET.get('code')

facebook = Pyfb(settings.FACEBOOK_APP_ID)
facebook.get_access_token(settings.FACEBOOK_SECRET_KEY, code, redirect_uri=settings.FACEBOOK_REDIRECT_URL)

return _render_user(facebook)



#Login with the js sdk and backend queries with pyfb
def facebook_javascript_login_sucess(request):

access_token = request.GET.get("access_token")

facebook = Pyfb(settings.FACEBOOK_APP_ID)
facebook.set_access_token(access_token)

return _render_user(facebook)


def _render_user(facebook):

me = facebook.get_myself()

welcome = "Welcome <b>%s</b>. Your Facebook login has been completed successfully!"
return HttpResponse(welcome % me.name)


Empty file.
127 changes: 127 additions & 0 deletions examples/djangoapp3/djangoapp3/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
"""
Django settings for djangoapp3 project.
Generated by 'django-admin startproject' using Django 2.0.13.
For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '_v#-7jls#!h7y)(nd(me2xvf=nncp6b-^)jqpm0)q8r0g=9gbe'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'django_pyfb',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'djangoapp3.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, "templates")],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]


WSGI_APPLICATION = 'djangoapp3.wsgi.application'

# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}


# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/

STATIC_URL = '/static/'

# Facebook related Settings
FACEBOOK_APP_ID = '535394336554651'
FACEBOOK_SECRET_KEY = 'c26350ed86d9d98de5e9e5461e5ac492'
FACEBOOK_REDIRECT_URL = 'http://localhost:8000/facebook_login_success'
26 changes: 26 additions & 0 deletions examples/djangoapp3/djangoapp3/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""djangoapp3 URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django_pyfb import views

urlpatterns = [

path(r'', views.index),
path(r'facebook_login', views.facebook_login),
path(r'facebook_login_success', views.facebook_login_success),
path(r'facebook_javascript_login_sucess', views.facebook_javascript_login_sucess),
]
16 changes: 16 additions & 0 deletions examples/djangoapp3/djangoapp3/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for djangoapp3 project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djangoapp3.settings")

application = get_wsgi_application()
15 changes: 15 additions & 0 deletions examples/djangoapp3/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djangoapp3.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
58 changes: 58 additions & 0 deletions examples/djangoapp3/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<html>
<head>

</head>
<div id="fb-root"></div>
<script>

function isConnected(response) {
return response.status == 'connected';
}

function getLoginStatus(FB) {

FB.getLoginStatus(function(response) {

if (isConnected(response)) {
onLogin(response);
}
else {
FB.login(onLogin);
}
});
}

function onLogin(response) {

if (isConnected(response)) {
location.href = '/facebook_javascript_login_sucess?access_token=' + response.authResponse.accessToken;
}
}

window.fbAsyncInit = function() {

FB.init({
appId : '{{FACEBOOK_APP_ID}}',
channelUrl : 'http://localhost:8000/media/channel.html',
status : true,
cookie : true,
xfbml : true,
oauth : true,
});

};

(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "http://connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));

</script>

<body>
<button onclick="location.href='/facebook_login'">Facebook Python Login</button><br/><br/>
<button onclick="getLoginStatus(FB)">Facebook Javascript Login</button>
</body>
</html>
2 changes: 1 addition & 1 deletion pyfb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
__version__ = "0.5.0"
__license__ = 'GPL v3'

from pyfb import Pyfb, PyfbException
from .pyfb import Pyfb, PyfbException
8 changes: 4 additions & 4 deletions pyfb/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
USER_ADS_MANAGEMENT = "ads_management"

vars = locals().copy()
USER_ALL_PERMISSIONS = [value for key, value in vars.iteritems() if key.startswith("USER_")]
USER_ALL_PERMISSIONS = [value for key, value in vars.items() if key.startswith("USER_")]

#Friends related permissions

Expand Down Expand Up @@ -67,7 +67,7 @@
FRIENDS_WORK_HISTORY = "friends_work_history"

vars = locals().copy()
FRIENDS_ALL_PERMISSIONS = [value for key, value in vars.iteritems() if key.startswith("FRIENDS_")]
FRIENDS_ALL_PERMISSIONS = [value for key, value in vars.items() if key.startswith("FRIENDS_")]

#Write related permissions

Expand All @@ -80,14 +80,14 @@
WRITE_MANAGE_FRIENDLISTS = "manage_friendlists"

vars = locals().copy()
WRITE_ALL_PERMISSIONS = [value for key, value in vars.iteritems() if key.startswith("WRITE_")]
WRITE_ALL_PERMISSIONS = [value for key, value in vars.items() if key.startswith("WRITE_")]

#Page related permissions

PAGE_MANAGE_PAGES = "manage_pages"

vars = locals().copy()
PAGE_ALL_PERMISSIONS = [value for key, value in vars.iteritems() if key.startswith("PAGE_")]
PAGE_ALL_PERMISSIONS = [value for key, value in vars.items() if key.startswith("PAGE_")]

#All permisssions
ALL_PERMISSIONS = USER_ALL_PERMISSIONS + FRIENDS_ALL_PERMISSIONS + WRITE_ALL_PERMISSIONS + PAGE_ALL_PERMISSIONS
Expand Down
Loading

0 comments on commit ef7abdf

Please sign in to comment.