Coverage for lino/modlib/countries/mixins.py : 45%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- coding: UTF-8 -*- # Copyright 2008-2015 Luc Saffre # License: BSD (see file COPYING for details)
These include :class:`CountryCity`, :class:`CountryRegionCity` and :class:`AddressLocation`.
"""
"""Model mixin that adds two fields `country` and `city` and defines a context-sensitive chooser for `city`, a `create_city_choice` method, ...
.. attribute:: country .. attribute:: zip_code
.. attribute:: city
A pointer to :class:`Place`.
"""
"countries.Country", blank=True, null=True) 'countries.Place', verbose_name=_('City'), blank=True, null=True)
# active fields cannot be used in insert_layout
def city_choices(cls, country): return rt.modules.countries.Place.get_cities(country)
def country_choices(cls): return rt.modules.countries.Country.get_actual_countries()
""" Called when an unknown city name was given. Try to auto-create it. """ if self.country is not None: return rt.modules.countries.Place.lookup_or_create( 'name', text, country=self.country)
raise ValidationError( "Cannot auto-create city %r if country is empty", text)
""" If user changes the `country`, then the `city` gets lost. """ if self.city is not None and self.country != self.city.country: self.city = None
if self.country and self.zip_code: qs = rt.modules.countries.Place.objects.filter( country=self.country, zip_code=self.zip_code) if qs.count() > 0: self.city = qs[0]
"""Fills my :attr:`zip_code` from my :attr:`city` if my `zip_code` is not empty and differs from that of the city.
""" city = self.city if city is None: self.zip_code_changed(None) else: if city.country is not None and self.country != city.country: self.country = city.country if city.zip_code: self.zip_code = city.zip_code super(CountryCity, self).full_clean(*args, **kw)
""" Adds a `region` field to a :class:`CountryCity`.
""" 'countries.Place', blank=True, null=True, verbose_name=dd.plugins.countries.region_label, related_name="%(app_label)s_%(class)s_set_by_region") #~ related_name='regions')
def region_choices(cls, country): Place = rt.modules.countries.Place if country is not None: cd = getattr(CountryDrivers, country.isocode, None) if cd: flt = models.Q(type__in=cd.region_types) else: flt = models.Q(type__lt=PlaceTypes.get_by_value('50')) #~ flt = flt | models.Q(type=PlaceTypes.blank_item) flt = flt & models.Q(country=country) return Place.objects.filter(flt).order_by('name') else: flt = models.Q(type__lt=PlaceTypes.get_by_value('50')) return Place.objects.filter(flt).order_by('name')
# if a Place is created by super, then we add our region obj = super(CountryRegionCity, self).create_city_choice(text) obj.region = self.region return obj
def city_choices(cls, country, region): qs = super(CountryRegionCity, cls).city_choices(country)
if region is not None: parent_list = [p.pk for p in region.get_parents()] + [None] #~ print 20120822, region,region.get_parents(), parent_list qs = qs.filter(parent__id__in=parent_list) #~ print flt
return qs
#~ return country.place_set.filter(flt).order_by('name') #~ return cls.city.field.rel.model.objects.order_by('name')
"""A mixin for models which contain a postal address location.
.. attribute:: addr1 .. attribute:: street_prefix .. attribute:: street .. attribute:: street_no .. attribute:: street_box .. attribute:: addr2
.. attribute:: addess_column
Virtual field which returns the location as a comma-separated one-line string.
"""
_("Address line before street"), max_length=200, blank=True, help_text=_("Address line before street"))
_("Street prefix"), max_length=200, blank=True, help_text=_("Text to print before name of street, " "but to ignore for sorting."))
_("Street"), max_length=200, blank=True, help_text=_("Name of street, without house number."))
_("No."), max_length=10, blank=True, help_text=_("House number."))
_("Box"), max_length=10, blank=True, help_text=_("Text to print after street nuber on the same line."))
_("Address line after street"), max_length=200, blank=True, help_text=_("Address line to print below street line."))
sc = settings.SITE.site_config.site_company if sc and sc.country: self.country = sc.country super(AddressLocation, self).on_create(ar)
#~ lines = [] #~ lines = [self.name] af = get_address_formatter(self.country)
if self.addr1: yield self.addr1 for ln in af.get_street_lines(self): yield ln if self.addr2: yield self.addr2
for ln in af.get_city_lines(self): yield ln
if self.country is not None: kodu = dd.plugins.countries.get_my_country() if kodu is None or self.country != kodu: # (if self.country != sender's country) yield str(self.country)
#~ logger.debug('%s : as_address() -> %r',self,lines)
"""Return the plain text postal address location part. Lines are separated by `linesep` which defaults to ``"\\n"``.
The following example creates a Partner, then calls its :meth:`address_location` method:
>>> BE = countries.Country.objects.get(pk='BE') >>> p = contacts.Partner( ... name="Foo", ... street_prefix="Rue de l'", street="Abattoir", ... street_no=5, country=BE, zip_code="4000") >>> p.full_clean() >>> p.save() >>> print(p.address_location()) Rue de l' Abattoir 5 4000 Liège Belgium
""" return linesep.join(self.address_location_lines())
def address_column(self, ar): return self.address_location(', ')
|