You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.5 KiB

9 years ago
9 years ago
9 years ago
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015 GRAP - Sylvain LE GAL
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import urllib
  5. import json
  6. from odoo import api, fields, models
  7. class ResBannedRemote(models.Model):
  8. _name = 'res.banned.remote'
  9. _rec_name = 'remote'
  10. _GEOLOCALISATION_URL = "http://ip-api.com/json/{}"
  11. # Column Section
  12. description = fields.Text(
  13. string='Description', compute='_compute_description', store=True)
  14. ban_date = fields.Datetime(
  15. string='Ban Date', required=True, default=fields.Datetime.now)
  16. remote = fields.Char(string='Remote ID', required=True)
  17. active = fields.Boolean(
  18. string='Active', help="Uncheck this box to unban the remote",
  19. default=True)
  20. attempt_ids = fields.Many2many(
  21. comodel_name='res.authentication.attempt', string='Attempts',
  22. compute='_compute_attempt_ids')
  23. # Compute Section
  24. @api.multi
  25. @api.depends('remote')
  26. def _compute_description(self):
  27. for item in self:
  28. url = self._GEOLOCALISATION_URL.format(item.remote)
  29. res = json.loads(urllib.urlopen(url).read())
  30. item.description = ''
  31. for k, v in res.iteritems():
  32. item.description += '%s : %s\n' % (k, v)
  33. @api.multi
  34. def _compute_attempt_ids(self):
  35. for item in self:
  36. attempt_obj = self.env['res.authentication.attempt']
  37. item.attempt_ids = attempt_obj.search_last_failed(item.remote)