Skip to content

Commit ab6510d

Browse files
authored
Merge pull request #318 from PROCOLLAB-github/flexivanov237-pro-212
fixed bug. also slightly optimized other signal
2 parents 71272d3 + c2d64bd commit ab6510d

6 files changed

Lines changed: 64 additions & 30 deletions

File tree

feed/constants.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from projects.models import Project
2+
from vacancy.models import Vacancy
3+
4+
SIGNALS_MODELS = Vacancy | Project

feed/services.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from django.contrib.contenttypes.models import ContentType
2+
3+
from feed.constants import SIGNALS_MODELS
4+
from news.models import News
5+
6+
7+
def delete_news_for_model(instance: SIGNALS_MODELS):
8+
content_type = ContentType.objects.get_for_model(instance)
9+
obj = News.objects.filter(content_type=content_type, object_id=instance.id).first()
10+
if obj:
11+
obj.delete()
12+
13+
14+
def create_news_for_model(instance: SIGNALS_MODELS):
15+
content_type = ContentType.objects.get_for_model(instance)
16+
News.objects.get_or_create(content_type=content_type, object_id=instance.id)

feed/signals.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
1-
from django.contrib.contenttypes.models import ContentType
2-
from django.db.models.signals import post_save
1+
from django.db.models.signals import post_save, post_delete
32
from django.dispatch import receiver
43

5-
from news.models import News
6-
4+
from feed.services import create_news_for_model, delete_news_for_model
5+
from projects.models import Project
76

87
from vacancy.models import Vacancy
98

109

1110
@receiver(post_save, sender=Vacancy)
1211
def create_news_on_save(sender, instance, created, **kwargs):
13-
if created:
14-
content_type = ContentType.objects.filter(model="vacancy").first()
15-
news_instance, created = News.objects.get_or_create(
16-
content_type=content_type, object_id=instance.id
17-
)
12+
if instance.is_active:
13+
create_news_for_model(instance)
14+
else:
15+
delete_news_for_model(instance)
16+
17+
18+
@receiver(post_delete, sender=Vacancy)
19+
def delete_news_vacancy(sender, instance, **kwargs):
20+
delete_news_for_model(instance)
21+
22+
23+
@receiver(post_save, sender=Project)
24+
def create_news_on_created_project(sender, instance, created, **kwargs):
25+
if not instance.draft:
26+
create_news_for_model(instance)
27+
else:
28+
delete_news_for_model(instance)
29+
30+
31+
@receiver(post_delete, sender=Project)
32+
def delete_news_project(sender, instance, **kwargs):
33+
delete_news_for_model(instance)

feed/views.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,36 @@ def get(self, *args, **kwargs):
5959

6060
class DevScript(CreateAPIView):
6161
def create(self, request):
62-
content_type = ContentType.objects.filter(model="project").first()
62+
content_type_project = ContentType.objects.filter(model="project").first()
6363
for project in Project.objects.filter(draft=False):
6464
if not News.objects.filter(
65-
content_type=content_type, object_id=project.id
65+
content_type=content_type_project, object_id=project.id
6666
).exists():
6767
News.objects.create(
68-
content_type=content_type,
68+
content_type=content_type_project,
6969
object_id=project.id,
7070
datetime_created=project.datetime_created,
7171
)
7272

73-
content_type = ContentType.objects.filter(model="vacancy").first()
73+
content_type_vacancy = ContentType.objects.filter(model="vacancy").first()
7474
for vacancy in Vacancy.objects.filter(is_active=True):
7575
if not News.objects.filter(
76-
content_type=content_type, object_id=vacancy.id
76+
content_type=content_type_vacancy, object_id=vacancy.id
7777
).exists():
7878
News.objects.create(
79-
content_type=content_type,
79+
content_type=content_type_vacancy,
8080
object_id=vacancy.id,
8181
datetime_created=vacancy.datetime_created,
8282
)
83+
84+
news_to_delete = list(
85+
News.objects.filter(
86+
content_type__in=[content_type_vacancy, content_type_project]
87+
)
88+
)
89+
90+
for news in news_to_delete:
91+
if not news.content_object:
92+
news.delete()
93+
8394
return Response({"status": "success"}, status=201)

projects/models.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
from django.contrib.auth import get_user_model
44
from django.contrib.contenttypes.fields import GenericRelation
5-
from django.contrib.contenttypes.models import ContentType
65
from django.db import models
76
from django.db.models import UniqueConstraint
87

98
from core.models import Like, View
109
from files.models import UserFile
1110
from industries.models import Industry
12-
from news.models import News
11+
1312
from projects.constants import VERBOSE_STEPS
1413
from projects.managers import AchievementManager, CollaboratorManager, ProjectManager
1514
from users.models import CustomUser
@@ -135,16 +134,6 @@ def get_collaborators_user_list(self) -> list[User]:
135134
def __str__(self):
136135
return f"Project<{self.id}> - {self.name}"
137136

138-
def save(
139-
self, force_insert=False, force_update=False, using=None, update_fields=None
140-
):
141-
if not self.draft:
142-
content_type = ContentType.objects.filter(model="project").first()
143-
news_instance, created = News.objects.get_or_create(
144-
content_type=content_type, object_id=self.id
145-
)
146-
super().save(force_insert, force_update, using, update_fields)
147-
148137
class Meta:
149138
ordering = ["-hidden_score", "-datetime_created"]
150139
verbose_name = "Проект"

projects/signals.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ def create_project(sender, instance, created, **kwargs):
1212
"""
1313

1414
if not instance.draft:
15-
# if not a draft, check if project chat exists and if not create it
16-
if not ProjectChat.objects.filter(project=instance).exists():
17-
ProjectChat.objects.create(project=instance)
15+
ProjectChat.objects.get_or_create(project=instance)
1816

1917
if created:
2018
Collaborator.objects.create(

0 commit comments

Comments
 (0)