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/__manifest__.py | 1 + indoteknik_custom/models/__init__.py | 1 + indoteknik_custom/models/ip_lookup.py | 59 ++++++++++++++++++++++ indoteknik_custom/security/ir.model.access.csv | 4 +- indoteknik_custom/views/ip_lookup.xml | 69 ++++++++++++++++++++++++++ 5 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 indoteknik_custom/models/ip_lookup.py create mode 100644 indoteknik_custom/views/ip_lookup.xml diff --git a/indoteknik_custom/__manifest__.py b/indoteknik_custom/__manifest__.py index 1643d9d1..da82d2bd 100755 --- a/indoteknik_custom/__manifest__.py +++ b/indoteknik_custom/__manifest__.py @@ -59,6 +59,7 @@ 'views/custom_mail_marketing.xml', 'views/website_ads.xml', 'views/leads_monitoring.xml', + 'views/ip_lookup.xml', 'report/report.xml', 'report/report_banner_banner.xml', 'report/report_banner_banner2.xml', 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') + diff --git a/indoteknik_custom/security/ir.model.access.csv b/indoteknik_custom/security/ir.model.access.csv index 83d9e2ed..2cfe86a9 100755 --- a/indoteknik_custom/security/ir.model.access.csv +++ b/indoteknik_custom/security/ir.model.access.csv @@ -30,4 +30,6 @@ access_website_ads,access.website.ads,model_website_ads,,1,1,1,1 access_leads_monitoring,access.leads.monitoring,model_leads_monitoring,,1,1,1,1 access_midtrans_notification,access.midtrans.notification,model_midtrans_notification,,1,1,1,1 access_midtrans_recurring,access.midtrans.recurring,model_midtrans_recurring,,1,1,1,1 -access_midtrans_account,access.midtrans.account,model_midtrans_account,,1,1,1,1 \ No newline at end of file +access_midtrans_account,access.midtrans.account,model_midtrans_account,,1,1,1,1 +access_ip_lookup,access.ip.lookup,model_ip_lookup,,1,1,1,1 +access_ip_lookup_line,access.ip.lookup.line,model_ip_lookup_line,,1,1,1,1 \ No newline at end of file diff --git a/indoteknik_custom/views/ip_lookup.xml b/indoteknik_custom/views/ip_lookup.xml new file mode 100644 index 00000000..c276e9d5 --- /dev/null +++ b/indoteknik_custom/views/ip_lookup.xml @@ -0,0 +1,69 @@ + + + + ip.lookup.tree + ip.lookup + + + + + + + + + + ip.lookup.line.tree + ip.lookup.line + + + + + + + + + + + ip.lookup.form + ip.lookup + +
+ +
+ + +
+
+ + +
+
+ + + + + + + + + + + + IP Lookup + ir.actions.act_window + ip.lookup + tree,form + + + + \ No newline at end of file -- cgit v1.2.3