From bba45f736d39d3a92904088efbe6100a4db5cfdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Siz=C3=ADlio?= Date: Tue, 24 Apr 2018 09:54:32 -0300 Subject: [PATCH] =?UTF-8?q?Gerando=20relat=C3=B3rio=20por=20catadores=20se?= =?UTF-8?q?lecionados?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + app_site/api/admin.py | 77 +++++++++++++++++++ .../admin/api/catador/change_list.html | 2 +- 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 4797810..d426813 100644 --- a/.gitignore +++ b/.gitignore @@ -57,6 +57,7 @@ target/ venv/ env/ env34/ +.vEnv/ # cache results from py.test integration_test/.cache diff --git a/app_site/api/admin.py b/app_site/api/admin.py index d1fe872..35f6efc 100644 --- a/app_site/api/admin.py +++ b/app_site/api/admin.py @@ -120,6 +120,83 @@ def get_registered_by_another_user(self, obj): get_registered_by_another_user.short_description = 'Cadastrado por' + def export_xls(self, request, queryset): + import xlwt + from django.shortcuts import get_object_or_404, HttpResponse + response = HttpResponse(content_type='application/ms-excel') + response['Content-Disposition'] = 'attachment; filename="catadores.xls"' + + wb = xlwt.Workbook(encoding='utf-8') + ws = wb.add_sheet('Catadores') + + # Sheet header, first row + row_num = 0 + + font_style = xlwt.XFStyle() + font_style.font.bold = True + + columns = ['pk', 'Nome', 'Apelido', 'Telefone(s)', + 'Possui foto?', 'Lat/Long', 'Cidade', + 'Endereço onde costuma trabalhar', + 'Número', 'Bairro', 'Frase de apresentação', + 'Cadastrado por'] + + for col_num in range(len(columns)): + ws.write(row_num, col_num, columns[col_num], font_style) + + # Sheet body, remaining rows + font_style = xlwt.XFStyle() + + db_columns = ['pk', 'name', 'nickname', 'phones', 'avatar', 'georef', + 'city', 'address_base', 'number', 'address_region', + 'presentation_phrase', 'registered_by_another_user'] + + rows = queryset + + for row in rows: + row_num += 1 + count = 0 + for col in db_columns: + if col in ['pk', 'name', 'nickname', 'city', 'address_base', + 'number', 'address_region', 'presentation_phrase']: + ws.write(row_num, count, row.__getattribute__(col), font_style) + else: + value = '' + if col == 'phones': + try: + value = ', '.join([p.phone for p in row.phones]) + except: + value = 'Não informado' + + if col == 'avatar': + try: + value = 'Sim' if row.user.userprofile.avatar else 'Não' + except: + value = 'Não' + + if col == 'georef': + try: + geo = GeorefCatador.objects.get(catador_id=row.id) + if geo: + value = str(geo.georef.latitude) + ', ' + str(geo.georef.longitude) + except: + value = 'Não informado' + + if col == 'registered_by_another_user': + value = row.another_user_name if row.registered_by_another_user else 'Próprio catador' + + ws.write(row_num, count, value, font_style) + + count += 1 + + wb.save(response) + return response + + export_xls.short_description = 'Exportar selecionados para Excel' + + actions = [export_xls] + + # USER class UserProfileInline(admin.StackedInline): diff --git a/app_site/templates/admin/api/catador/change_list.html b/app_site/templates/admin/api/catador/change_list.html index 4754514..940f2c2 100644 --- a/app_site/templates/admin/api/catador/change_list.html +++ b/app_site/templates/admin/api/catador/change_list.html @@ -61,7 +61,7 @@ {% endblock %}
  • - Exportar para Excel + Exportar todos para Excel