changes
This commit is contained in:
@@ -8,6 +8,7 @@ import logging
|
||||
import requests
|
||||
from odoo import models, fields, api, _
|
||||
from odoo.exceptions import UserError
|
||||
from odoo.http import request as http_request
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -126,16 +127,18 @@ class FusionClockLocation(models.Model):
|
||||
return False
|
||||
|
||||
def action_detect_ip(self):
|
||||
"""Detect the current public IP and append it to the whitelist."""
|
||||
"""Detect the IP the Odoo server sees from your browser and add it."""
|
||||
self.ensure_one()
|
||||
try:
|
||||
resp = requests.get('https://ipapi.co/json/', timeout=10)
|
||||
data = resp.json()
|
||||
ip = data.get('ip', '')
|
||||
ip = ''
|
||||
if http_request:
|
||||
ip = http_request.httprequest.environ.get(
|
||||
'HTTP_X_FORWARDED_FOR', ''
|
||||
).split(',')[0].strip()
|
||||
if not ip:
|
||||
raise UserError(_("Could not detect public IP."))
|
||||
except requests.exceptions.RequestException as e:
|
||||
raise UserError(_("Network error detecting IP: %s") % str(e))
|
||||
ip = http_request.httprequest.remote_addr or ''
|
||||
|
||||
if not ip:
|
||||
raise UserError(_("Could not detect your IP from the request."))
|
||||
|
||||
existing = (self.ip_whitelist or '').strip()
|
||||
existing_lines = [l.strip() for l in existing.split('\n') if l.strip()] if existing else []
|
||||
@@ -145,7 +148,7 @@ class FusionClockLocation(models.Model):
|
||||
'tag': 'display_notification',
|
||||
'params': {
|
||||
'title': _('Already Whitelisted'),
|
||||
'message': _('%s is already in the whitelist.') % ip,
|
||||
'message': _('IP %s is already in the whitelist.') % ip,
|
||||
'type': 'warning',
|
||||
'sticky': False,
|
||||
},
|
||||
@@ -153,20 +156,25 @@ class FusionClockLocation(models.Model):
|
||||
|
||||
existing_lines.append(ip)
|
||||
self.ip_whitelist = '\n'.join(existing_lines)
|
||||
city = data.get('city', '')
|
||||
org = data.get('org', '')
|
||||
detail = f"{ip}"
|
||||
if city:
|
||||
detail += f" ({city}"
|
||||
if org:
|
||||
detail += f" - {org}"
|
||||
detail += ")"
|
||||
|
||||
extra = ''
|
||||
try:
|
||||
resp = requests.get(f'https://ipapi.co/{ip}/json/', timeout=5)
|
||||
if resp.ok:
|
||||
data = resp.json()
|
||||
city = data.get('city', '')
|
||||
org = data.get('org', '')
|
||||
if city or org:
|
||||
extra = f" ({', '.join(filter(None, [city, org]))})"
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return {
|
||||
'type': 'ir.actions.client',
|
||||
'tag': 'display_notification',
|
||||
'params': {
|
||||
'title': _('IP Detected & Added'),
|
||||
'message': _('Added %s to the whitelist.') % detail,
|
||||
'message': _('Added %s%s to the whitelist. This is the IP the server sees from your browser.') % (ip, extra),
|
||||
'type': 'success',
|
||||
'sticky': False,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user