| 1 |
from decimal import Decimal |
| 2 |
|
| 3 |
from django.contrib.auth import get_user_model |
| 4 |
from django.core.validators import MaxLengthValidator, MinValueValidator |
| 5 |
from rest_framework import serializers |
| 6 |
|
| 7 |
from snippets.models import LANGUAGE_CHOICES, STYLE_CHOICES, Snippet, SnippetViewer |
| 8 |
|
| 9 |
|
| 10 |
class LanguageSerializer(serializers.Serializer): |
| 11 |
name = serializers.ChoiceField( |
| 12 |
choices=LANGUAGE_CHOICES, |
| 13 |
default="python", |
| 14 |
help_text="The name of the programming language", |
| 15 |
) |
| 16 |
read_only_nullable = serializers.CharField(read_only=True, allow_null=True) |
| 17 |
|
| 18 |
class Meta: |
| 19 |
ref_name = None |
| 20 |
|
| 21 |
|
| 22 |
class ExampleProjectSerializer(serializers.Serializer): |
| 23 |
project_name = serializers.CharField( |
| 24 |
label="project name custom title", help_text="Name of the project" |
| 25 |
) |
| 26 |
github_repo = serializers.CharField( |
| 27 |
required=True, help_text="Github repository of the project" |
| 28 |
) |
| 29 |
|
| 30 |
class Meta: |
| 31 |
ref_name = "Project" |
| 32 |
|
| 33 |
|
| 34 |
class UnixTimestampField(serializers.DateTimeField): |
| 35 |
def to_representation(self, value): |
| 36 |
"""Return epoch time for a datetime object or ``None``""" |
| 37 |
from django.utils.dateformat import format |
| 38 |
|
| 39 |
try: |
| 40 |
return int(format(value, "U")) |
| 41 |
except (AttributeError, TypeError): |
| 42 |
return None |
| 43 |
|
| 44 |
def to_internal_value(self, value): |
| 45 |
import datetime |
| 46 |
|
| 47 |
return datetime.datetime.fromtimestamp(int(value)) |
| 48 |
|
| 49 |
class Meta: |
| 50 |
swagger_schema_fields = { |
| 51 |
"format": "integer", |
| 52 |
"title": "Client date time suu", |
| 53 |
"description": "Date time in unix timestamp format", |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
class SnippetSerializer(serializers.Serializer): |
| 58 |
"""SnippetSerializer classdoc |
| 59 |
|
| 60 |
create: docstring for create from serializer classdoc |
| 61 |
""" |
| 62 |
|
| 63 |
id = serializers.IntegerField(read_only=True, help_text="id serializer help text") |
| 64 |
created = UnixTimestampField(read_only=True) |
| 65 |
owner = serializers.PrimaryKeyRelatedField( |
| 66 |
queryset=get_user_model().objects.all(), |
| 67 |
default=serializers.CurrentUserDefault(), |
| 68 |
help_text="The ID of the user that created this snippet; if none is provided, " |
| 69 |
"defaults to the currently logged in user.", |
| 70 |
) |
| 71 |
owner_as_string = serializers.PrimaryKeyRelatedField( |
| 72 |
help_text="The ID of the user that created this snippet.", |
| 73 |
pk_field=serializers.CharField(help_text="this help text should not show up"), |
| 74 |
read_only=True, |
| 75 |
source="owner", |
| 76 |
) |
| 77 |
title = serializers.CharField(required=False, allow_blank=True, max_length=100) |
| 78 |
code = serializers.CharField(style={"base_template": "textarea.html"}) |
| 79 |
tags = serializers.ListField( |
| 80 |
child=serializers.CharField(min_length=2), min_length=3, max_length=15 |
| 81 |
) |
| 82 |
linenos = serializers.BooleanField(required=False) |
| 83 |
language = LanguageSerializer(help_text="Sample help text for language") |
| 84 |
styles = serializers.MultipleChoiceField( |
| 85 |
choices=STYLE_CHOICES, default=["solarized-dark"] |
| 86 |
) |
| 87 |
lines = serializers.ListField( |
| 88 |
child=serializers.IntegerField(), |
| 89 |
allow_empty=True, |
| 90 |
allow_null=True, |
| 91 |
required=False, |
| 92 |
) |
| 93 |
example_projects = serializers.ListSerializer( |
| 94 |
child=ExampleProjectSerializer(), |
| 95 |
read_only=True, |
| 96 |
validators=[MaxLengthValidator(100)], |
| 97 |
) |
| 98 |
difficulty_factor = serializers.FloatField( |
| 99 |
help_text="this is here just to test FloatField", |
| 100 |
read_only=True, |
| 101 |
default=lambda: 6.9, |
| 102 |
) |
| 103 |
rate_as_string = serializers.DecimalField( |
| 104 |
max_digits=6, |
| 105 |
decimal_places=3, |
| 106 |
default=Decimal("0.0"), |
| 107 |
validators=[MinValueValidator(Decimal("0.0"))], |
| 108 |
) |
| 109 |
rate = serializers.DecimalField( |
| 110 |
max_digits=6, |
| 111 |
decimal_places=3, |
| 112 |
default=Decimal("0.0"), |
| 113 |
coerce_to_string=False, |
| 114 |
validators=[MinValueValidator(Decimal("0.0"))], |
| 115 |
) |
| 116 |
|
| 117 |
nullable_secondary_language = LanguageSerializer(allow_null=True) |
| 118 |
|
| 119 |
def create(self, validated_data): |
| 120 |
""" |
| 121 |
Create and return a new `Snippet` instance, given the validated data. |
| 122 |
""" |
| 123 |
del validated_data["styles"] |
| 124 |
del validated_data["lines"] |
| 125 |
del validated_data["difficulty_factor"] |
| 126 |
return Snippet.objects.create(**validated_data) |
| 127 |
|
| 128 |
def update(self, instance, validated_data): |
| 129 |
""" |
| 130 |
Update and return an existing `Snippet` instance, given the validated data. |
| 131 |
""" |
| 132 |
instance.title = validated_data.get("title", instance.title) |
| 133 |
instance.code = validated_data.get("code", instance.code) |
| 134 |
instance.linenos = validated_data.get("linenos", instance.linenos) |
| 135 |
instance.language = validated_data.get("language", instance.language) |
| 136 |
instance.style = validated_data.get("style", instance.style) |
| 137 |
instance.save() |
| 138 |
return instance |
| 139 |
|
| 140 |
|
| 141 |
class SnippetViewerSerializer(serializers.ModelSerializer): |
| 142 |
class Meta: |
| 143 |
model = SnippetViewer |
| 144 |
fields = "__all__" |