-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added django example * Added django examples tests to CI * Lint fixes * Fixed security issue with DjangoWorker.Dockerfile * Cleanup
- Loading branch information
Showing
20 changed files
with
458 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions | ||
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions | ||
|
||
name: examples | ||
|
||
on: | ||
push: | ||
branches: ["main"] | ||
paths: | ||
- "**.py" | ||
- "**.txt" | ||
- ".github/workflows/examples.yml" | ||
- "**.toml" | ||
pull_request: | ||
paths: | ||
- "**.py" | ||
- "**.txt" | ||
- "**.toml" | ||
- ".github/workflows/examples.yml" | ||
|
||
permissions: | ||
contents: read # to fetch code (actions/checkout) | ||
|
||
jobs: | ||
django: | ||
runs-on: ${{ matrix.os }} | ||
|
||
strategy: | ||
fail-fast: false | ||
matrix: | ||
python-version: ["3.12"] | ||
os: ["ubuntu-latest"] | ||
|
||
steps: | ||
- name: Install apt packages | ||
if: startsWith(matrix.os, 'ubuntu-') | ||
run: | | ||
sudo apt update | ||
- uses: actions/checkout@v4 | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v5 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
cache: 'pip' | ||
cache-dependency-path: '**/setup.py' | ||
- name: Install dependencies | ||
working-directory: examples/django | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -r requirements.txt | ||
- name: Run Migrations | ||
working-directory: examples/django | ||
run: | | ||
./manage.py migrate | ||
- name: Run tests | ||
working-directory: examples/django | ||
timeout-minutes: 5 | ||
run: | | ||
export DJANGO_SETTINGS_MODULE=proj.settings | ||
pytest -vv tests -n auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Generated by Django 2.2.1 on 2019-05-24 21:37 | ||
|
||
from django.db import migrations | ||
from django.db import models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
initial = True | ||
|
||
dependencies = [] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="Widget", | ||
fields=[ | ||
("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")), | ||
("name", models.CharField(max_length=140)), | ||
], | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.db import models | ||
|
||
|
||
class Widget(models.Model): | ||
name = models.CharField(max_length=140) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Create your tasks here | ||
|
||
from celery import shared_task | ||
|
||
from .models import Widget | ||
|
||
|
||
@shared_task | ||
def add(x, y): | ||
return x + y | ||
|
||
|
||
@shared_task | ||
def mul(x, y): | ||
return x * y | ||
|
||
|
||
@shared_task | ||
def xsum(numbers): | ||
return sum(numbers) | ||
|
||
|
||
@shared_task | ||
def count_widgets(): | ||
return Widget.objects.count() | ||
|
||
|
||
@shared_task | ||
def rename_widget(widget_id, name): | ||
w = Widget.objects.get(id=widget_id) | ||
w.name = name | ||
w.save() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Create your views here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env python | ||
|
||
import os | ||
import sys | ||
|
||
if __name__ == "__main__": | ||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proj.settings") | ||
|
||
from django.core.management import execute_from_command_line | ||
|
||
execute_from_command_line(sys.argv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# This will make sure the app is always imported when | ||
# Django starts so that shared_task will use this app. | ||
from .celery import app as celery_app | ||
|
||
__all__ = ("celery_app",) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import os | ||
|
||
from celery import Celery | ||
|
||
# Set the default Django settings module for the 'celery' program. | ||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proj.settings") | ||
|
||
app = Celery("proj") | ||
|
||
# Using a string here means the worker doesn't have to serialize | ||
# the configuration object to child processes. | ||
# - namespace='CELERY' means all celery-related configuration keys | ||
# should have a `CELERY_` prefix. | ||
app.config_from_object("django.conf:settings", namespace="CELERY") | ||
|
||
# Load task modules from all registered Django apps. | ||
app.autodiscover_tasks() | ||
|
||
|
||
@app.task(bind=True, ignore_result=True) | ||
def debug_task(self): | ||
print(f"Request: {self.request!r}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import os | ||
|
||
# ^^^ The above is required if you want to import from the celery | ||
# library. If you don't have this then `from celery.schedules import` | ||
# becomes `proj.celery.schedules` in Python 2.x since it allows | ||
# for relative imports by default. | ||
|
||
# Celery settings | ||
|
||
CELERY_BROKER_URL = "amqp://guest:guest@localhost" | ||
|
||
#: Only add pickle to this list if your broker is secured | ||
#: from unwanted access (see userguide/security.html) | ||
CELERY_ACCEPT_CONTENT = ["json"] | ||
CELERY_RESULT_BACKEND = "db+sqlite:///results.sqlite" | ||
CELERY_TASK_SERIALIZER = "json" | ||
|
||
|
||
""" | ||
Django settings for proj project. | ||
Generated by 'django-admin startproject' using Django 2.2.1. | ||
For more information on this file, see | ||
https://docs.djangoproject.com/en/2.2/topics/settings/ | ||
For the full list of settings and their values, see | ||
https://docs.djangoproject.com/en/2.2/ref/settings/ | ||
""" | ||
|
||
|
||
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) | ||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
|
||
|
||
# Quick-start development settings - unsuitable for production | ||
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ | ||
|
||
# SECURITY WARNING: keep the secret key used in production secret! | ||
SECRET_KEY = "l!t+dmzf97rt9s*yrsux1py_1@odvz1szr&6&m!f@-nxq6k%%p" | ||
|
||
# SECURITY WARNING: don't run with debug turned on in production! | ||
DEBUG = True | ||
|
||
ALLOWED_HOSTS = [] | ||
|
||
|
||
# Application definition | ||
|
||
INSTALLED_APPS = [ | ||
"django.contrib.admin", | ||
"django.contrib.auth", | ||
"django.contrib.contenttypes", | ||
"django.contrib.sessions", | ||
"django.contrib.messages", | ||
"django.contrib.staticfiles", | ||
"demoapp", | ||
] | ||
|
||
MIDDLEWARE = [ | ||
"django.middleware.security.SecurityMiddleware", | ||
"django.contrib.sessions.middleware.SessionMiddleware", | ||
"django.middleware.common.CommonMiddleware", | ||
"django.middleware.csrf.CsrfViewMiddleware", | ||
"django.contrib.auth.middleware.AuthenticationMiddleware", | ||
"django.contrib.messages.middleware.MessageMiddleware", | ||
"django.middleware.clickjacking.XFrameOptionsMiddleware", | ||
] | ||
|
||
ROOT_URLCONF = "proj.urls" | ||
|
||
TEMPLATES = [ | ||
{ | ||
"BACKEND": "django.template.backends.django.DjangoTemplates", | ||
"DIRS": [], | ||
"APP_DIRS": True, | ||
"OPTIONS": { | ||
"context_processors": [ | ||
"django.template.context_processors.debug", | ||
"django.template.context_processors.request", | ||
"django.contrib.auth.context_processors.auth", | ||
"django.contrib.messages.context_processors.messages", | ||
], | ||
}, | ||
}, | ||
] | ||
|
||
WSGI_APPLICATION = "proj.wsgi.application" | ||
|
||
|
||
# Database | ||
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases | ||
|
||
DATABASES = { | ||
"default": { | ||
"ENGINE": "django.db.backends.sqlite3", | ||
"NAME": os.path.join(BASE_DIR, "db.sqlite3"), | ||
} | ||
} | ||
|
||
|
||
# Password validation | ||
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators | ||
|
||
AUTH_PASSWORD_VALIDATORS = [ | ||
{ | ||
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", | ||
}, | ||
{ | ||
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", | ||
}, | ||
{ | ||
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", | ||
}, | ||
{ | ||
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", | ||
}, | ||
] | ||
|
||
|
||
# Internationalization | ||
# https://docs.djangoproject.com/en/2.2/topics/i18n/ | ||
|
||
LANGUAGE_CODE = "en-us" | ||
|
||
TIME_ZONE = "UTC" | ||
|
||
USE_I18N = True | ||
|
||
USE_L10N = True | ||
|
||
USE_TZ = True | ||
|
||
|
||
# Static files (CSS, JavaScript, Images) | ||
# https://docs.djangoproject.com/en/2.2/howto/static-files/ | ||
|
||
STATIC_URL = "/static/" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Uncomment the next two lines to enable the admin: | ||
# from django.contrib import admin | ||
# admin.autodiscover() | ||
|
||
urlpatterns = [ | ||
# Examples: | ||
# url(r'^$', 'proj.views.home', name='home'), | ||
# url(r'^proj/', include('proj.foo.urls')), | ||
# Uncomment the admin/doc line below to enable admin documentation: | ||
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')), | ||
# Uncomment the next line to enable the admin: | ||
# url(r'^admin/', include(admin.site.urls)), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
""" | ||
WSGI config for proj project. | ||
This module contains the WSGI application used by Django's development server | ||
and any production WSGI deployments. It should expose a module-level variable | ||
named ``application``. Django's ``runserver`` and ``runfcgi`` commands discover | ||
this application via the ``WSGI_APPLICATION`` setting. | ||
Usually you will have the standard Django WSGI application here, but it also | ||
might make sense to replace the whole Django WSGI application with a custom one | ||
that later delegates to the Django one. For example, you could introduce WSGI | ||
middleware here, or combine a Django application with an application of another | ||
framework. | ||
""" | ||
|
||
import os | ||
|
||
# This application object is used by any WSGI server configured to use this | ||
# file. This includes Django's development server, if the WSGI_APPLICATION | ||
# setting points here. | ||
from django.core.wsgi import get_wsgi_application | ||
|
||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proj.settings") | ||
|
||
application = get_wsgi_application() | ||
|
||
# Apply WSGI middleware here. | ||
# from helloworld.wsgi import HelloWorldApplication | ||
# application = HelloWorldApplication(application) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[pytest] | ||
DJANGO_SETTINGS_MODULE = 'proj.settings' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
sqlalchemy>=1.2.18 | ||
django>=2.2.1 | ||
pytest-django>=4.7.0 | ||
# pytest-celery>=1.0.0 | ||
git+https://github.com/celery/pytest-celery.git | ||
pytest-xdist>=3.5.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
FROM python:3.11-bookworm | ||
|
||
# Create a user to run the worker | ||
RUN adduser --disabled-password --gecos "" test_user | ||
|
||
# Install system dependencies | ||
RUN apt-get update && apt-get install -y build-essential | ||
|
||
# Set arguments | ||
ARG CELERY_LOG_LEVEL=INFO | ||
ARG CELERY_WORKER_NAME=celery_dev_worker | ||
ARG CELERY_WORKER_QUEUE=celery | ||
ENV LOG_LEVEL=$CELERY_LOG_LEVEL | ||
ENV WORKER_NAME=$CELERY_WORKER_NAME | ||
ENV WORKER_QUEUE=$CELERY_WORKER_QUEUE | ||
|
||
# Install packages | ||
WORKDIR /src | ||
|
||
COPY --chown=test_user:test_user requirements.txt . | ||
RUN pip install --no-cache-dir --upgrade pip | ||
RUN pip install -r ./requirements.txt | ||
|
||
# Switch to the test_user | ||
USER test_user | ||
|
||
# Start the celery worker | ||
CMD celery -A proj worker --loglevel=$LOG_LEVEL -n $WORKER_NAME@%h -Q $WORKER_QUEUE |
Empty file.
Oops, something went wrong.