manager.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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((filter_string, value), *args, **kwargs)
  29. class NetWhere(sql.where.WhereNode):
  30. def make_atom(self, child, qn):
  31. table_alias, name, db_type, lookup_type, value_annot, params = child
  32. if db_type in ['cidr', 'inet'] and lookup_type in NET_TERMS:
  33. return ('%s.%s %s inet %%s' % (table_alias, name, NET_TERMS[lookup_type]), params)
  34. return super(NetWhere, self).make_atom(child, qn)
  35. class NetManger(models.Manager):
  36. use_for_related_fields = True
  37. def get_query_set(self):
  38. q = NetQuery(self.model, connection, NetWhere)
  39. return query.QuerySet(self.model, q)
  40. # FIXME formfields etc?
  41. # - regexp field for mac
  42. # - IP try catch for ip and cidr
  43. class _NetAddressField(models.Field):
  44. # FIXME null and blank handling needs to be done right.
  45. def to_python(self, value):
  46. if value is None:
  47. if self.null:
  48. return value
  49. else:
  50. raise ValidationError(
  51. ugettext_lazy("This field cannot be null."))
  52. try:
  53. return IP(value)
  54. except ValueError, e:
  55. raise ValidationError(e)
  56. def get_db_prep_value(self, value):
  57. # FIXME does this need to respect null and blank?
  58. if value is None:
  59. return value
  60. return unicode(self.to_python(value))
  61. def get_db_prep_lookup(self, lookup_type, value):
  62. if value is None:
  63. return value
  64. value = unicode(value)
  65. if lookup_type in NET_TERMS:
  66. return [value]
  67. return super(_NetAddressField, self).get_db_prep_lookup(lookup_type, value)
  68. class InetAddressField(_NetAddressField):
  69. description = "PostgreSQL INET field"
  70. __metaclass__ = models.SubfieldBase
  71. def db_type(self):
  72. return 'inet'
  73. class CidrAddressField(_NetAddressField):
  74. description = "PostgreSQL CIDR field"
  75. __metaclass__ = models.SubfieldBase
  76. def db_type(self):
  77. return 'cidr'
  78. class MACAddressField(models.Field):
  79. description = "PostgreSQL MACADDR field"
  80. def __init__(self, *args, **kwargs):
  81. kwargs['max_length'] = 17
  82. super(MACAddressField, self).__init__(*args, **kwargs)
  83. # FIXME does this need proper validation?
  84. def db_type(self):
  85. return 'macaddr'
  86. class Foo(models.Model):
  87. inet = InetAddressField(null=True)
  88. cidr = CidrAddressField(null=True)
  89. mac = MACAddressField(null=True)
  90. text = models.CharField(max_length=10, blank=True, null=True)
  91. objects = NetManger()