Started Tutorial: Serialization; Created model snippet, and related serializer class, didn't successfully develop test case
parent
0dd8c5bd01
commit
a5ae5aa978
|
@ -0,0 +1,3 @@
|
||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
|
# Register your models here.
|
|
@ -0,0 +1,5 @@
|
||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class SnippetsConfig(AppConfig):
|
||||||
|
name = 'snippets'
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,21 @@
|
||||||
|
# from django.db.models.fields import BLANK_CHOICE_DASH
|
||||||
|
# from src.tutorial.settings import LANGUAGE_CODE
|
||||||
|
from django.db import models
|
||||||
|
from pygments.lexers import get_all_lexers
|
||||||
|
from pygments.styles import get_all_styles
|
||||||
|
|
||||||
|
LEXERS = [item for item in get_all_lexers() if item[1]] # 词法分析程序
|
||||||
|
LANGUAGE_CHOICES = sorted([(item[1][0], item[0]) for item in LEXERS])
|
||||||
|
STYLE_CHOICES = sorted([(item, item) for item in get_all_styles()])
|
||||||
|
|
||||||
|
|
||||||
|
class Snippet(models.Model):
|
||||||
|
created = models.DateTimeField(auto_now_add=True)
|
||||||
|
title = models.CharField(max_length=100, blank=True, default='')
|
||||||
|
code = models.TextField()
|
||||||
|
linenos = models.BooleanField(default=False)
|
||||||
|
language = models.CharField(choices=LANGUAGE_CHOICES, default='python', max_length=100)
|
||||||
|
style = models.CharField(choices=STYLE_CHOICES, default='friendly', max_length=100)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
ordering = ['created']
|
|
@ -0,0 +1,36 @@
|
||||||
|
from rest_framework import serializers
|
||||||
|
from snippets.models import Snippet, LANGUAGE_CHOICES, STYLE_CHOICES
|
||||||
|
|
||||||
|
|
||||||
|
class SnippetSerializer(serializers.Serializer):
|
||||||
|
"""
|
||||||
|
Provide a way of serializing/deserializing the snippet instance into representations such as json.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Define fields that get serialized/deserialized
|
||||||
|
id = serializers.IntegerField(read_only=True)
|
||||||
|
title = serializers.CharField(required=False, allow_blank=True, max_length=100)
|
||||||
|
code = serializers.CharField(style={'base_template': 'textarea.html'})
|
||||||
|
linenos = serializers.BooleanField(required=False)
|
||||||
|
language = serializers.ChoiceField(choices=LANGUAGE_CHOICES, default='python')
|
||||||
|
style = serializers.ChoiceField(choices=STYLE_CHOICES, default='friendly')
|
||||||
|
|
||||||
|
# `create()` & `update()` define how fully fledged instances are created/modified when calling serializer.save()`
|
||||||
|
|
||||||
|
def create(self, validated_data):
|
||||||
|
"""
|
||||||
|
Create and return a new `Snippet` instance, given the validated data.
|
||||||
|
"""
|
||||||
|
return Snippet.objects.create(**validated_data)
|
||||||
|
|
||||||
|
def update(self, instance, validated_data):
|
||||||
|
"""
|
||||||
|
Update and return an existing `Snippet` instance, given the validated data.
|
||||||
|
"""
|
||||||
|
instance.title = validated_data.get('title', instance.title)
|
||||||
|
instance.code = validated_data.get('code', instance.code)
|
||||||
|
instance.linenos = validated_data.get('linenos', instance.linenos)
|
||||||
|
instance.language = validated_data.get('language', instance.language)
|
||||||
|
instance.style = validated_data.get('style', instance.style)
|
||||||
|
instance.save()
|
||||||
|
return instance
|
|
@ -0,0 +1,22 @@
|
||||||
|
from django.test import TestCase
|
||||||
|
from rest_framework.renderers import JSONRenderer
|
||||||
|
from rest_framework.parsers import JSONParser
|
||||||
|
|
||||||
|
from .models import Snippet
|
||||||
|
from .serializers import SnippetSerializer
|
||||||
|
|
||||||
|
# class SerializerTests(TestCase):
|
||||||
|
|
||||||
|
# def test_serializer_work(self):
|
||||||
|
|
||||||
|
# snippet = Snippet(code='foo = "bar"\n')
|
||||||
|
# snippet.save()
|
||||||
|
# snippet = Snippet(code='print("hello, world")\n')
|
||||||
|
# snippet.save()
|
||||||
|
|
||||||
|
# serializer = SnippetSerializer(snippet)
|
||||||
|
# content = JSONRenderer().render(serializer.data)
|
||||||
|
|
||||||
|
# # self.assertEqual(type(content), b'{"id": 2, "title": "", "code": "print(\\"hello, world\\")\\n", "linenos": false, "language": "python", "style": "friendly"}')
|
||||||
|
|
||||||
|
# self.assertEqual(type(content), True)
|
|
@ -0,0 +1,3 @@
|
||||||
|
from django.shortcuts import render
|
||||||
|
|
||||||
|
# Create your views here.
|
|
@ -38,7 +38,9 @@ INSTALLED_APPS = [
|
||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
# 3rd party
|
# 3rd party
|
||||||
'rest_framework'
|
'rest_framework',
|
||||||
|
# 1st party
|
||||||
|
'snippets.apps.SnippetsConfig',
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
|
|
Loading…
Reference in New Issue