Skip to content

Commit 226489d

Browse files
authored
Added Django Example (#152)
* Added django example * Added django examples tests to CI * Lint fixes * Fixed security issue with DjangoWorker.Dockerfile * Cleanup
1 parent a327b0e commit 226489d

20 files changed

+458
-0
lines changed

.github/workflows/examples.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
3+
4+
name: examples
5+
6+
on:
7+
push:
8+
branches: ["main"]
9+
paths:
10+
- "**.py"
11+
- "**.txt"
12+
- ".github/workflows/examples.yml"
13+
- "**.toml"
14+
pull_request:
15+
paths:
16+
- "**.py"
17+
- "**.txt"
18+
- "**.toml"
19+
- ".github/workflows/examples.yml"
20+
21+
permissions:
22+
contents: read # to fetch code (actions/checkout)
23+
24+
jobs:
25+
django:
26+
runs-on: ${{ matrix.os }}
27+
28+
strategy:
29+
fail-fast: false
30+
matrix:
31+
python-version: ["3.12"]
32+
os: ["ubuntu-latest"]
33+
34+
steps:
35+
- name: Install apt packages
36+
if: startsWith(matrix.os, 'ubuntu-')
37+
run: |
38+
sudo apt update
39+
- uses: actions/checkout@v4
40+
- name: Set up Python ${{ matrix.python-version }}
41+
uses: actions/setup-python@v5
42+
with:
43+
python-version: ${{ matrix.python-version }}
44+
cache: 'pip'
45+
cache-dependency-path: '**/setup.py'
46+
- name: Install dependencies
47+
working-directory: examples/django
48+
run: |
49+
python -m pip install --upgrade pip
50+
pip install -r requirements.txt
51+
52+
- name: Run Migrations
53+
working-directory: examples/django
54+
run: |
55+
./manage.py migrate
56+
57+
- name: Run tests
58+
working-directory: examples/django
59+
timeout-minutes: 5
60+
run: |
61+
export DJANGO_SETTINGS_MODULE=proj.settings
62+
pytest -vv tests -n auto

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ repos:
4040
- id: check-ast
4141
- id: check-case-conflict
4242
- id: check-docstring-first
43+
exclude: "examples/django/proj/settings.py"
4344
- id: check-json
4445
- id: check-merge-conflict
4546
- id: check-shebang-scripts-are-executable

examples/django/demoapp/__init__.py

Whitespace-only changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Generated by Django 2.2.1 on 2019-05-24 21:37
2+
3+
from django.db import migrations
4+
from django.db import models
5+
6+
7+
class Migration(migrations.Migration):
8+
initial = True
9+
10+
dependencies = []
11+
12+
operations = [
13+
migrations.CreateModel(
14+
name="Widget",
15+
fields=[
16+
("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
17+
("name", models.CharField(max_length=140)),
18+
],
19+
),
20+
]

examples/django/demoapp/migrations/__init__.py

Whitespace-only changes.

examples/django/demoapp/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.db import models
2+
3+
4+
class Widget(models.Model):
5+
name = models.CharField(max_length=140)

examples/django/demoapp/tasks.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Create your tasks here
2+
3+
from celery import shared_task
4+
5+
from .models import Widget
6+
7+
8+
@shared_task
9+
def add(x, y):
10+
return x + y
11+
12+
13+
@shared_task
14+
def mul(x, y):
15+
return x * y
16+
17+
18+
@shared_task
19+
def xsum(numbers):
20+
return sum(numbers)
21+
22+
23+
@shared_task
24+
def count_widgets():
25+
return Widget.objects.count()
26+
27+
28+
@shared_task
29+
def rename_widget(widget_id, name):
30+
w = Widget.objects.get(id=widget_id)
31+
w.name = name
32+
w.save()

examples/django/demoapp/views.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Create your views here.

examples/django/manage.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env python
2+
3+
import os
4+
import sys
5+
6+
if __name__ == "__main__":
7+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proj.settings")
8+
9+
from django.core.management import execute_from_command_line
10+
11+
execute_from_command_line(sys.argv)

examples/django/proj/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# This will make sure the app is always imported when
2+
# Django starts so that shared_task will use this app.
3+
from .celery import app as celery_app
4+
5+
__all__ = ("celery_app",)

0 commit comments

Comments
 (0)