350 else: |
350 else: |
351 sql += " WHERE domainname = '%s'" % pattern |
351 sql += " WHERE domainname = '%s'" % pattern |
352 sql += ' ORDER BY is_primary DESC, domainname' |
352 sql += ' ORDER BY is_primary DESC, domainname' |
353 dbc = dbh.cursor() |
353 dbc = dbh.cursor() |
354 dbc.execute(sql) |
354 dbc.execute(sql) |
355 doms = dbc.fetchall() |
355 result = dbc.fetchall() |
356 dbc.close() |
356 dbc.close() |
357 |
357 |
358 domdict = {} |
358 gids = [domain[0] for domain in result if domain[2]] |
359 order = [dom[0] for dom in doms if dom[2]] |
359 domains = {} |
360 if not order: |
360 for gid, domain, is_primary in result: |
361 for dom in doms: |
|
362 if dom[0] not in order: |
|
363 order.append(dom[0]) |
|
364 for gid, dom, is_primary in doms: |
|
365 if is_primary: |
361 if is_primary: |
366 domdict[gid] = [dom] |
362 if not gid in domains: |
|
363 domains[gid] = [domain] |
|
364 else: |
|
365 domains[gid].insert(0, domain) |
367 else: |
366 else: |
368 try: |
367 if gid in gids: |
369 domdict[gid].append(dom) |
368 if gid in domains: |
370 except KeyError: |
369 domains[gid].append(domain) |
371 domdict[gid] = [None, dom] |
370 else: |
372 del doms |
371 domains[gid] = [domain] |
373 return order, domdict |
372 else: |
|
373 gids.append(gid) |
|
374 domains[gid] = [None, domain] |
|
375 return gids, domains |
374 |
376 |
375 |
377 |
376 def get_gid(dbh, domainname): |
378 def get_gid(dbh, domainname): |
377 """Returns the group id of the domain *domainname*. |
379 """Returns the group id of the domain *domainname*. |
378 |
380 |