def rnd(request): # return HttpResponse('RnD')
obj_list = obj_list.filter(item__sku__contains=sku) if request.GET['is_real_qty']: is_real_qty = int(request.GET['is_real_qty'].strip()) if is_real_qty == 0: obj_list = obj_list.filter(real_qty=0) else: obj_list = obj_list.filter(real_qty__gt=0) if request.GET['is_purchase']: is_purchase = int(request.GET['is_purchase'].strip()) obj_list = obj_list.filter(is_purchase=is_purchase) if request.GET['created_from'] and request.GET['created_to']: try: time.strptime(request.GET['created_from'],"%Y-%m-%d") time.strptime(request.GET['created_to'],"%Y-%m-%d") except Exception, e: return HttpResponse('请检查时间格式!') date_from = request.GET['created_from'] date_to = request.GET['created_to'] obj_list = obj_list.filter(created__gte=date_from).filter(created__lte=date_to) elif request.GET['created_from']: try: time.strptime(request.POST['created_from'],"%Y-%m-%d") except Exception, e: return HttpResponse('请检查时间格式!') date_from = request.GET['created_from'] date_to = datetime.datetime.strptime(date_from, "%Y-%m-%d") + datetime.timedelta(days=1) date_to = str(date_to) obj_list = obj_list.filter(created__gte=date_from).filter(created__lte=date_to) if request.GET['per_num']:
def export(self, request): if not request.user.is_superuser: return HttpResponse(status=403) if request.method == 'POST': form = ExportForm(request.POST) if form.is_valid(): backend_url = settings.BACKEND_BASE_URL ddjj_url = backend_url + 'admin/admin_ddjj_app/ddjj/' date_from = form.cleaned_data['date_from'] date_to = form.cleaned_data['date_to'] # http://djangotricks.blogspot.com.ar/2013/12/how-to-export-data-as-excel.html response = HttpResponse(content_type='text/csv') response[ 'Content-Disposition'] = 'attachment; filename=ddjj_{0}_{1}.csv'.format( date_from, date_to) response['Cache-Control'] = 'no-cache' import sys reload(sys) sys.setdefaultencoding("utf-8") import unicodecsv writer = unicodecsv.writer(response, csv.excel, delimiter='|', quotechar='"', quoting=csv.QUOTE_ALL, encoding='utf-8') writer.writerow([ smart_str(u"DDJJ ID"), smart_str(u"DDJJ Admin"), smart_str(u"DDJJ Status"), smart_str(u"DDJJ Tipo"), smart_str(u"DDJJ Funcionario Nombre"), smart_str(u"DDJJ Funcionario Cargo"), smart_str(u"DDJJ Funcionario Jurisdiccion"), smart_str(u"DDJJ Poder"), smart_str(u"DDJJ Presenta"), smart_str(u"DDJJ Carta URL"), smart_str(u"DDJJ Observaciones Publicas"), smart_str(u"DDJJ Observaciones Privadas"), smart_str(u"Bien ID"), smart_str(u"Bien Vinculo"), smart_str(u"Bien Persona"), smart_str(u"Bien Tipo"), smart_str(u"Bien Nombre"), smart_str(u"Bien Moneda Valor Fiscal"), smart_str(u"Bien Valor Fiscal"), smart_str(u"Bien Porcentaje"), smart_str(u"Bien Fecha Desde"), smart_str(u"Bien Fecha Hasta"), smart_str(u"Bien Pais"), smart_str(u"Bien Origen"), smart_str(u"Bien Destino"), smart_str(u"Bien Descripcion"), smart_str(u"Bien Observaciones"), smart_str(u"Usuario Username"), smart_str(u"Usuario Nombre"), smart_str(u"Organizaciones"), smart_str(u"DDJJ Fecha Creacion"), smart_str(u"DDJJ Fecha Modificacion") ]) from django.db import connection ddjj_content_type = ContentType.objects.get(model='ddjj').id cursor = connection.cursor() query = """ SELECT d.id, CONCAT(%s, d.id), CASE WHEN d.status=0 THEN 'No publicada' ELSE 'Publicada' END, CASE WHEN d.tipo_ddjj_id=0 THEN 'Alta' WHEN d.tipo_ddjj_id=1 THEN 'Baja' WHEN d.tipo_ddjj_id=2 THEN 'Inicial' ELSE 'Anual' END, CONCAT(p.nombre, ' ', p.apellido), c.cargo, j.nombre, CASE WHEN j.poder_id=0 THEN 'Ejecutivo' WHEN j.poder_id=1 THEN 'Legislativo' WHEN j.poder_id=2 THEN 'Judicial' ELSE 'Otro' END, d.flag_presenta, d.url, REPLACE(REPLACE(d.obs, '\n', ' '), '\r', ''), REPLACE(REPLACE(d.obs_privadas, '\n', ' '), '\r', ''), b.id, b.vinculo, CONCAT(pb.nombre, ' ', pb.apellido), tb.nombre, nb.nombre, CASE WHEN b.m_valor_fiscal_id=0 THEN '$' WHEN b.m_valor_fiscal_id=1 THEN 'us$' WHEN b.m_valor_fiscal_id=2 THEN 'E' WHEN b.m_valor_fiscal_id=3 THEN 'Uruguayos' WHEN b.m_valor_fiscal_id=4 THEN '£' WHEN b.m_valor_fiscal_id=5 THEN 'A' WHEN b.m_valor_fiscal_id=6 THEN 'A$' WHEN b.m_valor_fiscal_id=7 THEN '$L' ELSE 'No especifica' END, b.valor_fiscal, b.porcentaje, b.fecha_desde, b.fecha_hasta, b.pais, b.origen, b.destino, REPLACE(REPLACE(b.descripcion, '\n', ' '), '\r', ''), REPLACE(REPLACE(b.obs, '\n', ' '), '\r', ''), au.username, g.groups, CONCAT(au.first_name, ' ', au.last_name), d.created_at, d.updated_at FROM ddjjs d, personas p, persona_cargos pc, cargos c, jurisdiccions j, (SELECT dl.user_id as user_id, dl.object_id as ddjj_id FROM django_admin_log dl, (SELECT MAX(action_time) as action_time, content_type_id, object_id FROM django_admin_log GROUP by content_type_id, object_id) dl_max WHERE dl_max.action_time = dl.action_time AND dl_max.content_type_id = dl.content_type_id AND dl_max.object_id = dl.object_id) logentry, auth_user au, biens b, personas pb, tipo_biens tb, (SELECT GROUP_CONCAT(ag.name) as groups, aug.user_id as user_id FROM auth_group ag, auth_user_groups aug WHERE aug.group_id = ag.id GROUP by aug.user_id) g, nombre_biens nb WHERE d.persona_id = p.id AND d.persona_cargo_id = pc.id AND c.id = pc.cargo_id AND pc.jurisdiccion_id = j.id AND logentry.ddjj_id = d.id AND logentry.user_id = au.id AND b.ddjj_id = d.id AND b.persona_id = pb.id AND b.nombre_bien_id = nb.id AND b.tipo_bien_id = tb.id AND g.user_id = au.id AND d.created_at >= %s AND d.created_at <= %s ORDER BY d.created_at """ cursor.execute(query, [ddjj_url, date_from, date_to]) writer.writerows(cursor) return response else: form = ExportForm() context = dict(self.admin_site.each_context(), form=form) return TemplateResponse(request, "admin/export.html", context)