user association 1
This commit is contained in:
parent
a004b3eb0e
commit
e8cd3c33d2
Binary file not shown.
File diff suppressed because one or more lines are too long
@ -1,8 +1,12 @@
|
||||
from django.db import models
|
||||
|
||||
# pygments
|
||||
|
||||
from pygments import highlight
|
||||
from pygments.lexers import get_all_lexers
|
||||
from pygments.styles import get_all_styles
|
||||
|
||||
from pygments.lexers import get_lexer_by_name
|
||||
from pygments.formatters.html import HtmlFormatter
|
||||
|
||||
LEXERS = [item for item in get_all_lexers() if item[1]]
|
||||
LANGUAGE_CHOICES = sorted([(item[1][0], item[0]) for item in LEXERS])
|
||||
@ -16,6 +20,20 @@ class Snippet(models.Model):
|
||||
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)
|
||||
|
||||
owner = models.ForeignKey('auth.user', related_name='snippets', on_delete=models.CASCADE)
|
||||
highlighted = models.TextField
|
||||
class Meta:
|
||||
ordering = ['created']
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
"""
|
||||
Use the `pygments` library to create a highlighted HTML
|
||||
representation of the code snippet.
|
||||
"""
|
||||
lexer = get_lexer_by_name(self.language)
|
||||
linenos = 'table' if self.linenos else False
|
||||
options = {'title': self.title} if self.title else {}
|
||||
formatter = HtmlFormatter(style=self.style, linenos=linenos,
|
||||
full=True, **options)
|
||||
self.highlighted = highlight(self.code, lexer, formatter)
|
||||
super().save(*args, **kwargs)
|
@ -1,3 +1,5 @@
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
from rest_framework import serializers
|
||||
from .models import Snippet, LANGUAGE_CHOICES, STYLE_CHOICES
|
||||
|
||||
@ -5,3 +7,10 @@ class SnippetSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Snippet
|
||||
fields = ['id', 'title', 'code', 'linenos', 'language', 'style']
|
||||
|
||||
class UserSerializer(serializers.ModelSerializer):
|
||||
snippets = serializers.PrimaryKeyRelatedField(many=True, queryset=Snippet.objects.all())
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ['id', 'username', 'snippets']
|
@ -5,6 +5,8 @@ from . import views
|
||||
urlpatterns = [
|
||||
path('snippets/', views.SnippetList.as_view()),
|
||||
path('snippets/<int:pk>/', views.SnippetDetail.as_view()),
|
||||
path('users/', views.UserList.as_view()),
|
||||
path('users/<int:pk>/', views.UserDetail.as_view()),
|
||||
]
|
||||
|
||||
urlpatterns = format_suffix_patterns(urlpatterns)
|
@ -1,5 +1,7 @@
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
from .models import Snippet
|
||||
from .serializers import SnippetSerializer
|
||||
from .serializers import SnippetSerializer, UserSerializer
|
||||
from rest_framework import generics
|
||||
|
||||
class SnippetList(generics.ListCreateAPIView):
|
||||
@ -10,3 +12,12 @@ class SnippetList(generics.ListCreateAPIView):
|
||||
class SnippetDetail(generics.RetrieveUpdateDestroyAPIView):
|
||||
queryset = Snippet.objects.all()
|
||||
serializer_class = SnippetSerializer
|
||||
|
||||
class UserList(generics.ListAPIView):
|
||||
queryset = User.objects.all()
|
||||
serializer_class = UserSerializer
|
||||
|
||||
|
||||
class UserDetail(generics.RetrieveAPIView):
|
||||
queryset = User.objects.all()
|
||||
serializer_class = UserSerializer
|
Reference in New Issue
Block a user