Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion child_compassion/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
# pylint: disable=C8101
{
"name": "Compassion Children",
"version": "14.0.1.4.0",
"version": "14.0.1.5.0",
"category": "Compassion",
"author": "Compassion CH",
"license": "AGPL-3",
Expand Down
23 changes: 23 additions & 0 deletions child_compassion/migrations/14.0.1.5.0/pre-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from openupgradelib import openupgrade


def migrate(cr, version):
if not openupgrade.column_exists(
cr, "compassion_project", "gps_latitude_obfuscated"
):
openupgrade.logged_query(
cr,
"""
ALTER TABLE compassion_project
ADD COLUMN gps_latitude_obfuscated float,
ADD COLUMN gps_longitude_obfuscated float;
""",
)
openupgrade.logged_query(
cr,
"""
UPDATE compassion_project
SET gps_longitude_obfuscated = TRUNC(CAST(gps_longitude AS numeric), 0),
gps_latitude_obfuscated = TRUNC(CAST(gps_latitude AS numeric), 0);
""",
)
46 changes: 46 additions & 0 deletions child_compassion/models/project_compassion.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import logging
import re
from datetime import datetime, timedelta
from random import random

import requests

Expand Down Expand Up @@ -85,6 +86,12 @@ class CompassionProject(models.Model):
zip_code = fields.Char(readonly=True)
gps_latitude = fields.Float(readonly=True)
gps_longitude = fields.Float(readonly=True)
gps_latitude_obfuscated = fields.Float(
compute="_compute_gps_obfuscated", store=True
)
gps_longitude_obfuscated = fields.Float(
compute="_compute_gps_obfuscated", store=True
)
google_link = fields.Char(readonly=True, compute="_compute_google_link")
timezone = fields.Char(readonly=True, compute="_compute_timezone", store=True)
cluster = fields.Char(readonly=True)
Expand Down Expand Up @@ -510,6 +517,45 @@ def _get_materials(self):
("Plastic", _("Plastic")),
]

@api.depends("gps_latitude", "gps_longitude", "closest_city")
def _compute_gps_obfuscated(self):
"""
This method calculates and stores the obfuscated coordinates
(latitude and longitude).
"""
api_key = (
self.env["ir.config_parameter"].sudo().get_param("google_maps_api_key")
)
base_url = "https://maps.googleapis.com/maps/api/geocode/json"
for project in self:
try:
parts = [
project.closest_city,
project.state_province,
project.country_id.name,
]
address_string = ", ".join(filter(None, parts))
params = {"address": address_string, "key": api_key}
response = requests.get(base_url, params=params, timeout=3)
data = response.json()
if data["status"] == "OK":
location = data["results"][0]["geometry"]["location"]
project.gps_latitude_obfuscated = location["lat"]
project.gps_longitude_obfuscated = location["lng"]
except Exception:
# Fallback to randomized gps coords
logging.warning("Request failed", exc_info=True)
project.gps_latitude_obfuscated = (
(int(project.gps_latitude) + random())
if project.gps_latitude
else 0
)
project.gps_longitude_obfuscated = (
(int(project.gps_longitude) + random())
if project.gps_longitude
else 0
)

@api.depends("gps_longitude", "gps_latitude")
def _compute_timezone(self):
tf = TimezoneFinder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@
<field name="intervention_id" />
<field name="field_office_id" />
<field name="fcp_ids" widget="many2many_tags" />
<field name="additional_marketing_information" widget="html" />
<field
name="additional_marketing_information"
widget="html"
/>
<field name="category_id" />
<field
name="subcategory_ids"
Expand Down