summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/ip_lookup.py
blob: a708cd94eb77f9a01ba9f4ec316ff1eddca93dc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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:
                query = [
                    ('ip_address', '=', log.ip_address),
                    ('lookup', '!=', False)
                ]
                last_data = self.env['ip.lookup.line'].search(query, limit=1)
                if last_data:
                    log.lookup = last_data.lookup
                    country = json.loads(last_data.lookup)['country']
                    timezone = json.loads(last_data.lookup)['timezone']
                    _logger.info('Parsing IP using last data %s' % log.id)
                else:
                    ipinfo = requests.get('http://ip-api.com/json/%s' % log.ip_address).json()
                    del ipinfo['status']
                    lookup_json = json.dumps(ipinfo, indent=4, sort_keys=True) 
                    log.lookup = lookup_json
                    country = json.loads(lookup_json)['country']
                    timezone = json.loads(lookup_json)['timezone']
                    _logger.info('Parsing IP using API JSON %s' % log.id)
                log.country = country
                log.timezone = timezone
                log.continent = timezone.split('/')[0]
            except:
                # log.lookup = ''
                _logger.info('Failed parsing IP Lookup Line %s' % log.id)

    def _load_info_address_lookup(self):
        lines = self.env['ip.lookup.line'].search([('country', '=', False), ('lookup', '!=', False)], limit=500)
        for line in lines:
            line.country = json.loads(line.lookup)['country']
            timezone = json.loads(line.lookup)['timezone']
            continent = timezone.split('/')[0]
            line.timezone = timezone
            line.continent = continent
            _logger.info('Success parsing ip lookup line id %s' % line.id)

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')
    timezone = fields.Char(string='Timezone')
    continent = fields.Char(string='Continent', help='diparsing dari field timezone')