summaryrefslogtreecommitdiff
path: root/objectapp/views/history.py
blob: e5e2ff1fd9c9120fa64093d5f1ead2c0d332c291 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
from django.http import *
from reversion.models import *
from gstudio.models import *
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.views.generic.date_based import object_detail
from reversion.helpers import *
import pprint
from gstudio.models import *
from reversion.models import *
from gstudio.views.decorators import protect_nodetype
from gstudio.views.decorators import update_queryset
import ast
from objectapp.models import *

def history(request,ssid,version_no):
   # iden=request.GET["id"]
    nt1=Version.objects.get(id=ssid)
    nt=nt1.object.ref
    ver_dict=nt.version_info(ssid)
    ver_nbh=ver_dict['nbhood']
    ver_nbh_dict=ast.literal_eval(ver_nbh) 
    content=ver_dict['content']
    content=content[3:-4]
    ver_nbh_dict['content']=content
	
    variables = RequestContext(request,{'ver_nbh_dict':ver_nbh_dict ,'nt':nt,'ssid':ssid,'version_no':version_no})
    template="objectapp/display.html"
    return render_to_response(template,variables)

def get_version_counter(value):
    counter1=str(value)
    index=counter1.rfind(".")
    counter1=counter1[index+1:]
    version_no=int(counter1)
    return version_no
	

def get_diff_from_dict(ver_new_nbh_dict,ver_old_nbh_dict,field):
    ver_new=""
    ver_old=""    
    diffs=""
    ver_new_dict={}
    ver_old_dict={}
             
    if ver_new_nbh_dict[field] or ver_old_nbh_dict[field]:
#	if isinstance(ver_new_nbh_dict[field],dict):
		for each in ver_new_nbh_dict[field]:
			#ver_new=ver_new_dict[each]=
			ver_new+=str(each)+","
		ver_new=ver_new[0:-1]
		for each in ver_old_nbh_dict[field]:
			ver_old+=str(each)+","
		ver_old=ver_old[0:-1]
		diffs = dmp.diff_main(ver_new, ver_old)
    return diffs

def get_diff_from_nested_dict(ver_new_nbh_dict,ver_old_nbh_dict,field):
    
		
    return compare	  
def compare_history(request,ssid):
    ssid1=ssid
    
    version_counter1=request.GET["group1"]
    version_no1=get_version_counter(version_counter1)
    version_counter2=request.GET["group2"]
    version_no2=get_version_counter(version_counter2)
    counter2=float(version_counter2)
    ssid2=int(counter2)

    ver_obj=Version.objects.get(id=ssid1)
    ot=ver_obj.object.ref
    pp=pprint.PrettyPrinter(indent=4)
    
    ver_new_dict=ot.version_info(ssid1)
    content=str(ver_new_dict['content'])
    content=content[3:-4]
    ver_new_dict['content']=content

    ver_old_dict=ot.version_info(ssid2)
    content=str(ver_old_dict['content'])
    content=content[3:-4]
    ver_old_dict['content']=content
    
    ver_new_nbh=ver_new_dict['nbhood']
    ver_new_nbh_dict=ast.literal_eval(ver_new_nbh)
    
    ver_old_nbh=ver_old_dict['nbhood']
    ver_old_nbh_dict=ast.literal_eval(ver_old_nbh)

    compare_dict={}
    for each in ver_new_nbh_dict:
	 ver_new=""
         ver_old=""
	 if each=='altnames':
		if ver_new_nbh_dict['altnames'] or ver_old_nbh_dict['altnames']:
         		ver_new+=ver_new_nbh_dict['altnames']
			ver_old+=ver_old_nbh_dict['altnames']  
		  	diffs = dmp.diff_main(ver_new, ver_old)
			compare_dict['altnames']=dmp.diff_prettyHtml(diffs)
	
    		
         elif each=='title':
		if ver_new_nbh_dict['title'] or ver_old_nbh_dict['title']:
			ver_new+=ver_new_nbh_dict['title']
			ver_old+=ver_old_nbh_dict['title'] 
			diffs = dmp.diff_main(ver_new, ver_old)
    			compare_dict['title']=dmp.diff_prettyHtml(diffs)
	 elif each =='plural':
		if ver_new_nbh_dict['plural'] or ver_old_nbh_dict['plural']:
         		ver_new+=ver_new_nbh_dict['plural']
			ver_old+=ver_old_nbh_dict['plural'] 
			diffs = dmp.diff_main(ver_new, ver_old)
    			compare_dict['plural']=dmp.diff_prettyHtml(diffs)
	 elif each =='member_of':
	        diffs=get_diff_from_dict(ver_new_nbh_dict,ver_old_nbh_dict,each)
    		compare_dict['member_of']=dmp.diff_prettyHtml(diffs)
	 elif each =='leftroles':
		diffs=get_diff_from_dict(ver_new_nbh_dict,ver_old_nbh_dict,each)
    		compare_dict['leftroles']=dmp.diff_prettyHtml(diffs)
	 elif each =='ats':
		diffs=get_diff_from_dict(ver_new_nbh_dict,ver_old_nbh_dict,each)
    		compare_dict['ats']=dmp.diff_prettyHtml(diffs)
	 elif each =='rightroles':
		diffs=get_diff_from_dict(ver_new_nbh_dict,ver_old_nbh_dict,each)
    		compare_dict['rightroles']=dmp.diff_prettyHtml(diffs)
	 elif each =='attributes':
		
		compare_rel_new={}
		compare_rel_old={}
		compare={}
		for rkey,rvalue in ver_new_nbh_dict[each].items():
			ver_new=""
        		for relvalue in rvalue:
				ver_new+=str(relvalue) + ","
                	ver_new=ver_new[0:-1]
	        	compare_rel_new[str(rkey)]=ver_new
    		for rkey,rvalue in ver_old_nbh_dict[each].items():
			ver_old=""
        		for rv in rvalue:
				ver_old+=str(rv) + ","
                	ver_old=ver_old[0:-1]
	        	compare_rel_old[str(rkey)]=ver_old
    		if len(compare_rel_new) >= len(compare_rel_old): 
        		for rkey,rvalue in compare_rel_new.items():
				if compare_rel_old.has_key(rkey):
					diffs=dmp.diff_main(compare_rel_new[str(rkey)],compare_rel_old[str(rkey)])		
  					compare[str(rkey)]=dmp.diff_prettyHtml(diffs)
			
				else:
					diffs=dmp.diff_main(compare_rel_new[str(rkey)],"")		
  					compare[str(rkey)]=dmp.diff_prettyHtml(diffs)
    		else: 
			for rkey,rvalue in compare_rel_old.items():
				if compare_rel_new.has_key(rkey):
					diffs=dmp.diff_main(compare_rel_new[str(rkey)],compare_rel_old[str(rkey)])		
  					compare[str(rkey)]=dmp.diff_prettyHtml(diffs)
				else:	
					diffs=dmp.diff_main("",compare_rel_old[str(rkey)])		
  					compare[str(rkey)]=dmp.diff_prettyHtml(diffs)
		compare_dict['attributes']=compare
    	  
	 elif each =='relations':
		ver_new=""
    		ver_old=""
   	 	compare_rel_new={}
    		compare_rel_old={}
    		compare={}
    		for rkey,rvalue in ver_new_nbh_dict[each].items():
			ver_new=""
        		for relk,relvalue in rvalue.items():
				ver_new+=str(relk) + ","
                		ver_new=ver_new[0:-1]
	        		compare_rel_new[str(rkey)]=ver_new
    		for rkey,rvalue in ver_old_nbh_dict[each].items():
			ver_old=""
        		for relk,relvalue in rvalue.items():
				ver_old+=str(relk) + ","
                		ver_old=ver_old[0:-1]
	        		compare_rel_old[str(rkey)]=ver_old
    		if len(compare_rel_new) >= len(compare_rel_old): 
        		for rkey,rvalue in compare_rel_new.items():
				if compare_rel_old.has_key(rkey):
					diffs=dmp.diff_main(compare_rel_new[str(rkey)],compare_rel_old[str(rkey)])		
  					compare[str(rkey)]=dmp.diff_prettyHtml(diffs)
			
				else:
					diffs=dmp.diff_main(compare_rel_new[str(rkey)],"")		
  					compare[str(rkey)]=dmp.diff_prettyHtml(diffs)
    		else: 
			for rkey,rvalue in compare_rel_old.items():
				if compare_rel_new.has_key(rkey):
					diffs=dmp.diff_main(compare_rel_new[str(rkey)],compare_rel_old[str(rkey)])		
  					compare[str(rkey)]=dmp.diff_prettyHtml(diffs)
				else:	
					diffs=dmp.diff_main("",compare_rel_old[str(rkey)])		
  					compare[str(rkey)]=dmp.diff_prettyHtml(diffs)
		compare_dict['relations']=compare
	 elif each =='priornodes':
		diffs=get_diff_from_dict(ver_new_nbh_dict,ver_old_nbh_dict,each)
    		compare_dict['priornodes']=dmp.diff_prettyHtml(diffs)
	 elif each =='posteriornodes':
		diffs=get_diff_from_dict(ver_new_nbh_dict,ver_old_nbh_dict,each)
    		compare_dict['posteriornodes']=dmp.diff_prettyHtml(diffs)
	 elif each =='type_of':
		diffs=get_diff_from_dict(ver_new_nbh_dict,ver_old_nbh_dict,each)
    		compare_dict['type_of']=dmp.diff_prettyHtml(diffs)
	 elif each =='contains_subtypes':
		diffs=get_diff_from_dict(ver_new_nbh_dict,ver_old_nbh_dict,each)
    		compare_dict['contains_subtypes']=dmp.diff_prettyHtml(diffs)
	 elif each =='member_of_metatypes':
		diffs=get_diff_from_dict(ver_new_nbh_dict,ver_old_nbh_dict,each)
    		compare_dict['member_of_metatypes']=dmp.diff_prettyHtml(diffs)
	
 
    ver_new=""
    ver_old=""
    ver_new+=ver_new_dict['content']
    ver_old+=ver_old_dict['content']
    diffs = dmp.diff_main(ver_new, ver_old)
    compare_dict['content']=dmp.diff_prettyHtml(diffs)
    ver_new_nbh_dict['content']=ver_new_dict['content']
    ver_old_nbh_dict['content']=ver_old_dict['content']

    

    variables=RequestContext(request,{'nt':ot,'ver_old_dict':ver_old_dict,'ver_new_dict':ver_new_dict,'compare_dict':compare_dict ,'ssid1':ssid1,'ssid2':ssid2,'version_no1':version_no1,'version_no2':version_no2,'ver_new_nbh_dict':ver_new_nbh_dict,'ver_old_nbh_dict':ver_old_nbh_dict})
    template="objectapp/version_diff.html"
    return render_to_response(template,variables)


def get_merge_dict(ssid1,ssid2,direction):
     ver_merge={}
     ot=Version.objects.get(id=ssid1)
     obj=ot.object.ref
     ver_left_dict=obj.version_info(ssid1)
     ver_right_dict=obj.version_info(ssid2)

    
     
     if direction =='right':
        # swap left and right  
	 temp_dict={}
	 temp_dict=ver_left_dict
	 ver_left_dict=ver_right_dict
	 ver_right_dict=temp_dict
        # swap ssid1 and ssid2 for managing nbhood history
         temp=int(ssid1)
         ssid1=int(ssid2)
         ssid2=temp
     ver_left_nbh_dict=ast.literal_eval(ver_left_dict['nbhood'])
     ver_right_nbh_dict=ast.literal_eval(ver_right_dict['nbhood'])
     # By default value of content is removed 
     if ver_left_dict['content']=='<br />':
	 ver_left_dict['content']=''
     if ver_right_dict['content']=='<br />':
	 ver_right_dict['content']=''
     # Getting merged dictionary
     for each in ver_left_dict:
         if ver_left_dict[each] and ver_right_dict[each]:
         	ver_merge[each]=ver_right_dict[each]
         elif ver_left_dict[each]:
                  if not ver_right_dict[each]:
         		ver_merge[each]=ver_left_dict[each]
         elif ver_right_dict[each]:
         	 if not ver_left_dict[each]:
         		ver_merge[each]=ver_right_dict[each]
         elif not ver_left_dict[each]:
		 if not ver_right_dict[each]:
                        ver_merge[each]=''
     ver_merge_nbh_dict={}    
     # processing nbhood for merged version
     for each in ver_left_nbh_dict:
         if isinstance(ver_left_nbh_dict[each],dict):
         	if ver_left_nbh_dict[each] and ver_right_nbh_dict[each]:
			ver_merge_nbh_dict[each]=dict(ver_left_nbh_dict[each].items()+ver_right_nbh_dict[each].items())
                elif ver_left_nbh_dict[each]:
			if not ver_right_nbh_dict[each]:
				ver_merge_nbh_dict[each]=ver_left_nbh_dict[each]
		elif ver_right_nbh_dict[each]:
			if not ver_left_nbh_dict[each]:
				ver_merge_nbh_dict[each]=ver_right_nbh_dict[each]
		else:
			if not ver_left_nbh_dict[each]: 
				if not ver_right_nbh_dict[each]:
					ver_merge_nbh_dict[each]=ver_right_nbh_dict[each]
         else:
		if ver_left_nbh_dict[each] and ver_right_nbh_dict[each]:
         		ver_merge_nbh_dict[each]=ver_right_nbh_dict[each]
         	elif ver_left_nbh_dict[each]:
                  	if not ver_right_nbh_dict[each]:
         			ver_merge_nbh_dict[each]=ver_left_nbh_dict[each]
         	elif ver_right_nbh_dict[each]:
         		if not ver_left_nbh_dict[each]:
         			ver_merge_nbh_dict[each]=ver_right_nbh_dict[each]
         	else:
			if not ver_left_nbh_dict[each]:
			  	if not ver_right_nbh_dict[each]:
                        		ver_merge_nbh_dict[each]=ver_right_nbh_dict[each]
		 
     # Removing auto generated fields
     del(ver_merge['start_publication'])
     del(ver_merge['end_publication'])
     del(ver_merge['creation_date'])
     del(ver_merge['last_update'])
     
     history_left_list=[]
     history_right_list=[]
     history_merged_list=[]

     history_left_list=ver_left_nbh_dict['history']
     history_left_list.append(ssid1)
     history_right_list=ver_right_nbh_dict['history']
     history_right_list.append(ssid2)
     history_merged_list.append(history_left_list)
     history_merged_list.append(history_right_list)
     ver_merge_nbh_dict['history']=history_merged_list
     obj.nbhood = unicode(ver_merge_nbh_dict)
     
     # setting the objecttypes fields	
     obj.slug = ver_merge['slug']
     obj.altnames=ver_merge['altnames']
     obj.nodemodel = ver_merge['nodemodel']
     obj.comment_enabled = ver_merge['comment_enabled']
     obj.title = ver_merge['title']
     obj.sites = ver_merge['sites']
     obj.content = ver_merge['content']
     obj.template = ver_merge['template']
     
     obj.plural = ver_merge['plural']
     obj.status = ver_merge['status']
     obj.nid_ptr = NID.objects.get(id=ver_merge['nid_ptr'])
     obj.nbhood = ver_merge_nbh_dict
    # obj.nbh=ver_merge['nbh']
     obj.id = ver_merge['id']
     obj.pingback_enabled = ver_merge['pingback_enabled']
     obj.save_revert_or_merge()	
     # formatting content field
     content=ver_merge['content']
     content=content[3:-4]
     ver_merge['content']= content 			
      
     return ver_merge
     

def merge_version(request,ssid1,ssid2):
     direction=""
     ver_merge={}
     for each in request.GET:
	 direction=each
     ot=Version.objects.get(id=ssid1)
     obj=ot.object.ref
     ver_merge=get_merge_dict(ssid1,ssid2,direction)
     slist=[]
     slist=obj.get_ssid
     version_counter=len(slist)
     merged_ver_ssid=slist[version_counter-1]
     ver_merged_dict=obj.version_info(merged_ver_ssid)
     ver_merged_nbh_dict=ast.literal_eval(ver_merged_dict['nbhood'])
     ver_merged_nbh_dict['content']=ver_merge['content']
     variables = RequestContext(request,{'ver_nbh_dict':ver_merged_nbh_dict ,'nt':obj,'ssid':merged_ver_ssid,'version_no':version_counter})
     template="objectapp/display.html"
     return render_to_response(template,variables)

def revert(ssid):
     ver_revert={}
     ot=Version.objects.get(id=ssid)
     obj=ot.object.ref
     ver_revert=obj.version_info(ssid)
     
     # Removing auto generated fields
     del(ver_revert['start_publication'])
     del(ver_revert['end_publication'])
     del(ver_revert['creation_date'])
     del(ver_revert['last_update'])
     
     # setting nbhood history 
     history=[]
     ver_revert_nbh_dict=ast.literal_eval(ver_revert['nbhood'])
     
     history=ver_revert_nbh_dict['history']
     history.append(ssid)
     ver_revert_nbh_dict['history']=history
     
     # setting the revert version fields	
     obj.slug = ver_revert['slug']
     obj.altnames=ver_revert['altnames']
    
     obj.nodemodel = ver_revert['nodemodel']
    
     obj.comment_enabled = ver_revert['comment_enabled']
     obj.title = ver_revert['title']
     obj.sites = ver_revert['sites']
     obj.content = ver_revert['content']
     obj.template = ver_revert['template']
   
     obj.plural = ver_revert['plural']
     obj.status = ver_revert['status']
     obj.nid_ptr = NID.objects.get(id=ver_revert['nid_ptr'])
     obj.nbhood = unicode(ver_revert_nbh_dict)
    # obj.nbh=ver_revert['nbh']
     obj.id = ver_revert['id']
     obj.pingback_enabled = ver_revert['pingback_enabled']
     obj.save_revert_or_merge()	
     # formatting content field
     content=ver_revert['content']
     content=content[3:-4]
     ver_revert['content']= content 			
      
     return ver_revert
     
def revert_version(request):
     ver_revert={}
     for each in request.GET:
     	   ssid=each
     ssid=int(ssid)
     ot=Version.objects.get(id=ssid)
     obj=ot.object.ref
     
     ver_revert=revert(ssid)
     ver_revert['nbhood']
     slist=[]
     slist=obj.get_ssid
     version_counter=len(slist)
     revert_ver_ssid=slist[version_counter-1]
     ver_revert_dict=obj.version_info(revert_ver_ssid)
     ver_revert_nbh_dict=ast.literal_eval(ver_revert_dict['nbhood'])
     ver_revert_nbh_dict['content']=ver_revert['content']

     variables = RequestContext(request,{'ver_nbh_dict':ver_revert_nbh_dict ,'nt':obj,'ssid':revert_ver_ssid,'version_no':version_counter})
     template="objectapp/display.html"
     return render_to_response(template,variables)
    # return HttpResponse(ver_revert['nbhood'])