Skip to content

Instantly share code, notes, and snippets.

@jordic
Last active September 12, 2019 14:07
Show Gist options
  • Save jordic/ba1d3cc27eefdedfae94 to your computer and use it in GitHub Desktop.
Save jordic/ba1d3cc27eefdedfae94 to your computer and use it in GitHub Desktop.
ModelAdmin + Changelist as ClassBased views
'''
Class Based changelist, composition
'''
class MisOfertas(ModelAdmin):
list_display = ( 'get_estado', 'nombre', 'origen_', 'destino_', 'owner_',
'caducidad', 'editar', 'borrar')
list_filter = ( 'status', CargadorFilter, OrigenFilter, DestinoFilter )
search_fields = ( 'nombre', 'owner__business_name', 'owner__comercial_name',
'origen__codigo', 'origen__parent__codigo')
change_list_template = ('oferta/mis_ofertas.html')
list_display_links = ('nombre', 'editar')
list_per_page = 15
def origen_(self,obj):
#pais = obj.origen.get_ancestors(ascending=True, include_self=False)
pais = self.paisos[obj.origen.parent_id]
return "<span class='flag flag-%s'></span> %s%s" % (pais.codigo,
pais.codigo.upper(), obj.origen)
origen_.admin_order_field = 'origen'
origen_.allow_tags = True
def destino_(self,obj):
destino = self.paisos[obj.destino_id]
return "<span class='flag flag-%s'></span> %s" % (destino.codigo,destino.codigo.upper())
destino_.admin_order_field = 'destino'
destino_.allow_tags = True
def owner_(self, obj):
return "<a href='%s'>%s</a>" % (
reverse('view_empresa_directorio', args=[obj.owner_id]),
obj.owner )
owner_.admin_order_field = 'owner'
owner_.allow_tags = True
owner_.short_description = _(u'Cargador')
def has_change_permission(self, request, obj=None):
self.empresa = request.user.empresa
if self.empresa.is_cargador():
return None
return True
def get_estado( self, object ):
icon = ('ion-close-circled', 'ion-checkmark-circled')
return "<span class='%s icon'> </span>" % icon[ object.status ]
get_estado.allow_tags = True
get_estado.short_description = _(u'Estado')
def editar(self,obj):
return "<span class='action_icon ion-compose'></span>"
editar.allow_tags = True
editar.list_editable = True
editar.short_description = _(u'Ver')
def borrar(self,obj):
return "<a href=%s class='doffer'><span class='action_icon ion-trash-b'></span><a/>" % (
reverse('delete_oferta', args=(obj.pk,)))
borrar.allow_tags = True
borrar.list_editable = True
def get_changelist(self, request, **kwargs):
class VistaCanvis( ChangeList ):
def url_for_result(self, result):
pk = getattr(result, self.pk_attname)
return reverse('view_oferta', args=[pk])
return VistaCanvis
def queryset(self, request):
''' nomes veu les seves ofertes '''
empresa = self.empresa
self.paisos = get_countries_key()
import datetime
d = datetime.date.today()
return self.model.objects.filter(empresa=empresa).filter(
Q(caducidad__gte=d) | Q(caducidad__isnull=True) ).select_related()
'''
El default site, e
rom django.contrib.admin.views.main import ChangeList
from django.contrib.admin.sites import AdminSite
class DefaultSite(AdminSite):
def has_permission(self, request):
return True
'''
def mis_ofertas(request):
l = MisOfertas( Oferta, DefaultSite() )
return l.changelist_view( request )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment