Skip to content

Commit 06257c8

Browse files
authored
docs: ✏️ django examples (#56)
1 parent c82108a commit 06257c8

File tree

22 files changed

+483
-0
lines changed

22 files changed

+483
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ pip install pyencrypt-pye
3434
✨🍰✨
3535
```
3636
Or you can use `pip install git+https://github.com/ZhaoQi99/pyencrypt-pye.git` install latest version.
37+
38+
## Examples
39+
View examples in the [examples](./examples) directory.
40+
3741
## Usage
3842

3943
```shell

examples/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Pyencrypt Examples
2+
3+
This directory contains examples of how to use pyencrypt.
4+
5+
* [`django`](./django): Pyencrypt Django Example

examples/django/Dockerfile

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
FROM python:3.10-bullseye as build
2+
3+
WORKDIR /root/demo
4+
5+
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
6+
7+
ARG PYPI_URL=https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
8+
RUN pip config set global.index-url $PYPI_URL
9+
10+
RUN sed -i '[email protected]@mirrors.aliyun.com@g' /etc/apt/sources.list
11+
RUN sed -i '[email protected]@mirrors.aliyun.com@g' /etc/apt/sources.list
12+
13+
RUN apt update
14+
RUN apt install gettext git vim lrzsz less gcc -y
15+
16+
ADD requirements.txt /root/demo
17+
RUN pip install -r requirements.txt
18+
COPY . /root/demo/
19+
20+
RUN python manage.py collectstatic --noinput
21+
22+
# --- Encryption ---
23+
RUN pip install git+https://github.com/ZhaoQi99/pyencrypt-pye.git
24+
RUN pyencrypt encrypt --in-place --yes .
25+
RUN cp encrypted/loader*.so .
26+
RUN rm -rf encrypted build/
27+
28+
RUN echo "import loader\n$(cat demo/__init__.py)" > demo/__init__.py
29+
30+
COPY manage.py /root/demo
31+
COPY demo/gunicorn.py /root/demo/demo
32+
33+
RUN pip uninstall pyencrypt-pye pycryptodome Cython python-minifier -y
34+
# --- Encryption ---
35+
36+
37+
FROM scratch
38+
COPY --from=build / /
39+
40+
WORKDIR /root/demo
41+
EXPOSE 8000
42+
# ENTRYPOINT [ "bash", "/root/demo/bin/start.sh"]

examples/django/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Pyencrypt Django Example
2+
3+
This example shows how to use `pyencrypt` with Django.
4+
5+
## How to use
6+
```shell
7+
docker compose up -d
8+
```
9+
10+
## Test
11+
* runserver: `curl http://127.0.0.1:8001/account/login/?username=admin&password=admin`
12+
* gunicorn: `curl http://127.0.0.1:8002/account/login/?username=admin&password=admin`
13+
14+
## Note
15+
* `manage.py` shouldn't be encrypted.
16+
* `gunicorn.py` shouldn't be encrypted.
17+
18+
### Loader
19+
* Copy `encrypted/loader*.so` to where `manage.py` is located.
20+
* Add `import loader` at the top of `<project>/__init__.py`.
21+
* Don't forget to remove `encrypted` and `build` directory.
22+
23+
### Docker
24+
* For preventing to extract origin layer from image, using [`scratch`](https://docs.docker.com/build/building/base-images/#create-a-base-image) to convert image to single layer.
25+
> [docker: extracting a layer from a image - Stack Overflow](https://stackoverflow.com/questions/40575752/docker-extracting-a-layer-from-a-image)
26+
* Remember to specify `WORKDIR`, `ENTRYPOINT` and other in `Dockerfile` again after `scratch`.

examples/django/account/__init__.py

Whitespace-only changes.

examples/django/account/admin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.

examples/django/account/apps.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class AccountConfig(AppConfig):
5+
default_auto_field = 'django.db.models.BigAutoField'
6+
name = 'account'
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# Generated by Django 4.1.10 on 2024-12-03 05:34
2+
3+
import django.contrib.auth.models
4+
import django.contrib.auth.validators
5+
from django.db import migrations, models
6+
import django.utils.timezone
7+
8+
9+
class Migration(migrations.Migration):
10+
11+
initial = True
12+
13+
dependencies = [
14+
("auth", "0012_alter_user_first_name_max_length"),
15+
]
16+
17+
operations = [
18+
migrations.CreateModel(
19+
name="User",
20+
fields=[
21+
(
22+
"id",
23+
models.BigAutoField(
24+
auto_created=True,
25+
primary_key=True,
26+
serialize=False,
27+
verbose_name="ID",
28+
),
29+
),
30+
("password", models.CharField(max_length=128, verbose_name="password")),
31+
(
32+
"last_login",
33+
models.DateTimeField(
34+
blank=True, null=True, verbose_name="last login"
35+
),
36+
),
37+
(
38+
"is_superuser",
39+
models.BooleanField(
40+
default=False,
41+
help_text="Designates that this user has all permissions without explicitly assigning them.",
42+
verbose_name="superuser status",
43+
),
44+
),
45+
(
46+
"username",
47+
models.CharField(
48+
error_messages={
49+
"unique": "A user with that username already exists."
50+
},
51+
help_text="Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.",
52+
max_length=150,
53+
unique=True,
54+
validators=[
55+
django.contrib.auth.validators.UnicodeUsernameValidator()
56+
],
57+
verbose_name="username",
58+
),
59+
),
60+
(
61+
"first_name",
62+
models.CharField(
63+
blank=True, max_length=150, verbose_name="first name"
64+
),
65+
),
66+
(
67+
"last_name",
68+
models.CharField(
69+
blank=True, max_length=150, verbose_name="last name"
70+
),
71+
),
72+
(
73+
"email",
74+
models.EmailField(
75+
blank=True, max_length=254, verbose_name="email address"
76+
),
77+
),
78+
(
79+
"is_staff",
80+
models.BooleanField(
81+
default=False,
82+
help_text="Designates whether the user can log into this admin site.",
83+
verbose_name="staff status",
84+
),
85+
),
86+
(
87+
"is_active",
88+
models.BooleanField(
89+
default=True,
90+
help_text="Designates whether this user should be treated as active. Unselect this instead of deleting accounts.",
91+
verbose_name="active",
92+
),
93+
),
94+
(
95+
"date_joined",
96+
models.DateTimeField(
97+
default=django.utils.timezone.now, verbose_name="date joined"
98+
),
99+
),
100+
(
101+
"groups",
102+
models.ManyToManyField(
103+
blank=True,
104+
help_text="The groups this user belongs to. A user will get all permissions granted to each of their groups.",
105+
related_name="user_set",
106+
related_query_name="user",
107+
to="auth.group",
108+
verbose_name="groups",
109+
),
110+
),
111+
(
112+
"user_permissions",
113+
models.ManyToManyField(
114+
blank=True,
115+
help_text="Specific permissions for this user.",
116+
related_name="user_set",
117+
related_query_name="user",
118+
to="auth.permission",
119+
verbose_name="user permissions",
120+
),
121+
),
122+
],
123+
options={
124+
"verbose_name": "user",
125+
"verbose_name_plural": "users",
126+
"abstract": False,
127+
},
128+
managers=[
129+
("objects", django.contrib.auth.models.UserManager()),
130+
],
131+
),
132+
]

examples/django/account/migrations/__init__.py

Whitespace-only changes.

examples/django/account/models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.contrib.auth.models import AbstractUser
2+
from django.db import models
3+
4+
5+
class User(AbstractUser):
6+
pass

0 commit comments

Comments
 (0)