-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserializer.py
103 lines (92 loc) · 3.1 KB
/
serializer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from rest_framework import serializers
from opportunity.models import Opportunity
from accounts.serializer import AccountSerializer
from contacts.serializer import ContactSerializer
from teams.serializer import TeamsSerializer
from common.serializer import ProfileSerializer, AttachmentsSerializer
from accounts.models import Tags
class TagsSerializer(serializers.ModelSerializer):
class Meta:
model = Tags
fields = ("id", "name", "slug")
class OpportunitySerializer(serializers.ModelSerializer):
account = AccountSerializer()
closed_by = ProfileSerializer()
created_by = ProfileSerializer()
tags = TagsSerializer(read_only=True, many=True)
assigned_to = ProfileSerializer(read_only=True, many=True)
contacts = ContactSerializer(read_only=True, many=True)
teams = TeamsSerializer(read_only=True, many=True)
opportunity_attachment = AttachmentsSerializer(read_only=True, many=True)
class Meta:
model = Opportunity
# fields = ‘__all__’
fields = (
"id",
"name",
"stage",
"currency",
"amount",
"lead_source",
"probability",
"contacts",
"closed_by",
"closed_on",
"description",
"assigned_to",
"created_by",
"created_on",
"is_active",
"tags",
"opportunity_attachment",
"teams",
"created_on_arrow",
"account",
# "get_team_users",
# "get_team_and_assigned_users",
# "get_assigned_users_not_in_teams",
)
class OpportunityCreateSerializer(serializers.ModelSerializer):
probability = serializers.IntegerField(max_value=100)
closed_on = serializers.DateField
def __init__(self, *args, **kwargs):
request_obj = kwargs.pop("request_obj", None)
super().__init__(*args, **kwargs)
self.org = request_obj.org
def validate_name(self, name):
if self.instance:
if (
Opportunity.objects.filter(name__iexact=name, org=self.org)
.exclude(id=self.instance.id)
.exists()
):
raise serializers.ValidationError(
"Opportunity already exists with this name"
)
else:
if Opportunity.objects.filter(name__iexact=name, org=self.org).exists():
raise serializers.ValidationError(
"Opportunity already exists with this name"
)
return name
class Meta:
model = Opportunity
fields = (
"name",
"account",
"stage",
"currency",
"amount",
"lead_source",
"probability",
"closed_on",
"description",
"created_by",
"created_on",
"is_active",
"created_on_arrow",
"org"
# "get_team_users",
# "get_team_and_assigned_users",
# "get_assigned_users_not_in_teams",
)