from _common.models import ActionButtons from _common.models import Table from django.db import models class UPPER_CASE(models.Model): nome = models.CharField(max_length=100) descricao = models.TextField(blank=True, null=True) tenant = models.ForeignKey('tenantapp.Tenant', on_delete=models.CASCADE) criado_em = models.DateField(auto_now_add=True, auto_now=False) def __str__(self): return self.nome class UPPER_CASECrudList(): def get_data(self, request): data = {} data['icon'] = 'fas fa-tools' data['title'] = 'Lista de UPPER_CASE_PLURAL' data['btn_label'] = 'Novo UPPER_CASE' data['btn_link'] = 'LOWER_CASE_create' data['table'] = self.generate_table(request) return data def generate_table(self, request): table = {} if request.user.is_superadmin(): LOWER_CASE_PLURAL = UPPER_CASE.objects.select_related('tenant').order_by('-criado_em') else: LOWER_CASE_PLURAL = UPPER_CASE.objects.filter(tenant=request.user.tenant).order_by('-criado_em') table['rows'] = [] for LOWER_CASE in LOWER_CASE_PLURAL: buttons = [("edit", "/LOWER_CASE/update/" + str(LOWER_CASE.id)), ("delete", "/LOWER_CASE/delete/" + str(LOWER_CASE.id))] aux = {} aux['id'] = LOWER_CASE.id aux['nome'] = LOWER_CASE.nome aux['descricao'] = LOWER_CASE.descricao aux['action'] = ActionButtons.generate(buttons) table['rows'] += [aux] table['labels'] = ['Id', 'Nome', 'Descrição', ''] table['alignment'] = [1, 1, 1, 2] tbl = Table(table['labels'], table['rows'], table['alignment']) table['body'] = tbl.getHTML() return table