From 797d3a3973b8c863014bbe631868da1be179d282 Mon Sep 17 00:00:00 2001 From: dormantman Date: Mon, 30 Dec 2019 18:13:06 +0300 Subject: [PATCH] Optimize --- apps/notification/models.py | 8 +++----- apps/notification/tests.py | 30 ++++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/apps/notification/models.py b/apps/notification/models.py index 4bdabcae..fd386c2d 100644 --- a/apps/notification/models.py +++ b/apps/notification/models.py @@ -1,4 +1,5 @@ """Notification app models.""" + from django.conf import settings from django.db import models 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) 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]) - - for sub in old_subscribes: - sub.unsubscribe_date = now() - sub.save() + old_subscribes = Subscribe.objects.filter(subscriber=self, subscription_types__in=old_subscribes_types) + old_subscribes.update(unsubscribe_date=now()) self.save() diff --git a/apps/notification/tests.py b/apps/notification/tests.py index e65e049d..410365ff 100644 --- a/apps/notification/tests.py +++ b/apps/notification/tests.py @@ -4,7 +4,7 @@ from http.cookies import SimpleCookie from rest_framework import status 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 main.models import SiteSettings from news.models import News, NewsType @@ -22,9 +22,9 @@ class BaseTestCase(APITestCase): # get tokens tokens = User.create_jwt_tokens(self.user) 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') + }) class NotificationAnonSubscribeTestCase(APITestCase): @@ -113,7 +113,7 @@ class NotificationUnsubscribeTestCase(APITestCase): class NotificationManySubscribeTestCase(APITestCase): - def test_unsubscribe(self): + def test_subscribe(self): self.username = 'sedragurda' self.password = 'sedragurdaredips19' self.email = 'sedragurda@desoz.com' @@ -123,8 +123,10 @@ class NotificationManySubscribeTestCase(APITestCase): # get tokens tokens = User.create_jwt_tokens(self.user) 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.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") self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(response.json()["email"], test_data["email"]) \ No newline at end of file + 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)