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

Adit radis shared subrepo #4

Merged
merged 40 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
b369603
Move accounts and token_authentications apps to shared folder
medihack Mar 23, 2024
ca551b8
Make common a real Django app and put external templates and vendor s…
medihack Mar 23, 2024
bd55511
Moved commands to shared common app
medihack Mar 23, 2024
2c97de5
Move more stuff to shared folder
medihack Mar 23, 2024
dd9fbb6
Move even more stuff to shared folder (inclusive templates)
medihack Mar 23, 2024
f88166d
Move some parts of broadcast to shared folder
medihack Mar 24, 2024
266b033
Move footer to shared folder
medihack Mar 24, 2024
8420bd9
Move some more utils to shared folder
medihack Mar 24, 2024
741a337
git subrepo init --remote=https://github.com/openradx/adit-radis-shar…
medihack Mar 24, 2024
e78893f
git subrepo push adit_radis_shared
medihack Mar 24, 2024
9966690
git subrepo pull adit_radis_shared
medihack Mar 24, 2024
219b5c3
Add git-subrepo to dev container images
medihack Mar 24, 2024
4040650
git subrepo pull adit_radis_shared
medihack Mar 24, 2024
a83c3f8
git subrepo pull adit_radis_shared
medihack Mar 24, 2024
75194d3
Watch correct path in server commands
medihack Mar 24, 2024
ac2d7f5
Delete unneeded sent_test_mail command from core
medihack Mar 25, 2024
fefb279
Correctly use sites framework
medihack Mar 25, 2024
28fd594
Make shared fixtures available in project
medihack Mar 29, 2024
d904b17
Remove unneeded maintenance template from core (in shared now)
medihack Mar 29, 2024
29f4770
Update env file example to make use of SiteProfile
medihack Mar 29, 2024
39c2aa3
Moved some data migration to common, so we don't need it in core
medihack Mar 29, 2024
8486f5d
Add site settings to base
medihack Mar 29, 2024
e591f66
Use shared maintenance middleware directly
medihack Mar 29, 2024
c9ae6e4
git subrepo pull adit_radis_shared
medihack Mar 29, 2024
fa9dd4f
Use BaseHomeView in core
medihack Mar 29, 2024
7700e10
Remove unneeded CoreSettings model
medihack Mar 29, 2024
1e8a7ed
Fix home test
medihack Mar 29, 2024
fb47991
Cleanup core layout
medihack Mar 29, 2024
91211e9
git subrepo pull adit_radis_shared
medihack Mar 29, 2024
37b9cfb
Report coverage also on shared tests
medihack Mar 29, 2024
d3ab400
git subrepo pull adit_radis_shared
medihack Mar 29, 2024
e064984
Make some comment more clear
medihack Mar 29, 2024
7593313
Fix some comments
medihack Mar 29, 2024
7fe3c56
Fix copy statics task (to copy in shared folder)
medihack Mar 29, 2024
3de11dd
git subrepo pull adit_radis_shared
medihack Mar 29, 2024
5474100
Use FlowerProxyView directly from common app
medihack Mar 29, 2024
9f19446
Set fixed site parameters in settings file
medihack Mar 29, 2024
daa55fa
Reuse SITE_DOMAIN instead of custom BASE_URL
medihack Mar 29, 2024
a81d3e3
git subrepo pull adit_radis_shared
medihack Mar 29, 2024
68c1d43
git subrepo pull adit_radis_shared
medihack Mar 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ RUN apt-get update && \

USER vscode

RUN git clone https://github.com/ingydotnet/git-subrepo $HOME/.git-subrepo && \
printf "\nsource \$HOME/.git-subrepo/.rc\n" >> $HOME/.bashrc

# pipx is already installed in the base devcontainers Python image
RUN pipx install invoke && \
invoke --print-completion-script=bash >> ~/.bash_completion
Expand Down
3 changes: 3 additions & 0 deletions .gitpod.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ FROM gitpod/workspace-python-3.12

USER gitpod

RUN git clone https://github.com/ingydotnet/git-subrepo $HOME/.git-subrepo && \
printf "\nsource \$HOME/.git-subrepo/.rc\n" >> $HOME/.bashrc

ENV NVM_DIR $HOME/.nvm
ENV NODE_VERSION 20.11.0

Expand Down
12 changes: 12 additions & 0 deletions adit_radis_shared/.gitrepo
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
; DO NOT EDIT (unless you know what you are doing)
;
; This subdirectory is a git "subrepo", and this file is maintained by the
; git-subrepo command. See https://github.com/ingydotnet/git-subrepo#readme
;
[subrepo]
remote = https://github.com/openradx/adit-radis-shared-subrepo.git
branch = main
commit = e70b021ca05bdaf649fe3df1e621de0d564ee329
method = merge
cmdver = 0.4.6
parent = a81d3e377f4110d062300794d23538caa9143dc3
674 changes: 674 additions & 0 deletions adit_radis_shared/LICENSE

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions adit_radis_shared/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ADIT RADIS Shared Subrepo

## About

Shared resources between ADIT and RADIS managed as a [git-subrepo](https://github.com/ingydotnet/git-subrepo).

## License

- GPL 3.0 or later
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


class AccountsConfig(AppConfig):
name = "radis.accounts"
name = "adit_radis_shared.accounts"
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
from typing import Generic, TypeVar, cast
from typing import cast

import factory
from django.contrib.auth.models import Group

from .models import User

T = TypeVar("T")
from adit_radis_shared.common.factories import BaseDjangoModelFactory


# We can't use BaseDjangoModelFactory of radis.core.factories because of circular imports
class BaseDjangoModelFactory(Generic[T], factory.django.DjangoModelFactory):
@classmethod
def create(cls, *args, **kwargs) -> T:
return super().create(*args, **kwargs)
from .models import User


class UserFactory(BaseDjangoModelFactory[User]):
Expand Down Expand Up @@ -41,7 +34,7 @@ def password(obj, create, extracted, **kwargs):

class AdminUserFactory(UserFactory):
username = "admin"
email = "admin@radis.test"
email = "admin@openradx.test"
is_superuser = True
is_staff = True

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.http import HttpRequest

from radis.accounts.models import User
from adit_radis_shared.accounts.models import User


class ActiveGroupMiddleware:
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends "common/common_layout.html" %}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from django.views import View
from django.views.generic import TemplateView

from radis.core.http import trigger_toast
from radis.core.types import AuthenticatedHttpRequest
from adit_radis_shared.common.types import AuthenticatedHttpRequest
from adit_radis_shared.common.utils.htmx_triggers import trigger_toast


class UserProfileView(LoginRequiredMixin, AccessMixin, TemplateView):
Expand Down
5 changes: 5 additions & 0 deletions adit_radis_shared/common/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.contrib import admin

from .models import SiteProfile

admin.site.register(SiteProfile, admin.ModelAdmin)
5 changes: 5 additions & 0 deletions adit_radis_shared/common/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class CommonConfig(AppConfig):
name = "adit_radis_shared.common"
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion radis/core/forms.py → adit_radis_shared/common/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def build_filter_set_layout(self):
filter_field.button_label,
css_class="btn-secondary btn-sm",
),
template="core/_filter_set_field.html",
template="common/_filter_set_field.html",
),
)

Expand Down
47 changes: 47 additions & 0 deletions adit_radis_shared/common/management/base/celery_beat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import logging
import shlex
import subprocess
from pathlib import Path
from typing import Literal

from .server_command import ServerCommand

logger = logging.getLogger(__name__)


class CeleryBeatCommand(ServerCommand):
project: Literal["adit", "radis"]
help = "Starts Celery beat scheduler"
server_name = "Celery beat scheduler"

def __init__(self, *args, **kwargs):
self.beat_process = None
super().__init__(*args, **kwargs)

def add_arguments(self, parser):
super().add_arguments(parser)

# https://docs.celeryproject.org/en/stable/reference/cli.html
parser.add_argument(
"-l",
"--loglevel",
default="INFO",
help="Logging level.",
)

def run_server(self, **options):
folder_path = Path(f"/var/www/{self.project}/celery/")
folder_path.mkdir(parents=True, exist_ok=True)
schedule_path = folder_path / "celerybeat-schedule"
loglevel = options["loglevel"]

# --pidfile= disables pidfile creation as we can control the process with subprocess
cmd = f"celery -A {self.project} beat -l {loglevel} -s {str(schedule_path)} --pidfile="

self.beat_process = subprocess.Popen(shlex.split(cmd))
self.beat_process.wait()

def on_shutdown(self):
assert self.beat_process
self.beat_process.terminate()
self.beat_process.wait()
63 changes: 63 additions & 0 deletions adit_radis_shared/common/management/base/celery_worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import logging
import shlex
import socket
import subprocess
from typing import Literal

from .server_command import ServerCommand

logger = logging.getLogger(__name__)


class CeleryWorkerCommand(ServerCommand):
project: Literal["adit", "radis"]
help = "Starts a Celery worker"
server_name = "Celery worker"
worker_process: subprocess.Popen | None

def __init__(self, *args, **kwargs):
self.worker_process = None
super().__init__(*args, **kwargs)

def add_arguments(self, parser):
super().add_arguments(parser)

# https://docs.celeryproject.org/en/stable/reference/cli.html
parser.add_argument(
"-Q",
"--queue",
required=True,
help="The celery queue.",
)
parser.add_argument(
"-l",
"--loglevel",
default="INFO",
help="Logging level.",
)
parser.add_argument(
"-c",
"--concurrency",
type=int,
default=0,
help="Number of child processes processing the queue (defaults to number of CPUs).",
)

def run_server(self, **options):
queue = options["queue"]
loglevel = options["loglevel"]
hostname = f"worker_{queue}_{socket.gethostname()}"

cmd = f"celery -A {self.project} worker -Q {queue} -l {loglevel} -n {hostname}"

concurrency = options["concurrency"]
if concurrency >= 1:
cmd += f" -c {concurrency}"

self.worker_process = subprocess.Popen(shlex.split(cmd))
self.worker_process.wait()

def on_shutdown(self):
assert self.worker_process
self.worker_process.terminate()
self.worker_process.wait()
24 changes: 24 additions & 0 deletions adit_radis_shared/common/management/base/hard_reset_migrations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from typing import Literal

from django.conf import settings
from django.core.management import call_command
from django.core.management.base import BaseCommand


class HardResetMigrationsCommand(BaseCommand):
project: Literal["adit", "radis"]
help = "Reset all migration files (dangerous!!!)."

def handle(self, *args, **options):
migration_paths = settings.BASE_DIR.glob(f"./{self.project}/*/migrations/**/*.py")
migration_paths = [i for i in migration_paths if i.name != "__init__.py"]
for migration_path in migration_paths:
migration_path.unlink()

pyc_paths = settings.BASE_DIR.glob("*/migrations/**/*.pyc")
for pyc_path in pyc_paths:
pyc_path.unlink()

call_command("reset_db", "--noinput") # needs django_extensions installed
call_command("makemigrations")
call_command("migrate")
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import threading
from abc import ABC, abstractmethod
from datetime import datetime
from pathlib import Path
from threading import Event
from types import FrameType

from django.conf import settings
from django.core.management.base import BaseCommand
from watchfiles import PythonFilter, watch

Expand All @@ -23,6 +23,7 @@ class ServerCommand(BaseCommand, ABC):

help = "Starts a custom server"
server_name = "custom server"
paths_to_watch: list[str | Path] = []

_popen: subprocess.Popen | None = None
_stop = Event()
Expand Down Expand Up @@ -57,7 +58,15 @@ def on_terminate(signum: int, frame: FrameType | None) -> None:

def run(self, **options):
if options["autoreload"]:
self.stdout.write(f"Autoreload enabled for {self.server_name}.")
if not self.paths_to_watch:
raise ValueError(
"If autoreload is enabled you must specify at least one path to watch."
)

self.stdout.write(
f"Autoreload enabled for {self.server_name} and "
f"watching paths: {", ".join(map(str, self.paths_to_watch))}."
)

# We debounce to give the Django webserver time to restart first
@debounce(wait_time=2)
Expand All @@ -73,8 +82,8 @@ def inner_run():
inner_run()

try:
for changes in watch(
settings.BASE_DIR / "radis", watch_filter=PythonFilter(), stop_event=self._stop
for _ in watch(
*self.paths_to_watch, watch_filter=PythonFilter(), stop_event=self._stop
):
self.stdout.write("Changes detected. Restarting server...")
inner_run()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.contrib.auth.models import UserManager
from django.core.management.base import BaseCommand

from radis.accounts.models import User
from adit_radis_shared.accounts.models import User


class Command(BaseCommand):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio

from ..base.server_command import AsyncServerCommand
from adit_radis_shared.common.management.base.server_command import AsyncServerCommand


class Command(AsyncServerCommand):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import time

from ..base.server_command import ServerCommand
from adit_radis_shared.common.management.base.server_command import ServerCommand


class Command(ServerCommand):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
from datetime import datetime, timedelta
from pathlib import Path

import environ
from cryptography import x509
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.x509.oid import NameOID
from django.core.management.base import BaseCommand

from radis.settings.base import env
env = environ.Env()


def generate_selfsigned_cert(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.conf import settings
from django.contrib.sites.models import Site
from django.core.mail import send_mail
from django.core.management.base import BaseCommand

Expand All @@ -11,10 +12,11 @@ def add_arguments(self, parser):

def handle(self, *args, **options):
to_address = options["to_address"]
site = Site.objects.get_current()

send_mail(
"[RADIS] Test Mail",
"This is a test mail sent by RADIS.",
f"[{site.name}] Test Mail",
f"This is a test mail sent by {site.name}.",
settings.SERVER_EMAIL,
[to_address],
fail_silently=False,
Expand Down
Loading
Loading