summaryrefslogtreecommitdiff
path: root/postgresqleu/countries/utils/isoflag.py
blob: 9fb626c6bacbc78a38caa9aa3b1b51356da7b2c7 (plain)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django.conf import settings


def iso_flag(iso, flag_path=''):
    """
    Returns a full path to the ISO 3166-1 alpha-2 country code flag image.
    
    ``flag_path`` is given in the form
    ``'<path relative to media root>/%s.gif'``
    and is appended to ``settings.MEDIA_URL`` 
    
    if a valid flag_path is not given trys to use 
    ``settings.COUNTRIES_FLAG_PATH``
    defaults to ``'flags/%s.gif'``
    
    """
    if not settings.MEDIA_URL:
        return ''
    deafult = '-'
    if not iso:
        iso = deafult
    else:
        iso = iso.lower().strip()
    try:
        flag_name = flag_path % iso
    except (ValueError, TypeError):
        flag_path = getattr(settings, 'COUNTRIES_FLAG_PATH', 'flags/%s.gif')
        try:
            flag_name = flag_path % iso
        except (ValueError, TypeError):
            return ''
    return ''.join((settings.MEDIA_URL, flag_name))