def expiry(z): # Get prop record record = Props.objects.get(id=z) # Get article number article_number = record.microcons_id # Save into object instance. record.currency = "expired" # Send back into database. record.save() # Get article record article = MicroCons.objects.get(id=article_number) # get instance for foreign key relationship instance = MicroCons.objects.get(id=article.id) if record.pass_status == "pass": dfunction = diff_match_patch() patch = record.patch qset = (Articles.objects.filter(cons_id=article_number).order_by('version_id').reverse())[:1] q = qset[0] content = q.articlecontent previous_version_id = q.version_id new_version_id = previous_version_id + 1 patched_content = dfunction.patch_apply(patch, content) # Gets the new content and leaves out the result. formatted_content = patched_content[0] # creating the new record to be saved. n = Articles( cons_id = article_number, cons_id_key = instance, version_id=new_version_id, articlecontent=formatted_content, ) # Ensures a new row, not an amendment to old row. n.pk = None n.save() record = Props.objects.get(id=z) record.success = "yes" record.save() else: record = Props.objects.get(id=z) record.success = "no" record.save()
def prop_accept(request, propid): prop_record = Props.objects.get(id=propid) dfunction = diff_match_patch() articleid = prop_record.microcons_id patch = prop_record.patch article_record = MicroCons.objects.get(id=articleid) articlecontent = article_record.articlecontent article_record = MicroCons.objects.get(id=articleid) prop_text = article_record.articlecontent.encode("utf8") result = dfunction.patch_apply(patch, prop_text) newcontent = result[0] MicroCons.objects.filter(id=articleid).update(articlecontent=newcontent) return render_to_response('done.html', context_instance=RequestContext(request))
def create_prop(request, articleid): if request.method == 'POST': dfunction = diff_match_patch() u = request.user # Creating version to be amended to run diff against. first = MicroCons.objects.get(id__contains=articleid) content = (Articles.objects.filter(cons_id=articleid).order_by('version_id').reverse())[:1] blah = content[0] data = blah.articlecontent hours_number = first.prop_hours r = first.majority formatted = data.encode("utf8") form = PropForm(request.POST) if form.is_valid(): # Running diff functions and creating entries. data = form.cleaned_data['article'] utf_data = data.encode("utf8") # creating callable function for diff diff = main_diff_word_mode(formatted, utf_data) patchdata = dfunction.patch_make(formatted, diff) # Grabbing all the outstanding propositions. x = Props.objects.filter(microcons_id=articleid).filter(success="undetermined") # add outstanding patches and current patch to list patch_list = [] for i in x: patch_list.append(i.patch[0]) patch_list.append(patchdata[0]) print patch_list patch_attempt = dfunction.patch_apply(patch_list, formatted) for i in patch_attempt[1]: if i == False: return render_to_response('conflict_alert.html') break else: pass # creating pretty html diff to display diffhtml = dfunction.diff_prettyHtml(diff) time_object = datetime.now() + timedelta(minutes=hours_number) # Processing diff into different lengths. diff_html_long = long_diff_html(diff) p = Props( microcons_id=articleid, cons_id_key=first, author=u, maindiff=diff, short_diff="", medium_diff="", long_diff=diff_html_long, patch=patchdata, htmldiff=diffhtml, expiry_time=time_object, currency="current", vote_for=0, vote_against=0, total_votes=0, percentage_for=0, threshold=r, pass_status="fail", success = "undetermined") p.save() next=p.id # Programming the expiry of the prop. expiry.apply_async(args=[next], eta=time_object) # Updating latest prop time for microconstitution model f = MicroCons.objects.get(id=p.microcons_id) f.last_prop = datetime.now() f.save() return render_to_response('prop_confirm.html', {'diff': diffhtml, 'time_object': time_object, 'hours_number': hours_number, 'micro_cons': r}, context_instance=RequestContext(request)) else: # Put together initial data for the form. q = (Articles.objects.filter(cons_id=articleid).order_by('version_id').reverse())[:1] data = q[0] first = data.articlecontent # Put together data for text affected by outstanding props. x = Props.objects.filter(microcons_id=articleid).filter(success="undetermined") outstanding_changes = arrange_props(articleid) print outstanding_changes # initial = first.encode("utf8") thesis = MicroCons.objects.get(id__contains=articleid).thesis # Displaying initial data in form. populate = MicroCons.objects.get(id=articleid) form = PropForm(initial={'article': first}) return render_to_response( 'edit_article.html', { 'outstanding_changes':outstanding_changes, 'form': form, 'thesis': thesis }, context_instance=RequestContext(request) )