/usr/share/pyshared/django_countries/fields.py is in python-django-countries 1.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | from django.db.models.fields import CharField
from django.utils.encoding import force_unicode, StrAndUnicode
from django_countries import settings
class Country(StrAndUnicode):
def __init__(self, code):
self.code = code
def __unicode__(self):
return force_unicode(self.code or u'')
def __eq__(self, other):
return unicode(self) == force_unicode(other)
def __ne__(self, other):
return not self.__eq__(other)
def __cmp__(self, other):
return cmp(unicode(self), force_unicode(other))
def __hash__(self):
return hash(unicode(self))
def __repr__(self):
return "%s(code=%r)" % (self.__class__.__name__, unicode(self))
def __nonzero__(self):
return bool(self.code)
def __len__(self):
return len(unicode(self))
@property
def name(self):
# Local import so the countries aren't loaded unless they are needed.
from django_countries.countries import COUNTRIES
for code, name in COUNTRIES:
if self.code == code:
return name
return ''
@property
def flag(self):
if not self.code:
return ''
return settings.FLAG_URL % {'code_upper': self.code,
'code': self.code.lower()}
class CountryDescriptor(object):
"""
A descriptor for country fields on a model instance. Returns a Country when
accessed so you can do stuff like::
>>> instance.country.name
u'New Zealand'
>>> instance.country.flag
'/static/flags/nz.gif'
"""
def __init__(self, field):
self.field = field
def __get__(self, instance=None, owner=None):
if instance is None:
raise AttributeError(
"The '%s' attribute can only be accessed from %s instances."
% (self.field.name, owner.__name__))
return Country(code=instance.__dict__[self.field.name])
def __set__(self, instance, value):
if value is not None:
value = force_unicode(value)
instance.__dict__[self.field.name] = value
class CountryField(CharField):
"""
A country field for Django models that provides all ISO 3166-1 countries as
choices.
"""
descriptor_class = CountryDescriptor
def __init__(self, *args, **kwargs):
# Local import so the countries aren't loaded unless they are needed.
from django_countries.countries import COUNTRIES
kwargs.setdefault('max_length', 2)
kwargs.setdefault('choices', COUNTRIES)
super(CharField, self).__init__(*args, **kwargs)
def get_internal_type(self):
return "CharField"
def contribute_to_class(self, cls, name):
super(CountryField, self).contribute_to_class(cls, name)
setattr(cls, self.name, self.descriptor_class(self))
def get_prep_lookup(self, lookup_type, value):
if hasattr(value, 'code'):
value = value.code
return super(CountryField, self).get_prep_lookup(lookup_type, value)
def pre_save(self, *args, **kwargs):
"Returns field's value just before saving."
value = super(CharField, self).pre_save(*args, **kwargs)
return self.get_prep_value(value)
def get_prep_value(self, value):
"Returns field's value prepared for saving into a database."
# Convert the Country to unicode for database insertion.
if value is None:
return None
return unicode(value)
# If south is installed, ensure that CountryField will be introspected just
# like a normal CharField.
try:
from south.modelsinspector import add_introspection_rules
add_introspection_rules([], ['^django_countries\.fields\.CountryField'])
except ImportError:
pass
|