Skip to content

Commit 06c05a1

Browse files
author
Ahmed Ibrahim
committedApr 26, 2023
add example app
1 parent 06f88d5 commit 06c05a1

File tree

10 files changed

+130
-0
lines changed

10 files changed

+130
-0
lines changed
 

‎example/core/__init__.py

Whitespace-only changes.

‎example/core/admin.py

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

‎example/core/apps.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class CoreConfig(AppConfig):
5+
default_auto_field = "django.db.models.BigAutoField"
6+
name = "core"
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Generated by Django 4.2 on 2023-04-26 02:57
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
initial = True
8+
9+
dependencies = []
10+
11+
operations = [
12+
migrations.CreateModel(
13+
name="Human",
14+
fields=[
15+
(
16+
"id",
17+
models.BigAutoField(
18+
auto_created=True,
19+
primary_key=True,
20+
serialize=False,
21+
verbose_name="ID",
22+
),
23+
),
24+
("name", models.CharField(max_length=128)),
25+
(
26+
"level",
27+
models.IntegerField(
28+
choices=[(0, "Beginner"), (1, "Intermediate"), (2, "Advanced")]
29+
),
30+
),
31+
(
32+
"military_status",
33+
models.CharField(
34+
choices=[
35+
("exempted", "Exempted"),
36+
("served", "Served"),
37+
("postponed", "Postponed"),
38+
],
39+
max_length=128,
40+
),
41+
),
42+
],
43+
),
44+
]

‎example/core/migrations/__init__.py

Whitespace-only changes.

‎example/core/models.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from django.db import models
2+
3+
4+
class Human(models.Model):
5+
class Level(models.IntegerChoices):
6+
BEGINNER = 0
7+
INTERMEDIATE = 1
8+
ADVANCED = 2
9+
10+
class MilitaryStatus(models.TextChoices):
11+
EXEMPTED = "exempted", "Exempted"
12+
SERVED = "served", "Served"
13+
POSTPONED = "postponed", "Postponed"
14+
15+
name = models.CharField(max_length=128)
16+
level = models.IntegerField(choices=Level.choices)
17+
military_status = models.CharField(choices=MilitaryStatus.choices, max_length=128)

‎example/core/serializers.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from rest_framework import serializers
2+
3+
from django_rest_commons import EnumSerializer
4+
5+
from . import models
6+
7+
8+
class HumanSerializer(serializers.ModelSerializer):
9+
level = EnumSerializer(enum=models.Human.Level)
10+
military_status = EnumSerializer(enum=models.Human.MilitaryStatus)
11+
12+
class Meta:
13+
model = models.Human
14+
fields = [
15+
"id",
16+
"level",
17+
"military_status",
18+
]

‎example/core/tests.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

‎example/core/views.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from rest_framework.permissions import IsAdminUser, IsAuthenticated
2+
3+
from django_rest_commons import ModelViewSet
4+
5+
from .models import Human
6+
from .serializers import HumanSerializer
7+
8+
9+
class HumanViewSet(ModelViewSet):
10+
queryset = Human.objects.all()
11+
serializer_class = HumanSerializer
12+
13+
permission_classes_create = [IsAdminUser]
14+
permission_classes_destroy = [IsAdminUser]
15+
permission_classes_update = [IsAdminUser]
16+
permission_classes_list = [IsAuthenticated]
17+
permission_classes_retrieve = [IsAuthenticated]

‎example/manage.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
"""Django's command-line utility for administrative tasks."""
3+
import os
4+
import sys
5+
6+
7+
def main():
8+
"""Run administrative tasks."""
9+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dummy.settings")
10+
try:
11+
from django.core.management import execute_from_command_line
12+
except ImportError as exc:
13+
raise ImportError(
14+
"Couldn't import Django. Are you sure it's installed and "
15+
"available on your PYTHONPATH environment variable? Did you "
16+
"forget to activate a virtual environment?"
17+
) from exc
18+
execute_from_command_line(sys.argv)
19+
20+
21+
if __name__ == "__main__":
22+
main()

0 commit comments

Comments
 (0)
Please sign in to comment.