Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Django admin always enabled #1881

Merged
merged 19 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
e4794bc
refactor(admin): assume database and superuser already exist
angela-tran Jan 23, 2024
4625f25
refactor(admin): admin interface is always enabled
angela-tran Jan 23, 2024
d9b2c28
test(admin): update unit test assertion
angela-tran Jan 23, 2024
2174ac8
feat: add script for resetting the database
angela-tran Feb 8, 2024
0e0f37c
refactor(settings): consolidate template processors
thekaveman Feb 13, 2024
84956df
test(admin): assert redirects to login page
thekaveman Feb 13, 2024
eeffe2e
test(admin): pre_login_user success and failure
thekaveman Feb 13, 2024
f394a3c
chore(settings): remove unused variable
thekaveman Feb 13, 2024
6f9ba82
chore(config): add sample SUPERUSER env vars
thekaveman Feb 13, 2024
409391e
feat(devcontainer): startup with the reset_db script
thekaveman Feb 13, 2024
c2b074f
refactor(migrations): remove data migration, use local fixtures
thekaveman Feb 13, 2024
4bac379
fix(tests): container startup script specifically for Cypress
thekaveman Feb 14, 2024
aeaa3aa
chore(terraform): remove env vars used in data migration
thekaveman Feb 15, 2024
b71b525
refactor(rest_db): allow more local customization
thekaveman Feb 15, 2024
0a562fb
feat(git): ignore fixtures except the included sample
thekaveman Feb 15, 2024
94312c0
docs(deployment): update language around config database
thekaveman Feb 20, 2024
e37ed91
refactor(migrations): update helper script and docs
thekaveman Feb 20, 2024
d6bf990
chore(docs): update references to Django docs
thekaveman Feb 20, 2024
0f8ab20
docs: fix typo in Django URL
thekaveman Feb 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"service": "dev",
"runServices": ["dev", "docs", "server"],
"workspaceFolder": "/home/calitp/app",
"postStartCommand": ["/bin/bash", "bin/init.sh"],
"postStartCommand": ["/bin/bash", "bin/reset_db.sh"],
"postAttachCommand": ["/bin/bash", ".devcontainer/postAttach.sh"],
"customizations": {
"vscode": {
Expand Down
9 changes: 9 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
DJANGO_SUPERUSER_USERNAME=benefits-admin
DJANGO_SUPERUSER_EMAIL=benefits-admin@calitp.org
DJANGO_SUPERUSER_PASSWORD=superuser12345!

DJANGO_DB_RESET=true
DJANGO_DB_DIR=.
DJANGO_DB_FILE=django.db
DJANGO_DB_FIXTURES="benefits/core/migrations/local_fixtures.json"

testsecret=Hello from the local environment!
auth_provider_client_id=benefits-oauth-client-id
courtesy_card_verifier_api_auth_key=server-auth-token
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/tests-cypress.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ jobs:
- name: Start app
run: |
cp .env.sample .env
docker compose up --detach client server
docker compose up --detach server
docker compose run --detach --service-ports client bin/test_start.sh

- name: Run Cypress tests
uses: cypress-io/github-action@v6
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
*.db
*.env
*fixtures.json
!benefits/core/migrations/local_fixtures.json
*.mo
*.tfbackend
*.tmp
Expand Down
78 changes: 41 additions & 37 deletions benefits/core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,47 @@
The core application: Admin interface configuration.
"""

import logging
import requests

from django.conf import settings

if settings.ADMIN:
import logging
from django.contrib import admin
from . import models

logger = logging.getLogger(__name__)

for model in [
models.EligibilityType,
models.EligibilityVerifier,
models.PaymentProcessor,
models.PemData,
models.TransitAgency,
]:
logger.debug(f"Register {model.__name__}")
admin.site.register(model)

def pre_login_user(user, request):
logger.debug(f"Running pre-login callback for user: {user.username}")
token = request.session.get("google_sso_access_token")
if token:
headers = {
"Authorization": f"Bearer {token}",
}

# Request Google user info to get name and email
url = "https://www.googleapis.com/oauth2/v3/userinfo"
response = requests.get(url, headers=headers, timeout=settings.REQUESTS_TIMEOUT)
user_data = response.json()
logger.debug(f"Updating admin user data from Google for user with email: {user_data['email']}")

user.first_name = user_data["given_name"]
user.last_name = user_data["family_name"]
user.username = user_data["email"]
user.email = user_data["email"]
user.save()
from django.contrib import admin
from . import models

logger = logging.getLogger(__name__)


GOOGLE_USER_INFO_URL = "https://www.googleapis.com/oauth2/v3/userinfo"


for model in [
models.EligibilityType,
models.EligibilityVerifier,
models.PaymentProcessor,
models.PemData,
models.TransitAgency,
]:
logger.debug(f"Register {model.__name__}")
admin.site.register(model)


def pre_login_user(user, request):
logger.debug(f"Running pre-login callback for user: {user.username}")
token = request.session.get("google_sso_access_token")
if token:
headers = {
"Authorization": f"Bearer {token}",
}

# Request Google user info to get name and email
response = requests.get(GOOGLE_USER_INFO_URL, headers=headers, timeout=settings.REQUESTS_TIMEOUT)
user_data = response.json()
logger.debug(f"Updating user data from Google for user with email: {user_data['email']}")

user.first_name = user_data["given_name"]
user.last_name = user_data["family_name"]
user.username = user_data["email"]
user.email = user_data["email"]
user.save()
else:
logger.warning("google_sso_access_token not found in session.")