vidjil_utils.py 3.14 KB
Newer Older
1
import math
2
import re
3
from gluon import current
4

5
def format_size(n, unit='B'):
6 7
    '''
    Takes an integer n, representing a filesize and returns a string
8 9
    where the size is formatted with the correct SI prefix and
    with a constant number of significant digits.
10 11

    Example:
12 13 14 15 16 17
    >>> format_size(42)
    '42 B'
    >>> format_size(123456)
    '123 kB'
    >>> format_size(1000*1000)
    '1.00 MB'
18
    >>> format_size(1024*1024*1024)
19 20 21
    '1.07 GB'
    >>> format_size(42*(2**40))
    '46.2 TB'
22
    '''
23 24 25 26

    if n == 0:
        return '0'

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    size = float(n)
    PREFIXES = ['', 'k', 'M', 'G', 'T', 'P']

    for prefix in PREFIXES:
        if size < 1000:
            break
        size /= 1000


    if size > 100 or not prefix:
        fmt = '%.0f'
    elif size > 10:
        fmt = '%.1f'
    else:
        fmt = '%.2f'

    return fmt % size + ' ' + prefix + unit

45 46 47 48 49 50 51 52 53

def anon(patient_id, user_id):
    db = current.db
    auth=current.auth
    
    last_name = db.patient[patient_id].last_name
    first_name = db.patient[patient_id].first_name
    
    if auth.has_permission("anon", "patient", patient_id, user_id):
54
        name = last_name + " " + first_name
55
    else:
56 57 58 59 60 61
        try:
            ln = unicode(last_name, 'utf-8')
        except UnicodeDecodeError:
            ln = last_name
        name = ln[:3]

62 63 64 65
    # Admins also see the patient id
    if auth.has_membership("admin"):
        name += ' (%s)' % patient_id

66
    return name
67 68 69 70 71 72 73 74 75


# take a string to check and a filter_str (list of word to find (or not)) 
# return true if the string respect the filter list 
def filter(str, filter_str):
    filter_list = filter_str.split(" ")
    
    for f in filter_list :
        if len(f) > 0 and f[0] == "-" :
76
            if f[1:].lower() in str.lower():
77 78
                return False
        else :
79
            if f.lower() not in str.lower():
80 81
                return False
    return True
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 127 128 129 130 131 132 133 134


log_patient = re.compile('\((\d+)\)')
log_config = re.compile(' c(\d+)')

def log_links(s):
    '''Add HTML links to a log string

    >>> log_links("abcdef")
    'abcdef'
    >>> log_links("abcdef(234)")
    'abcdef(<a class="loglink" onclick="db.call(\\'patient/info\\', {\\'id\\': \\'234\\'})">234</a>)'
    >>> log_links("abcdef(234)abcdef c11")
    'abcdef(234)abcdef <a class="loglink" href="?patient=234&config=11">c11</a>'
    '''

    ### Parses the input string

    m_patient = log_patient.search(s)
    patient = m_patient.group(1) if m_patient else None

    m_config = log_config.search(s)
    config = m_config.group(1) if m_config else None

    ### Rules

    url = ''  # href link
    call = '' # call to db

    if patient and config:
        url = "?patient=%s&config=%s" % (patient, config)
        (start, end) = m_config.span()
        start += 1

    elif patient:
        call = "patient/info"
        args = {'id': patient}
        (start, end) = m_patient.span()
        start += 1
        end -= 1

    ### Build final string

    link = ''
    if url:
        link = 'href="%s"' % url
    if call:
        link = '''onclick="db.call('%s', %s)"''' % (call, str(args))

    if link:
        s = '%s<a class="loglink" %s>%s</a>%s' % (s[:start], link, s[start:end], s[end:])

    return s