summaryrefslogtreecommitdiffstats
path: root/ipa-server/ipa-gui/ipagui/helpers/ipahelper.py
blob: e5c2bd378e388b0ab1730ca55fbb0591c6b01f21 (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
36
37
38
39
40
import re

def javascript_string_escape(input):
    """Escapes the ' " and \ characters in a string so
       it can be embedded inside a dynamically generated string."""

    return re.sub(r'[\'\"\\]',
            lambda match: "\\%s" % match.group(),
            input)

def setup_mv_fields(field, fieldname):
    """Given a field (must be a list) and field name, convert that
       field into a list of dictionaries of the form:
          [ { fieldname : v1}, { fieldname : v2 }, .. ]

   This is how we pre-fill values for multi-valued fields.
    """
    mvlist = []
    if field:
        for v in field:
            if v:
                mvlist.append({ fieldname : v } )
    if len(mvlist) == 0:
        # We need to return an empty value so something can be
        # displayed on the edit page. Otherwise only an Add link
        # will show, not an empty field.
        mvlist.append({ fieldname : '' } )
    return mvlist

def fix_incoming_fields(fields, fieldname, multifieldname):
    """This is called by the update() function. It takes the incoming
       list of dictionaries and converts it into back into the original
       field, then removes the multiple field.
    """
    fields[fieldname] = []
    for i in range(len(fields[multifieldname])):
        fields[fieldname].append(fields[multifieldname][i][fieldname])
    del(fields[multifieldname])

    return fields