Optimize
This commit is contained in:
parent
e772663f44
commit
797d3a3973
|
|
@ -1,4 +1,5 @@
|
||||||
"""Notification app models."""
|
"""Notification app models."""
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils.timezone import now
|
from django.utils.timezone import now
|
||||||
|
|
@ -116,11 +117,8 @@ class Subscriber(ProjectBaseMixin):
|
||||||
|
|
||||||
new_ids = set(existing_answer.answer.id for existing_answer in subscribes)
|
new_ids = set(existing_answer.answer.id for existing_answer in subscribes)
|
||||||
old_subscribes_types = [sub for sub in old_subscribes if sub.id not in new_ids]
|
old_subscribes_types = [sub for sub in old_subscribes if sub.id not in new_ids]
|
||||||
old_subscribes = Subscribe.objects.filter(subscriber=self, subscription_types__in=[old_subscribes_types])
|
old_subscribes = Subscribe.objects.filter(subscriber=self, subscription_types__in=old_subscribes_types)
|
||||||
|
old_subscribes.update(unsubscribe_date=now())
|
||||||
for sub in old_subscribes:
|
|
||||||
sub.unsubscribe_date = now()
|
|
||||||
sub.save()
|
|
||||||
|
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ from http.cookies import SimpleCookie
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from rest_framework.test import APITestCase
|
from rest_framework.test import APITestCase
|
||||||
|
|
||||||
from account.models import User, Role, UserRole
|
from account.models import Role, User, UserRole
|
||||||
from location.models import Country
|
from location.models import Country
|
||||||
from main.models import SiteSettings
|
from main.models import SiteSettings
|
||||||
from news.models import News, NewsType
|
from news.models import News, NewsType
|
||||||
|
|
@ -22,9 +22,9 @@ class BaseTestCase(APITestCase):
|
||||||
# get tokens
|
# get tokens
|
||||||
tokens = User.create_jwt_tokens(self.user)
|
tokens = User.create_jwt_tokens(self.user)
|
||||||
self.client.cookies = SimpleCookie({
|
self.client.cookies = SimpleCookie({
|
||||||
'access_token': tokens.get('access_token'),
|
'access_token': tokens.get('access_token'),
|
||||||
'refresh_token': tokens.get('refresh_token')
|
'refresh_token': tokens.get('refresh_token')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
class NotificationAnonSubscribeTestCase(APITestCase):
|
class NotificationAnonSubscribeTestCase(APITestCase):
|
||||||
|
|
@ -113,7 +113,7 @@ class NotificationUnsubscribeTestCase(APITestCase):
|
||||||
|
|
||||||
class NotificationManySubscribeTestCase(APITestCase):
|
class NotificationManySubscribeTestCase(APITestCase):
|
||||||
|
|
||||||
def test_unsubscribe(self):
|
def test_subscribe(self):
|
||||||
self.username = 'sedragurda'
|
self.username = 'sedragurda'
|
||||||
self.password = 'sedragurdaredips19'
|
self.password = 'sedragurdaredips19'
|
||||||
self.email = 'sedragurda@desoz.com'
|
self.email = 'sedragurda@desoz.com'
|
||||||
|
|
@ -123,8 +123,10 @@ class NotificationManySubscribeTestCase(APITestCase):
|
||||||
# get tokens
|
# get tokens
|
||||||
tokens = User.create_jwt_tokens(self.user)
|
tokens = User.create_jwt_tokens(self.user)
|
||||||
self.client.cookies = SimpleCookie(
|
self.client.cookies = SimpleCookie(
|
||||||
{'access_token': tokens.get('access_token'),
|
{
|
||||||
'refresh_token': tokens.get('refresh_token')})
|
'access_token': tokens.get('access_token'),
|
||||||
|
'refresh_token': tokens.get('refresh_token')
|
||||||
|
})
|
||||||
self.test_news_type = NewsType.objects.create(name="Test news type")
|
self.test_news_type = NewsType.objects.create(name="Test news type")
|
||||||
|
|
||||||
self.lang, created = Language.objects.get_or_create(
|
self.lang, created = Language.objects.get_or_create(
|
||||||
|
|
@ -178,4 +180,16 @@ class NotificationManySubscribeTestCase(APITestCase):
|
||||||
|
|
||||||
response = self.client.post("/api/web/notifications/subscribe/", data=test_data, format="json")
|
response = self.client.post("/api/web/notifications/subscribe/", data=test_data, format="json")
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
self.assertEqual(response.json()["email"], test_data["email"])
|
self.assertEqual(response.json()["email"], test_data["email"])
|
||||||
|
|
||||||
|
def test_unsubscribe(self):
|
||||||
|
test_data = {
|
||||||
|
"email": self.email
|
||||||
|
}
|
||||||
|
|
||||||
|
test_subscriber = Subscriber.objects.create(user=self.user, email=self.email)
|
||||||
|
|
||||||
|
response = self.client.patch(f"/api/web/notifications/unsubscribe/{test_subscriber.update_code}/",
|
||||||
|
data=test_data, format="json")
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user