| Line 7... |
Line 7... |
| 7 |
from dtr.storage.DataService import Users
|
7 |
from dtr.storage.DataService import Users
|
| 8 |
from elixir import *
|
8 |
from elixir import *
|
| 9 |
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
|
9 |
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
|
| 10 |
from sqlalchemy.sql import func
|
10 |
from sqlalchemy.sql import func
|
| 11 |
from sqlalchemy.sql.expression import and_, or_, desc, not_, distinct, cast, \
|
11 |
from sqlalchemy.sql.expression import and_, or_, desc, not_, distinct, cast, \
|
| 12 |
between, in_
|
12 |
between
|
| 13 |
|
13 |
|
| 14 |
db = MySQLdb.connect('localhost',"root","shop2020","dtr" )
|
14 |
db = MySQLdb.connect('localhost',"root","shop2020","dtr" )
|
| 15 |
cursor = db.cursor()
|
15 |
cursor = db.cursor()
|
| 16 |
|
16 |
|
| 17 |
DataService.initialize(db_hostname="localhost")
|
17 |
DataService.initialize(db_hostname="localhost")
|
| 18 |
|
18 |
|
| 19 |
def main():
|
19 |
def main():
|
| - |
|
20 |
|
| 20 |
sql = "select GROUP_CONCAT( distinct x.user_ids ) user_ids, x.mobileNos, GROUP_CONCAT( distinct x.imeis) imeis, GROUP_CONCAT(distinct x.referrers) referrers from (SELECT GROUP_CONCAT( distinct u.id ) user_ids, GROUP_CONCAT(distinct u.mobile_number) mobileNos, d.imeinumber imeis, GROUP_CONCAT(distinct u.referrer) referrers FROM `users` u LEFT JOIN devices d ON u.id = d.user_id where d.imeinumber is not null and u.mobile_number is not null GROUP BY d.imeinumber) as x group by x.mobileNos UNION SELECT GROUP_CONCAT( distinct u.id ) user_ids, u.mobile_number mobileNos, GROUP_CONCAT( distinct d.imeinumber) imeis, GROUP_CONCAT(distinct u.referrer) referrers FROM `users` u LEFT JOIN devices d ON u.id = d.user_id where d.imeinumber is null and u.mobile_number is not null GROUP BY u.mobile_number UNION SELECT GROUP_CONCAT( distinct u.id ) user_ids, u.mobile_number mobileNos, GROUP_CONCAT( distinct d.imeinumber) imeis, GROUP_CONCAT(distinct u.referrer) referrers FROM `users` u LEFT JOIN devices d ON u.id = d.user_id where d.imeinumber is null and u.mobile_number is null and LOWER(u.referrer) not like 'emp%' and LOWER(u.referrer) not like 'crm%' and LOWER(u.referrer) not like 'fos%' GROUP BY u.referrer"
|
21 |
sql = "SELECT GROUP_CONCAT( distinct u.id ) user_ids, GROUP_CONCAT(distinct u.mobile_number) mobileNos, d.imeinumber imeis, GROUP_CONCAT(distinct u.referrer) referrers, 'Imeis' Group_Basis FROM users u LEFT OUTER JOIN devices d ON u.id = d.user_id where trim(coalesce(d.imeinumber, '')) <>'' GROUP BY d.imeinumber UNION SELECT GROUP_CONCAT( distinct u.id ) user_ids, u.mobile_number mobileNos, GROUP_CONCAT( distinct d.imeinumber) imeis, GROUP_CONCAT(distinct u.referrer) referrers, 'Mobile_Nos' Group_Basis FROM users u LEFT OUTER JOIN devices d ON u.id = d.user_id where trim(coalesce(u.mobile_number, '')) <>'' GROUP BY u.mobile_number"
|
| 21 |
|
22 |
|
| 22 |
cursor.execute(sql)
|
23 |
cursor.execute(sql)
|
| 23 |
result_data = cursor.fetchall()
|
24 |
result_data = cursor.fetchall()
|
| 24 |
if result_data:
|
25 |
if result_data:
|
| 25 |
for record_data in result_data:
|
26 |
for record_data in result_data:
|
| - |
|
27 |
print "User Ids",record_data[0]
|
| - |
|
28 |
userIds = []
|
| - |
|
29 |
imeis = []
|
| - |
|
30 |
mobileNos = []
|
| - |
|
31 |
referrers = []
|
| - |
|
32 |
groupbasis = []
|
| - |
|
33 |
for x in record_data[0].split(','):
|
| - |
|
34 |
userIds.append(int(x.strip()))
|
| - |
|
35 |
if record_data[1]:
|
| - |
|
36 |
for x in record_data[1].split(','):
|
| - |
|
37 |
mobileNos.append(str(x.strip()))
|
| - |
|
38 |
if record_data[2]:
|
| - |
|
39 |
for x in record_data[2].split(','):
|
| - |
|
40 |
imeis.append(str(x.strip()))
|
| - |
|
41 |
if record_data[3]:
|
| - |
|
42 |
for x in record_data[3].split(','):
|
| - |
|
43 |
referrers.append(str(x.strip()))
|
| - |
|
44 |
for x in record_data[4].split(','):
|
| - |
|
45 |
groupbasis.append(str(x.strip()))
|
| - |
|
46 |
|
| 26 |
users = Users.query.filter(Users.id.in_(record_data[0]))
|
47 |
users = Users.query.filter(Users.id.in_(tuple(userIds)))
|
| 27 |
existingGroupId = 0
|
48 |
existingGroupIds = []
|
| 28 |
for user in users:
|
49 |
for user in users:
|
| - |
|
50 |
print user.usergroup_id
|
| 29 |
if user.userGroupId and user.userGroupId >0:
|
51 |
if user.usergroup_id and user.usergroup_id >0:
|
| 30 |
existingGroupId = user.userGroupId
|
52 |
existingGroupIds.append(user.usergroup_id)
|
| - |
|
53 |
|
| - |
|
54 |
existingGroupIds = set(existingGroupIds)
|
| - |
|
55 |
existingGroupIds = list(existingGroupIds)
|
| 31 |
|
56 |
|
| 32 |
if existingGroupId >0:
|
57 |
print existingGroupIds
|
| - |
|
58 |
if len(existingGroupIds) >0 and len(existingGroupIds)==1:
|
| - |
|
59 |
existingGroupId = existingGroupIds[0]
|
| 33 |
checkForExistingUserGroup = "select * from usergroup where id=%d"%(existingGroupId)
|
60 |
checkForExistingUserGroup = "select * from usergroups where id=%d"%(existingGroupId)
|
| 34 |
cursor.execute(checkForExistingUserGroup)
|
61 |
cursor.execute(checkForExistingUserGroup)
|
| 35 |
existingUserGroup = cursor.fetchone()
|
62 |
existingUserGroup = cursor.fetchone()
|
| - |
|
63 |
|
| - |
|
64 |
newUserIds = existingUserGroup[1]
|
| - |
|
65 |
newMobileNos = existingUserGroup[2]
|
| - |
|
66 |
newImeis = existingUserGroup[3]
|
| - |
|
67 |
newReferrers = existingUserGroup[4]
|
| - |
|
68 |
newGroupBasis = existingUserGroup[5]
|
| - |
|
69 |
|
| - |
|
70 |
fguserIds = []
|
| - |
|
71 |
for x in record_data[0].split(','):
|
| - |
|
72 |
fguserIds.append(int(x.strip()))
|
| - |
|
73 |
|
| - |
|
74 |
for userId in userIds:
|
| - |
|
75 |
if not str(userId) in newUserIds:
|
| - |
|
76 |
newUserIds = newUserIds +','+str(userId)
|
| - |
|
77 |
|
| - |
|
78 |
for mobileNo in mobileNos :
|
| - |
|
79 |
if not mobileNo in newMobileNos:
|
| - |
|
80 |
newMobileNos = newMobileNos +','+mobileNo
|
| - |
|
81 |
|
| - |
|
82 |
for imei in imeis:
|
| - |
|
83 |
if not imei in newImeis:
|
| - |
|
84 |
newImeis = newImeis+','+imei
|
| - |
|
85 |
|
| - |
|
86 |
for referrer in referrers:
|
| - |
|
87 |
if not referrer in newReferrers:
|
| - |
|
88 |
newReferrers = newReferrers +','+referrer
|
| - |
|
89 |
|
| - |
|
90 |
if len(newGroupBasis)> 0:
|
| - |
|
91 |
if len(userIds) > 1 and len(fguserIds) < len(userIds):
|
| - |
|
92 |
newGroupBasis = str(record_data[4])
|
| - |
|
93 |
else:
|
| - |
|
94 |
if len(userIds) > 1 and len(fguserIds) < len(userIds):
|
| - |
|
95 |
newGroupBasis = str(record_data[4])
|
| - |
|
96 |
else:
|
| - |
|
97 |
for groupbase in groupbasis:
|
| - |
|
98 |
if not groupbase in newGroupBasis:
|
| - |
|
99 |
newGroupBasis = newGroupBasis +','+groupbase
|
| - |
|
100 |
|
| - |
|
101 |
newGroupBasis = newGroupBasis.strip(',')
|
| 36 |
updateUserGroup = "update usergroup set user_ids ='%s', mobileNos ='%s', imeis ='%s', referrers ='%s' where id = %d"%(record_data[0], record_data[1], record_data[2], record_data[3], existingUserGroup[0])
|
102 |
updateUserGroup = "update usergroups set userids ='%s', mobilenos ='%s', imeis ='%s', referrers ='%s', groupbasis ='%s' where id = %d"%(newUserIds, newMobileNos, newImeis, newReferrers, newGroupBasis, existingGroupId)
|
| - |
|
103 |
print updateUserGroup
|
| 37 |
try:
|
104 |
try:
|
| 38 |
cursor.execute(updateUserGroup)
|
105 |
cursor.execute(updateUserGroup)
|
| 39 |
db.commit()
|
106 |
db.commit()
|
| 40 |
except:
|
107 |
except:
|
| 41 |
# Rollback in case there is any error
|
108 |
# Rollback in case there is any error
|
| 42 |
db.rollback()
|
109 |
db.rollback()
|
| 43 |
|
110 |
|
| 44 |
for user in users:
|
111 |
for user in users:
|
| 45 |
if not user.userGroupId or user.userGroupId==0:
|
- |
|
| 46 |
user.userGroupId = existingGroupId
|
112 |
user.usergroup_id = existingGroupId
|
| 47 |
session.commit()
|
113 |
session.commit()
|
| - |
|
114 |
elif len(existingGroupIds)>1:
|
| - |
|
115 |
existingGroupIds.sort()
|
| 48 |
else:
|
116 |
stringGroupIds =[]
|
| - |
|
117 |
for groupId in (existingGroupIds):
|
| - |
|
118 |
stringGroupIds.append(str(groupId))
|
| - |
|
119 |
allUserGroupsSql = "select * from usergroups where id in %s"%(str(tuple(stringGroupIds)))
|
| 49 |
updateUserGroupUserIds = False
|
120 |
print allUserGroupsSql
|
| - |
|
121 |
cursor.execute(allUserGroupsSql)
|
| 50 |
updateUserGroupMobileNos = False
|
122 |
allUserGroups = cursor.fetchall()
|
| 51 |
updateUserGroupImeis = False
|
123 |
allUserGroups = list(allUserGroups)
|
| 52 |
updateUserGroupReferrers = False
|
124 |
firstUserGroup = allUserGroups[0]
|
| - |
|
125 |
|
| - |
|
126 |
newUserIds = firstUserGroup[1]
|
| - |
|
127 |
newMobileNos = firstUserGroup[2]
|
| 53 |
print record_data[0]
|
128 |
newImeis = firstUserGroup[3]
|
| 54 |
checkForCurrentRecord = "select * from usergroup where mobileNos like '%%%s%%' or imeis like '%%%s%%' or referrers like '%%%s%%'"%(record_data[1], record_data[2], record_data[3])
|
129 |
newReferrers = firstUserGroup[4]
|
| - |
|
130 |
newGroupBasis = firstUserGroup[5]
|
| 55 |
print checkForCurrentRecord
|
131 |
moreUsersToConsider = userIds
|
| 56 |
|
132 |
|
| - |
|
133 |
fguserIds = []
|
| 57 |
cursor.execute(checkForCurrentRecord)
|
134 |
for x in record_data[0].split(','):
|
| 58 |
currentRecord = cursor.fetchone()
|
135 |
fguserIds.append(int(x.strip()))
|
| - |
|
136 |
|
| 59 |
if currentRecord is not None:
|
137 |
for usergroup in allUserGroups:
|
| 60 |
if currentRecord[1] != record_data[0]:
|
138 |
print 'UserGroupId',usergroup[0]
|
| - |
|
139 |
|
| 61 |
updateUserGroupUserIds = True
|
140 |
ug_userIds = []
|
| 62 |
if currentRecord[2] != record_data[1]:
|
141 |
ug_imeis = []
|
| 63 |
updateUserGroupMobileNos = True
|
142 |
ug_mobileNos = []
|
| 64 |
if currentRecord[3] != record_data[2]:
|
143 |
ug_referrers = []
|
| 65 |
updateUserGroupImeis = True
|
144 |
ug_groupbasis = []
|
| - |
|
145 |
|
| 66 |
if currentRecord[4] != record_data[3]:
|
146 |
if usergroup[1]:
|
| 67 |
updateUserGroupReferrers = True
|
147 |
for x in usergroup[1].split(','):
|
| - |
|
148 |
ug_userIds.append(str(x.strip()))
|
| 68 |
updateUserGroup = None
|
149 |
if usergroup[2]:
|
| 69 |
if updateUserGroupUserIds and updateUserGroupMobileNos and updateUserGroupImeis and updateUserGroupReferrers:
|
150 |
for x in usergroup[2].split(','):
|
| 70 |
updateUserGroup = "update usergroup set user_ids ='%s', mobileNos ='%s', imeis ='%s', referrers ='%s' where id = %d"%(record_data[0], record_data[1], record_data[2], record_data[3], currentRecord[0])
|
151 |
ug_mobileNos.append(str(x.strip()))
|
| - |
|
152 |
if usergroup[3]:
|
| 71 |
elif not updateUserGroupUserIds and not updateUserGroupMobileNos and not updateUserGroupImeis and not updateUserGroupReferrers:
|
153 |
for x in usergroup[3].split(','):
|
| - |
|
154 |
ug_imeis.append(str(x.strip()))
|
| 72 |
updateUserGroup =None
|
155 |
if usergroup[4]:
|
| 73 |
elif updateUserGroupUserIds and updateUserGroupMobileNos and updateUserGroupImeis and not updateUserGroupReferrers:
|
156 |
for x in usergroup[4].split(','):
|
| 74 |
updateUserGroup = "update usergroup set user_ids ='%s', mobileNos ='%s', imeis ='%s', referrers ='%s' where id = %d"%(record_data[0], record_data[1], record_data[2], currentRecord[4], currentRecord[0])
|
157 |
ug_referrers.append(str(x.strip()))
|
| 75 |
elif updateUserGroupUserIds and updateUserGroupMobileNos and not updateUserGroupImeis and not updateUserGroupReferrers:
|
158 |
for x in usergroup[5].split(','):
|
| 76 |
updateUserGroup = "update usergroup set user_ids ='%s', mobileNos ='%s', imeis ='%s', referrers ='%s' where id = %d"%(record_data[0], record_data[1], currentRecord[3], currentRecord[4], currentRecord[0])
|
159 |
ug_groupbasis.append(str(x.strip()))
|
| - |
|
160 |
|
| - |
|
161 |
ug_userIds = set(ug_userIds)
|
| - |
|
162 |
ug_userIds = list(ug_userIds)
|
| - |
|
163 |
ug_imeis = set(ug_imeis)
|
| - |
|
164 |
ug_imeis = list(ug_imeis)
|
| 77 |
elif updateUserGroupUserIds and not updateUserGroupMobileNos and not updateUserGroupImeis and not updateUserGroupReferrers:
|
165 |
ug_mobileNos = set(ug_mobileNos)
|
| 78 |
updateUserGroup = "update usergroup set user_ids ='%s', mobileNos ='%s', imeis ='%s', referrers ='%s' where id = %d"%(record_data[0], currentRecord[2], currentRecord[3], currentRecord[4], currentRecord[0])
|
166 |
ug_mobileNos = list(ug_mobileNos)
|
| 79 |
elif not updateUserGroupUserIds and updateUserGroupMobileNos and not updateUserGroupImeis and not updateUserGroupReferrers:
|
167 |
ug_referrers = set(ug_referrers)
|
| 80 |
updateUserGroup = "update usergroup set user_ids ='%s', mobileNos ='%s', imeis ='%s', referrers ='%s' where id = %d"%(currentRecord[1], record_data[1], currentRecord[3], currentRecord[4], currentRecord[0])
|
168 |
ug_referrers = list(ug_referrers)
|
| 81 |
elif not updateUserGroupUserIds and updateUserGroupMobileNos and updateUserGroupImeis and not updateUserGroupReferrers:
|
169 |
ug_groupbasis = set(ug_groupbasis)
|
| 82 |
updateUserGroup = "update usergroup set user_ids ='%s', mobileNos ='%s', imeis ='%s', referrers ='%s' where id = %d"%(currentRecord[1], record_data[1], record_data[2], currentRecord[4], currentRecord[0])
|
170 |
ug_groupbasis = list(ug_groupbasis)
|
| - |
|
171 |
|
| - |
|
172 |
for userId in ug_userIds:
|
| 83 |
elif not updateUserGroupUserIds and updateUserGroupMobileNos and not updateUserGroupImeis and updateUserGroupReferrers:
|
173 |
if not userId in newUserIds:
|
| 84 |
updateUserGroup = "update usergroup set user_ids ='%s', mobileNos ='%s', imeis ='%s', referrers ='%s' where id = %d"%(currentRecord[1], record_data[1], currentRecord[3], record_data[3], currentRecord[0])
|
174 |
newUserIds = newUserIds +','+str(userId)
|
| 85 |
elif not updateUserGroupUserIds and not updateUserGroupMobileNos and updateUserGroupImeis and updateUserGroupReferrers:
|
175 |
moreUsersToConsider.append(userId)
|
| - |
|
176 |
|
| 86 |
updateUserGroup = "update usergroup set user_ids ='%s', mobileNos ='%s', imeis ='%s', referrers ='%s' where id = %d"%(currentRecord[1], currentRecord[2], record_data[2], record_data[3], currentRecord[0])
|
177 |
for mobileNo in ug_mobileNos :
|
| 87 |
elif not updateUserGroupUserIds and not updateUserGroupMobileNos and updateUserGroupImeis and not updateUserGroupReferrers:
|
178 |
if not mobileNo in newMobileNos:
|
| 88 |
updateUserGroup = "update usergroup set user_ids ='%s', mobileNos ='%s', imeis ='%s', referrers ='%s' where id = %d"%(currentRecord[1], currentRecord[2], record_data[2], currentRecord[4], currentRecord[0])
|
179 |
newMobileNos = newMobileNos +','+mobileNo
|
| - |
|
180 |
|
| - |
|
181 |
for imei in ug_imeis:
|
| 89 |
elif not updateUserGroupUserIds and not updateUserGroupMobileNos and not updateUserGroupImeis and updateUserGroupReferrers:
|
182 |
if not imei in newImeis:
|
| 90 |
updateUserGroup = "update usergroup set user_ids ='%s', mobileNos ='%s', imeis ='%s', referrers ='%s' where id = %d"%(currentRecord[1], currentRecord[2], currentRecord[3], record_data[3], currentRecord[0])
|
183 |
newImeis = newImeis+','+imei
|
| - |
|
184 |
|
| - |
|
185 |
for referrer in ug_referrers:
|
| 91 |
elif not updateUserGroupUserIds and updateUserGroupMobileNos and updateUserGroupImeis and updateUserGroupReferrers:
|
186 |
if not referrer in newReferrers:
|
| 92 |
updateUserGroup = "update usergroup set user_ids ='%s', mobileNos ='%s', imeis ='%s', referrers ='%s' where id = %d"%(currentRecord[1], record_data[1], record_data[2], record_data[3], currentRecord[0])
|
187 |
newReferrers = newReferrers +','+referrer
|
| - |
|
188 |
|
| - |
|
189 |
if len(newGroupBasis) == 0:
|
| - |
|
190 |
if len(ug_userIds) > 1:
|
| 93 |
elif updateUserGroupUserIds and not updateUserGroupMobileNos and updateUserGroupImeis and not updateUserGroupReferrers:
|
191 |
for groupbase in ug_groupbasis:
|
| 94 |
updateUserGroup = "update usergroup set user_ids ='%s', mobileNos ='%s', imeis ='%s', referrers ='%s' where id = %d"%(record_data[0], currentRecord[2], record_data[2], currentRecord[4], currentRecord[0])
|
192 |
if not groupbase in newGroupBasis:
|
| 95 |
elif updateUserGroupUserIds and not updateUserGroupMobileNos and updateUserGroupImeis and updateUserGroupReferrers:
|
193 |
newGroupBasis = newGroupBasis +','+groupbase
|
| - |
|
194 |
else:
|
| 96 |
updateUserGroup = "update usergroup set user_ids ='%s', mobileNos ='%s', imeis ='%s', referrers ='%s' where id = %d"%(record_data[0], currentRecord[2], record_data[2], record_data[3], currentRecord[0])
|
195 |
for groupbase in ug_groupbasis:
|
| 97 |
elif updateUserGroupUserIds and not updateUserGroupMobileNos and not updateUserGroupImeis and updateUserGroupReferrers:
|
196 |
if not groupbase in newGroupBasis:
|
| 98 |
updateUserGroup = "update usergroup set user_ids ='%s', mobileNos ='%s', imeis ='%s', referrers ='%s' where id = %d"%(record_data[0], currentRecord[2], currentRecord[3], record_data[3], currentRecord[0])
|
197 |
newGroupBasis = newGroupBasis +','+groupbase
|
| 99 |
|
198 |
|
| 100 |
if updateUserGroup is not None:
|
199 |
newGroupBasis = newGroupBasis.strip(',')
|
| - |
|
200 |
|
| 101 |
try:
|
201 |
for userId in userIds:
|
| 102 |
cursor.execute(updateUserGroup)
|
202 |
if not str(userId) in newUserIds:
|
| - |
|
203 |
newUserIds = newUserIds +','+str(userId)
|
| - |
|
204 |
|
| 103 |
db.commit()
|
205 |
for mobileNo in mobileNos :
|
| - |
|
206 |
if not mobileNo in newMobileNos:
|
| - |
|
207 |
newMobileNos = newMobileNos +','+mobileNo
|
| 104 |
except:
|
208 |
|
| - |
|
209 |
for imei in imeis:
|
| 105 |
# Rollback in case there is any error
|
210 |
if not imei in newImeis:
|
| 106 |
db.rollback()
|
211 |
newImeis = newImeis+','+imei
|
| 107 |
|
212 |
|
| 108 |
for user in users:
|
213 |
for referrer in referrers:
|
| 109 |
if not user.userGroupId or user.userGroupId==0:
|
214 |
if not referrer in newReferrers:
|
| 110 |
user.userGroupId = currentRecord[0]
|
215 |
newReferrers = newReferrers +','+referrer
|
| 111 |
session.commit()
|
216 |
|
| - |
|
217 |
if len(newGroupBasis)> 0:
|
| - |
|
218 |
if len(userIds) > 1 and len(fguserIds) < len(userIds):
|
| - |
|
219 |
newGroupBasis = str(record_data[4])
|
| 112 |
else:
|
220 |
else:
|
| - |
|
221 |
if len(userIds) > 1 and len(fguserIds) < len(userIds):
|
| - |
|
222 |
newGroupBasis = str(record_data[4])
|
| - |
|
223 |
else:
|
| - |
|
224 |
for groupbase in groupbasis:
|
| - |
|
225 |
if not groupbase in newGroupBasis:
|
| - |
|
226 |
newGroupBasis = newGroupBasis +','+groupbase
|
| - |
|
227 |
|
| - |
|
228 |
newGroupBasis = newGroupBasis.strip(',')
|
| - |
|
229 |
|
| - |
|
230 |
updateUserGroup = "update usergroups set userids ='%s', mobilenos ='%s', imeis ='%s', referrers ='%s', groupbasis ='%s' where id = %d"%(newUserIds, newMobileNos, newImeis, newReferrers, newGroupBasis, firstUserGroup[0])
|
| - |
|
231 |
print updateUserGroup
|
| - |
|
232 |
try:
|
| - |
|
233 |
cursor.execute(updateUserGroup)
|
| - |
|
234 |
db.commit()
|
| - |
|
235 |
except:
|
| - |
|
236 |
# Rollback in case there is any error
|
| - |
|
237 |
db.rollback()
|
| - |
|
238 |
|
| - |
|
239 |
usersToConsider = Users.query.filter(Users.id.in_(tuple(moreUsersToConsider)))
|
| - |
|
240 |
|
| - |
|
241 |
for user in usersToConsider:
|
| - |
|
242 |
user.usergroup_id = firstUserGroup[0]
|
| - |
|
243 |
session.commit()
|
| - |
|
244 |
elif len(existingGroupIds)==0:
|
| - |
|
245 |
|
| 113 |
sql = "insert into usergroup (user_ids, mobileNos, imeis, referrers) values('%s', '%s', '%s', '%s')"%(record_data[0], record_data[1], record_data[2], record_data[3])
|
246 |
sql = "insert into usergroups (userids, mobilenos, imeis, referrers, groupbasis) values('%s', '%s', '%s', '%s', '%s')"%(record_data[0], record_data[1], record_data[2], record_data[3], record_data[4])
|
| - |
|
247 |
|
| 114 |
print sql
|
248 |
print sql
|
| - |
|
249 |
try:
|
| - |
|
250 |
cursor.execute(sql)
|
| - |
|
251 |
db.commit()
|
| - |
|
252 |
except:
|
| - |
|
253 |
# Rollback in case there is any error
|
| - |
|
254 |
db.rollback()
|
| - |
|
255 |
|
| - |
|
256 |
userGroupRecordSql = "select * from usergroups where mobilenos like '%s' and imeis like '%s' and referrers like '%s' and groupbasis like '%s'"%(record_data[1], record_data[2], record_data[3], record_data[4])
|
| - |
|
257 |
print userGroupRecordSql
|
| - |
|
258 |
cursor.execute(userGroupRecordSql)
|
| - |
|
259 |
|
| - |
|
260 |
userGroupRecord = cursor.fetchone()
|
| - |
|
261 |
|
| - |
|
262 |
for user in users:
|
| - |
|
263 |
if not user.usergroup_id or user.usergroup_id==0:
|
| - |
|
264 |
user.usergroup_id = userGroupRecord[0]
|
| - |
|
265 |
session.commit()
|
| - |
|
266 |
|
| - |
|
267 |
userGroupDetails = session.query(Users.usergroup_id, func.count(Users.id)).group_by(Users.usergroup_id).all()
|
| - |
|
268 |
|
| - |
|
269 |
groupBasisUpdate = []
|
| - |
|
270 |
for usergroupDetail in userGroupDetails:
|
| - |
|
271 |
if usergroupDetail[1] ==1:
|
| - |
|
272 |
groupBasisUpdate.append(str(usergroupDetail[0]))
|
| - |
|
273 |
|
| - |
|
274 |
updateUserGroupBasis ="update usergroups set groupbasis='Not_Defined' where id in %s"%(str(tuple(groupBasisUpdate)))
|
| - |
|
275 |
|
| - |
|
276 |
print updateUserGroupBasis
|
| 115 |
|
277 |
|
| 116 |
try:
|
278 |
try:
|
| 117 |
cursor.execute(sql)
|
279 |
cursor.execute(updateUserGroupBasis)
|
| 118 |
db.commit()
|
280 |
db.commit()
|
| 119 |
except:
|
281 |
except:
|
| 120 |
# Rollback in case there is any error
|
282 |
# Rollback in case there is any error
|
| 121 |
db.rollback()
|
283 |
db.rollback()
|
| 122 |
|
- |
|
| 123 |
userGroupRecordSql = "select * from usergroup where mobileNos like '%%%s%%' or imeis like '%%%s%%' or referrers like '%%%s%%'"%(record_data[1], record_data[2], record_data[3])
|
- |
|
| 124 |
cursor.execute(userGroupRecordSql)
|
- |
|
| 125 |
|
- |
|
| 126 |
userGroupRecord = cursor.fetchone()
|
- |
|
| 127 |
|
- |
|
| 128 |
for user in users:
|
- |
|
| 129 |
if not user.userGroupId or user.userGroupId==0:
|
- |
|
| 130 |
user.userGroupId = userGroupRecord[0]
|
- |
|
| 131 |
session.commit()
|
- |
|
| 132 |
|
284 |
|
| 133 |
db.close()
|
285 |
db.close()
|
| 134 |
if session.is_active:
|
286 |
if session.is_active:
|
| 135 |
print "session is active. closing it."
|
287 |
print "session is active. closing it."
|
| 136 |
session.close()
|
288 |
session.close()
|