Skip to content

Instantly share code, notes, and snippets.

View icarovirtual's full-sized avatar
🐕
É o Bubu Show

Ícaro icarovirtual

🐕
É o Bubu Show
View GitHub Profile
// Deixar vazio, preencher caso deseje testar uma nota
const CHAVE_FORCADA = ""
// Para substituir múltiplos espaços
const MULTI_ESPACOS_REGEX = /\s{2,}/g;
// Para substituir um espaço
const UNI_ESPACO_REGEX = /\s{1}/g;
/** Pela assinatura da função executa ao abrir a planilha. */
function onOpen() {
SpreadsheetApp.getUi()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Articles</title>
</head>
<body>
<h3>Filter</h3>
<form method="get">
class BaseFilterView:
# List of admin filters
filters = [StatusFilter]
def get_form_kwargs(self):
form_kwargs = super().get_form_kwargs()
# Pass the filters to the form
form_kwargs['filters'] = self.filters
return form_kwargs
class BaseFilterForm(forms.Form):
def __init__(self, *args, **kwargs):
# Receive the filter instances from the view
filters = kwargs.pop('filters', [])
super(BaseFilterForm, self).__init__(*args, **kwargs)
for f in filters:
# Create dynamic form fields from the filter
self.fields[f.parameter_name] = \
forms.ChoiceField(label=f.title, choices=[('', "Select")] + f._lookups(), required=False)
class StatusFilter(admin.SimpleListFilter):
# Change your lookups function and include the class method
def lookups(self, request, model_admin):
return self._lookups()
@classmethod
def _lookups(cls):
# Static choices that will be used in the forms
return Article.Status.choices
class Article(models.Model):
class Status(models.TextChoices):
DRAFT = '0', "Draft"
PUBLISHED = '1', "Published"
REMOVED = '2', "Removed"
status = models.CharField(choices=Status.choices, max_length=1)
class StatusFilter(admin.SimpleListFilter):
@icarovirtual
icarovirtual / admin_mixins.py
Created October 19, 2019 13:04
copy_queryset_without_annotations: mixins
class PaginatorWithOptimizedCount(Paginator):
@cached_property
def count(self):
try:
# First usage of optimization function
return copy_queryset_without_annotations(self.object_list).count()
except (AttributeError, TypeError):
return len(self.object_list)
@icarovirtual
icarovirtual / copy_queryset_without_annotations.py
Last active October 19, 2019 13:38
copy_queryset_without_annotations: final investigation
def copy_queryset_without_annotations(original_qs):
# Create a new queryset based on the same model
optimized_qs = original_qs.model.objects.all()
# Copy the filters
optimized_qs.query.where = original_qs.query.where
optimized_qs.query.where_class = original_qs.query.where_class
return optimized_qs
@icarovirtual
icarovirtual / guard_clauses.py
Last active August 11, 2019 18:01
guard clauses: longer example
def func_not_guarded(self, param):
if param == 'something':
self.counter += 1
if self.counter > 10:
self.reached_ten()
else:
if self.counter < 5:
self.has_not_reached_5()
else:
self.has_not_reached_5()
@icarovirtual
icarovirtual / is_platypus.py
Last active February 26, 2021 13:15
guard clauses: good example
def is_platypus(self):
# Not a platypus for everything below
if not self.is_mammal():
return False
if not self.has_fur():
return False
if not self.has_beak():
return False
if not self.has_tail():
return False