Skip to content

Instantly share code, notes, and snippets.

View Tobi-De's full-sized avatar
👁️‍🗨️
Trade-offs everywhere!

Tobi DEGNON Tobi-De

👁️‍🗨️
Trade-offs everywhere!
View GitHub Profile

Media Setup for CapRover NGINX and Django

Use NGINX to serve the media files on CapRover for a Django project.

To give a brief overview, everything in CapRover is in a container, including NGINX, so no container can access other container data. But if you want to serve your media files (this will work the same for static files), NGINX needs to be able to access these files. Fortunately, the NGINX CapRover container provides a folder on the host that it can read and can be shared with other containers. So, basically, what you need to do is tell your Django app to write the media files in this shared folder so that NGINX can read them.

# Django media settings
STORAGES = {
    "BACKEND": "django.core.files.storage.FileSystemStorage",
from __future__ import annotations
import argparse
import logging
import platform
import shutil
import stat
import subprocess
import sys
import tempfile
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "pytesseract",
# "pillow",
# "rich"
# ]
# ///
import pytesseract
@Tobi-De
Tobi-De / gh_mirrors_to_gitea.py
Last active March 30, 2024 21:33
backup github repos to gitea
# requirements: httpx, python-dotenv
import os
import httpx
from dotenv import load_dotenv
from datetime import datetime as dt
import pytz
from dateutil import parser
load_dotenv()
@Tobi-De
Tobi-De / bulk.py
Last active April 3, 2024 08:36
Account Update without explicit lock
from django.db.models import Case, Value, When, F, Q
def transfer(from_account: Account, to_account: Account, amount: int):
with transaction.atomic():
count = Account.objects.filter(
Q(pk=from_account.pk, balance__gte=amount) | Q(pk=to_account.pk)
).update(
balance=Case(
@Tobi-De
Tobi-De / sse_litestar.py
Created December 8, 2023 11:30
sse litestar
import time
from litestar import Litestar, get
from litestar.channels.backends.redis import RedisChannelsPubSubBackend
from litestar.channels.plugin import ChannelsPlugin
from litestar.response.sse import ServerSentEvent
import redis.asyncio as Redis
from typing import Generator, AsyncGenerator
import json
from contextlib import asynccontextmanager
sse-starlette
starlette
redis
psycopg[c]
from typing import TYPE_CHECKING
import structlog
from .config import get_redis_url, get_client_tracking_enabled
from functools import wraps
import threading
import redis
import asyncio
if TYPE_CHECKING:
from .brokers import Broker
pg_dump "sslmode=require host=<host> dbname=leerming user=leerming" --data-only -f leerming.sql
psql "host=<host> dbname=leerming user=leerming" < leer.sql
@Tobi-De
Tobi-De / filters.py
Created February 15, 2023 11:15
filter in multiple fields with django-filter
class LocationFilter(django_filters.FilterSet):
q = django_filters.CharFilter(method='my_custom_filter', label="Search")
class Meta:
model = Location
fields = ['q']
def my_custom_filter(self, queryset, name, value):
return queryset.filter(
Q(loc__icontains=value) |