Skip to content

Commit

Permalink
Merge branch 'main'
Browse files Browse the repository at this point in the history
  • Loading branch information
RustamovAkrom committed Nov 21, 2024
2 parents 37139a3 + 1564ffb commit e670178
Show file tree
Hide file tree
Showing 94 changed files with 5,720 additions and 351 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ exclude =
__pycache__,
migrations,
static,
env, # your virtual environment directory
venv, # your virtual environment directory

# Enable checking for complexity
max-complexity = 10
Expand Down
18 changes: 8 additions & 10 deletions .github/workflows/django.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Django CI
name: Django Test

on:
push:
Expand All @@ -20,23 +20,21 @@ jobs:

steps:
- name: Check out the repository
uses: actions/checkout@v4 # This is an action, so it uses 'uses'
uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3 # This is an action, so it uses 'uses'
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}

- name: Install OpenSSL
run: sudo apt-get install -y openssl # This is a command, so it uses 'run'
run: sudo apt-get install -y openssl

- name: Generate private and public keys
run: |
# Очистка старых файлов перед созданием новых
rm -rf security
mkdir -p security
# Генерация приватного и публичного ключа
openssl genpkey -algorithm RSA -out security/private_key.pem -pkeyopt rsa_keygen_bits:2048
openssl rsa -pubout -in security/private_key.pem -out security/public_key.pem
Expand All @@ -53,17 +51,17 @@ jobs:
- name: Check if .env has been updated
run: cat .env # This is a command, so it uses 'run'
run: cat .env

- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt # This is a command, so it uses 'run'
pip install -r requirements.txt
- name: Run Tests
run: python manage.py test # This is a command, so it uses 'run'
run: python manage.py test

- name: Clean up keys (Optional)
run: |
rm -rf security
echo "Keys removed after use." # This is a command, so it uses 'run'
echo "Keys removed after use."
68 changes: 68 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Lint Code

on:
push:
branches:
- "main"
- "master"
pull_request:
branches:
- "main"
- "master"

jobs:

build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.8, 3.9]

steps:
- name: Check out the repository
uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}

- name: Install OpenSSL
run: sudo apt-get install -y openssl

- name: Generate private and public keys
run: |
rm -rf security
mkdir -p security
openssl genpkey -algorithm RSA -out security/private_key.pem -pkeyopt rsa_keygen_bits:2048
openssl rsa -pubout -in security/private_key.pem -out security/public_key.pem
echo "Private key saved as security/private_key.pem"
echo "Public key saved as security/public_key.pem"
- name: Create .env file
run: |
touch .env
echo PRIVATE_KEY_PATH=$(pwd)/security/private_key.pem >> .env
echo PUBLIC_KEY_PATH=$(pwd)/security/public_key.pem >> .env
DJANGO_SETTINGS_MODULE=core.settings.development >> .env
echo DATABASE_ENVIRON=sqlite3 >> .env
- name: Check if .env has been updated
run: cat .env

- name: Install Dependencies
run: |
pip install flake8
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Lint Code
run: flake8 .

- name: Clean up keys (Optional)
run: |
rm -rf security
echo "Keys removed after use."
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,5 @@ GitHub.sublime-settings
# private_key.pem
# public_key.pem

db.sqlite3
db.sqlite3
poetry.lock
12 changes: 7 additions & 5 deletions apps/blog/admin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from django.contrib import admin
from .models import Post, PostComment, PostLike, PostDislike, PostCommentLike

from unfold.admin import ModelAdmin


@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
class PostAdmin(ModelAdmin):
list_display = ["title", "content", "author", "is_active"]
search_fields = ["title", "content"]
list_filter = ["author", "is_active"]
Expand All @@ -12,20 +14,20 @@ class PostAdmin(admin.ModelAdmin):


@admin.register(PostComment)
class PostCommentAdmin(admin.ModelAdmin):
class PostCommentAdmin(ModelAdmin):
pass


@admin.register(PostLike)
class PostLikeAdmin(admin.ModelAdmin):
class PostLikeAdmin(ModelAdmin):
pass


@admin.register(PostDislike)
class PostDislike(admin.ModelAdmin):
class PostDislike(ModelAdmin):
pass


@admin.register(PostCommentLike)
class PostCommentLikeAdmin(admin.ModelAdmin):
class PostCommentLikeAdmin(ModelAdmin):
pass
18 changes: 18 additions & 0 deletions apps/blog/api_endpoints/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from rest_framework import routers

from .blog import (
PostViewSet,
PostCommentViewSet,
PostCommentLikeViewSet,
PostLikeViewSet,
PostDislikeViewSet,
)

router = routers.DefaultRouter()
router.register("post", PostViewSet, basename="post")
router.register("post_comment", PostCommentViewSet, basename="post-comment")
router.register(
"post_comment_like", PostCommentLikeViewSet, basename="post-comment-like"
)
router.register("post_like", PostLikeViewSet, basename="post-like")
router.register("post_dislike", PostDislikeViewSet, basename="post-dislike")
1 change: 1 addition & 0 deletions apps/blog/api_endpoints/blog/Post/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .views import * # noqa
24 changes: 24 additions & 0 deletions apps/blog/api_endpoints/blog/Post/serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from rest_framework import serializers

from apps.blog.models import Post


class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = [
"id",
"title",
"get_absolute_url",
"status",
"description",
"publisher_at",
"is_active",
"author",
"like_count",
"dislike_count",
"comment_count",
"watching",
"created_at",
"updated_at",
]
Empty file.
17 changes: 17 additions & 0 deletions apps/blog/api_endpoints/blog/Post/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from rest_framework import viewsets, permissions

from apps.blog.models import Post
from .serializer import PostSerializer


class PostViewSet(viewsets.ModelViewSet):
queryset = Post.published.all().order_by("-created_at")
serializer_class = PostSerializer

def get_permissions(self):
if self.action in ["list", "retrieve"]:
return [permissions.AllowAny()]
return [permissions.IsAuthenticated()]


__all__ = ("PostViewSet",)
1 change: 1 addition & 0 deletions apps/blog/api_endpoints/blog/PostComment/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .views import * # noqa
25 changes: 25 additions & 0 deletions apps/blog/api_endpoints/blog/PostComment/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from rest_framework import serializers

from apps.blog.models import PostComment, Post
from apps.users.models import User


class MiniPostCommentUser(serializers.ModelSerializer):
class Meta:
model = User
fields = ["id", "username", "email", "created_at", "updated_at"]


class MiniPostCommentPost(serializers.ModelSerializer):
class Meta:
model = Post
fields = ["id", "title", "created_at", "updated_at"]


class PostCommentSerializer(serializers.ModelSerializer):
user = MiniPostCommentUser(read_only=True)
comment = MiniPostCommentPost(read_only=True)

class Meta:
model = PostComment
fields = ["id", "user", "comment", "created_at", "updated_at"]
Empty file.
17 changes: 17 additions & 0 deletions apps/blog/api_endpoints/blog/PostComment/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from rest_framework import viewsets, permissions

from apps.blog.models import PostComment
from .serializers import PostCommentSerializer


class PostCommentViewSet(viewsets.ModelViewSet):
queryset = PostComment.objects.all().order_by("-created_at")
serializer_class = PostCommentSerializer

def get_permissions(self):
if self.action in ["list", "retrieve"]:
return [permissions.AllowAny()]
return [permissions.IsAuthenticated()]


__all__ = ("PostCommentViewSet",)
1 change: 1 addition & 0 deletions apps/blog/api_endpoints/blog/PostCommentLike/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .views import * # noqa
24 changes: 24 additions & 0 deletions apps/blog/api_endpoints/blog/PostCommentLike/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from rest_framework import serializers

from apps.blog.models import PostCommentLike
from apps.users.models import User


class MiniPostCommentLikeUserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ["id", "username", "created_at", "updated_at"]


class MiniPostCommentLikePostCommentSerializer(serializers.ModelSerializer):
class Meta:
fields = ["id", "post", "message", "created_at", "updated_at"]


class PostCommentLikeSerializer(serializers.ModelSerializer):
user = MiniPostCommentLikeUserSerializer(read_only=True)
comment = MiniPostCommentLikePostCommentSerializer(read_only=True)

class Meta:
model = PostCommentLike
fields = ["id", "user", "comment"]
Empty file.
13 changes: 13 additions & 0 deletions apps/blog/api_endpoints/blog/PostCommentLike/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from rest_framework import viewsets, permissions

from apps.blog.models import PostCommentLike
from .serializers import PostCommentLikeSerializer


class PostCommentLikeViewSet(viewsets.ModelViewSet):
queryset = PostCommentLike.objects.all().order_by("-id")
serializer_class = PostCommentLikeSerializer
permission_classes = [permissions.IsAuthenticated]


__all__ = ("PostCommentLikeViewSet",)
1 change: 1 addition & 0 deletions apps/blog/api_endpoints/blog/PostDislike/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .views import * # noqa
9 changes: 9 additions & 0 deletions apps/blog/api_endpoints/blog/PostDislike/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from rest_framework import serializers

from apps.blog.models import PostDislike


class PostDislikeSerializer(serializers.ModelSerializer):
class Meta:
model = PostDislike
fields = ["id", "user", "post"]
Empty file.
34 changes: 34 additions & 0 deletions apps/blog/api_endpoints/blog/PostDislike/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from django.shortcuts import get_object_or_404

from rest_framework import viewsets, response, status, permissions

from apps.blog.models import PostDislike, PostLike, Post
from .serializers import PostDislikeSerializer


class PostDislikeViewSet(viewsets.ModelViewSet):
queryset = PostDislike.objects.all()
serializer_class = PostDislikeSerializer
permission_classes = [permissions.IsAuthenticated]

def create(self, request, *args, **kwargs):
post_id = request.data.get("post")
post = get_object_or_404(Post, id=post_id)
user = request.user

PostLike.objects.filter(post=post, user=user).delete()

existing_dislike = PostDislike.objects.filter(post=post, user=user)
if existing_dislike.exists():
existing_dislike.delete()
return response.Response(
{"message": "Dislike removed"}, status=status.HTTP_200_OK
)

dislike = PostDislike.objects.create(post=post, user=user)
serializer = self.get_serializer(dislike)

return response.Response(serializer.data, status=status.HTTP_201_CREATED)


__all__ = ("PostDislikeViewSet",)
1 change: 1 addition & 0 deletions apps/blog/api_endpoints/blog/PostLike/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .views import * # noqa
9 changes: 9 additions & 0 deletions apps/blog/api_endpoints/blog/PostLike/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from rest_framework import serializers

from apps.blog.models import PostLike


class PostLikeSerializer(serializers.ModelSerializer):
class Meta:
model = PostLike
fields = ["id", "user", "post"]
Empty file.
Loading

0 comments on commit e670178

Please sign in to comment.