-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
118 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,60 @@ | ||
from django.contrib.auth import get_user_model | ||
|
||
from rest_framework import serializers | ||
|
||
from server.models import Product | ||
|
||
from .category_serializers import CategorySerializer | ||
from server.exeption import RESOURCE_NOT_FOUND | ||
|
||
User = get_user_model() | ||
|
||
class ProductCreateSerializer(serializers.ModelSerializer): | ||
user = serializers.HiddenField(default=serializers.CurrentUserDefault()) | ||
class ProductFieldsAllSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Product | ||
fields = "__all__" | ||
|
||
class ProductBaseSerializer(serializers.ModelSerializer): | ||
user_id = serializers.IntegerField(required=False) | ||
|
||
class Meta: | ||
model = Product | ||
fields = ( | ||
'id', 'title', 'metaTitle', 'summary', | ||
'id', 'slug', 'title', 'metaTitle', 'summary', | ||
'accessibility', 'condition', 'warehouse', | ||
'promotional', 'checks', 'price', 'discount', | ||
'category', 'user' | ||
'category', 'user_id', | ||
) | ||
extra_kwargs = {'id': {'read_only': True}} | ||
extra_kwargs = { | ||
'id': { | ||
'read_only': True | ||
}, | ||
'slug': { | ||
"read_only": True | ||
}, | ||
} | ||
def validate(self, attrs): | ||
user_id = attrs.pop("user_id", None) | ||
|
||
try: | ||
if user_id is None and not user_id: | ||
user = self.context["request"].user | ||
else: | ||
user = User.objects.get(pk=user_id) | ||
except User.DoesNotExist: | ||
raise serializers.ValidationError(RESOURCE_NOT_FOUND) | ||
|
||
attrs['user'] = user | ||
return super().validate(attrs) | ||
|
||
class ProductCreateSerializer(ProductBaseSerializer): | ||
pass | ||
|
||
class ProductUpdateSerializer(ProductBaseSerializer): | ||
def validate(self, attrs): | ||
request = self.context.get("request") | ||
if not request or not request.user.is_authenticated: | ||
raise serializers.ValidationError("User is not authenticated") | ||
return super().validate(attrs) | ||
|
||
class ProductDetailSerializer(serializers.ModelSerializer): | ||
class ProductDetailSerializer(ProductFieldsAllSerializer): | ||
category = CategorySerializer(read_only=True) | ||
|
||
class Meta: | ||
model = Product | ||
fields = "__all__" | ||
|
||
|
||
class ProductListSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Product | ||
fields = "__all__" | ||
class ProductListSerializer(ProductFieldsAllSerializer): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { axios } from "@/service/axios"; | ||
import { AuthenticatedFields } from "@/types/app/auth.types"; | ||
|
||
async function requestTokenAuthorize<T>(body: AuthenticatedFields): Promise<T> { | ||
"use server" | ||
|
||
try { | ||
const data = await axios.post<T, AuthenticatedFields>("/api/token/", body); | ||
return data; | ||
} catch(error) { | ||
console.error(error) | ||
throw error | ||
} | ||
} | ||
|
||
async function refreshAccessToken(token: string) { | ||
try { | ||
const data = { | ||
refresh: token | ||
} | ||
const resData = await axios.post<{access: string, access_expires_in: number}, {refresh: string}>( | ||
"/api/v1/refresh/", | ||
data | ||
) | ||
return { | ||
accessToken: resData.access, | ||
accessTokenExpires: resData.access_expires_in * 300, | ||
refreshToken: token | ||
} | ||
} catch(error) { | ||
console.error(error) | ||
throw error | ||
} | ||
} | ||
|
||
export {requestTokenAuthorize, refreshAccessToken}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters