Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM python:3.7-stretch

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider using more fresh python

RUN apt-get update

@mvoitko mvoitko Sep 21, 2021

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. add --yes flag
  2. Remove cache with rm -rf /var/lib/apt/lists/*
  3. Why do you need update if there is no installation here.

RUN apt-get upgrade -y

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better not to upgrade distribution

COPY ./ /

@mvoitko mvoitko Sep 21, 2021

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding

ENV DEBIAN_FRONTEND=noninteractive
# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE 1
# Prevents Python from buffering stdout and stderr
ENV PYTHONUNBUFFERED 1

WORKDIR ./

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specify workdir explicitly

RUN pip3 install --no-cache-dir -r requirements.txt && \

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use specific pip version

rm -v requirements.txt
EXPOSE 8000

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding:

COPY scripts/entrypoint.sh /app/scripts/entrypoint.sh

ENTRYPOINT ["./scripts/entrypoint.sh"]

6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

up:
@docker-compose -f docker/docker-compose.yml up -d
@docker-compose build
@docker-compose up -d
@docker ps

down:
@docker-compose -f docker/docker-compose.yml down
@docker-compose down
10 changes: 9 additions & 1 deletion docker/docker-compose.yml → docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
version: "3"
services:
app:
container_name: app
build: .
command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you have more than 1 command it's better to have them in entrypoint.sh file with appropriate linting. It would be easier to maintain and read

ports:
- "8552:8000"
depends_on:
- db

db:
image: "postgres:12"
environment:
Expand All @@ -11,6 +20,5 @@ services:
ports:
- "5432:5432"


volumes:
redit_volume:
2 changes: 0 additions & 2 deletions posts/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
from django.contrib import admin

# Register your models here.
22 changes: 0 additions & 22 deletions posts/migrations/0001_initial.py

This file was deleted.

18 changes: 0 additions & 18 deletions posts/migrations/0002_post_approved.py

This file was deleted.

2 changes: 2 additions & 0 deletions posts/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.db import models
from users.models import User


class Post(models.Model):
text = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
approved = models.BooleanField(default=False)
user = models.ForeignKey(User, on_delete=models.CASCADE)
2 changes: 1 addition & 1 deletion posts/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ class PostSerializer(serializers.ModelSerializer):

class Meta:
model = Post
fields = ("id", "text", "created_at")
fields = ("id", "text", "created_at", "user")
3 changes: 2 additions & 1 deletion redit/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

APPS = [
'posts',
'users',
]

INSTALLED_APPS = CORE_APPS + THIRD_PARTY_APPS + APPS
Expand Down Expand Up @@ -91,7 +92,7 @@
'NAME': 'redit_db',
'USER': 'user',
'PASSWORD': 'password',
'HOST': '127.0.0.1',
'HOST': 'db',

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all should be read from env vars

'PORT': '5432',
}
}
Expand Down
1 change: 1 addition & 0 deletions redit/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
urlpatterns = [
path('admin/', admin.site.urls),
path('posts/', include('posts.urls')),
path('user/', include('users.urls'))
]
Empty file added users/__init__.py
Empty file.
1 change: 1 addition & 0 deletions users/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from django.contrib import admin
5 changes: 5 additions & 0 deletions users/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class UsersConfig(AppConfig):
name = 'users'
Empty file added users/migrations/__init__.py
Empty file.
18 changes: 18 additions & 0 deletions users/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.db import models
from django.core import validators
from django.contrib.auth.models import AbstractBaseUser, UserManager


class User(AbstractBaseUser):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need in empty line here. Consider adding linting and type checking configs in setup.cfg

username = models.CharField(db_index=True, max_length=255, unique=True)
email = models.EmailField(validators=[validators.validate_email], unique=True, blank=False)
is_staff = models.BooleanField(default=False) # a admin user; non super-user
is_superuser = models.BooleanField(default=False) # a superuser
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ('username',)

objects = UserManager()

def __str__(self):
return self.username

@mvoitko mvoitko Sep 16, 2021

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider adding more unique fields to identify user. Are you sure this works? I guess it should be smth like f"{self.username}"

18 changes: 18 additions & 0 deletions users/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from rest_framework import serializers
from .models import User


class RegistrationSerializer(serializers.ModelSerializer):

password = serializers.CharField(
max_length=128,
min_length=8,
write_only=True,
)

class Meta:
model = User
fields = ('email', 'username', 'password')

def create(self, validated_data):
return User.objects.create_user(**validated_data)
9 changes: 9 additions & 0 deletions users/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.urls import re_path

from .views import RegistrationAPIView
# from .views import LoginAPIView
from django.urls import path

urlpatterns = [
path("registration", RegistrationAPIView.as_view(), name='user_registration'),
]
23 changes: 23 additions & 0 deletions users/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from rest_framework import status
# from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework.views import APIView

# from .models import User
# from .serializers import LoginSerializer
from .serializers import RegistrationSerializer


class RegistrationAPIView(APIView):

serializer_class = RegistrationSerializer

def post(self, request):

serializer = self.serializer_class(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()

return Response(
status=status.HTTP_201_CREATED,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider responding with user_id and maybe even all her data

)