Skip to content

Commit

Permalink
Merge pull request #59 from JahongirHakimjonov/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
JahongirHakimjonov authored Dec 9, 2024
2 parents 80068f1 + 5903a40 commit 94f154f
Show file tree
Hide file tree
Showing 37 changed files with 537 additions and 473 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ assets/staticfiles/
assets/staticfiles/*
assets/media/*
assets/media/
assets/client_secret_*.json
assets/client_secret_367993672441-sg99c5dmc7padjbirvok3esbi8j8j0cj.apps.googleusercontent.com.json
assets/*.json
assets/mystories.json
83 changes: 82 additions & 1 deletion apps/mystories/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.0.8 on 2024-11-27 11:53
# Generated by Django 5.0.8 on 2024-12-09 17:52

from django.db import migrations, models

Expand Down Expand Up @@ -101,13 +101,52 @@ class Migration(migrations.Migration):
auto_now=True, db_index=True, verbose_name="Updated at"
),
),
(
"banner",
models.FileField(
blank=True,
null=True,
upload_to="notifications",
verbose_name="Banner",
),
),
(
"title",
models.CharField(
db_index=True, max_length=255, verbose_name="Title"
),
),
(
"title_uz",
models.CharField(
db_index=True, max_length=255, null=True, verbose_name="Title"
),
),
(
"title_ru",
models.CharField(
db_index=True, max_length=255, null=True, verbose_name="Title"
),
),
(
"title_en",
models.CharField(
db_index=True, max_length=255, null=True, verbose_name="Title"
),
),
("message", models.TextField(db_index=True, verbose_name="Message")),
(
"message_uz",
models.TextField(db_index=True, null=True, verbose_name="Message"),
),
(
"message_ru",
models.TextField(db_index=True, null=True, verbose_name="Message"),
),
(
"message_en",
models.TextField(db_index=True, null=True, verbose_name="Message"),
),
("is_send", models.BooleanField(default=False, verbose_name="Is Send")),
("is_read", models.BooleanField(default=False, verbose_name="Is Read")),
],
Expand Down Expand Up @@ -258,6 +297,24 @@ class Migration(migrations.Migration):
db_index=True, max_length=255, verbose_name="Name"
),
),
(
"name_uz",
models.CharField(
db_index=True, max_length=255, null=True, verbose_name="Name"
),
),
(
"name_ru",
models.CharField(
db_index=True, max_length=255, null=True, verbose_name="Name"
),
),
(
"name_en",
models.CharField(
db_index=True, max_length=255, null=True, verbose_name="Name"
),
),
(
"is_active",
models.BooleanField(default=True, verbose_name="Is active"),
Expand Down Expand Up @@ -300,6 +357,30 @@ class Migration(migrations.Migration):
db_index=True, max_length=255, verbose_name="Name"
),
),
(
"name_uz",
models.CharField(
db_index=True, max_length=255, null=True, verbose_name="Name"
),
),
(
"name_ru",
models.CharField(
db_index=True, max_length=255, null=True, verbose_name="Name"
),
),
(
"name_en",
models.CharField(
db_index=True, max_length=255, null=True, verbose_name="Name"
),
),
(
"image",
models.ImageField(
blank=True, null=True, upload_to="themes/", verbose_name="Image"
),
),
(
"is_active",
models.BooleanField(default=True, verbose_name="Is active"),
Expand Down
2 changes: 1 addition & 1 deletion apps/mystories/migrations/0002_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 5.0.8 on 2024-11-27 11:53
# Generated by Django 5.0.8 on 2024-12-09 17:52

import django.db.models.deletion
from django.conf import settings
Expand Down

This file was deleted.

19 changes: 0 additions & 19 deletions apps/mystories/migrations/0004_notification_banner.py

This file was deleted.

7 changes: 5 additions & 2 deletions apps/mystories/serializers/posts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from rest_framework import serializers

from drf_spectacular.utils import extend_schema_field
from apps.mystories.models import Post


Expand All @@ -25,12 +25,15 @@ class Meta:
"is_saved",
)

@extend_schema_field(serializers.BooleanField)
def get_is_author(self, obj):
return obj.author == self.context["request"].user

@extend_schema_field(serializers.BooleanField)
def get_is_liked(self, obj):
return obj.likes.filter(user=self.context["request"].user).exists()

@extend_schema_field(serializers.BooleanField)
def get_is_saved(self, obj):
return obj.saves.filter(user=self.context["request"].user).exists()

Expand Down Expand Up @@ -82,7 +85,7 @@ class Meta:

def to_representation(self, instance):
representation = super().to_representation(instance)
representation["content"] = instance.content[:100]
representation["content"] = instance.content
return representation


Expand Down
1 change: 1 addition & 0 deletions apps/mystories/signals/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .notification import * # noqa
from .posts import * # noqa
15 changes: 15 additions & 0 deletions apps/mystories/signals/notification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import logging

from django.db.models.signals import post_save
from django.dispatch import receiver

from apps.mystories.models import Notification
from apps.mystories.tasks import send_notification_task

logger = logging.getLogger(__name__)


@receiver(post_save, sender=Notification)
def send_notification(sender, instance, created, **kwargs): # noqa
if created:
send_notification_task.delay(instance.id)
1 change: 1 addition & 0 deletions apps/mystories/tasks/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .notification import * # noqa
41 changes: 41 additions & 0 deletions apps/mystories/tasks/notification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import logging
import time

from celery import shared_task
from firebase_admin import messaging

from apps.mystories.models import Notification
from apps.users.models import ActiveSessions

logger = logging.getLogger(__name__)


@shared_task
def send_notification_task(notification_id):
try:
time.sleep(1)
notification = Notification.objects.get(id=notification_id)
fcm_tokens = ActiveSessions.objects.filter(
user=notification.user, is_active=True, fcm_token__isnull=False
).values_list("fcm_token", flat=True)
for fcm_token in fcm_tokens:
if fcm_token:
message = messaging.Message(
notification=messaging.Notification(
title=notification.title,
body=notification.message,
image=(
notification.banner.url
if notification.banner
and hasattr(notification.banner, "url")
else None
),
),
token=fcm_token,
)
messaging.send(message)
notification.is_send = True
notification.save()
logger.info(f"Notification sent to user {notification.user.id}")
except Notification.DoesNotExist:
logger.error(f"Notification with id {notification_id} does not exist")
8 changes: 4 additions & 4 deletions apps/mystories/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

urlpatterns = [
path("posts/", PostListCreateView.as_view(), name="posts"),
path("posts/<int:pk>/", PostDetailUpdateDeleteView.as_view(), name="posts-detail"),
path("theme/", ThemeApiView.as_view(), name="theme"),
path("theme/<int:pk>/", TagsByThemeApiView.as_view(), name="theme-detail"),
path("posts/<int:pk>/", PostDetailUpdateDeleteView.as_view(), name="post_detail"),
path("theme/", ThemeApiView.as_view(), name="theme_list"),
path("theme/<int:pk>/", TagsByThemeApiView.as_view(), name="tags_by_theme"),
path("like/", LikeApiView.as_view(), name="like"),
path("save/", SavedApiView.as_view(), name="save"),
path("comment/", CommentApiView.as_view(), name="comment"),
path("comment/<int:pk>/", CommentDeleteApiView.as_view(), name="comment-delete"),
path("comment/<int:pk>/", CommentDeleteApiView.as_view(), name="comment_delete"),
path("notification/", NotificationApiView.as_view(), name="notification"),
]
Loading

0 comments on commit 94f154f

Please sign in to comment.