def test_post_destroy(self): # 포스트 생성 pk = self.create_post().data['id'] # /post/pk/으로 DELETE 요청을 보냄 factory = APIRequestFactory() request = factory.delete(f'/post/{pk}/') view = PostDetail.as_view() response = view(request, pk=pk) # 인증 데이터가 없을 경우 포스트 삭제 불가 테스트 self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) # 작성자가 아닌 경우 포스트 삭제 불가능한지 테스트 user = self.create_user('*****@*****.**', 'testuser2') force_authenticate(request, user=user) response = view(request, pk=pk) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) # 작성자인 경우 삭제 가능 user = User.objects.get(email='*****@*****.**') force_authenticate(request, user=user) response = view(request, pk=pk) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) # 포스트가 삭제되었는지 확인 request = factory.get(f'/post/{pk}/') view = PostDetail.as_view() response = view(request, pk=pk) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_post_update(self): # 포스트 생성 pk = self.create_post().data['id'] # /post/pk/으로 PATCH 요청을 보냄 factory = APIRequestFactory() data = { 'title': 'updated_title', } request = factory.patch(f'/post/{pk}/', data) view = PostDetail.as_view() response = view(request, pk=pk) # 인증 정보가 없는 경우 포스트 수정 불가인지 확인 self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) # 작성자가 아닌 경우 포스트 수정 불가능한지 확인 user = self.create_user('*****@*****.**', 'testuser2') force_authenticate(request, user=user) response = view(request, pk=pk) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) # 작성자인 경우 포스트 수정 성공 확인 user = User.objects.get(email='*****@*****.**') force_authenticate(request, user=user) response = view(request, pk=pk) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['title'], 'updated_title')
''' Пути к постам ''' from django.urls import path from posts.views import PostList, PostDetail app_name='posts' urlpatterns = [ path('', PostList.as_view(), name='list'), path('<slug:slug>/', PostDetail.as_view(), name='detail'), ]
"""Post urls.""" # Django from django.contrib.auth.decorators import login_required from django.urls import include, path # Django REST Framework from rest_framework import routers # Pyrty from posts.rest.views import PostViewSet from posts.views import CreatePost, delete_post, PostDetail, submit_vote router = routers.SimpleRouter() router.register(r'posts', PostViewSet, basename='posts') urlpatterns = [ path('<int:pk>', PostDetail.as_view(), name='post'), path('create/<int:subforum_id>', login_required(CreatePost.as_view()), name='create_post'), path('submit_vote/', login_required(submit_vote), name='submit_post_vote'), path('delete/', login_required(delete_post), name='delete_post'), path('rest/', include(router.urls)), ]
from django.conf.urls import include, url from rest_framework.urlpatterns import format_suffix_patterns from posts.views import TagList, TagDetail, PostList, PostDetail urlpatterns = [ url(r'^posts/$', PostList.as_view(), name='post-list'), url(r'^posts/(?P<pk>[0-9]+)/$', PostDetail.as_view(), name='post-detail'), url(r'^tags/$', TagList.as_view(), name='tag-list'), url(r'^tags/(?P<pk>[0-9]+)/$', TagDetail.as_view(), name='tag-detail'), ] urlpatterns = format_suffix_patterns(urlpatterns)
from django.conf.urls import url from posts.views import PostList, PostDetail, CommentTrackList, CommentTrackDetail, PostLikeToggle, MixTracks urlpatterns = [ # Post url(r'^$', PostList.as_view(), name='list'), url(r'^(?P<pk>\d+)/$', PostDetail.as_view(), name='detail'), # MixTracks url(r'^(?P<pk>\d+)/mix/$', MixTracks.as_view(), name='mix-tracks'), # PostLike url(r'^(?P<pk>\d+)/like/$', PostLikeToggle.as_view(), name='like'), # Comment_track url(r'^(?P<pk>\d+)/comments/$', CommentTrackList.as_view(), name='comment-track-list'), url(r'^comment/(?P<pk>\d+)/$', CommentTrackDetail.as_view(), name='comment-track-detail'), ]
The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/2.2/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin from django.urls import path from django.urls import include from django.conf import settings from django.conf.urls.static import static from posts.views import HomePage from posts.views import PostDetail urlpatterns = [ path('admin/doc/', include('django.contrib.admindocs.urls')), path('admin/', admin.site.urls), path('filer/', include('filer.urls')), path('', HomePage.as_view()), path('post/<slug:slug>/', PostDetail.as_view()), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
from django.conf.urls import url from posts.views import PageDetail, PostDetail, PostList urlpatterns = [ url(r'^posts/(?P<year>[0-9]{4})/(?P<pk>[0-9]+)/$', PostDetail.as_view(), name='post'), url(r'^tag/(?P<tag>[-\w]+)/$', PostList.as_view(), name='tag'), url(r'^(?P<slug>[-\w]+)/$', PageDetail.as_view(), name='page'), url(r'^$', PostList.as_view(), name='posts'), ]
from posts.api import PostListAPI, PostDetailAPI from posts.views import PostDetail, PostCreationView from django.conf.urls import url urlpatterns = [ # web urls url(r'^posts/(?P<pk>[0-9]+)$', PostDetail.as_view(), name='post_detail'), url(r'^newpost$', PostCreationView.as_view()), # api urls url(r'^api/1.0/posts/$', PostListAPI.as_view(), name='api_post_list'), url(r'^api/1.0/posts/(?P<pk>[0-9]+)$', PostDetailAPI.as_view(), name='api_post_list'), ]
from django.conf.urls import url from rest_framework.urlpatterns import format_suffix_patterns from django.views.decorators.csrf import csrf_exempt from posts.views import PostList, PostDetail, PostSortingList, get_tags urlpatterns = [ url(r'^posts/$', PostList.as_view()), url(r'^posts/(?P<pk>[0-9]+)/$', PostDetail.as_view()), url(r'^posts/filters/$', get_tags), url(r'^posts/sortings/$', PostSortingList.as_view()), ] urlpatterns = format_suffix_patterns(urlpatterns)
# encoding: utf-8 """ @desc: 帖子路由 """ from django.urls import path from . import views from posts.views import PostDetail, LikePost, AddPost, TopicDetail, StickyPost, DeletePost app_name = 'posts' urlpatterns = [ path('detail/<int:post_id>/', PostDetail.as_view(), name='post-detail'), path('like/', LikePost.as_view(), name='like-post'), path('add/', AddPost.as_view(), name='add-post'), path('delete/<int:post_id>/', DeletePost.as_view(), name='post-delete'), # url中使用了as_view()方法后,返回的闭包会根据请求的方式到对应的视图函数中 # 找以请求方式命名的函数进行执行。 path('topic/detail/<int:topic_id>/', TopicDetail.as_view(), name='topic-detail'), path('sticky/', StickyPost.as_view()), ]
# cSpell: Disable """pygram URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/3.0/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin from django.urls import path from django.conf import settings from django.conf.urls.static import static from posts.views import PostList, PostCreate, PostDetail urlpatterns = [ path("admin/", admin.site.urls), path("", PostList.as_view(), name="List"), path("post/", PostCreate.as_view(), name="post"), path("post/<pk>/", PostDetail.as_view(), name="detail"), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
from rest_framework_nested import routers from authentication.views import AccountViewSet, IndexView, LoginView, LogoutView from posts.views import AccountPostsViewSet, PostViewSet, LabelViewSet, CategoryViewSet, PostDetail #url(r'^notes/(?P<pk>[0-9]+)/$', views.NoteDetail.as_view()), router = routers.SimpleRouter() router.register(r'accounts', AccountViewSet) router.register(r'posts', PostViewSet) router.register(r'labels', LabelViewSet) router.register(r'category', CategoryViewSet) accounts_router = routers.NestedSimpleRouter( router, r'accounts', lookup='account' ) accounts_router.register(r'posts', AccountPostsViewSet) urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^api/v1/', include(router.urls)), url(r'^api/v1/', include(router.urls)), url(r'^api/v1/', include(accounts_router.urls)), # url(r'^api/v1/accounts', AccountViewSet.as_view({ # 'get': 'list', # 'post': 'create' # }), name='account'), url(r'^posts/(?P<pk>[0-9]+)/$', PostDetail.as_view()), url(r'^api/v1/auth/login/$', LoginView.as_view(), name='login'), url(r'^api/v1/auth/logout/$', LogoutView.as_view(), name='logout'), url('^.*$', IndexView.as_view(), name='index'), ]
from django.conf.urls import patterns, url from posts.views import PostView,ShowPost,PostDetail from posts import views urlpatterns = patterns('', url(r'^$', PostView.as_view(success_url= "/posts" ), name="create" ), url(r'^list/$', ShowPost.as_view()), url(r'^list/(?P<pk>\d+)/detail/$', PostDetail.as_view()) )
from django.urls import path from posts.views import CreatePost, DeletePost, PostDetail, UserPost, PostList app_name = 'posts' urlpatterns = [ path('', PostList.as_view(), name='all'), path('new/', CreatePost.as_view(), name='create'), path('by/<username>/', UserPost.as_view(), name='for_user'), path('by/<username>/<int:pk>/', PostDetail.as_view(), name='single'), path('delete/<int:pk>', DeletePost.as_view(), name='delete'), ]
from django.conf.urls import url from django.contrib.auth.decorators import login_required from posts.views import BlogList, BlogView, PostView, EditBlog, CreateBlog, CreatePost, PostDetail, EditPost, PostList, PostLikeAjaxView, CommentsView urlpatterns = [ url(r'^blogs/$', BlogList.as_view(), name='showBloglist'), url(r'^blogs/(?P<pk>\d+)/$', BlogView.as_view(), name='showBlog'), url(r'^posts/(?P<pk>\d+)/$', PostDetail.as_view(), name='showComments'), url(r'^posts/(?P<pk>\d+)/comments/$', CommentsView.as_view(), name='onlyComments'), url(r'^posts/(?P<pk>\d+)/like/$', login_required(PostLikeAjaxView.as_view()), name='post_ajax_like'), url(r'^blogs/add/$', login_required(CreateBlog.as_view()), name='createblog'), url(r'^blogs/(?P<pk>\d+)/add/$', login_required(CreatePost.as_view()), name='createpost'), url(r'^blogs/(?P<pk>\d+)/edit/$', EditBlog.as_view(), name='editblog'), url(r'^posts/(?P<pk>\d+)/edit/$', EditPost.as_view(), name='editpost'), url(r'^feed/$', PostList.as_view(), name='showFeed'), ]
from django.conf.urls import url from django.urls import path, include from posts.views import PostCreate, PostLikeToggle, PostDetail, PostList, PostEdit app_name = 'posts' urlpatterns = [ path("", PostList.as_view(), name="list"), path('new/', PostCreate.as_view(), name='new'), path('<pk>/edit/', PostEdit.as_view(), name='edit'), path("<pk>/like/", PostLikeToggle.as_view(), name='like'), path("<pk>/", PostDetail.as_view(), name='detail') ]
"""pypics URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/3.0/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin from django.urls import path from django.conf import settings from django.conf.urls.static import static from posts.views import PostList, PostCreate, PostDetail urlpatterns = [ path('admin/', admin.site.urls), path('', PostList.as_view(), name='list'), path('new/', PostCreate.as_view(), name='new'), path('posts/<pk>/', PostDetail.as_view(), name='detail'), ] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
from django.conf.urls import url from posts.views import PostDetail, PostList urlpatterns = [ url(r'^(?P<id>[\w-]+)/(?P<slug>[\w-]+)/', PostDetail.as_view(), name='post_detail'), url(r'^', PostList.as_view(), name='posts_list'), ]
from django.urls import path from posts.views import PostList, PostDetail app_name = 'posts' urlpatterns = [ path('', PostList.as_view(), name='post_list'), path('<int:pk>/', PostDetail.as_view(), name='post_detail'), ]
from django.urls import path,re_path from posts.views import PostList,UserPosts,PostDetail,CreatePost,DeletePost,UpdatePost,AddComment,DeleteComment app_name='posts' urlpatterns = [ path('',PostList.as_view(),name='all'), path('new/',CreatePost.as_view(),name='create'), re_path(r'by/(?P<username>[-\w]+)/$',UserPosts.as_view(),name='for_user'), re_path(r'by/(?P<username>[-\w]+)/(?P<pk>\d+)/$',PostDetail.as_view(),name='single'), re_path(r'delete/(?P<pk>\d+)/$',DeletePost.as_view(),name='delete'), re_path(r'edit/(?P<pk>\d+)/$',UpdatePost.as_view(),name='edit'), re_path(r'by/(?P<username>[-\w]+)/(?P<pk>\d+)/comment/$',AddComment.as_view(),name='addcomment'), re_path(r'by/(?P<username>[-\w]+)/(?P<postpk>\d+)/comment/(?P<pk>\d+)/delete/$',DeleteComment.as_view(),name='deletecomment'), ]