This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SerializerCollection(ModelSerializer): | |
class Meta: | |
model = Collection | |
fields = ['id', 'name_collection','collector'] | |
read_only_fields = ['collector'] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HistoricoManager(models.Manager): | |
def get_queryset(self): | |
qs = super().get_queryset() | |
qs.annotate(_valor_total=models.Sum('conta_set__valor')) | |
return qs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyDateInput(forms.DateInput): | |
input_type = 'date' | |
def __init__(self, **kwargs): | |
super().__init__(format='%Y-%m-%d') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), |