Skip to content

Instantly share code, notes, and snippets.

@n0an
Last active April 28, 2017 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n0an/152452eecb7c9842187c2e15ad9ca24d to your computer and use it in GitHub Desktop.
Save n0an/152452eecb7c9842187c2e15ad9ca24d to your computer and use it in GitHub Desktop.
linux django
===== INITIALIZE PROJECT ====
-- Create virtual environment on Desktop:
cd ~/Desktop
virtualenv myvirtualenv/foodtasker
*** macOS:
python3 -m venv myvirtualenv/foodtasker
-- Activate virtual environment:
source myvirtualenv/foodtasker/bin/activate
-- Install and start django:
pip install django
django-admin startproject foodtasker
-- Run web server:
cd foodtasker/
python manage.py runserver
-- Create app inside context:
python manage.py startapp foodtaskerapp
===== CREATE HOME PAGE ====
-- In foodtasker/view.py add method:
def home(request):
return render(request, 'home.html', {})
-- In foodtasker/settings.py add to INSTALLED_APPS array:
'foodtaskerapp',
-- In foodtasker/urls.py add import:
from foodtaskerapp import views
-And add to urlpatterns array:
url(r'^$', views.home, name='home'),
-- In foodtaskerapp, create templates/home.html:
Press ! and then TAB, to create boilerplate HTML code.
---- RUN SERVER AND TEST localhost:8000 ----
===== CREATE DJANGO DASHBOARD ====
-- Stop server CTRL+C
-- Migrate:
python manage.py migrate
-- Create superuser:
python manage.py createsuperuser
---- RUN SERVER AND TEST localhost:8000/admin ----
===== ADDING BOOTSTRAP ====
-- Download bootstrap
-- In foodtaskerapp/ create folders: /static/css, /static/img, /static/font, /static/js
-- From bootstrap copy:
- all fonts to /static/font
- bootstrap.min.css to /static/css
- bootstrap.min.js to /static/js
-- In templates/home.html add to header:
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
<script src="{% static 'js/bootstrap.min.js' %}"></script>
-- In static/css add file:
style.css
-- In home.html add line AFTER bootstrap.min.css link:
<link rel="stylesheet" href="{% static 'css/style.css' %}">
==== EVERY TIME CHANGES MODEL ====:
python manage.py makemigrations
python manage.py migrate
==== UPLOAD TO HEROKU ====
heroku login
heroku create
-- In root dir create runtime.txt, add text to it (with corresponding python ver):
python-3.5.1
-- Install gunicorn:
pip install gunicorn
-- Add dependencies to requirements.txt:
pip freeze > requirements.txt
-- In root dir create Procfile with text:
web: gunicorn foodtasker.wsgi --log-file -
-- Install whitenoise:
pip install whitenoise
pip freeze > requirements.txt
-- In settings.py add:
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
-- In wsgi.py add:
# Use whitenoise package to serve static files on Heroku
from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)
-- Install database:
pip install dj-database-url
pip freeze > requirements.txt
-- Add to requirements.txt (because local db is sqlite, and Heroku uses Postgres):
psycopg2==2.6.2
-- In settings.py add at end of file:
import dj_database_url
db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)
-- Deploy to Heroku:
git add .
git commit -m "ready to deploy to heroku"
git push heroku master
-- Migrate at Heroku (migrate all db structure from local to heroku):
heroku run python manage.py migrate
-- Create superuser on Heroku:
heroku run python manage.py createsuperuser
-- Run:
heroku open
-- IF CATCH THE ERROR 'DisallowedHost at /' :
- In settings.py, to ALLOWED_HOSTS add 'URL_TO_YOUR_HEROKU_APP'
==== ADD FACEBOOK AUTH ====
-- Create Facebook app
-- Add oauth
pip install django-rest-framework-social-oauth2
pip freeze > requirements.txt
-- Edit settings.py
https://github.com/PhilipGarnero/django-rest-framework-social-oauth2
-- Add to INSTALLED APPS:
INSTALLED_APPS = (
...
'oauth2_provider',
'social_django',
'rest_framework_social_oauth2',
)
-- Add to TEMPLATES:
TEMPLATES = [
{
...
'OPTIONS': {
'context_processors': [
...
'social_django.context_processors.backends',
'social_django.context_processors.login_redirect',
],
},
}
]
-- Add this block:
# Facebook configuration
SOCIAL_AUTH_FACEBOOK_KEY = '<your app id goes here>'
SOCIAL_AUTH_FACEBOOK_SECRET = '<your app secret goes here>'
# Define SOCIAL_AUTH_FACEBOOK_SCOPE to get extra permissions from facebook. Email is not sent by default, to get it, you must request the email permission:
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
SOCIAL_AUTH_FACEBOOK_PROFILE_EXTRA_PARAMS = {
'fields': 'id, name, email'
}
-- Add this block:
AUTHENTICATION_BACKENDS = (
'rest_framework_social_oauth2.backends.DjangoOAuth2',
'django.contrib.auth.backends.ModelBackend',
'social_core.backends.facebook.FacebookOAuth2',
)
-- In urls.py, add url scheme:
url(r'^auth/', include('rest_framework_social_oauth2.urls')),
FACEBOOK TOKENS:
https://developers.facebook.com/tools/accesstoken/
https://developers.facebook.com/tools/explorer/
-- Create APPLICATION in Djagno Admin Dashboard:
Id, Secret - NO CHANGE - USE THIS ID IN API REQUESTS
Choose admin as User
Client type: Confidential
Authorization grant type: Resource owner password-based
-- Make Postman POST request
- CONVERT TOKEN:
http://192.168.131.128:8000/api/social/convert-token/
PARAMS:
grant_type: convert_token
client_id: FROM DJANGO ADMIN APPLICATIONS
client_secret: FROM DJANGO ADMIN APPLICATIONS
backend: facebook
token: FROM FACEBOOK
- REVOKE TOKEN:
http://192.168.131.128:8000/api/social/revoke-token/
PARAMS:
client_id: FROM DJANGO ADMIN APPLICATIONS
client_secret: FROM DJANGO ADMIN APPLICATIONS
token: FROM DJANGO ADMIN TOKEN
-- In models.py add Customer, Driver
-- Edit admin.py accordingly
-- Make migrate, migrate
-- In settings.py add:
SOCIAL_AUTH_PIPELINE = (
'social_core.pipeline.social_auth.social_details',
'social_core.pipeline.social_auth.social_uid',
'social_core.pipeline.social_auth.auth_allowed',
'social_core.pipeline.social_auth.social_user',
'social_core.pipeline.user.get_username',
'social_core.pipeline.user.create_user',
'foodtaskerapp.social_auth_pipeline.create_user_by_type', # <--- set the path to the function
'social_core.pipeline.social_auth.associate_user',
'social_core.pipeline.social_auth.load_extra_data',
'social_core.pipeline.user.user_details',
)
-- Create file foodtaskerapp/social_auth_pipeline.py with content:
from foodtaskerapp.models import Customer, Driver
def create_user_by_type(backend, user, response, *args, **kwargs):
request = backend.strategy.request_data()
if backend.name == 'facebook':
avatar = 'https://graph.facebook.com/%s/picture?type=large' % response['id']
if request['user_type'] == "driver" and not Driver.objects.filter(user_id=user.id):
Driver.objects.create(user_id=user.id, avatar = avatar)
elif not Customer.objects.filter(user_id=user.id):
Customer.objects.create(user_id=user.id, avatar = avatar)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment