Ejemplo n.º 1
0
def partnerlookup(value,
                  field,
                  field_where_value_is_searched='idpartner',
                  safe=False):
    ''' lookup via table partner.
        lookup value is returned, exception if not there.
        when using 'field_where_value_is_searched' with other values as ='idpartner',
        partner tabel is only indexed on idpartner (so uniqueness is not guaranteerd). 
        should work OK if not too many partners.
        parameter safe can be:
        - True: if not found, return value
        - False: if not found throw exception
        - None: if not found, return None
    '''
    for row in botslib.query(
            u'''SELECT ''' + field + '''
                                FROM partner
                                WHERE ''' + field_where_value_is_searched +
            ''' = %(value)s
                                ''', {'value': value}):
        if row[field]:
            return row[field]
    #nothing found in partner table
    if safe is None:
        return None
    elif safe:  #if safe is True
        return value
    else:
        raise botslib.CodeConversionError(
            _(u'No result found for partner lookup; either partner "%(idpartner)s" does not exist or field "%(field)s" has no value.'
              ), {
                  'idpartner': value,
                  'field': field
              })
Ejemplo n.º 2
0
def rcodeconversion(modulename,value):
    ''' as codeconversion but reverses the dictionary first'''
    module,filename = botslib.botsimport('codeconversions',modulename)
    if not hasattr(module,'botsreversed'+'codeconversions'):
        reversedict = dict((value,key) for key,value in module.codeconversions.items())
        setattr(module,'botsreversed'+'codeconversions',reversedict)
    try:
        return module.botsreversedcodeconversions[value]
    except KeyError:
        raise botslib.CodeConversionError(_(u'Value "$value" not in file for reversed codeconversion "$filename".'),value=value,filename=filename)
Ejemplo n.º 3
0
def codeconversion(modulename,value):
    ''' converts code using a codelist.
        converted value is returned. 
        codelist is first imported from file in codeconversions (lookup right place/mudule in bots.ini)
    '''
    module,filename = botslib.botsimport('codeconversions',modulename)
    try:
        return module.codeconversions[value]
    except KeyError:
        raise botslib.CodeConversionError(_(u'Value "$value" not in file for codeconversion "$filename".'),value=value,filename=filename)
Ejemplo n.º 4
0
def rcodetconversion(ccodeid,rightcode,field='leftcode'):
    ''' as codetconversion but reverses the dictionary first'''
    for row in botslib.query(u'''SELECT ''' +field+ '''
                                FROM    ccode
                                WHERE   ccodeid_id = %(ccodeid)s
                                AND     rightcode = %(rightcode)s''',
                                {'ccodeid':ccodeid,
                                 'rightcode':rightcode,
                                }):
        return row[field]
    raise botslib.CodeConversionError(_(u'Value "$value" not in codetconversions, user table "$table".'),value=rightcode,tabel=ccodeid)
Ejemplo n.º 5
0
def codetconversion(ccodeid,leftcode,field='rightcode'):
    ''' converts code using a db-table.
        converted value is returned. 
    '''
    for row in botslib.query(u'''SELECT ''' +field+ '''
                                FROM    ccode
                                WHERE   ccodeid_id = %(ccodeid)s
                                AND     leftcode = %(leftcode)s''',
                                {'ccodeid':ccodeid,
                                 'leftcode':leftcode,
                                }):
        return row[field]
    raise botslib.CodeConversionError(_(u'Value "$value" not in codetconversions, user table "$table".'),value=leftcode,tabel=ccodeid)
Ejemplo n.º 6
0
def reverse_ccode(ccodeid, rightcode, field='leftcode'):
    ''' as ccode but reversed lookup.'''
    for row in botslib.query(
            u'''SELECT ''' + field + '''
                                FROM    ccode
                                WHERE   ccodeid_id = %(ccodeid)s
                                AND     rightcode = %(rightcode)s''', {
                'ccodeid': ccodeid,
                'rightcode': rightcode,
            }):
        return row[field]
    raise botslib.CodeConversionError(
        _(u'Value "$value" not in code-conversion, user table "$table".'),
        value=rightcode,
        table=ccodeid)
Ejemplo n.º 7
0
def reverse_ccode(ccodeid, rightcode, field='leftcode', safe=False):
    ''' as ccode but reversed lookup.'''
    for row in botslib.query(
            u'''SELECT ''' + field + '''
                                FROM ccode
                                WHERE ccodeid_id = %(ccodeid)s
                                AND rightcode = %(rightcode)s''', {
                'ccodeid': ccodeid,
                'rightcode': rightcode
            }):
        return row[field]
    if safe is None:
        return None
    elif safe:
        return rightcode
    else:
        raise botslib.CodeConversionError(
            _(u'Value "%(value)s" not in code-conversion, user table "%(table)s".'
              ), {
                  'value': rightcode,
                  'table': ccodeid
              })
Ejemplo n.º 8
0
def ccode(ccodeid, leftcode, field='rightcode', safe=False):
    ''' converts code using a db-table.
        converted value is returned, exception if not there.
    '''
    for row in botslib.query(
            u'''SELECT ''' + field + '''
                                FROM ccode
                                WHERE ccodeid_id = %(ccodeid)s
                                AND leftcode = %(leftcode)s''', {
                'ccodeid': ccodeid,
                'leftcode': leftcode
            }):
        return row[field]
    if safe is None:
        return None
    elif safe:
        return leftcode
    else:
        raise botslib.CodeConversionError(
            _(u'Value "%(value)s" not in code-conversion, user table "%(table)s".'
              ), {
                  'value': leftcode,
                  'table': ccodeid
              })