def test_override_complex(self): with self.settings( COUNTRIES_OVERRIDE={ "XX": {"names": ["New", "Newer"], "alpha3": "XXX", "numeric": 900}, "YY": {"name": "y", "numeric": 950}, } ): self.assertEqual(countries.name("XX"), "New") self.assertEqual(countries.alpha3("XX"), "XXX") self.assertEqual(countries.numeric("XX"), 900) self.assertEqual(countries.name("YY"), "y") self.assertEqual(countries.alpha3("YY"), None) self.assertEqual(countries.numeric("YY"), 950)
def get_countries(self): country_codes = set() country_codes.update(MirrorUrl.objects.filter(active=True, mirror__active=True).exclude(country='').values_list( 'country', flat=True).order_by().distinct()) code_list = [(code, countries.name(code)) for code in country_codes] return sorted(code_list, key=itemgetter(1))
def get_member_country_stats(): result = [] geocoded = geocoded_countries_dict() memb_countries = User.objects.values('country').all()\ .annotate(models.Count('country', distinct=True)) for mc in memb_countries: # A count of entries with `None` country is also returned so we make sure this is not # included in the result we send out country = mc.get('country', None) country_count = mc.get('country__count', 0) if country: c = dict( type="Feature", properties=dict( description="", country_name=unicode(countries.name(country)), count=country_count, ), geometry=dict( type="Point", coordinates=list(geocoded.get(country, ())) ) ) result.append(c) return result
def display_place_of_birth(contract_information): country_of_birth = countries.name(contract_information["country_of_birth"]) if contract_information["country_of_birth"] == "FR": return f'{contract_information["city_of_birth"]} ({contract_information["departement_of_birth"]})' else: return f'{contract_information["city_of_birth"]} ({country_of_birth})'
def allowed_countries_error_message(self): return self.error_messages["forbidden_country"] % { "countries": ", ".join( countries.name(code) or code for code in self.allowed_countries) }
def get_profile_value(self, profile_attr): if profile_attr == None: return None value = getattr(self.profile, profile_attr, None) if profile_attr in ('living_country', 'native_country'): value = countries.name(value) return value
def get_countries(self): country_codes = set() country_codes.update( MirrorUrl.objects.filter(active=True, mirror__active=True).exclude( country='').values_list('country', flat=True).order_by().distinct()) code_list = [(code, countries.name(code)) for code in country_codes] return sorted(code_list, key=itemgetter(1))
def test_translation_fallback_override_names(self): with self.settings(COUNTRIES_OVERRIDE={ "NZ": { "names": ["Hobbiton", translation.gettext_lazy("New Zealand")] } }): self.assertEqual(countries.name("NZ"), "Hobbiton") lang = translation.get_language() translation.activate("eo") try: self.assertEqual(countries.name("NZ"), "Nov-Zelando") finally: translation.activate(lang)
def test_translation_fallback_no_override(self): lang = translation.get_language() translation.activate("eo") try: self.assertEqual(countries.name("NZ"), "Nov-Zelando") self.assertEqual(countries.name("YE"), "Jemeno") with self.settings(COUNTRIES_OVERRIDE={ "NZ": "Hobbiton", "YE": "YYemen" }): del countries.countries self.assertEqual(countries.name("NZ"), "Hobbiton") self.assertEqual(countries.name("YE"), "YYemen") finally: translation.activate(lang)
def get_apply_to_display(self): if self.type == Voucher.SHIPPING_TYPE and self.apply_to: return countries.name(self.apply_to) if self.type == Voucher.SHIPPING_TYPE: return pgettext('voucher', 'Any country') if self.apply_to and self.type in { Voucher.PRODUCT_TYPE, Voucher.CATEGORY_TYPE}: choices = dict(self.APPLY_TO_PRODUCT_CHOICES) return choices[self.apply_to]
def get_apply_to_display(self): if self.type == VoucherType.SHIPPING and self.apply_to: return countries.name(self.apply_to) if self.type == VoucherType.SHIPPING: return pgettext('Voucher', 'Any country') if self.apply_to and self.type in { VoucherType.PRODUCT, VoucherType.CATEGORY}: choices = dict(VoucherApplyToProduct.CHOICES) return choices[self.apply_to]
def html(self, plain=False): old = '\n'.join(sorted(str(countries.name(c)) for c in self.old)) new = '\n'.join(sorted(str(countries.name(c)) for c in self.new)) dmp = diff_match_patch() a, b, lineArray = dmp.diff_linesToChars(old, new) diff = dmp.diff_main(a, b, checklines=False) dmp.diff_cleanupSemantic(diff) dmp.diff_charsToLines(diff, lineArray) result = [] for op, country in diff: if op: result.append('<div class="{}">{}</div>'.format( 'inserted' if op > 0 else 'deleted', escape(country))) else: result.append('<div>{}</div>'.format(escape(country))) return '\n'.join(result)
def get_apply_to_display(self): if self.type == VoucherType.SHIPPING and self.apply_to: return countries.name(self.apply_to) if self.type == VoucherType.SHIPPING: return pgettext('Voucher', 'Any country') if self.apply_to and self.type in { VoucherType.PRODUCT, VoucherType.CATEGORY}: choices = dict(VoucherApplyToProduct.CHOICES) return choices[self.apply_to] return None
def get_purchase_country(self): """ Return legacy stored country name. Or name corresponding to country code """ pc = self.purchase_country if len(pc) > 2: return pc return countries.name(pc)
class Participant(models.Model): GENDER_CHOICES = ( ('f', 'female'), ('m', 'male'), ) name = models.CharField(max_length=500, verbose_name="Username") user = models.OneToOneField(settings.AUTH_USER_MODEL) real_name = models.CharField(max_length=50, verbose_name="Real name") email = models.CharField(max_length=500, verbose_name="E-Mail address", default="") email2 = models.CharField(max_length=500, verbose_name="E-Mail address (confirmation)", default="") accepted_terms_conditions = models.BooleanField( default=False, verbose_name="I accept the terms and conditions as stated above") ip = models.CharField(max_length=100, blank=True) gender = models.CharField(max_length=2, verbose_name="Gender", choices=GENDER_CHOICES) picture = models.ImageField(upload_to=content_file_name, blank=True, verbose_name="Profile picture") birthdate = models.DateField(verbose_name="Date of Birth", default="") country = CountryField(choices=list(countries), verbose_name="Country", default=countries.name('DE')) matriculation_number = models.CharField(max_length=10, default="0") gps_long = models.CharField(max_length=15) gps_lat = models.CharField(max_length=15) grade = models.CharField(max_length=5) personality_answers = models.ManyToManyField( PersonalityQuestion, through="ParticipantPersonalityQuestion", through_fields=("participant", "personality_question")) groups = models.ManyToManyField("Group", through="GroupParticipant", through_fields=("participant", "group")) personality_test_done = models.BooleanField( default=False, verbose_name="Personality test done") def __unicode__(self): return self.name
def display_full_address(contract_information): street_address = contract_information["location_address1"] if contract_information["location_address2"]: street_address += ", " + contract_information["location_address2"] if contract_information["location_zip"]: city_address = ( contract_information["location_zip"] + " " + contract_information["location_city"] ) else: city_address = contract_information["location_city"] country_address = countries.name(contract_information["location_country"]) return f"{street_address}, {city_address}, {country_address}"
def product_detail(request, id, slug): product = get_object_or_404(Product, id=id, slug=slug, available=True) cart_product_form = CartAddProductForm() product_stock = cart_product_form.get_product_stock(id) categories = Category.objects.all() crypto_data = crypto_currencies() message = len(Message.objects.filter(check=False)) product_vendor = Product.objects.values('productOwnerID', 'country').get(id=id) country = countries.name(product_vendor.get('country')) product_vendor = product_vendor.get('productOwnerID') vendor = User.objects.values('id', 'username').filter(id=product_vendor, vendor=True) term_conditions = VendorTerm.objects.filter(userId=product_vendor) rating_total = 0 rating_count = 0 for order_item in product.order_items.filter(order__paid='8'): for rating in order_item.ratings.all(): rating_total += rating.total * order_item.quantity rating_count += rating.count * order_item.quantity context = { 'product': product, 'categories': categories, 'cart_product_form': cart_product_form, 'product_stock': product_stock, 'vendor': vendor[0], 'avg_rating': 0 if rating_count == 0 else rating_total / rating_count, 'country': country, 'crypto_data': crypto_data, 'new_message': message, 'term_conditions': term_conditions[0] } return render(request, 'main/product/detail.html', context)
def test_override_replace(self): with self.settings(COUNTRIES_OVERRIDE={'NZ': 'Middle Earth'}): self.assertEqual(countries.name('NZ'), 'Middle Earth')
def test_override_additional(self): with self.settings(COUNTRIES_OVERRIDE={'XX': 'New'}): self.assertEqual(countries.name('XX'), 'New')
def test_override_additional(self): with self.settings(COUNTRIES_OVERRIDE={"XX": "New"}): self.assertEqual(countries.name("XX"), "New")
def test_override_only(self): with self.settings(COUNTRIES_ONLY={"AU": "Desert"}): self.assertTrue(len(countries.countries) == 1) self.assertIn("AU", countries) self.assertEqual(countries.name("AU"), "Desert")
def lookups(self, request, model_admin): # Get the list of unique country codes where there is an institution qs = model_admin.get_queryset(request) codes = qs.order_by('country').values_list('country', flat=True).distinct() return [(code, countries.name(code)) for code in codes]
def name(self): return countries.name(self.code)
def get_country_name(country_code): if country_code == UNKNOWN_COUNTRY_CODE: return unicode(_("Unknown")) return unicode(countries.name(country_code))
def test_override_replace(self): with self.settings(COUNTRIES_OVERRIDE={"NZ": "Middle Earth"}): self.assertEqual(countries.name("NZ"), "Middle Earth")
def get_country_name(country_code): if country_code == '': return unicode(_("Unknown")) return unicode(countries.name(country_code))
def test_override_remove(self): with self.settings(COUNTRIES_OVERRIDE={'AU': None}): self.assertNotIn('AU', countries.countries) self.assertEqual(countries.name('AU'), '')
def test_override_remove(self): with self.settings(COUNTRIES_OVERRIDE={'AU': None}): self.assertNotIn('AU', countries) self.assertEqual(countries.name('AU'), '')
def test_override_only(self): with self.settings(COUNTRIES_ONLY={'AU': 'Desert'}): self.assertTrue(len(countries.countries) == 1) self.assertIn('AU', countries) self.assertEqual(countries.name('AU'), 'Desert')
def get_substance_p_c_t_countries_display(self): c = [str(countries.name(c)) for c in self.substance_p_c_t_countries] return ', '.join(sorted(c))
def test_override_remove(self): with self.settings(COUNTRIES_OVERRIDE={"AU": None}): self.assertNotIn("AU", countries) self.assertEqual(countries.name("AU"), "")
def name(self): return self.maybe_escape(countries.name(self.code))