Skip to content

Instantly share code, notes, and snippets.

@marcelcaraciolo
Created February 25, 2015 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcelcaraciolo/b7aa1cf3c60dae85785b to your computer and use it in GitHub Desktop.
Save marcelcaraciolo/b7aa1cf3c60dae85785b to your computer and use it in GitHub Desktop.
Simple admin example
# -*- coding: utf-8 -*-
import xlwt
from django.contrib import admin
from django.http import HttpResponse
from models import (PaymentForm, Order, OrderItem,
Discount, Affiliate)
from forms import DiscountForm
def report(modeladmin, request, queryset):
resp = HttpResponse(mimetype='application/vnd.ms-excel')
resp['Content-Disposition'] = u'attachment; filename=Vendas.xls'
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('Vendas')
worksheet.write(0, 0, u'Id')
worksheet.write(0, 1, u'Usuário')
worksheet.write(0, 2, u'Forma de Pagamento')
worksheet.write(0, 3, u'Status')
worksheet.write(0, 4, u'Data')
worksheet.write(0, 5, u'Produtos')
worksheet.write(0, 6, u'Desconto')
worksheet.write(0, 7, u'Valor Total')
x = 1
for order in queryset:
worksheet.write(x, 0, order.id)
worksheet.write(x, 1, unicode(order.user))
worksheet.write(x, 2, unicode(order.payment))
worksheet.write(x, 3, order.status_description)
worksheet.write(x, 4, order.created_on,
style=xlwt.easyxf(num_format_str='dd/mm/yyyy hh:mm:ss'))
worksheet.write(x, 5, u', '.join(order.products()))
worksheet.write(x, 6, order.discount, style=xlwt.easyxf("", "#,###.00"))
worksheet.write(x, 7, order.total, style=xlwt.easyxf("", "#,###.00"))
x = x + 1
workbook.save(resp)
return resp
report.short_description = u'Relatório de Vendas'
class OrderAdmin(admin.ModelAdmin):
list_display = ['id', 'user', 'payment', 'status', 'created_on']
list_filter = ['status', 'created_on']
actions = [report]
admin.site.register(Order, OrderAdmin)
class DiscountAdmin(admin.ModelAdmin):
list_display = ['code', 'value_description',
'order', 'created_on']
form = DiscountForm
admin.site.register(Discount, DiscountAdmin)
class AffiliateAdmin(admin.ModelAdmin):
list_display = ['name', 'code', 'created_on']
admin.site.register(Affiliate, AffiliateAdmin)
admin.site.register([PaymentForm, OrderItem])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment