Repository: DjangoGirls/djangogirls

Issue:
DataError: value too long for type character varying(30)
Sentry Issue: [DJANGO-GIRLS-WEBSITE-60](https://django-girls.sentry.io/issues/4635355583/?referrer=github_integration)

```
StringDataRightTruncation: value too long for type character varying(30)

  File "django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)

DataError: value too long for type character varying(30)

(20 additional frame(s) were not displayed)
...
  File "organize/views.py", line 50, in done
    application = EventApplication.object.create(**data_dict)
  File "organize/models.py", line 75, in create
    return super().create(**data_dict)
  File "organize/models.py", line 146, in save
    super().save(*args, **kwargs)
```
DataError: value too long for type character varying(30)
Sentry Issue: [DJANGO-GIRLS-WEBSITE-60](https://django-girls.sentry.io/issues/4635355583/?referrer=github_integration)

```
StringDataRightTruncation: value too long for type character varying(30)

  File "django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)

DataError: value too long for type character varying(30)

(20 additional frame(s) were not displayed)
...
  File "organize/views.py", line 50, in done
    application = EventApplication.object.create(**data_dict)
  File "organize/models.py", line 75, in create
    return super().create(**data_dict)
  File "organize/models.py", line 146, in save
    super().save(*args, **kwargs)
```


Discussion:
Hi,
I noticed this issue reported by the bot and would like to work on resolving it. If there are any specific guidelines or additional information needed before I begin, please let me know.

Hello @dostogircse171. Thank you for your interest in solving this issue.

The screenshots below are from Sentry, to show the stack trace better.

<img width="827" alt="Screenshot 2023-12-31 at 04 16 27" src="https://github.com/DjangoGirls/djangogirls/assets/13593285/fd0fab2f-c840-41da-8605-c1485434cdc4">
<img width="829" alt="Screenshot 2023-12-31 at 04 17 15" src="https://github.com/DjangoGirls/djangogirls/assets/13593285/b004d641-e269-4622-94e8-67d0654b90f2">


latlng is defined with a `max_length=30` here https://github.com/DjangoGirls/djangogirls/blob/main/organize/models.py#L84C1-L88 but when latlng is being generated here by the `get_coordinates_for_city()` function here https://github.com/DjangoGirls/djangogirls/blob/main/organize/models.py#L144, the result is sometimes longer than 30 characters hence the data error while trying to save the event.
@amakarudze Thank you very much for providing more clear instructions. I will make these changes and do a PR when done so you can review them. 

One question for you. Is it ok to increase the chars limit from model class or I should handle it from save() method by limiting chars to 30 in **self.latlng** instance of **EventApplication** class
Hi,
I noticed this issue reported by the bot and would like to work on resolving it. If there are any specific guidelines or additional information needed before I begin, please let me know.

Hello @dostogircse171. Thank you for your interest in solving this issue.

The screenshots below are from Sentry, to show the stack trace better.

<img width="827" alt="Screenshot 2023-12-31 at 04 16 27" src="https://github.com/DjangoGirls/djangogirls/assets/13593285/fd0fab2f-c840-41da-8605-c1485434cdc4">
<img width="829" alt="Screenshot 2023-12-31 at 04 17 15" src="https://github.com/DjangoGirls/djangogirls/assets/13593285/b004d641-e269-4622-94e8-67d0654b90f2">


latlng is defined with a `max_length=30` here https://github.com/DjangoGirls/djangogirls/blob/main/organize/models.py#L84C1-L88 but when latlng is being generated here by the `get_coordinates_for_city()` function here https://github.com/DjangoGirls/djangogirls/blob/main/organize/models.py#L144, the result is sometimes longer than 30 characters hence the data error while trying to save the event.
@amakarudze Thank you very much for providing more clear instructions. I will make these changes and do a PR when done so you can review them. 

One question for you. Is it ok to increase the chars limit from model class or I should handle it from save() method by limiting chars to 30 in **self.latlng** instance of **EventApplication** class

Resolving patch:
diff --git a/core/utils.py b/core/utils.py
--- a/core/utils.py
+++ b/core/utils.py
@@ -15,7 +15,9 @@ def get_coordinates_for_city(city, country):
 
     try:
         data = req.json()[0]
-        return f'{data["lat"]}, {data["lon"]}'
+        formatted_lat = "{:.7f}".format(float(data["lat"]))
+        formatted_lon = "{:.7f}".format(float(data["lon"]))
+        return f"{formatted_lat}, {formatted_lon}"
     except (IndexError, KeyError):
         return None
 


Test patch:
diff --git a/tests/core/test_utils.py b/tests/core/test_utils.py
--- a/tests/core/test_utils.py
+++ b/tests/core/test_utils.py
@@ -24,7 +24,9 @@ def test_get_coordinates_for_city(mock_get):
             "q": "London, UK",
         },
     )
-    assert result == "1.23, 4.56"
+    expected_lat = "{:.7f}".format(float("1.23"))
+    expected_lon = "{:.7f}".format(float("4.56"))
+    assert result == f"{expected_lat}, {expected_lon}"
 
 
 @mock.patch("requests.get")