CommonFilter.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. from swyzxwb_photovote.common.DataConvert import Str2Date,str2int
  2. from django.template.loader import render_to_string
  3. from django.conf import settings
  4. class AddableDict(dict):
  5. def __add__(d1,d2):
  6. return AddableDict(d1.items()+d2.items())
  7. class FilterCondition:
  8. def __init__(self):
  9. self.fcd={
  10. "index":[],
  11. "ceq":AddableDict(),
  12. "cueq":AddableDict(),
  13. "cc":AddableDict(),
  14. "cuc":AddableDict(),
  15. "csw":AddableDict(),
  16. "cew":AddableDict(),
  17. "cnsw":AddableDict(),
  18. "cnew":AddableDict(),
  19. "deq":AddableDict(),
  20. "drg":AddableDict(),
  21. "neq":AddableDict(),
  22. "nneq":AddableDict(),
  23. "ngt":AddableDict(),
  24. "nlt":AddableDict(),
  25. "csc":AddableDict(),
  26. "cmc":AddableDict(),
  27. "sf":AddableDict(),
  28. }
  29. def addTextEqual(self,name,title,fieldname):
  30. self.fcd['index'].append(name)
  31. self.fcd['ceq'].update({name:(title,fieldname)})
  32. def addTextUnequal(self,name,title,fieldname):
  33. self.fcd['index'].append(name)
  34. self.fcd['cueq'].update({name:(title,fieldname)})
  35. def addTextContain(self,name,title,fieldname):
  36. self.fcd['index'].append(name)
  37. self.fcd['cc'].update({name:("*"+title+"*",fieldname)})
  38. def addTextUncontain(self,name,title,fieldname):
  39. self.fcd['index'].append(name)
  40. self.fcd['cuc'].update({name:("*"+title+"*",fieldname)})
  41. def addTextStartWith(self,name,title,fieldname):
  42. self.fcd['index'].append(name)
  43. self.fcd['csw'].update({name:(title+"*",fieldname)})
  44. def addTextEndWith(self,name,title,fieldname):
  45. self.fcd['index'].append(name)
  46. self.fcd['cew'].update({name:("*"+title,fieldname)})
  47. def addTextNotStartWith(self,name,title,fieldname):
  48. self.fcd['index'].append(name)
  49. self.fcd['cnsw'].update({name:(title+"*",fieldname)})
  50. def addTextNotEndWith(self,name,title,fieldname):
  51. self.fcd['index'].append(name)
  52. self.fcd['cnew'].update({name:("*"+title,fieldname)})
  53. def addDateEqual(self,name,title,fieldname):
  54. self.fcd['index'].append(name)
  55. self.fcd['deq'].update({name:(title,fieldname)})
  56. def addDateRange(self,name,title,fieldname):
  57. self.fcd['index'].append(name)
  58. self.fcd['drg'].update({name:(title,fieldname)})
  59. def addNumberEqual(self,name,title,fieldname):
  60. self.fcd['index'].append(name)
  61. self.fcd['neq'].update({name:(title,fieldname)})
  62. def addNumberUnEqual(self,name,title,fieldname):
  63. self.fcd['index'].append(name)
  64. self.fcd['nneq'].update({name:(title,fieldname)})
  65. def addNumberGreatThan(self,name,title,fieldname,equal):
  66. self.fcd['index'].append(name)
  67. self.fcd['ngt'].update({name:(title,fieldname,equal)})
  68. def addNumberLessThan(self,name,title,fieldname,equal):
  69. self.fcd['index'].append(name)
  70. self.fcd['nlt'].update({name:(title,fieldname,equal)})
  71. def addSingleChoice(self,name,title,fieldname,clist,alias=None):
  72. self.fcd['index'].append(name)
  73. self.fcd['csc'].update({name:(title,fieldname,clist,alias)})
  74. def addMultiChoice(self,name,title,fieldname,clist,alias=None):
  75. self.fcd['index'].append(name)
  76. self.fcd['cmc'].update({name:(title,fieldname,clist,alias)})
  77. def AddSortField(self,name,title,fieldname):
  78. self.fcd['sf'].update({name:(title,fieldname)})
  79. def RenderHTML(self,request):
  80. def fcondget(kn):
  81. try:
  82. r=fcond.get(kn)
  83. except:
  84. r=""
  85. if not r:
  86. r=""
  87. return r
  88. fcond = request.GET
  89. dtext=self.fcd['ceq']+self.fcd['cueq']+self.fcd['cc']+self.fcd['cuc']+self.fcd['csw']+self.fcd['cew']+self.fcd['cnsw']+self.fcd['cnew']
  90. dval=self.fcd['neq']+self.fcd['nneq']+self.fcd['ngt']+self.fcd['nlt']
  91. #dtext=self.fcd['ceq']+self.fcd['cueq']
  92. iText=[]
  93. iVal=[]
  94. iDateE=[]
  95. iDateR=[]
  96. iSC=[]
  97. iMC=[]
  98. kl=filter(lambda x:x in dtext.keys(),self.fcd['index'])
  99. for i in kl:
  100. iText.append({"ctname":i,"ctid":"ipc_"+i,"title":dtext[i][0],"value":fcondget(i)})
  101. kl=filter(lambda x:x in dval.keys(),self.fcd['index'])
  102. for i in kl:
  103. iVal.append({"ctname":i,"ctid":"ipc_"+i,"title":dval[i][0],"value":fcondget(i)})
  104. kl=filter(lambda x:x in self.fcd['deq'].keys(),self.fcd['index'])
  105. for i in kl:
  106. iDateE.append({"ctname":i,"ctid":"ipc_"+i,"title":self.fcd['deq'][i][0],"value":fcondget(i)})
  107. kl=filter(lambda x:x in self.fcd['drg'].keys(),self.fcd['index'])
  108. for i in kl:
  109. try:
  110. vst,ved=fcond.get(i).split('~')
  111. except:
  112. vst,ved="",""
  113. if not vst:
  114. vst=""
  115. if not ved:
  116. ved=""
  117. iDateR.append({"ctname":i,"ctid":"ipc_"+i,"title":self.fcd['drg'][i][0],"valuest":vst,"valueed":ved})
  118. kl=filter(lambda x:x in self.fcd['csc'].keys(),self.fcd['index'])
  119. for i in kl:
  120. fc=fcondget(i)
  121. if fc=="":
  122. fc="all"
  123. if self.fcd['csc'][i][3]:
  124. iSC.append({"ctname":i,"ctid":"ipc_"+i,"title":self.fcd['csc'][i][0],"oplst":self.fcd['csc'][i][3],"value":fc})
  125. else:
  126. iSC.append({"ctname":i,"ctid":"ipc_"+i,"title":self.fcd['csc'][i][0],"oplst":self.fcd['csc'][i][2],"value":fc})
  127. kl=filter(lambda x:x in self.fcd['cmc'].keys(),self.fcd['index'])
  128. for i in kl:
  129. if self.fcd['cmc'][i][3]:
  130. iMC.append({"ctname":i,"ctid":"ipc_"+i,"title":self.fcd['cmc'][i][0],"oplst":self.fcd['cmc'][i][3],"value":fcondget(i)})
  131. else:
  132. iMC.append({"ctname":i,"ctid":"ipc_"+i,"title":self.fcd['cmc'][i][0],"oplst":self.fcd['cmc'][i][2],"value":fcondget(i)})
  133. kwvars={
  134. "textitems":iText,
  135. "valitems":iVal,
  136. "dateeqi":iDateE,
  137. "datergi":iDateR,
  138. "scitems":iSC,
  139. "mcitems":iMC
  140. }
  141. return render_to_string("common/Filter/maindiv.html",kwvars)
  142. def _CommonFilter(request,fco,data):
  143. fcond = request.GET
  144. td=data
  145. wl=filter(lambda x:x in fco.fcd['ceq'].keys(),fcond.keys())
  146. if wl:
  147. for i in wl:
  148. argv={fco.fcd['ceq'][i][1]+"__exact":fcond[i]}
  149. td=td.filter(**argv)
  150. wl=filter(lambda x:x in fco.fcd['cueq'].keys(),fcond.keys())
  151. if wl:
  152. for i in wl:
  153. argv={fco.fcd['cueq'][i][1]+"__exact":fcond[i]}
  154. td=td.exclude(**argv)
  155. wl=filter(lambda x:x in fco.fcd['cc'].keys(),fcond.keys())
  156. if wl:
  157. for i in wl:
  158. argv={fco.fcd['cc'][i][1]+"__contains":fcond[i]}
  159. td=td.filter(**argv)
  160. wl=filter(lambda x:x in fco.fcd['cuc'].keys(),fcond.keys())
  161. if wl:
  162. for i in wl:
  163. argv={fco.fcd['cuc'][i][1]+"__contains":fcond[i]}
  164. td=td.exclude(**argv)
  165. wl=filter(lambda x:x in fco.fcd['csw'].keys(),fcond.keys())
  166. if wl:
  167. for i in wl:
  168. argv={fco.fcd['csw'][i][1]+"__startswith":fcond[i]}
  169. td=td.filter(**argv)
  170. wl=filter(lambda x:x in fco.fcd['cnsw'].keys(),fcond.keys())
  171. if wl:
  172. for i in wl:
  173. argv={fco.fcd['cnsw'][i][1]+"__startswith":fcond[i]}
  174. td=td.exclude(**argv)
  175. wl=filter(lambda x:x in fco.fcd['cew'].keys(),fcond.keys())
  176. if wl:
  177. for i in wl:
  178. argv={fco.fcd['cew'][i][1]+"__endswith":fcond[i]}
  179. td=td.filter(**argv)
  180. wl=filter(lambda x:x in fco.fcd['cnew'].keys(),fcond.keys())
  181. if wl:
  182. for i in wl:
  183. argv={fco.fcd['cnew'][i][1]+"__endswith":fcond[i]}
  184. td=td.exclude(**argv)
  185. #====================
  186. wl=filter(lambda x:x in fco.fcd['deq'].keys(),fcond.keys())
  187. if wl:
  188. for i in wl:
  189. argv={fco.fcd['deq'][i][1]+"__exact":Str2Date(fcond[i])}
  190. td=td.filter(**argv)
  191. wl=filter(lambda x:x in fco.fcd['drg'].keys(),fcond.keys())
  192. if wl:
  193. for i in wl:
  194. try:
  195. pl=fcond[i].split("~",2)
  196. argv={fco.fcd['drg'][i][1]+"__range":(Str2Date(pl[0]),Str2Date(pl[1]))}
  197. td=td.filter(**argv)
  198. except:
  199. pass
  200. #====================
  201. wl=filter(lambda x:x in fco.fcd['neq'].keys(),fcond.keys())
  202. if wl:
  203. for i in wl:
  204. argv={fco.fcd['neq'][i][1]+"__exact":str2int(fcond[i])}
  205. td=td.filter(**argv)
  206. wl=filter(lambda x:x in fco.fcd['nneq'].keys(),fcond.keys())
  207. if wl:
  208. for i in wl:
  209. argv={fco.fcd['nneq'][i][1]+"__exact":str2int(fcond[i])}
  210. td=td.exclude(**argv)
  211. wl=filter(lambda x:x in fco.fcd['ngt'].keys(),fcond.keys())
  212. if wl:
  213. for i in wl:
  214. if fco.fcd['ngt'][i][2]:
  215. argv={fco.fcd['ngt'][i][1]+"__gte":str2int(fcond[i])}
  216. else:
  217. argv={fco.fcd['ngt'][i][1]+"__gt":str2int(fcond[i])}
  218. td=td.filter(**argv)
  219. wl=filter(lambda x:x in fco.fcd['nlt'].keys(),fcond.keys())
  220. if wl:
  221. for i in wl:
  222. if fco.fcd['nlt'][i][2]:
  223. argv={fco.fcd['nlt'][i][1]+"__lte":str2int(fcond[i])}
  224. else:
  225. argv={fco.fcd['nlt'][i][1]+"__lt":str2int(fcond[i])}
  226. td=td.filter(**argv)
  227. #==============================
  228. wl=filter(lambda x:x in fco.fcd['csc'].keys(),fcond.keys())
  229. if wl:
  230. for i in wl:
  231. if not fcond[i]=="all":
  232. iid=str2int(fcond[i])
  233. idn=fco.fcd['csc'][i][2][iid]
  234. argv={fco.fcd['csc'][i][1]+"__exact":idn}
  235. td=td.filter(**argv)
  236. wl=filter(lambda x:x in fco.fcd['cmc'].keys(),fcond.keys())
  237. if wl:
  238. for i in wl:
  239. idlst=fcond[i].split(',')
  240. idn=map(lambda x:fco.fcd['cmc'][i][2][str2int(x)],idlst)
  241. argv={fco.fcd['cmc'][i][1]+"__in":idn}
  242. td=td.filter(**argv)
  243. return td
  244. def CommonFilter(request,fco,data):
  245. if settings.DEBUG:
  246. return _CommonFilter(request,fco,data)
  247. else:
  248. try:
  249. _CommonFilter(request,fco,data)
  250. except:
  251. return data