Skip to content

Instantly share code, notes, and snippets.

View walison17's full-sized avatar

Walison Filipe walison17

View GitHub Profile
class SlugFilterBackend(filters.BaseFilterBackend):
def filter_queryset(self, request, queryset, view):
slug = request.query_params.get('slug')
if slug:
for s in slug.split(','):
queryset.filter(slug=s)
return queryset
from functools import wraps
def check_recaptcha(function):
@wraps(function)
def wrapped(request, *args, **kwargs):
request.recaptcha_is_valid = None
if request.method == 'POST':
recaptcha_response = request.POST.get('g-recaptcha-response')
def populate(request):
PERSONS_ADD = (
{'cpf_cnpj': '00.000.000/3875-09','name': 'BANCO DO BRASIL S.A'},
{'cpf_cnpj': '00.000.320/6413-68','name': 'JOSE CELIO GURGEL DE CASTRO'},
{'cpf_cnpj': '00.000.885/0001-29','name': 'BRASAL BRASÍLIA SERVIÇOS AUTOMOTORES S/A'},
{'cpf_cnpj': '00.004.309/0001-50','name': 'DF VEICULOS LTDA'},
{'cpf_cnpj': '00.025.131/0001-23','name': 'RDA Com e Rep. e Imp. de Mat. Elet. S/A'}
)
objs = (Person(**attrs) for attrs in PERSONS_ADD)
tags = HomeTag.objects.filter(active=True)
sections = []
for t in tags:
sections.append({
'tag': t,
'products': Product.objects.filter(tags__name=t.tag)[:4]
})
sections = [
def validate_user(user_id):
try:
User.objects.get(pk=user_id)
except User.DoesNotExist:
raise forms.ValidationError('Usuário inválido')
class CriarReserva(forms.ModelForm):
usuario_id = forms.IntegerField(validators=[validate_user])
class SerializerCollection(ModelSerializer):
class Meta:
model = Collection
fields = ['id', 'name_collection','collector']
read_only_fields = ['collector']
class HistoricoManager(models.Manager):
def get_queryset(self):
qs = super().get_queryset()
qs.annotate(_valor_total=models.Sum('conta_set__valor'))
return qs
def get_ip_from_request(request):
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
return x_forwarded_for.split(',')[0]
return request.META.get('REMOTE_ADDR')
class MyDateInput(forms.DateInput):
input_type = 'date'
def __init__(self, **kwargs):
super().__init__(format='%Y-%m-%d')
from django.db.models import Case, Value, When, F
from django.db.models.functions import Cast
result = (
Live
.objects
.annotate(
like_frequence=Case(
When(like__gt=0, then=Cast('unlike', models.DecimalField()) / F('like')),
default=Value(0),