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 import forms | |
from django.conf import settings | |
from crispy_forms.bootstrap import Tab, TabHolder | |
from modeltranslation.fields import TranslationField | |
class DefaultForm(forms.ModelForm): | |
def get_fields(self): | |
fields = self.fields.keys() |
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 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: |
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
# -*- 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): |
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
# -*- coding: utf-8 -*- | |
import bleach | |
from django.db import models | |
class ModelSanitizerMixin(object): | |
allowed_tags = ['p', 'em', 'strong', 'h1', 'br', 'ul', 'ol', 'li', 'a'] |
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
# 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() |
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 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, |
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 NamedModelChoiceFilter(django_filters.ModelMultipleChoiceFilter): | |
def __init__(self, *args, **kwargs): | |
super(NamedModelChoiceFilter, self).__init__(*args, **kwargs) | |
self.field.widget.attrs['data-placeholder'] = self.label |
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 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() |
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 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)) |
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 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: |
OlderNewer