Skip to content

Commit

Permalink
image_uploader
Browse files Browse the repository at this point in the history
  • Loading branch information
amirkeyhani committed May 31, 2022
0 parents commit 302d36a
Show file tree
Hide file tree
Showing 58 changed files with 2,799 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#Virtual env
env/
uploader/__pycache__/

#Database
db.sqlite3

#Migrations
uploader/migrations/

#Credentials
.env

#Static files
media/
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM python:3.10.4
LABEL MAINTAINER="Amir Keyhani | https://amir.key_"

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN mkdir /imageuploader
WORKDIR /imageuploader
COPY . /imageuploader

ADD requirements.txt /imageuploader
RUN pip install --upgrade pip
RUN pip install -r requirements.txt

RUN python manage.py collectstatic --no-input

CMD ["gunicorn", "--chdir", "imageuploader", "--bind", ":8000", "imageuploader.wsgi:application"]
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: waitress-serve --port=$PORT imageuploader.wsgi:application
Empty file added README.md
Empty file.
Empty file added api/__init__.py
Empty file.
Binary file added api/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added api/__pycache__/admin.cpython-310.pyc
Binary file not shown.
Binary file added api/__pycache__/apps.cpython-310.pyc
Binary file not shown.
Binary file added api/__pycache__/models.cpython-310.pyc
Binary file not shown.
Binary file added api/__pycache__/serializers.cpython-310.pyc
Binary file not shown.
Binary file added api/__pycache__/urls.cpython-310.pyc
Binary file not shown.
Binary file added api/__pycache__/views.cpython-310.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions api/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions api/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class ApiConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'api'
Empty file added api/migrations/__init__.py
Empty file.
Binary file added api/migrations/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions api/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
20 changes: 20 additions & 0 deletions api/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from rest_framework import serializers
from uploader.models import Uploader, Comment, Profile


class UploaderSerializer(serializers.ModelSerializer):
class Meta:
model = Uploader
fields = '__all__'


class CommentSerializer(serializers.ModelSerializer):
class Meta:
model = Comment
fields = '__all__'


class ProfileSerializer(serializers.ModelSerializer):
class Meta:
model = Profile
fields = '__all__'
3 changes: 3 additions & 0 deletions api/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
25 changes: 25 additions & 0 deletions api/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from django.urls import path
from .views import *

urlpatterns = [
path('uploader-list/', UploaderList.as_view(), name="uploader-list"),
path('uploader-detail/<int:pk>/', UploaderDetail.as_view(), name="uploader-detail"),
path('uploader-create/', UploaderCreate.as_view(), name="uploader-create"),
path('uploader-update/<int:pk>/', UploaderUpdate.as_view(), name="uploader-update"),
path('uploader-patch/<int:pk>/', UploaderUpdate.as_view(), name="uploader-patch"),
path('uploader-delete/<int:pk>/', UploaderDelete.as_view(), name="uploader-delete"),

path('comment-list/', CommentList.as_view(), name="comment-list"),
path('comment-detail/<int:pk>/', CommentDetail.as_view(), name="comment-detail"),
path('comment-create/', CommentCreate.as_view(), name="comment-create"),
path('comment-update/<int:pk>/', CommentUpdate.as_view(), name="comment-update"),
path('comment-patch/<int:pk>/', CommentUpdate.as_view(), name="comment-patch"),
path('comment-delete/<int:pk>/', CommentDelete.as_view(), name="comment-delete"),

path('profile-list/', ProfileList.as_view(), name="profile-list"),
path('profile-detail/<int:pk>/', ProfileDetail.as_view(), name="profile-detail"),
path('profile-create/', ProfileCreate.as_view(), name="profile-create"),
path('profile-update/<int:pk>/', ProfileUpdate.as_view(), name="profile-update"),
path('profile-patch/<int:pk>/', ProfileUpdate.as_view(), name="profile-patch"),
path('profile-delete/<int:pk>/', ProfileDelete.as_view(), name="profile-delete"),
]
242 changes: 242 additions & 0 deletions api/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.decorators import api_view
from uploader.models import Uploader, Comment, Profile
from .serializers import UploaderSerializer, CommentSerializer, ProfileSerializer
from rest_framework.response import Response
from rest_framework import status
from rest_framework.permissions import IsAuthenticated, IsAuthenticatedOrReadOnly
from rest_framework.authentication import TokenAuthentication

# Create your views here.

class UploaderList(APIView):
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticatedOrReadOnly]

def get(self, request, format=None):
uploaders = Uploader.objects.all()
serializer = UploaderSerializer(uploaders, many=True)
return Response(serializer.data)

class UploaderCreate(APIView):
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]

def post(self, request, format=None):
serializer = UploaderSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

class UploaderDetail(APIView):
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticatedOrReadOnly]

def get_object(self, pk):
try:
return Uploader.objects.get(pk=pk)
except Uploader.DoesNotExist:
raise Response(status=status.HTTP_404_NOT_FOUND)

def get(self, request, pk, format=None):
uploader = self.get_object(pk)
serializer = UploaderSerializer(uploader)
return Response(serializer.data)

class UploaderUpdate(APIView):
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]

def get_object(self, pk):
try:
return Uploader.objects.get(pk=pk)
except Uploader.DoesNotExist:
raise Response(status=status.HTTP_404_NOT_FOUND)

def put(self, request, pk, format=None):
uploader = self.get_object(pk)
serializer = UploaderSerializer(uploader, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

def patch(self, request, pk, format=None):
uploader = self.get_object(pk)
serializer = UploaderSerializer(
uploader, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

class UploaderDelete(APIView):
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]

def get_object(self, pk):
try:
return Uploader.objects.get(pk=pk)
except Uploader.DoesNotExist:
raise Response(status=status.HTTP_404_NOT_FOUND)

def delete(self, request, pk, format=None):
uploader = self.get_object(pk)
uploader.delete()
return Response('Uploader deleted', status=status.HTTP_204_NO_CONTENT)

class CommentList(APIView):
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticatedOrReadOnly]

def get(self, request, format=None):
comments = Comment.objects.all()
serializer = CommentSerializer(comments, many=True)
return Response(serializer.data)

class CommentCreate(APIView):
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]

def post(self, request, format=None):
serializer = CommentSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

class CommentDetail(APIView):
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticatedOrReadOnly]

def get_object(self, pk):
try:
return Comment.objects.get(pk=pk)
except Comment.DoesNotExist:
raise Response(status=status.HTTP_404_NOT_FOUND)

def get(self, request, pk, format=None):
comment = self.get_object(pk)
serializer = CommentSerializer(comment)
return Response(serializer.data)

class CommentUpdate(APIView):
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]

def get_object(self, pk):
try:
return Comment.objects.get(pk=pk)
except Comment.DoesNotExist:
raise Response(status=status.HTTP_404_NOT_FOUND)

def put(self, request, pk, format=None):
comment = self.get_object(pk)
serializer = CommentSerializer(comment, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

def patch(self, request, pk, format=None):
comment = self.get_object(pk)
serializer = CommentSerializer(
comment, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

class CommentDelete(APIView):
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]

def get_object(self, pk):
try:
return Comment.objects.get(pk=pk)
except Comment.DoesNotExist:
raise Response(status=status.HTTP_404_NOT_FOUND)

def delete(self, request, pk, format=None):
comment = self.get_object(pk)
comment.delete()
return Response('Comment deleted', status=status.HTTP_204_NO_CONTENT)

class ProfileList(APIView):
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticatedOrReadOnly]

def get(self, request, format=None):
profiles = Profile.objects.all()
serializer = ProfileSerializer(profiles, many=True)
return Response(serializer.data)

class ProfileCreate(APIView):
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]

def post(self, request, format=None):
serializer = ProfileSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

class ProfileDetail(APIView):
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticatedOrReadOnly]

def get_object(self, pk):
try:
return Profile.objects.get(pk=pk)
except Profile.DoesNotExist:
raise Response(status=status.HTTP_404_NOT_FOUND)

def get(self, request, pk, format=None):
profile = self.get_object(pk)
serializer = ProfileSerializer(profile)
return Response(serializer.data)

class ProfileUpdate(APIView):
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]

def get_object(self, pk):
try:
return Profile.objects.get(pk=pk)
except Profile.DoesNotExist:
raise Response(status=status.HTTP_404_NOT_FOUND)

def put(self, request, pk, format=None):
profile = self.get_object(pk)
serializer = ProfileSerializer(profile, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

def patch(self, request, pk, format=None):
profile = self.get_object(pk)
serializer = ProfileSerializer(
profile, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

class ProfileDelete(APIView):
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]

def get_object(self, pk):
try:
return Profile.objects.get(pk=pk)
except Profile.DoesNotExist:
raise Response(status=status.HTTP_404_NOT_FOUND)

def delete(self, request, pk, format=None):
profile = self.get_object(pk)
profile.delete()
return Response('profile deleted', status=status.HTTP_204_NO_CONTENT)
7 changes: 7 additions & 0 deletions config/nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM nginx:1.21.6
LABEL MAINTAINER="Amir Keyhani | https://amir.key_"

COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
25 changes: 25 additions & 0 deletions config/nginx/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: "3.9"

services:
nginx:
container_name: nginx
build:
context: .
dockerfile: Dockerfile
ports:
- "80:80"
volumes:
- imageuploader_static_volume:/imageuploader/static
- imageuploader_media_volume:/imageuploader/media
networks:
- nginx_network


volumes:
imageuploader_static_volume:
external: true
imageuploader_media_volume:
external: true
networks:
nginx_network:
external: true
Loading

0 comments on commit 302d36a

Please sign in to comment.