manager.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. from IPy import IP
  2. from django.core.exceptions import ValidationError
  3. from django.utils.translation import ugettext_lazy
  4. from django.db import models, connection
  5. from django.db.models import sql, query
  6. # FIXME decide if we should use custom lookup names instead of overrides.
  7. # FIXME decide if other "standard" lookups should be disabled
  8. # FIXME test "standard" lookups
  9. # FIXME decide if HOST() cast should be ignored
  10. NET_TERMS = {
  11. 'lt': '<',
  12. 'lte': '<=',
  13. 'exact': '=',
  14. 'gte': '>=',
  15. 'gt': '>',
  16. 'contained': '<<',
  17. 'contained_or_equal': '<<=',
  18. 'contains': '>>',
  19. 'contains_or_equals': '>>=',
  20. }
  21. class NetQuery(sql.Query):
  22. query_terms = sql.Query.query_terms.copy()
  23. query_terms.update(NET_TERMS)
  24. def add_filter(self, (filter_string, value), *args, **kwargs):
  25. # IP(...) == '' fails so make sure to force to string while we can
  26. if isinstance(value, IP):
  27. value = unicode(value)
  28. return super(NetQuery, self).add_filter(
  29. (filter_string, value), *args, **kwargs)
  30. class NetWhere(sql.where.WhereNode):
  31. def make_atom(self, child, qn):
  32. table_alias, name, db_type, lookup_type, value_annot, params = child
  33. if db_type in ['cidr', 'inet'] and lookup_type in NET_TERMS:
  34. lookup = '%s.%s %s inet %%s' % (table_alias, name, NET_TERMS[lookup_type])
  35. return (lookup, params)
  36. return super(NetWhere, self).make_atom(child, qn)
  37. class NetManger(models.Manager):
  38. use_for_related_fields = True
  39. def get_query_set(self):
  40. q = NetQuery(self.model, connection, NetWhere)
  41. return query.QuerySet(self.model, q)
  42. # FIXME formfields etc?
  43. # - regexp field for mac
  44. # - IP try catch for ip and cidr
  45. class _NetAddressField(models.Field):
  46. # FIXME null and blank handling needs to be done right.
  47. def to_python(self, value):
  48. if value is None:
  49. return value
  50. try:
  51. return IP(value)
  52. except ValueError, e:
  53. raise ValidationError(e)
  54. def get_db_prep_value(self, value):
  55. # FIXME does this need to respect null and blank?
  56. if value is None:
  57. return value
  58. return unicode(self.to_python(value))
  59. def get_db_prep_lookup(self, lookup_type, value):
  60. if value is None:
  61. return value
  62. if lookup_type in NET_TERMS:
  63. return [unicode(value)]
  64. return super(_NetAddressField, self).get_db_prep_lookup(
  65. lookup_type, unicode(value))
  66. class InetAddressField(_NetAddressField):
  67. description = "PostgreSQL INET field"
  68. __metaclass__ = models.SubfieldBase
  69. def db_type(self):
  70. return 'inet'
  71. class CidrAddressField(_NetAddressField):
  72. description = "PostgreSQL CIDR field"
  73. __metaclass__ = models.SubfieldBase
  74. def db_type(self):
  75. return 'cidr'
  76. class MACAddressField(models.Field):
  77. # FIXME does this need proper validation?
  78. description = "PostgreSQL MACADDR field"
  79. def __init__(self, *args, **kwargs):
  80. kwargs['max_length'] = 17
  81. super(MACAddressField, self).__init__(*args, **kwargs)
  82. def db_type(self):
  83. return 'macaddr'
  84. class Foo(models.Model):
  85. inet = InetAddressField(null=True)
  86. cidr = CidrAddressField(null=True)
  87. mac = MACAddressField(null=True)
  88. objects = NetManger()