Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
A
ap-12_django
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Давид Ли
ap-12_django
Commits
18c1d616
Commit
18c1d616
authored
Apr 09, 2024
by
Давид Ли
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lesson 71
parent
8efb80b4
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
82 additions
and
17 deletions
+82
-17
serializers.py
ap_12/api/serializers.py
+28
-0
urls.py
ap_12/api/urls.py
+7
-0
views.py
ap_12/api/views.py
+24
-14
settings.py
ap_12/core/settings.py
+18
-1
urls.py
ap_12/core/urls.py
+5
-2
No files found.
ap_12/api/serializers.py
View file @
18c1d616
from
django.contrib.auth
import
get_user_model
from
rest_framework
import
serializers
from
webapp
import
models
from
accounts
import
models
as
accounts_models
class
_ArticleSerializer
(
serializers
.
Serializer
):
...
...
@@ -21,9 +23,35 @@ class _ArticleSerializer(serializers.Serializer):
instance
.
save
()
return
instance
class
ProfileSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
accounts_models
.
Profile
fields
=
[
'birth_date'
,
'avatar'
]
class
_UserSerializer
(
serializers
.
ModelSerializer
):
fullname
=
serializers
.
SerializerMethodField
()
class
Meta
:
model
=
get_user_model
()
fields
=
[
'id'
,
'fullname'
,
'first_name'
,
'last_name'
,
'email'
,
'username'
]
def
get_fullname
(
self
,
obj
):
return
f
'{obj.first_name} {obj.last_name}'
class
ArticleSerializer
(
serializers
.
ModelSerializer
):
author
=
_UserSerializer
(
read_only
=
True
)
class
Meta
:
model
=
models
.
Article
fields
=
[
'id'
,
'title'
,
'text'
,
'author'
,
'created_at'
,
'updated_at'
]
class
UserSerializer
(
serializers
.
ModelSerializer
):
profile
=
ProfileSerializer
()
articles
=
ArticleSerializer
(
many
=
True
)
class
Meta
:
model
=
get_user_model
()
fields
=
[
'id'
,
'first_name'
,
'last_name'
,
'email'
,
'username'
,
'profile'
,
'articles'
]
ap_12/api/urls.py
0 → 100644
View file @
18c1d616
from
rest_framework
import
routers
from
api
import
views
router
=
routers
.
DefaultRouter
()
router
.
register
(
'articles'
,
views
.
ArticleViewset
)
router
.
register
(
'users'
,
views
.
UserViewset
)
ap_12/api/views.py
View file @
18c1d616
import
json
from
django.views.generic
import
View
from
rest_framework.views
import
APIView
from
django.contrib.auth
import
get_user_model
from
rest_framework
import
views
,
viewsets
,
permissions
from
rest_framework.response
import
Response
from
api
import
serializers
from
webapp
import
models
class
ArticleListView
(
APIView
):
def
get
(
self
,
request
,
*
args
,
**
kwargs
):
articles
=
models
.
Article
.
objects
.
all
()
serializer
=
serializers
.
ArticleSerializer
(
articles
,
many
=
True
)
return
Response
(
serializer
.
data
,
safe
=
False
)
class
ArticleViewset
(
viewsets
.
ModelViewSet
):
queryset
=
models
.
Article
.
objects
.
all
()
serializer_class
=
serializers
.
ArticleSerializer
permission_classes
=
[
permissions
.
IsAuthenticated
]
def
get_permissions
(
self
):
if
self
.
request
.
method
in
permissions
.
SAFE_METHODS
:
return
[]
return
super
()
.
get_permissions
()
class
UserViewset
(
viewsets
.
ModelViewSet
):
queryset
=
get_user_model
()
.
objects
.
all
()
serializer_class
=
serializers
.
UserSerializer
class
LogoutAPIView
(
views
.
APIView
):
permission_classes
=
[]
class
ArticleCreateView
(
APIView
):
def
post
(
self
,
request
,
*
args
,
**
kwargs
):
serializer
=
serializers
.
ArticleSerializer
(
data
=
json
.
loads
(
request
.
body
))
user
=
request
.
user
if
serializer
.
is_valid
():
serializer
.
save
()
return
Response
(
serializer
.
data
)
if
user
.
is_authenticated
:
user
.
auth_token
.
delete
()
return
Response
(
serializer
.
errors
,
status
=
400
)
return
Response
(
{
'status'
:
'ok'
})
ap_12/core/settings.py
View file @
18c1d616
...
...
@@ -25,7 +25,10 @@ SECRET_KEY = 'django-insecure-ao+ma%k=n3c2^tsc4wicjqho8_60d2ja1m9ne+m6k&p8%y=^yo
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG
=
True
ALLOWED_HOSTS
=
[]
ALLOWED_HOSTS
=
[
'*'
]
CORS_ALLOWED_ORIGINS
=
[
'http://localhost:52330'
,
]
# Application definition
...
...
@@ -39,6 +42,8 @@ INSTALLED_APPS = [
'django.contrib.staticfiles'
,
'rest_framework'
,
'rest_framework.authtoken'
,
'corsheaders'
,
'api'
,
'webapp'
,
...
...
@@ -48,6 +53,7 @@ INSTALLED_APPS = [
MIDDLEWARE
=
[
'django.middleware.security.SecurityMiddleware'
,
'django.contrib.sessions.middleware.SessionMiddleware'
,
'corsheaders.middleware.CorsMiddleware'
,
'django.middleware.common.CommonMiddleware'
,
'django.middleware.csrf.CsrfViewMiddleware'
,
'django.contrib.auth.middleware.AuthenticationMiddleware'
,
...
...
@@ -149,3 +155,14 @@ MEDIA_URL = '/images/'
SESSION_COOKIE_AGE
=
5
*
60
# DATE_INPUT_FORMATS = ('%d.%m.%Y',)
# REST FRAMEWORK
REST_FRAMEWORK
=
{
'DEFAULT_AUTHENTICATION_CLASSES'
:
(
'rest_framework.authentication.TokenAuthentication'
,
),
'DEFAULT_PERMISSION_CLASSES'
:
(
'rest_framework.permissions.IsAuthenticated'
,
),
}
ap_12/core/urls.py
View file @
18c1d616
...
...
@@ -2,7 +2,9 @@ from django.contrib import admin
from
django.urls
import
path
,
include
from
django.conf.urls.static
import
static
from
django.conf
import
settings
from
rest_framework.authtoken.views
import
obtain_auth_token
from
api.urls
import
router
from
api
import
views
as
api_views
from
webapp
import
views
...
...
@@ -11,6 +13,7 @@ urlpatterns = [
path
(
''
,
views
.
IndexRedirectView
.
as_view
(),
name
=
'redirect_to_index'
),
path
(
'articles/'
,
include
(
'webapp.urls'
)),
path
(
'accounts/'
,
include
(
'accounts.urls'
)),
path
(
'api/articles/'
,
api_views
.
ArticleListView
.
as_view
()),
path
(
'api/articles/create'
,
api_views
.
ArticleCreateView
.
as_view
()),
path
(
'api/'
,
include
(
router
.
urls
)),
path
(
'api/login/'
,
obtain_auth_token
,
name
=
'api_token_auth'
),
path
(
'api/logout/'
,
api_views
.
LogoutAPIView
.
as_view
(),
name
=
'api_logout'
),
]
+
static
(
settings
.
MEDIA_URL
,
document_root
=
settings
.
MEDIA_ROOT
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment