Skip to content

Commit

Permalink
fix some bugs for carousel images
Browse files Browse the repository at this point in the history
  • Loading branch information
Heibert committed Sep 20, 2024
1 parent b06f90b commit 445d76b
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 41 deletions.
17 changes: 17 additions & 0 deletions INSIGHTSAPI/carousel_image/migrations/0004_remove_banner_active.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.0.7 on 2024-09-20 09:48

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('carousel_image', '0003_alter_banner_order'),
]

operations = [
migrations.RemoveField(
model_name='banner',
name='active',
),
]
4 changes: 4 additions & 0 deletions INSIGHTSAPI/carousel_image/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ def save(self, *args, **kwargs):
order=models.F("order") + 1
)
super().save(*args, **kwargs)

def delete(self, *args, **kwargs):
Banner.objects.filter(order__gt=self.order).update(order=models.F("order") - 1)
return super().delete(*args, **kwargs)
78 changes: 57 additions & 21 deletions INSIGHTSAPI/carousel_image/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from services.tests import BaseTestCase
from django.urls import reverse
from django.core.files.uploadedfile import SimpleUploadedFile
from django.contrib.auth.models import Permission
from .models import Banner


Expand All @@ -14,6 +15,7 @@ def setUp(self):
image="carousel_images/banners/test.jpg",
order=1,
)
self.user.user_permissions.add(Permission.objects.get(codename="add_banner"))
with open("static/test/Logo_cyc.png", "rb") as image_data:
self.image = SimpleUploadedFile(
"test3.jpg", image_data.read(), content_type="image/jpg"
Expand All @@ -31,19 +33,6 @@ def test_get_banner(self):
self.assertEqual(len(response.data), 2)
self.assertEqual(response.data[0]["title"], "Test Banner")
self.assertEqual(response.data[0]["link"], "https://www.google.com")
self.assertEqual(response.data[0]["active"], True)

def test_get_only_active_banner(self):
Banner.objects.create(
title="Test Banner 2",
link="https://www.google.com",
active=False,
image="carousel_images/banners/test2.jpg",
order=2,
)
response = self.client.get(reverse("banners-list"))
self.assertEqual(response.status_code, 200, response.data)
self.assertEqual(len(response.data), 1)

def test_create_banner(self):
data = {
Expand All @@ -52,20 +41,33 @@ def test_create_banner(self):
"image": self.image,
"order": 2,
}
print("Create")
response = self.client.post(reverse("banners-list"), data)
print("aqui",response.data.get("active"))
self.assertEqual(response.status_code, 201, response.data)
get_banners = self.client.get(reverse("banners-list"))
self.assertEqual(len(get_banners.data), 2)

def test_delete_banner(self):
response = self.client.delete(reverse("banners-detail", args=[self.banner.id]))
self.assertEqual(response.status_code, 204, response.data)
self.assertEqual(Banner.objects.count(), 1)
self.assertEqual(Banner.objects.filter(active=True).count(), 0)

def test_update_banner_order(self):
self.user.user_permissions.add(Permission.objects.get(codename="change_banner"))
banner2 = Banner.objects.create(
title="Test Banner 2",
link="https://www.google.com",
image="carousel_images/banners/test2.jpg",
order=2,
)
response = self.client.post(
reverse("banners-list"),
{
"title": "Test Banner 3",
"link": "https://www.google.com",
"image": self.image,
"order": 2,
},
)
self.assertEqual(response.status_code, 201, response.data)
self.assertEqual(Banner.objects.count(), 3)
self.assertEqual(Banner.objects.get(id=banner2.id).order, 3)

def test_banner_order_penultimate(self):
banner2 = Banner.objects.create(
title="Test Banner 2",
link="https://www.google.com",
Expand All @@ -84,3 +86,37 @@ def test_update_banner_order(self):
self.assertEqual(response.status_code, 201, response.data)
self.assertEqual(Banner.objects.count(), 3)
self.assertEqual(Banner.objects.get(id=banner2.id).order, 3)
self.assertEqual(Banner.objects.get(id=response.data["id"]).order, 2)

def test_banner_order_last(self):
banner2 = Banner.objects.create(
title="Test Banner 2",
link="https://www.google.com",
image="carousel_images/banners/test2.jpg",
order=2,
)
response = self.client.post(
reverse("banners-list"),
{
"title": "Test Banner 3",
"link": "https://www.google.com",
"image": self.image,
"order": 3,
},
)
self.assertEqual(response.status_code, 201, response.data)
self.assertEqual(Banner.objects.count(), 3)
self.assertEqual(Banner.objects.get(id=banner2.id).order, 2)

def test_delete_banner(self):
create_banner = Banner.objects.create(
title="Test Banner 2",
link="https://www.google.com",
image="carousel_images/banners/test2.jpg",
order=2,
)
self.user.user_permissions.add(Permission.objects.get(codename="delete_banner"))
response = self.client.delete(reverse("banners-detail", args=[self.banner.id]))
self.assertEqual(response.status_code, 204)
self.assertEqual(Banner.objects.count(), 1)
self.assertEqual(Banner.objects.get(id=create_banner.id).order, 1)
22 changes: 3 additions & 19 deletions INSIGHTSAPI/carousel_image/views.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
from rest_framework import viewsets
from rest_framework import status
from rest_framework.response import Response
from rest_framework.permissions import DjangoModelPermissions
from .models import Banner
from .serializer import BannerSerializer


class BannerViewSet(viewsets.ModelViewSet):
queryset = Banner.objects.filter(active=True)
queryset = Banner.objects.filter().order_by("order")
serializer_class = BannerSerializer

def create(self, request, *args, **kwargs):
print("Create2")
print("request:", request.data)
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
print("serializer:", serializer.validated_data)
response = super().create(request, *args, **kwargs)
print(response.data)
return response

def destroy(self, request, *args, **kwargs):
instance = self.get_object()
instance.active = False
instance.save()
return Response(status=status.HTTP_204_NO_CONTENT)
permission_classes = [DjangoModelPermissions]
6 changes: 5 additions & 1 deletion INSIGHTSAPI/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ def login_staffnet():
else:
url = "https://staffnet-api.cyc-bpo.com/login"
response = requests.post(url, json=data)
if response.status_code != 200 or "StaffNet" not in response.cookies:
if (
response.status_code != 200
or "StaffNet" not in response.cookies
and os.environ.get("StaffNetToken")
):
logger.error("Error logging in StaffNet: {}".format(response.text))
mail_admins(
"Error logging in StaffNet",
Expand Down

0 comments on commit 445d76b

Please sign in to comment.