Skip to content

Commit

Permalink
Bunch of changes to support superday
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite committed Apr 9, 2024
1 parent 8047b58 commit 72a5cc0
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
*
!bin
!requirements.txt
!requirements-test.txt
!requirements-dev.txt
!backend
!staticfiles
!manage.py
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci-backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
with:
python-version: 3.9
- name: Install dependencies
run: pip install -r requirements-test.txt && pip install -r requirements.txt
run: pip install -r requirements-dev.txt && pip install -r requirements.txt
- name: Lint
run: python runtests.py --lint-only

Expand All @@ -40,7 +40,7 @@ jobs:
sudo apt-get update
sudo apt-get install gdal-bin python3-gdal
- name: Install Python dependencies
run: pip install -r requirements-test.txt && pip install -r requirements.txt
run: pip install -r requirements-dev.txt && pip install -r requirements.txt
- name: Tests
env:
TEST: 1
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ sudo apt-get install libgeoip-dev libgdal-dev gdal-bin
sudo apt-get install postgresql-12-postgis-3
# On everything
pip install -r requirements-test.txt
pip install -r requirements-dev.txt
pip install -r requirements.txt
```

Expand Down
4 changes: 3 additions & 1 deletion backend/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ def social_create_user(

# TODO: whereintheworld is intended for internal use only just yet, multitenancy NOT YET supported
# team must be assigned based on email's TLD.
user = User.objects.create(email=email, first_name=name, team=Team.objects.first(), avatar_url=avatar_url)

team = Team.objects.get_or_create(name=email.split("@")[-1])[0]
user = User.objects.create(email=email, first_name=name, team=team, avatar_url=avatar_url)

report_user_signed_up(user)

Expand Down
22 changes: 17 additions & 5 deletions backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
from sentry_sdk.integrations.django import DjangoIntegration

from backend.utils import get_from_env, get_list, str_to_bool
from dotenv import load_dotenv


load_dotenv()

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand Down Expand Up @@ -103,7 +107,7 @@
"USER": os.getenv("DB_USER", "whereintheworld"),
"PASSWORD": os.getenv("DB_PASSWORD", "whereintheworld"),
"HOST": os.getenv("DB_HOST", "localhost"),
"PORT": "5432",
"PORT": os.getenv("DB_PORT", "5432"),
}
}

Expand Down Expand Up @@ -242,10 +246,18 @@
)
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = os.getenv("SOCIAL_AUTH_GOOGLE_OAUTH2_KEY")
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = os.getenv("SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET")
AUTHENTICATION_BACKENDS = (
"social_core.backends.google.GoogleOAuth2",
"django.contrib.auth.backends.ModelBackend",
)
SOCIAL_AUTH_GITHUB_KEY = os.getenv("SOCIAL_AUTH_GITHUB_KEY")
SOCIAL_AUTH_GITHUB_SECRET = os.getenv("SOCIAL_AUTH_GITHUB_SECRET")
SOCIAL_AUTH_GITHUB_SCOPE = ["user:email"]

AUTHENTICATION_BACKENDS = ["django.contrib.auth.backends.ModelBackend"]

if SOCIAL_AUTH_GOOGLE_OAUTH2_KEY:
AUTHENTICATION_BACKENDS.append("social_core.backends.google.GoogleOAuth2")
elif SOCIAL_AUTH_GITHUB_KEY:
AUTHENTICATION_BACKENDS.append("social_core.backends.github.GithubOAuth2")


SOCIAL_AUTH_REDIRECT_IS_HTTPS = not DEBUG


Expand Down
8 changes: 7 additions & 1 deletion backend/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from functools import wraps

from django.conf import settings
from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.contrib.auth.decorators import login_required as base_login_required
Expand Down Expand Up @@ -36,7 +37,12 @@ def login_required(view):
@wraps(view)
def handler(request, *args, **kwargs):
if not request.user or not request.user.is_authenticated:
return redirect("/login/google-oauth2/")
if settings.SOCIAL_AUTH_GOOGLE_OAUTH2_KEY:
return redirect("/login/google-oauth2/")
if settings.SOCIAL_AUTH_GITHUB_KEY:
return redirect("/login/github/")
return redirect("/login/")

return base_handler(request, *args, **kwargs)

return handler
Expand Down
3 changes: 2 additions & 1 deletion bin/docker
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/bin/sh
set -e

python manage.py migrate

yarn start &
gunicorn backend.wsgi \
--bind 0.0.0.0:${PORT:-8000} \
--log-file - \
Expand Down
1 change: 1 addition & 0 deletions bin/start
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -e

trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT


python manage.py runserver &
yarn start &

Expand Down
28 changes: 28 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
services:
postgres:
image: postgis/postgis:14-master
restart: on-failure
environment:
POSTGRES_USER: whereintheworld
POSTGRES_PASSWORD: whereintheworld
POSTGRES_DB: whereintheworld
ports:
- "5433:5432"
app:
build: .
environment:
DB_HOST: postgres
DEBUG: "1"
ALLOWED_HOSTS: "*"
ports:
- "8000:8000"
links:
- "postgres:postgres"

setup:
build: .
environment:
DB_HOST: postgres
command: python manage.py migrate && python manage.py cities --import=all
links:
- "postgres:postgres"
File renamed without changes.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ gunicorn==20.1.0
jsonschema==4.4.0
posthog==1.4.4
psycopg2-binary==2.9.2
python-dotenv==1.0.1
sentry-sdk==1.5.1
social-auth-app-django==5.0.0
whitenoise==5.3.0
whitenoise==5.3.0

0 comments on commit 72a5cc0

Please sign in to comment.