1
1
from django .db .models import Avg
2
2
from django .urls import reverse
3
3
from rest_framework import serializers
4
+ from frontend .view_helper import translater
4
5
5
6
from produk .models import (
6
7
GambarProduk ,
16
17
17
18
18
19
class KategoriSerializer (serializers .HyperlinkedModelSerializer ):
20
+ nama = serializers .SerializerMethodField ()
21
+
19
22
class Meta :
20
23
model = Kategori
21
24
fields = ["url" , "kode" , "icon" , "nama" ]
22
25
26
+ def get_nama (self , obj ):
27
+ user = self .context ["users" ]
28
+ languanges_code = user .languages .code if user .languages else "id"
29
+ translate_text = translater (
30
+ translate_to = languanges_code ,
31
+ page = obj .nama ,
32
+ values = obj .nama
33
+ )
34
+ return translate_text
35
+
23
36
24
37
class TipeProdukSerializer (serializers .HyperlinkedModelSerializer ):
38
+ nama = serializers .SerializerMethodField ()
39
+
25
40
class Meta :
26
41
model = TipeProduk
27
42
fields = ["url" , "nama" ]
28
43
44
+ def get_nama (self , obj ):
45
+ user = self .context ["users" ]
46
+ languanges_code = user .languages .code if user .languages else "id"
47
+ translate_text = translater (
48
+ translate_to = languanges_code ,
49
+ page = obj .nama ,
50
+ values = obj .nama
51
+ )
52
+ return translate_text
53
+
29
54
30
55
class WarnaProdukSerializer (serializers .HyperlinkedModelSerializer ):
56
+ nama = serializers .SerializerMethodField ()
57
+
31
58
class Meta :
32
59
model = WarnaProduk
33
60
fields = ["url" , "nama" ]
34
61
62
+ def get_nama (self , obj ):
63
+ user = self .context ["users" ]
64
+ languanges_code = user .languages .code if user .languages else "id"
65
+ translate_text = translater (
66
+ translate_to = languanges_code ,
67
+ page = obj .nama ,
68
+ values = obj .nama
69
+ )
70
+ return translate_text
71
+
35
72
36
73
class GambarProdukSerializer (serializers .HyperlinkedModelSerializer ):
74
+
37
75
class Meta :
38
76
model = GambarProduk
39
77
fields = ["gambar" , "nama" ]
@@ -48,6 +86,8 @@ class ProdukSerializer(serializers.HyperlinkedModelSerializer):
48
86
count_star = serializers .SerializerMethodField ()
49
87
produk_detail_url = serializers .SerializerMethodField ()
50
88
89
+ nama = serializers .SerializerMethodField ()
90
+
51
91
class Meta :
52
92
model = Produk
53
93
fields = [
@@ -72,6 +112,12 @@ class Meta:
72
112
"produk_detail_url" ,
73
113
]
74
114
115
+ def get_nama (self , obj ):
116
+ user = self .context ["users" ]
117
+ user_lang = user .languages .code if user .languages else "id"
118
+ print (translater (translate_to = user_lang , page = obj .nama , values = obj .nama ))
119
+ return translater (translate_to = user_lang , page = obj .nama , values = obj .nama )
120
+
75
121
def get_count_star (self , obj ):
76
122
countstar = (
77
123
UlasanCart .objects .filter (produkitem_id = obj .id ).aggregate (Avg ("produk" ))[
@@ -86,7 +132,7 @@ def get_produk_detail_url(self, obj):
86
132
return urldetail
87
133
88
134
def get_gambar (self , obj ):
89
- gambars = GambarProduk .objects .filter (produk__pk = obj .pk ).order_by ("-pk" )
135
+ gambars = GambarProduk .objects .filter (produk__pk = obj .pk , gambar__isnull = False ).order_by ("-pk" )
90
136
return GambarProdukSerializer (gambars , many = True ).data
91
137
92
138
def get_store (self , obj ):
0 commit comments