def downloadFamMatches(): import StringIO output = StringIO.StringIO() done = set() fileFormat = bottle.request.query.fileFormat titleRow = [bottle.request.query.workDB,'','','',bottle.request.query.matchDB,'',''] if fileFormat == 'xlsx': from openpyxl import Workbook from openpyxl.writer.write_only import WriteOnlyCell from openpyxl.styles import colors from openpyxl.styles import Border, Side, Font, Color, PatternFill, Alignment from openpyxl.utils import get_column_letter wb = Workbook(write_only=True) ws = wb.create_sheet() ws.title = "RGD matches" greyFill = PatternFill(start_color='DDDDDD', end_color='DDDDDD', fill_type='solid') greenFill = PatternFill(start_color='50FF50', end_color='50FF50', fill_type='solid') yellowFill = PatternFill(start_color='FFFF50', end_color='FFFF50', fill_type='solid') redFill = PatternFill(start_color='FF5050', end_color='FF5050', fill_type='solid') thick = Side(border_style="thick", color="000000") thin = Side(border_style="thin", color="000000") rowVals = [] for val in titleRow: cell = WriteOnlyCell(ws, value=val.encode("utf-8")) cell.font = Font(bold=True) rowVals.append(cell) ws.append(rowVals) else: import csv CSV = csv.writer(output, dialect='excel') CSV.writerow([s.encode("utf-8") for s in titleRow]) for fmatch in common.config['fam_matches'].find({'status': {'$in': list(common.statOK.union(common.statManuell))}}): rows = famDisp(None, None, fmatch) line = False lines=0 for r in rows: lines += 1 if lines == 1: continue try: k1 = r[1].split('<br/>')[1] except: k1 = '' try: k2 = r[5].split('<br/>')[1] except: k2 = '' key = k1+';'+k2 done.add(key) if 'Ignorerad' in r[0]: ignRelI = True else: ignRelI = False if 'Ignorerad' in r[8]: ignRelII = True else: ignRelII = False ignFam = False #remove html-code for buttons try: if 'Ignorerad' in r[4]: ignFam = True r[4] = r[4].split('<')[0] if ign: r[4] += ' Ignorerad' except: pass if r == ['', '', '', '', '', '', '', '', '']: line = True if fileFormat == 'xlsx': continue if fileFormat == 'xlsx': rowVals = [] i=0 green = False yellow = False red = False #if r[4].endswith(('EjMatch', 'EjOK', 'rEjOK')): red = True #elif r[4].endswith(('Manuell', 'rManuell')): yellow = True #elif r[4].endswith(('Match', 'OK', 'rOK')): green = True if ('EjMatch' in r[4]) or ('EjOK' in r[4]): red = True elif ('Match' in r[4]) or ('OK' in r[4]): green = True elif 'Manuell' in r[4]: yellow = True for val in r[1:8]: i+=1 if i == 4: #separator between workDB and matchDB cell = WriteOnlyCell(ws, value=val) cell.border = Border(top=thin, left=thick, right=thick, bottom=thin) else: cell = WriteOnlyCell(ws, value=val.replace('<br/>', "\n").rstrip().encode("utf-8")) cell.alignment = Alignment(wrapText=True) cell.border = Border(top=thin, left=thin, right=thin, bottom=thin) if lines <=2: #head cell.font = Font(bold=True) cell.border = Border(top=thick, left=thin, right=thin, bottom=thick) cell.fill = greyFill elif line: if i == 4: #separator between workDB and matchDB cell.border = Border(top=thick, left=thick, right=thick, bottom=thin) else: cell.border = Border(top=thick, left=thin, right=thin, bottom=thin) if (i == 4) and ignFam: cell.fill = redFill elif (i==1) and ignRelI: cell.fill = redFill elif (i==8) and ignRelII: cell.fill = redFill elif green: cell.fill = greenFill elif yellow: cell.fill = yellowFill elif red: cell.fill = redFill rowVals.append(cell) #line = False ws.append(rowVals) else: CSV.writerow([s.replace('<br/>', "\n").rstrip().encode("utf-8") for s in r]) if fileFormat != 'xlsx': CSV.writerow(['#####', '#####', '#####', '#####', '#####', '|', '#####', '#####', '#####']) #List matched persons without families for persmatch in common.config['matches'].find({'status': {'$in': list(common.statOK.union(common.statManuell))}}): if persmatch['pwork']['refId']+';'+persmatch['pmatch']['refId'] in done: continue rows = [] rows.append(['', u'Namn/refId', u'Född', u'Död', '', u'Namn/refId', u'Född', u'Död', '']) rows.append(persMatchDisp('Person', persmatch)) head = True for r in rows: #remove html-code for buttons try: r[4] = r[4].split('<')[0] except: pass if fileFormat == 'xlsx': rowVals = [] i=0 green = False yellow = False red = False if r[4].endswith(('EjMatch', 'EjOK', 'rEjOK')): red = True elif r[4].endswith(('Manuell', 'rManuell')): yellow = True elif r[4].endswith(('Match', 'OK', 'rOK')): green = True for val in r[1:8]: i+=1 if i==4: #separator between workDB and matchDB cell = WriteOnlyCell(ws, value=val) cell.border = Border(top=thin, left=thick, right=thick, bottom=thin) else: cell = WriteOnlyCell(ws, value=val.replace('<br/>', "\n").rstrip().encode("utf-8")) cell.alignment = Alignment(wrapText=True) cell.border = Border(top=thin, left=thin, right=thin, bottom=thin) if head: cell.font = Font(bold=True) cell.border = Border(top=thick, left=thin, right=thin, bottom=thick) cell.fill = greyFill if green: cell.fill = greenFill elif yellow: cell.fill = yellowFill elif red: cell.fill = redFill rowVals.append(cell) head = False line = False ws.append(rowVals) else: CSV.writerow([s.replace('<br/>', "\n").rstrip().encode("utf-8") for s in r]) if fileFormat != 'xlsx': CSV.writerow(['#####', '#####', '#####', '#####', '#####', '|', '#####', '#####', '#####']) # if fileFormat == 'xlsx': #doesn't really work? for i, column_width in enumerate([8,10,25,25,25,1,25,25,25]): ws.column_dimensions[get_column_letter(i+1)].width = (column_width + 2) * 1.2 wb.save(output) #Download filn = 'RGD_' + bottle.request.query.workDB + '-' + bottle.request.query.matchDB + '.' + fileFormat response.add_header('Expires', '0') response.add_header('Cache-Control', "must-revalidate, post-check=0, pre-check=0") response.set_header('Content-Type', "application/force-download") response.add_header('Content-Type', "application/octet-stream") response.add_header('Content-Type', "application/download") response.add_header('Content-Disposition', 'attachment; filename='+filn) response.add_header('Content-Transfer-Encoding', 'binary') return output.getvalue()