def test_sort_by_zipcode(self): """sort by zipcode""" contact1, contact2, contact3, contact4, contact5, contact6 = self._contacts( ) group1, group2 = self._groups(contact1, contact2, contact3, contact4, contact5, contact6) contact1.zip_code = "42100" contact1.city = mommy.make(models.City, parent=get_default_country(), name="Ville1") contact1.save() contact2.entity.zip_code = "42100" contact2.entity.city = mommy.make(models.City, parent=get_default_country(), name="City2") contact2.entity.save() contact3.entity.zip_code = "42000" contact3.entity.city = contact2.entity.city contact3.entity.save() contact4.zip_code = "" contact4.city = mommy.make(models.City, parent=mommy.make( models.Zone, name="ABC", type=get_default_country().type), name="City3") contact4.save() contact5.zip_code = "42000" contact5.city = contact2.entity.city contact5.save() contact6.zip_code = "42100" contact6.city = mommy.make(models.City, parent=mommy.make( models.Zone, name="DEF", type=get_default_country().type), name="Ville4") contact6.save() expected_order = (contact5, contact3, contact2, contact1, contact4, contact6) data = { "gr0-_-group-_-0": group1.id, "gr0-_-sort-_-1": 'zipcode', "gr1-_-group-_-0": group2.id, } self._post_and_check(data, expected_order)
def _get_zonegroup_data(self, form_name="zone_group"): """get zonegroup data""" default_country = get_default_country() region_type = mommy.make(models.ZoneType, type="region") region1 = mommy.make(models.Zone, parent=default_country, type=region_type) region2 = mommy.make(models.Zone, parent=default_country, type=region_type) departement_type = mommy.make(models.ZoneType, type="department") departement1 = mommy.make(models.Zone, parent=region1, type=departement_type) departement2 = mommy.make(models.Zone, parent=region2, type=departement_type) city1 = mommy.make(models.City, name="ZooPark", parent=departement1) city2 = mommy.make(models.City, name="VodooPark", parent=departement2) zone_group_type = mommy.make(models.ZoneType, type="zone_group") zone_group = mommy.make(models.Zone, parent=None, type=zone_group_type) city1.groups.add(zone_group) city1.save() data = {"gr0-_-{0}-_-0".format(form_name): zone_group.id} return (city1, city2), data
def _manage_country_field(self, field_prefix, *args, **kwargs): """""" setattr(self, field_prefix + 'country_id', 0) if len(args): try: setattr(self, field_prefix + 'country_id', int(args[0][field_prefix + "country"])) except (KeyError, ValueError): pass if not getattr(self, field_prefix + 'country_id', 0): setattr(self, field_prefix + 'country_id', get_default_country().id) self.fields[field_prefix + 'city'].widget = CityAutoComplete( attrs={'placeholder': _(u'Enter a city'), 'size': '80'} ) zones_choices = [ (zone.id, zone.name) for zone in models.Zone.objects.filter(parent__isnull=True).order_by('name') ] self.fields[field_prefix + 'country'].choices = [(0, '')] + zones_choices try: city = getattr(kwargs.get('instance'), field_prefix + 'city', 0) if city: city_parent = self._get_city_parent(city) if city_parent: self.fields[field_prefix + 'country'].initial = city_parent.id except models.City.DoesNotExist: pass
def _manage_country_field(self, field_prefix, *args, **kwargs): """""" setattr(self, field_prefix + 'country_id', 0) if len(args): try: setattr(self, field_prefix + 'country_id', int(args[0][field_prefix + "country"])) except (KeyError, ValueError): pass if not getattr(self, field_prefix + 'country_id', 0): setattr(self, field_prefix + 'country_id', get_default_country().id) self.fields[field_prefix + 'city'].widget = CityAutoComplete( attrs={ 'placeholder': _('Enter a city'), 'size': '80' }) zones_choices = [(zone.id, zone.name) for zone in models.Zone.objects.filter( parent__isnull=True).order_by('name')] self.fields[field_prefix + 'country'].choices = [(0, '')] + zones_choices try: city = getattr(kwargs.get('instance'), field_prefix + 'city', 0) if city: city_parent = self._get_city_parent(city) if city_parent: self.fields[field_prefix + 'country'].initial = city_parent.id except models.City.DoesNotExist: pass
def get_lookup(self): """lookup""" default_country = get_default_country() if int(self.value) == default_country.id: return Q(entity__city__parent__type__type='department') else: return Q(entity__city__parent__id=self.value, entity__city__parent__type__type="country")
def _get_country(self, country_id): """get the country""" if country_id: return models.Zone.objects.get(id=country_id, parent__isnull=True, type__type="country") else: return get_default_country()
def test_sort_by_zipcode(self): """sort by zipcode""" contact1, contact2, contact3, contact4, contact5, contact6 = self._contacts() group1, group2 = self._groups(contact1, contact2, contact3, contact4, contact5, contact6) contact1.zip_code = "42100" contact1.city = mommy.make(models.City, parent=get_default_country(), name="Ville1") contact1.save() contact2.entity.zip_code = "42100" contact2.entity.city = mommy.make(models.City, parent=get_default_country(), name="City2") contact2.entity.save() contact3.entity.zip_code = "42000" contact3.entity.city = contact2.entity.city contact3.entity.save() contact4.zip_code = "" contact4.city = mommy.make( models.City, parent=mommy.make(models.Zone, name="ABC", type=get_default_country().type), name="City3" ) contact4.save() contact5.zip_code = "42000" contact5.city = contact2.entity.city contact5.save() contact6.zip_code = "42100" contact6.city = mommy.make( models.City, parent=mommy.make(models.Zone, name="DEF", type=get_default_country().type), name="Ville4" ) contact6.save() expected_order = (contact5, contact3, contact2, contact1, contact4, contact6) data = { "gr0-_-group-_-0": group1.id, "gr0-_-sort-_-1": 'zipcode', "gr1-_-group-_-0": group2.id, } self._post_and_check(data, expected_order)
def get_lookup(self): """lookup""" default_country = get_default_country() if int(self.value) == default_country.id: return Q( city__parent__type__type='department' ) | (Q( city__isnull=True ) & Q(entity__city__parent__type__type='department')) else: return Q(city__parent__id=self.value) | (Q(city__isnull=True) & Q(entity__city__parent__id=self.value))
def _get_departements_data(self, form_name="department"): """get departemenst""" default_country = get_default_country() region_type = mommy.make(models.ZoneType, type="region") region = mommy.make(models.Zone, parent=default_country, type=region_type) departement_type = mommy.make(models.ZoneType, type="department") departement1 = mommy.make(models.Zone, parent=region, type=departement_type) departement2 = mommy.make(models.Zone, parent=region, type=departement_type) city1 = mommy.make(models.City, name="ZooPark", parent=departement1) city2 = mommy.make(models.City, name="VodooPark", parent=departement2) data = {"gr0-_-{0}-_-0".format(form_name): departement1.id} return (city1, city2), data
def _get_countries_mix_data(self, form_name="country"): """get date with full hierarchy""" country_type = mommy.make(models.ZoneType, type="country") country1 = default_country = get_default_country() country2 = mommy.make(models.Zone, type=country_type, parent=None) region_type = mommy.make(models.ZoneType, type="region") region1 = mommy.make(models.Zone, parent=default_country, type=region_type) departement_type = mommy.make(models.ZoneType, type="department") departement1 = mommy.make(models.Zone, parent=region1, type=departement_type) city1 = mommy.make(models.City, name="ZooPark", parent=departement1) city2 = mommy.make(models.City, name="VodooPark", parent=country2) data = {"gr0-_-{0}-_-0".format(form_name): country1.id} return (city1, city2), data
def _clean_city_field(self, field_prefix): """city validation""" city = self.cleaned_data[field_prefix + 'city'] if isinstance(city, models.City): return city else: try: if not city: return None try: city_id = int(city) return models.City.objects.get(id=city_id) except (ValueError, TypeError): pass zip_code = self.cleaned_data[field_prefix + 'zip_code'] try: country_id = int(self.cleaned_data.get(field_prefix + 'country')) \ or getattr(self, field_prefix + 'country_id') except (ValueError, TypeError): country_id = getattr(self, field_prefix + 'country_id') country = self._get_country(country_id) default_country = get_default_country() if country != default_country: city = models.City.objects.get_or_create(name=city, parent=country)[0] else: if len(zip_code) < 2: raise ValidationError( ugettext( 'You must enter a valid zip code for selecting a new city' )) dep = models.Zone.objects.get( Q(code=zip_code[:2]) | Q(code=zip_code[:3])) city = models.City.objects.get_or_create(name=city, parent=dep)[0] return city except ValidationError: raise except Exception as msg: raise ValidationError(msg)
def __init__(self, *args, **kwargs): super(SortContacts, self).__init__(*args, **kwargs) choices = ( ('name', _(u'Name')), ('entity', _(u'Entity')), ('contact', _(u'Contact')), ('zipcode', _(u'Zipcode')), ) field = forms.CharField( label=self.label, widget=forms.Select( choices=choices, attrs={ 'class': "chosen-select", 'style': "width: 100%", } ) ) self._add_field(field) self.default_country = get_default_country()
def _get_departements_data(self, form_name="department"): """get departments""" default_country = get_default_country() region_type = mommy.make(models.ZoneType, type="region") region = mommy.make(models.Zone, parent=default_country, type=region_type) departement_type = mommy.make(models.ZoneType, type="department") departement1 = mommy.make(models.Zone, parent=region, type=departement_type) departement2 = mommy.make(models.Zone, parent=region, type=departement_type) city1 = mommy.make(models.City, name="ZooPark", parent=departement1) city2 = mommy.make(models.City, name="VodooPark", parent=departement2) data = {"gr0-_-{0}-_-0".format(form_name): departement1.id} return (city1, city2), data
def _clean_city_field(self, field_prefix): """city validation""" city = self.cleaned_data[field_prefix + 'city'] if isinstance(city, models.City): return city else: try: if not city: return None try: city_id = int(city) return models.City.objects.get(id=city_id) except (ValueError, TypeError): pass zip_code = self.cleaned_data[field_prefix + 'zip_code'] try: country_id = int(self.cleaned_data.get(field_prefix + 'country')) \ or getattr(self, field_prefix + 'country_id') except (ValueError, TypeError): country_id = getattr(self, field_prefix + 'country_id') country = self._get_country(country_id) default_country = get_default_country() if country != default_country: city = models.City.objects.get_or_create(name=city, parent=country)[0] else: if len(zip_code) < 2: raise ValidationError(ugettext(u'You must enter a valid zip code for selecting a new city')) dep = models.Zone.objects.get(code=zip_code[:2]) city = models.City.objects.get_or_create(name=city, parent=dep)[0] return city except ValidationError: raise except Exception, msg: raise ValidationError(msg)