Skip to content

Commit

Permalink
Merge pull request #1006 from ita-social-projects/997-general-info-im…
Browse files Browse the repository at this point in the history
…provements-backend

997 general info improvements backend
  • Loading branch information
Chelakhovl authored Dec 9, 2024
2 parents 010281f + 72625d7 commit 1102419
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 4.2.3 on 2024-12-08 17:45

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("profiles", "0021_savedcompany_is_updated"),
]

operations = [
migrations.AlterField(
model_name="profile",
name="name",
field=models.CharField(default=None, max_length=45, null=True),
),
migrations.AlterField(
model_name="profile",
name="official_name",
field=models.CharField(default=None, max_length=255, null=True),
),
]
6 changes: 2 additions & 4 deletions BackEnd/profiles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Profile(models.Model):

id = models.AutoField(primary_key=True)

name = models.CharField(max_length=100, default=None, null=True)
name = models.CharField(max_length=45, default=None, null=True)
is_registered = models.BooleanField(default=None, null=True)
is_startup = models.BooleanField(default=None, null=True)
is_fop = models.BooleanField(default=False)
Expand All @@ -51,9 +51,7 @@ class Profile(models.Model):
person = models.OneToOneField(CustomUser, on_delete=models.PROTECT)
person_position = models.CharField(max_length=50, blank=True, default="")

official_name = models.CharField(
max_length=255, null=True, blank=True, default=None
)
official_name = models.CharField(max_length=255, null=True, default=None)

regions = models.ManyToManyField("Region", blank=True)
common_info = models.TextField(
Expand Down
8 changes: 7 additions & 1 deletion BackEnd/profiles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ def validate(self, data):
edrpou = data.get("edrpou", self.instance.edrpou)
rnokpp = data.get("rnokpp", self.instance.rnokpp)
is_fop = data.get("is_fop", self.instance.is_fop)
name = data.get("name", self.instance.name)
if rnokpp and not is_fop:
raise serializers.ValidationError(
{
Expand All @@ -331,11 +332,16 @@ def validate(self, data):
"is_fop": "For the EDRPOU field filled out, FOP must be set to False"
}
)
if name and len(name) > 45:
raise serializers.ValidationError(
{"name": "Ensure this field has no more than 45 characters."}
)

return data

# set optional unique fields to None if they are empty
def to_internal_value(self, data):
fields_to_check = ["official_name", "edrpou", "rnokpp"]
fields_to_check = ["edrpou", "rnokpp"]
for field in fields_to_check:
if data.get(field) == "":
data[field] = None
Expand Down
63 changes: 48 additions & 15 deletions BackEnd/profiles/tests/test_crud_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,11 @@ def test_partial_update_profile_official_name_empty_value(self):
data={"official_name": ""},
format="json",
)
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertIsNone(response.data.get("official_name"))
self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code)
self.assertEqual(
response.json(),
{"official_name": ["This field may not be blank."]},
)

def test_partial_update_profile_edrpou_empty_value(self):
self.client.force_authenticate(self.user)
Expand Down Expand Up @@ -563,11 +566,8 @@ def test_partial_update_profile_rnokpp_empty_value(self):
self.assertIsNone(response.data.get("rnokpp"))

# updating fields when another instance with empty fields already exists in db
def test_partial_update_profile_fields_with_empty_values(
self,
):
def test_partial_update_profile_fields_with_empty_values(self):
ProfileStartupFactory.create(
official_name=None,
edrpou=None,
)
self.client.force_authenticate(self.user)
Expand All @@ -576,21 +576,16 @@ def test_partial_update_profile_fields_with_empty_values(
path="/api/profiles/{profile_id}".format(
profile_id=self.profile.id
),
data={"official_name": "", "edrpou": ""},
data={"edrpou": ""},
format="json",
)
self.assertEqual(status.HTTP_200_OK, response.status_code)

self.profile.refresh_from_db()
self.assertIsNone(self.profile.edrpou)

response = self.client.get(path="/api/profiles/")
self.assertEqual(2, response.data["total_items"])
self.assertTrue(
all(
[
item.get("official_name") is None
for item in response.data["results"]
]
)
)
self.assertTrue(
all(
[
Expand Down Expand Up @@ -1154,3 +1149,41 @@ def test_create_profile_wrong_data(self):
},
)
self.assertEqual(400, response.status_code)

def test_partial_update_profile_name_exceeds_character_limit(self):
self.client.force_authenticate(self.user)

long_name = "a" * 46
response = self.client.patch(
path="/api/profiles/{profile_id}".format(
profile_id=self.profile.id
),
data={"name": long_name},
format="json",
)

self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code)

self.assertEqual(
response.json(),
{"name": ["Ensure this field has no more than 45 characters."]},
)

def test_partial_update_profile_name_within_limit(self):
self.client.force_authenticate(self.user)

valid_name = "Valid Company Name"
response = self.client.patch(
path="/api/profiles/{profile_id}".format(
profile_id=self.profile.id
),
data={"name": valid_name},
format="json",
)

self.assertEqual(status.HTTP_200_OK, response.status_code)

self.assertEqual(response.data.get("name"), valid_name)

self.profile.refresh_from_db()
self.assertEqual(self.profile.name, valid_name)

0 comments on commit 1102419

Please sign in to comment.