Skip to content

Commit

Permalink
Added a modal window for server-side error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
OlesiaBarannik committed Oct 27, 2024
2 parents b276dbb + 77897c7 commit 71290cd
Show file tree
Hide file tree
Showing 11 changed files with 354 additions and 54 deletions.
3 changes: 3 additions & 0 deletions BackEnd/administration/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
class AdminUserFactory(factory.django.DjangoModelFactory):
class Meta:
model = CustomUser
django_get_or_create = ("email",)

id = factory.Sequence(lambda n: n + 1)
email = factory.Sequence(lambda n: f"test{n + 1}@test.com")
name = factory.Sequence(lambda n: f"Test person {n + 1}")
surname = factory.Sequence(lambda n: f"Test person {n + 1} surname")
password = ""

is_active = True
is_staff = True
is_superuser = False


class AdminProfileFactory(factory.django.DjangoModelFactory):
Expand Down
46 changes: 46 additions & 0 deletions BackEnd/administration/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from django_filters import filters
from django_filters.rest_framework import FilterSet


class UsersFilter(FilterSet):
"""
Filters
/?id= , /?surname=, /?email= , /?is_active= , /?is_staff=,
/?is_superuser=, /?is_deleted=True or False, /?company_name=, /?registration_date=,
Ordering sample
/?ordering=id asc or /?ordering=-id desc
"""

id = filters.CharFilter(lookup_expr="icontains")
surname = filters.CharFilter(lookup_expr="icontains")
email = filters.CharFilter(lookup_expr="icontains")
is_active = filters.CharFilter(lookup_expr="icontains")
is_staff = filters.CharFilter(lookup_expr="icontains")
is_superuser = filters.CharFilter(lookup_expr="icontains")
is_deleted = filters.BooleanFilter(method="filter_is_deleted")
company_name = filters.CharFilter(
field_name="profile__name", lookup_expr="icontains"
)
registration_date = filters.CharFilter(
field_name="profile__created_at", lookup_expr="icontains"
)

def filter_is_deleted(self, queryset, name, value):
if value:
queryset = queryset.filter(email__startswith="is_deleted_")
return queryset

ordering = filters.OrderingFilter(
fields=(
("id", "id"),
("name", "name"),
("surname", "surname"),
("email", "email"),
("is_active", "is_active"),
("is_staff", "is_staff"),
("is_superuser", "is_superuser"),
("is_deleted", "is_deleted"),
("profile__name", "company_name"),
("profile__created_at", "registration_date"),
)
)
40 changes: 28 additions & 12 deletions BackEnd/administration/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.contrib.auth import get_user_model
from rest_framework import serializers
from utils.administration.feedback_category import FeedbackCategory
from authentication.models import CustomUser
from profiles.models import (
Profile,
Expand Down Expand Up @@ -51,15 +52,37 @@ def create(self, validated_data):


class AdminUserListSerializer(serializers.ModelSerializer):
company_name = serializers.SerializerMethodField()
registration_date = serializers.SerializerMethodField()
status = serializers.SerializerMethodField()

class Meta:
model = CustomUser
fields = (
"id",
"email",
"name",
"surname",
"status",
"company_name",
"registration_date",
)

def get_company_name(self, obj) -> str:
return obj.profile.name if hasattr(obj, "profile") else None

def get_registration_date(self, obj) -> str:
return obj.profile.created_at if hasattr(obj, "profile") else None

def get_status(self, obj) -> dict:
data = {
"is_active": obj.is_active,
"is_staff": obj.is_staff,
"is_superuser": obj.is_superuser,
"is_deleted": obj.email.startswith("is_deleted_"),
}
return data


class AdminUserDetailSerializer(serializers.ModelSerializer):
company_name = serializers.SerializerMethodField()
Expand Down Expand Up @@ -175,25 +198,18 @@ class Meta:
class FeedbackSerializer(serializers.Serializer):
email = serializers.EmailField(
required=True,
error_messages={
"required": "Будь ласка, вкажіть правильну адресу електронної скриньки."
},
error_messages={"required": "Please provide a valid email address."},
)
message = serializers.CharField(
min_length=10,
required=True,
error_messages={
"required": "Повідомлення не може бути порожнім.",
"min_length": "Повідомлення не може бути коротшим за 10 символів.",
"required": "Message cannot be empty.",
"min_length": "Message must be at least 10 characters long.",
},
)
category = serializers.ChoiceField(
choices=[
("Технічне питання", "Технічне питання"),
("Рекомендації", "Рекомендації"),
("Питання", "Питання"),
("Інше", "Інше"),
],
choices=FeedbackCategory.choices(),
required=True,
error_messages={"required": "Будь ласка, оберіть тип повідомлення."},
error_messages={"required": "Please select a category."},
)
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<div class="email-container">
<img src="{{protocol}}://178.212.110.52/craftMerge-logo.png" alt="CRAFTMERGE" />

<h2>Нове повідомлення від {{ user_email }}</h2>
<p>Нове повідомлення від {{ user_email }}<p>

<p><b>Категорія:</b> {{ category }}</p>
<p><b>Повідомлення:</b></p>
Expand Down
201 changes: 194 additions & 7 deletions BackEnd/administration/tests/test_admin_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,165 @@ def test_get_user_id_not_staff(self):
self.assertEqual(status.HTTP_403_FORBIDDEN, response.status_code)


class TestAdminUsersOrderingFilterAPITests(APITestCase):
def setUp(self):
self.users = AdminUserFactory.create_batch(2)
AdminUserFactory.reset_sequence(1)
self.user = self.users[0]

def test_get_users_ordering_desc(self):
self.client.force_authenticate(self.user)
response = self.client.get(path="/api/admin/users/?ordering=-id")
data = [
{
"id": 3,
"email": "test3@test.com",
"name": "Test person 3",
"surname": "Test person 3 surname",
"status": {
"is_active": True,
"is_staff": True,
"is_superuser": False,
"is_deleted": False,
},
"company_name": None,
"registration_date": None,
},
{
"id": 2,
"email": "test2@test.com",
"name": "Test person 2",
"surname": "Test person 2 surname",
"status": {
"is_active": True,
"is_staff": True,
"is_superuser": False,
"is_deleted": False,
},
"company_name": None,
"registration_date": None,
},
]
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(data, response.json())

def test_get_users_ordering_asc(self):
self.client.force_authenticate(self.user)
response = self.client.get(path="/api/admin/users/?ordering=id")
data = [
{
"id": 2,
"email": "test2@test.com",
"name": "Test person 2",
"surname": "Test person 2 surname",
"status": {
"is_active": True,
"is_staff": True,
"is_superuser": False,
"is_deleted": False,
},
"company_name": None,
"registration_date": None,
},
{
"id": 3,
"email": "test3@test.com",
"name": "Test person 3",
"surname": "Test person 3 surname",
"status": {
"is_active": True,
"is_staff": True,
"is_superuser": False,
"is_deleted": False,
},
"company_name": None,
"registration_date": None,
},
]
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(data, response.json())

def test_get_users_filter_id_surname(self):
self.client.force_authenticate(self.user)
response = self.client.get(path="/api/admin/users/?id=5&surname=5")
data = [
{
"id": 5,
"email": "test5@test.com",
"name": "Test person 5",
"surname": "Test person 5 surname",
"status": {
"is_active": True,
"is_staff": True,
"is_superuser": False,
"is_deleted": False,
},
"company_name": None,
"registration_date": None,
},
]
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(data, response.json())


class TestAdminUsersStatusAPITests(APITestCase):
def setUp(self):
self.users = AdminUserFactory.create_batch(2)
AdminUserFactory.reset_sequence(1)
self.user = self.users[0]

def test_get_users_filter_status_active_staff(self):
self.client.force_authenticate(self.user)
response = self.client.get(
path="/api/admin/users/?is_active=True&is_staff=True"
)
data = [
{
"id": 2,
"email": "test2@test.com",
"name": "Test person 2",
"surname": "Test person 2 surname",
"status": {
"is_active": True,
"is_staff": True,
"is_superuser": False,
"is_deleted": False,
},
"company_name": None,
"registration_date": None,
},
{
"id": 3,
"email": "test3@test.com",
"name": "Test person 3",
"surname": "Test person 3 surname",
"status": {
"is_active": True,
"is_staff": True,
"is_superuser": False,
"is_deleted": False,
},
"company_name": None,
"registration_date": None,
},
]
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(data, response.json())

def test_get_users_filter_status_superuser_staff_active(self):
self.client.force_authenticate(self.user)
response = self.client.get(
path="/api/admin/users/?is_superuser=True&is_deleted=True&is_active=False"
)
data = []
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(data, response.json())


class TestAdminUsersAPITests(APITestCase):
def setUp(self):
self.user = AdminUserFactory()
AdminUserFactory.reset_sequence(1)

def test_get_users_not_authorized(self):
response = self.client.get(path="/api/admin/users/?page=1&page_size=1")
Expand All @@ -40,10 +196,40 @@ def test_get_users_authenticated(self):
response = self.client.get(path="/api/admin/users/")
data = [
{
"id": 39,
"email": "test39@test.com",
"name": "Test person 39",
"surname": "Test person 39 surname",
"id": 2,
"email": "test2@test.com",
"name": "Test person 2",
"surname": "Test person 2 surname",
"status": {
"is_active": True,
"is_staff": True,
"is_superuser": False,
"is_deleted": False,
},
"company_name": None,
"registration_date": None,
}
]
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(data, response.json())

def test_get_users_filter(self):
self.client.force_authenticate(self.user)
response = self.client.get(path="/api/admin/users/?ordering=id")
data = [
{
"id": 2,
"email": "test2@test.com",
"name": "Test person 2",
"surname": "Test person 2 surname",
"status": {
"is_active": True,
"is_staff": True,
"is_superuser": False,
"is_deleted": False,
},
"company_name": None,
"registration_date": None,
}
]
self.assertEqual(response.status_code, status.HTTP_200_OK)
Expand All @@ -52,11 +238,12 @@ def test_get_users_authenticated(self):
def test_get_user_id_authenticated(self):
self.client.force_authenticate(self.user)
response = self.client.get(path=f"/api/admin/users/{self.user.id}/")
AdminUserFactory.reset_sequence(1)

data = {
"name": "Test person 37",
"surname": "Test person 37 surname",
"email": "test37@test.com",
"name": f"Test person {self.user.id}",
"surname": f"Test person {self.user.id} surname",
"email": f"test{self.user.id}@test.com",
"is_active": True,
"is_staff": True,
"is_superuser": False,
Expand Down
Loading

0 comments on commit 71290cd

Please sign in to comment.