Skip to content
Snippets Groups Projects
Commit a3c63eb1 authored by Tomáš Valenta's avatar Tomáš Valenta
Browse files

create base project

parent c371b2a6
No related branches found
No related tags found
No related merge requests found
Makefile 0 → 100644
#!/usr/bin/make -f
PYTHON = python
VENV = .venv
PORT = 8014
SETTINGS = ucebnice.settings.dev
.PHONY: help venv install install-hooks hooks build run shell migrations migrate
help:
@echo "Setup:"
@echo " venv Setup virtual environment"
@echo " install Install dependencies to venv"
@echo " install-hooks Install pre-commit hooks"
@echo " hooks Run pre-commit hooks manually"
@echo " build Build CSS and JS files"
@echo ""
@echo "Application:"
@echo " run Run the application on port ${PORT}"
@echo " shell Access the Django shell"
@echo ""
@echo "Database:"
@echo " migrations Generate migrations"
@echo " migrate Run migrations"
venv: .venv/bin/python
.venv/bin/python:
${PYTHON} -m venv ${VENV}
install: venv
${VENV}/bin/pip install -r requirements/base.txt -r requirements/production.txt
npm install
install-hooks:
pre-commit install --install-hooks
hooks:
pre-commit run -a
build: venv
npm run build
${VENV}/bin/python manage.py collectstatic --noinput --settings=${SETTINGS}
run: venv
${VENV}/bin/python manage.py runserver ${PORT} --settings=${SETTINGS}
shell: venv
${VENV}/bin/python manage.py shell --settings=${SETTINGS}
migrations: venv
${VENV}/bin/python manage.py makemigrations --settings=${SETTINGS}
migrate: venv
${VENV}/bin/python manage.py migrate --settings=${SETTINGS}
DATABASE_URL="postgresql://ucebnice:ucebnice@localhost:5432/ucebnice"
SECRET_KEY=supersecret
SITE_URL="http://localhost:8013"
OIDC_RP_REALM_URL="http://localhost:8080/realms/master/"
OIDC_RP_CLIENT_ID=ucebnice
OIDC_RP_CLIENT_SECRET=VCn4LVAUc6RGLSup7VaAKsmrKUbWguaP
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ucebnice.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)
if __name__ == '__main__':
main()
from django.contrib import admin
# Register your models here.
from django.apps import AppConfig
class OidcConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "oidc"
import logging
import typing
import jwt
from django.conf import settings
from django.contrib.auth.models import Group
from pirates.auth import PiratesOIDCAuthenticationBackend
logging.basicConfig(level=logging.DEBUG)
class UcebniceOIDCAuthenticationBackend(PiratesOIDCAuthenticationBackend):
def _assign_new_user_groups(
self, user, access_token: dict, user_groups: typing.Union[None, list] = None
) -> None:
if user_groups is None:
user_groups = user.groups.all()
for group in access_token["groups"]:
if group.startswith("_"): # Ignore internal Keycloak groups
continue
group_name = f"sso_{group}"
group = Group.objects.filter(name=group_name)
if not group.exists():
group = Group(name=group_name)
group.save()
else:
group = group[0]
if group not in user_groups:
user.groups.add(group)
def _remove_old_user_groups(
self, user, access_token: dict, user_groups: typing.Union[None, list] = None
) -> None:
if user_groups is None:
user_groups = user.groups.all()
for group in user_groups:
if group.name.replace("sso_", "") not in access_token["groups"]:
user.groups.remove(group)
def get_or_create_user(self, access_token, id_token, payload):
user = super().get_or_create_user(access_token, id_token, payload)
if user is None:
return
decoded_access_token = jwt.decode(
access_token, options={"verify_signature": False}
)
user_groups = user.groups.all()
self._remove_old_user_groups(
user, decoded_access_token, user_groups=user_groups
)
self._assign_new_user_groups(
user, decoded_access_token, user_groups=user_groups
)
user.save()
return user
from django.db import models
# Create your models here.
from django.test import TestCase
# Create your tests here.
from django.urls import path
from . import views
app_name = "oidc"
urlpatterns = []
This diff is collapsed.
{
"name": "ucebnice",
"version": "0.0.1",
"description": "",
"scripts": {
"build": "npx tailwindcss -i ./static_src/base.css -o ./shared/static/shared/style.css && npx webpack build"
},
"repository": {
"type": "git",
"url": "https://gitlab.pirati.cz/to/elearning.git"
},
"author": "",
"license": "AGPL-3.0-or-later",
"dependencies": {
"@tailwindcss/typography": "^0.5.9",
"css-loader": "^6.7.3",
"jquery": "^3.6.3",
"style-loader": "^3.3.1",
"tailwindcss": "^3.2.4",
"webpack": "^5.76.2",
"webpack-bundle-tracker": "^1.8.0",
"webpack-cli": "^5.0.1"
}
}
django==4.1.4
django-admin-index==2.0.2
django-admin-interface==0.24.2
django-database-url==1.0.3
django-ordered-model==3.7.1
psycopg2-binary==2.9.5
django-webpack-loader==1.8.0
pirates==0.6.0
django-markdownx==4.0.0b1
django-environ==0.9.0
django-http-exceptions==1.4.0
django-guardian==2.4.0
PyJWT==2.6.0
gunicorn==20.1.0
whitenoise==6.3.0
import enum
from django.contrib import admin
from guardian.admin import GuardedModelAdmin
from markdownx.admin import MarkdownxModelAdmin
class MarkdownxGuardedModelAdmin(MarkdownxModelAdmin, GuardedModelAdmin):
pass
class FieldsetInlineOrder(enum.Enum):
FIELDSET = "fieldset"
INLINE = "inline"
from django.apps import AppConfig
class SharedConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "shared"
class NameStrMixin:
name = ""
def __str__(self) -> str:
return self.name
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment