3
3
from django .contrib .sites .shortcuts import get_current_site
4
4
from django .core .mail import EmailMessage
5
5
from django .http import JsonResponse
6
- from django .shortcuts import get_object_or_404 , redirect
6
+ from django .shortcuts import get_object_or_404 , redirect , render
7
7
from django .template .loader import render_to_string
8
8
from django .views .generic import (
9
9
CreateView , UpdateView , DetailView , TemplateView , View , DeleteView )
@@ -49,7 +49,16 @@ def get_context_data(self, **kwargs):
49
49
context ['close_accounts' ] = close_accounts
50
50
context ["industries" ] = INDCHOICES
51
51
context ["per_page" ] = self .request .POST .get ('per_page' )
52
- context ['tags' ] = Tags .objects .all ()
52
+ context ["tag" ] = Tags .objects .all ()
53
+
54
+ search = False
55
+ if (
56
+ self .request .POST .get ('name' ) or self .request .POST .get ('city' ) or
57
+ self .request .POST .get ('industry' ) or self .request .POST .get ('tag' )
58
+ ):
59
+ search = True
60
+
61
+ context ["search" ] = search
53
62
54
63
tab_status = 'Open'
55
64
if self .request .POST .get ('tab_status' ):
@@ -73,14 +82,11 @@ class CreateAccountView(LoginRequiredMixin, CreateView):
73
82
74
83
def dispatch (self , request , * args , ** kwargs ):
75
84
self .users = User .objects .filter (is_active = True ).order_by ('email' )
76
- # if Contact.objects.count() == 0:
77
- # return JsonResponse({'message':'create Contact'})
78
- # if Lead.objects.count() == 0:
79
- # return JsonResponse({'message':'create Lead'})
80
85
return super (CreateAccountView , self ).dispatch (request , * args , ** kwargs )
81
86
82
87
def get_form_kwargs (self ):
83
88
kwargs = super (CreateAccountView , self ).get_form_kwargs ()
89
+ kwargs .update ({"account" : True })
84
90
return kwargs
85
91
86
92
def post (self , request , * args , ** kwargs ):
@@ -107,13 +113,16 @@ def form_valid(self, form):
107
113
else :
108
114
tag = Tags .objects .create (name = t .lower ())
109
115
account_object .tags .add (tag )
116
+ if self .request .POST .getlist ('contacts' , []):
117
+ account_object .contacts .add (* self .request .POST .getlist ('contacts' ))
110
118
if self .request .FILES .get ('account_attachment' ):
111
119
attachment = Attachments ()
112
120
attachment .created_by = self .request .user
113
121
attachment .file_name = self .request .FILES .get ('account_attachment' ).name
114
122
attachment .account = account_object
115
123
attachment .attachment = self .request .FILES .get ('account_attachment' )
116
124
attachment .save ()
125
+
117
126
if self .request .POST .get ("savenewform" ):
118
127
return redirect ("accounts:new_account" )
119
128
@@ -130,6 +139,8 @@ def get_context_data(self, **kwargs):
130
139
context ["users" ] = self .users
131
140
context ["industries" ] = INDCHOICES
132
141
context ["countries" ] = COUNTRIES
142
+ context ["contact_count" ] = Contact .objects .count ()
143
+ context ["lead_count" ] = Lead .objects .count ()
133
144
return context
134
145
135
146
@@ -179,6 +190,7 @@ def dispatch(self, request, *args, **kwargs):
179
190
180
191
def get_form_kwargs (self ):
181
192
kwargs = super (AccountUpdateView , self ).get_form_kwargs ()
193
+ kwargs .update ({"account" : True })
182
194
return kwargs
183
195
184
196
def post (self , request , * args , ** kwargs ):
@@ -207,6 +219,9 @@ def form_valid(self, form):
207
219
else :
208
220
tag = Tags .objects .create (name = t .lower ())
209
221
account_object .tags .add (tag )
222
+ if self .request .POST .getlist ('contacts' , []):
223
+ account_object .contacts .clear ()
224
+ account_object .contacts .add (* self .request .POST .getlist ('contacts' ))
210
225
if self .request .FILES .get ('account_attachment' ):
211
226
attachment = Attachments ()
212
227
attachment .created_by = self .request .user
@@ -228,7 +243,8 @@ def get_context_data(self, **kwargs):
228
243
context ["users" ] = self .users
229
244
context ["industries" ] = INDCHOICES
230
245
context ["countries" ] = COUNTRIES
231
-
246
+ context ["contact_count" ] = Contact .objects .count ()
247
+ context ["lead_count" ] = Lead .objects .count ()
232
248
return context
233
249
234
250
0 commit comments