Skip to content

Instantly share code, notes, and snippets.

View marctc's full-sized avatar

Marc Tudurí marctc

View GitHub Profile
@marctc
marctc / System Design.md
Last active November 25, 2021 12:30 — forked from vasanthk/System Design.md
System Design Cheatsheet

System Design Cheatsheet

To Remember

  • Have they gathered requirements?
  • Have they defined the systems interface?
  • Have they done scaling/capacity estimation?
  • Does the design actually work?
  • reliable and maintainable?
  • Is the design feasible to implement - fit within our practical constraints (budget, time, etc.)??
@marctc
marctc / filters.py
Created October 5, 2017 13:49
Wildcard filter
import django_filters
class WildcardFilter(django_filters.Filter):
@staticmethod
def process_wildcard_value(value):
"""
Given a filter value in wildcard mode it returns the clean value and the need lookup
Ex:
class ContentTypeField(serializers.Field):
def to_representation(self, value):
return value.model
def to_internal_value(self, data):
try:
return ContentType.objects.get(app_label='core', model=data)
except ContentType.DoesNotExist:
raise ValidationError("'{}' model does not exist".format(data))
def test_are_remaining_box_codes(self, box, box_codes):
"""
There are remaining box codes on the installation and so availability
"""
with patch('django.db.models.fields.related_descriptors.ForwardManyToOneDescriptor.__get__'):
qs = QuerySetMock(BoxCode, *box_codes)
box.box_codes.return_value = qs
assert box.paddle.are_remaining_box_codes()
class NamedModelChoiceFilter(django_filters.ModelMultipleChoiceFilter):
def __init__(self, *args, **kwargs):
super(NamedModelChoiceFilter, self).__init__(*args, **kwargs)
self.field.widget.attrs['data-placeholder'] = self.label
class DetailPagination(PageNumberPagination):
page_size_query_param = 'page_size'
def get_paginated_response(self, data):
return Response({
'next': self.get_next_link(),
'previous': self.get_previous_link(),
'count': self.page.paginator.count,
'current_page': self.page.number,
'num_pages': self.page.paginator.num_pages,
@marctc
marctc / drf.py
Last active October 5, 2015 10:04
# views
class BaseAPIView(APIView):
serializer_valid_function_name = 'process'
def get_serializer(self, data, request):
return self.serializer_class(data=data, context={'request': request})
def perform_request(self, request, **kwargs):
data = request.data.copy()
# -*- coding: utf-8 -*-
import bleach
from django.db import models
class ModelSanitizerMixin(object):
allowed_tags = ['p', 'em', 'strong', 'h1', 'br', 'ul', 'ol', 'li', 'a']
# -*- coding: utf-8 -*-
import os
from django.core.files.storage import FileSystemStorage
from django.utils.text import slugify
class SlugFileSystemStorage(FileSystemStorage):
def get_valid_name(self, name):
class BasePrimaryKeyField(IntegerField):
model = None
def to_representation(self, value):
return value.id
def to_internal_value(self, data):
try:
return self.model.objects.get(pk=data)
except self.model.DoesNotExist: