From 02bdb845c9722030f23d386ca6548e79f036a35a Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Mon, 13 Feb 2023 10:10:15 +0700 Subject: add model ip lookup (grouping ip) --- indoteknik_custom/models/__init__.py | 1 + indoteknik_custom/models/ip_lookup.py | 59 +++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 indoteknik_custom/models/ip_lookup.py (limited to 'indoteknik_custom/models') diff --git a/indoteknik_custom/models/__init__.py b/indoteknik_custom/models/__init__.py index 121bd0f4..55eca2e7 100755 --- a/indoteknik_custom/models/__init__.py +++ b/indoteknik_custom/models/__init__.py @@ -47,3 +47,4 @@ from . import x_product_tags from . import website_ads from . import leads_monitoring from . import midtrans +from . import ip_lookup diff --git a/indoteknik_custom/models/ip_lookup.py b/indoteknik_custom/models/ip_lookup.py new file mode 100644 index 00000000..0fbb03ea --- /dev/null +++ b/indoteknik_custom/models/ip_lookup.py @@ -0,0 +1,59 @@ +from odoo import models, api, fields +import logging +import requests +import json + +_logger = logging.getLogger(__name__) + + +class IpLookup(models.Model): + _name = 'ip.lookup' + + date_from = fields.Date(string='Date From', required=True) + date_to = fields.Date(string='Date To', required=True) + lookup_line = fields.One2many('ip.lookup.line', 'ip_lookup_id', string='Lookup Lines', auto_join=True) + + def generate_ip_lookup(self): + query = [ + ('create_date', '>=', self.date_from), + ('create_date', '<=', self.date_to), + ('ip_address', '!=', False) + ] + logss = self.env['user.activity.log'].search(query) + ips = [] + for log in logss: + ips.append(log.ip_address) + logs = list(dict.fromkeys(ips)) + + count = 0 + for log in logs: + self.env['ip.lookup.line'].create([{ + 'ip_lookup_id': self.id, + 'ip_address': log + }]) + count += 1 + _logger.info('IP Lookup Generated %s' % count) + + def _load_ip_address_lookup(self): + domain = [ + ('lookup', '=', False), + ] + logs = self.env['ip.lookup.line'].search(domain, limit=45, order='create_date asc') + for log in logs: + try: + ipinfo = requests.get('http://ip-api.com/json/%s' % log.ip_address).json() + del ipinfo['status'] + log.lookup = json.dumps(ipinfo, indent=4, sort_keys=True) + except: + log.lookup = '' + + +class IpLookupLine(models.Model): + _name = 'ip.lookup.line' + + ip_lookup_id = fields.Many2one('ip.lookup', string='Lookup Ref', required=True, ondelete='cascade', index=True, + copy=False) + ip_address = fields.Char(string='IP Address') + lookup = fields.Char(string='Lookup') + country = fields.Char(string='Country') + -- cgit v1.2.3