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
# Leaky bucket implementation from: | |
# https://blog.callr.tech/rate-limiting-for-distributed-systems-with-redis-and-lua/ | |
LEAKY_BUCKET_SCRIPT = b""" | |
local ts = tonumber(ARGV[1]) -- current timestamp | |
local cps = tonumber(ARGV[2]) -- calls per second | |
local key = KEYS[1] | |
-- remove tokens older than 1 second ago | |
local min = ts - 1 |
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
User.objects.filter( | |
Q(internal_groups__id=1) & | |
~Q( | |
enrollments__event__id=15, | |
enrollments__pre_status=ENROLLMENT_PRE_STATUS.going, | |
) | |
) |
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 import transaction, connection | |
from events.models import Event | |
def count_events(): | |
with transaction.atomic(): | |
print(Event.objects.count()) | |
input() |
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
# serializers.py | |
from django.contrib.auth.models import User | |
from rest_framework import serializers | |
from bikes.models import Bike | |
class BikeSerializer(serializers.ModelSerializer): | |
class Meta: | |
model = Bike |
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
spinners = ( | |
Spinner.objects | |
.using(request.customer.name) | |
.annotate( | |
avg_duration=Avg('owned_spinners__spins__duration')) | |
.order_by('-avg_duration')) |
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.utils import timezone | |
from myapp.models import Event | |
class EventListView(generics.ListView): | |
def get_queryset(self): | |
now = timezone.now() | |
upcoming = Event.objects.filter(date__gte=now).order_by('date') | |
passed = Event.objects.filter(date__lt=now).order_by('-date') |
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
import mock | |
from django.test import TestCase | |
from django.contrib.auth.models import User | |
class EmailTests(TestCase): | |
def setUp(self): | |
self.user = User.objects.create( | |
username='theusername', email='the@email.com', | |
first_name='Filipe', last_name='Ximenes', |
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 import models | |
class Bike(models.Model): | |
COLOR_OPTIONS = (('yellow', 'Yellow'), ('red', 'Red'), ('black', 'Black')) | |
color = models.CharField(max_length=255, null=True, blank=True, | |
choices=COLOR_OPTIONS) | |
size = models.DecimalField(max_digits=4, decimal_places=2, null=True, blank=True) |
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 categorize(lst, attrs): | |
""" | |
Generate a dictionary of lists where the keys are generated from | |
some specified attribute of the items in the list | |
""" | |
def dive_in(obj, attrs, index=0): | |
if not obj or index >= len(attrs): | |
return obj | |
new_obj = getattr(obj, attrs[index]) | |
if len(attrs) > 0: |
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
url(r'^example/$', 'account.views.example_view', name='user_info'), |
NewerOlder