Haha, at last it's almost over.
urls.py
fromDjango.conf.urlsImportinclude, url fromDjango.contribImportAdminurlpatterns=[url (r'^admin/', include (admin.site.urls)), url (r'^', Include ('Snippets.urls')),]urlpatterns+=[url (r'^api-auth/', Include ('Rest_framework.urls', Namespace='rest_framework'))]
fromDjango.conf.urlsImportURL fromRest_framework.urlpatternsImportFormat_suffix_patterns fromSnippetsImportViewsurlpatterns=[url (r'^snippets/$', views. Snippetlist.as_view ()), URL (r'snippets/(? p<pk>[0-9]+)/$', views. Snippetdetail.as_view ()), URL (r'^users/$', views. Userlist.as_view ()), URL (r'^users/(? p<pk>[0-9]+)/$', views. Userdetail.as_view ()),]urlpatterns= Format_suffix_patterns (Urlpatterns)
models.py
fromDjango.dbImportModels fromPygments.lexersImportget_all_lexers fromPygments.stylesImportGet_all_styles fromPygments.lexersImportGet_lexer_by_name fromPygments.formatters.htmlImportHtmlformatter fromPygmentsImporthighlightlexers= [item forIteminchGet_all_lexers ()ifItem[1]]language_choices= Sorted ([(Item[1][0], item[0]) forIteminchLexers]) Style_choices= Sorted (item, item) forIteminchget_all_styles ())classSnippet (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) owner= Models. ForeignKey ('Auth. User', Blank=true, Null=true, related_name='Snippets') highlighted= Models. TextField (Blank=true, null=True)defSave (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= Self.linenos and 'Table' orFalse Options= Self.title and{'title': Self.title}or{} Formatter= Htmlformatter (Style=self.style, linenos=Linenos, full=true, * *options) self.highlighted=Highlight (Self.code, Lexer, formatter) Super (Snippet, self). Save (*args, * *Kwargs)classmeta:ordering= ('created',)
permissions.py
__author__ ' CHENGANG882 ' from Import Permissions class isownerorreadonly (permissions. basepermission): def has_object_permission (self, request, view, obj): if in permissions. Safe_methods: return True return Obj.owner = = Request.user
views.py
fromSnippets.modelsImportSnippet fromSnippets.permissionsImportisownerorreadonly fromSnippets.serializersImportSnippetserializer fromSnippets.serializersImportUserserializer fromRest_frameworkImportgenerics fromDjango.contrib.auth.modelsImportUser fromRest_frameworkImportPermissionsclassuserlist (generics. Listapiview): Queryset=User.objects.all () Serializer_class=UserserializerclassUserdetail (generics. Retrieveapiview): Queryset=User.objects.all () Serializer_class=Userserializerclasssnippetlist (generics. Listcreateapiview): Queryset=Snippet.objects.all () Serializer_class=Snippetserializer permission_classes=(Permissions. Isauthenticatedorreadonly,)defperform_create (self, Serializer): Serializer.save (owner=self.request.user)classSnippetdetail (generics. Retrieveupdatedestroyapiview): Queryset=Snippet.objects.all () Serializer_class=Snippetserializer permission_classes=(Permissions. Isauthenticatedorreadonly, isownerorreadonly,)
serializers.py
fromRest_frameworkImportserializers fromSnippets.modelsImportSnippet, language_choices, Style_choices fromDjango.contrib.auth.modelsImportUserclassSnippetserializer (serializers. Modelserializer): Owner= serializers. Readonlyfield (source='Owner.username') classMeta:model=Snippet Fields= ('ID','title','Code','Linenos','language','style','owner')classUserserializer (serializers. Modelserializer): Snippets= serializers. Primarykeyrelatedfield (Many=true, queryset=Snippet.objects.all ())classMeta:model=User Fields= ('ID','username','Snippets')
Rest_framework deepening memory-added user login authentication, custom Permissions API interface