def test_partial_accept(self): text1 = "Organisation de conferences lors de la Fete de la science" text2 = "Organisation mon texte de la Fete de la autre texte science" soup_wrapped, textdiff = html_diff_wrapper.render_html_diff( text1, text2, 'modif') correction_tags = soup_wrapped.find_all('span', {'id': "modif"}) descriminator = 0 for correction_tag in correction_tags: correction_tag['data-item'] = str(descriminator) descriminator += 1 result = html_diff_wrapper.soup_to_text(soup_wrapped) #2 modifs self.assertEqual( result, 'Organisation <span data-item="0" id="modif"><del>de conferences lors</del><ins>mon texte</ins></span> de la Fete de la <span data-item="1" id="modif"><ins>autre texte </ins></span>science' ) items = ['0'] corrections = [] for item in items: corrections.extend( soup_wrapped.find_all('span', { 'id': 'modif', 'data-item': item })) #resfuse the first modif soup = html_diff_wrapper.include_diffs(soup_wrapped, corrections, "ins", "del", None) result = html_diff_wrapper.soup_to_text(soup) self.assertEqual( result, 'Organisation de conferences lors de la Fete de la <span data-item="1" id="modif"><ins>autre texte </ins></span>science' ) items = ['1'] corrections = [] for item in items: corrections.extend( soup_wrapped.find_all('span', { 'id': 'modif', 'data-item': item })) #accept the last modif soup = html_diff_wrapper.include_diffs(soup_wrapped, corrections, "del", "ins", None) result = html_diff_wrapper.soup_to_text(soup) #the text with only the last modif self.assertEqual( result, 'Organisation de conferences lors de la Fete de la autre texte science' ) soup_wrapped, textdiff = html_diff_wrapper.render_html_diff( text1, result, 'modif') result = html_diff_wrapper.soup_to_text(soup_wrapped) #1 modif (accepted modif) self.assertEqual( result, 'Organisation de conferences lors de la Fete de la <span id="modif"><ins>autre texte </ins></span>science' )
def start(self, context, request, appstruct, **kw): user = get_current(request) user = context.working_group.get_member(user) correction = appstruct['_object_data'] correction.text = html_diff_wrapper.normalize_text(correction.text) old_text = correction.text correction.setproperty('author', user) version = context.get_version(user, (context, 'version')) context.addtoproperty('corrections', correction) correction.setproperty('current_version', version) context.setproperty('version', version.version) souptextdiff, textdiff = html_diff_wrapper.render_html_diff( getattr(context, 'text', ''), getattr(correction, 'text', ''), "correction") soupdescriptiondiff, descriptiondiff = html_diff_wrapper.render_html_diff( getattr(context, 'description', ''), getattr(correction, 'description', ''), "correction") souptitlediff, titlediff = html_diff_wrapper.render_html_diff( getattr(context, 'title', ''), getattr(correction, 'title', ''), "correction") descriminator = 0 descriminator = self._identify_corrections(souptitlediff, correction, descriminator, 'title', user) self._add_actions(correction, request, souptitlediff) descriminator = self._identify_corrections(soupdescriptiondiff, correction, descriminator, 'description', user) self._add_actions(correction, request, soupdescriptiondiff) self._identify_corrections(souptextdiff, correction, descriminator, 'text', user) self._add_actions(correction, request, souptextdiff) correction.text = html_diff_wrapper.soup_to_text(souptextdiff) correction.description = html_diff_wrapper.soup_to_text(soupdescriptiondiff) correction.title = html_diff_wrapper.soup_to_text(souptitlediff) if souptextdiff.find_all("span", id="correction") or \ soupdescriptiondiff.find_all("span", id="correction") or\ souptitlediff.find_all("span", id="correction"): correction.state.append('in process') alert('internal', [request.root], context.working_group.members, internal_kind=InternalAlertKind.working_group_alert, subjects=[context], alert_kind='correction_added') else: context.text = old_text return {}
def start(self, context, request, appstruct, **kw): user = get_current(request) user = context.working_group.get_member(user) correction = appstruct['_object_data'] correction.text = html_diff_wrapper.normalize_text(correction.text) old_text = correction.text correction.setproperty('author', user) version = context.get_version(user, (context, 'version')) context.addtoproperty('corrections', correction) correction.setproperty('current_version', version) context.setproperty('version', version.version) souptextdiff, textdiff = html_diff_wrapper.render_html_diff( getattr(context, 'text', ''), getattr(correction, 'text', ''), "correction") soupdescriptiondiff, descriptiondiff = html_diff_wrapper.render_html_diff( getattr(context, 'description', ''), getattr(correction, 'description', ''), "correction") souptitlediff, titlediff = html_diff_wrapper.render_html_diff( getattr(context, 'title', ''), getattr(correction, 'title', ''), "correction") descriminator = 0 descriminator = self._identify_corrections(souptitlediff, correction, descriminator, 'title', user) self._add_actions(correction, request, souptitlediff) descriminator = self._identify_corrections(soupdescriptiondiff, correction, descriminator, 'description', user) self._add_actions(correction, request, soupdescriptiondiff) self._identify_corrections(souptextdiff, correction, descriminator, 'text', user) self._add_actions(correction, request, souptextdiff) correction.text = html_diff_wrapper.soup_to_text(souptextdiff) correction.description = html_diff_wrapper.soup_to_text( soupdescriptiondiff) correction.title = html_diff_wrapper.soup_to_text(souptitlediff) if souptextdiff.find_all("span", id="correction") or \ soupdescriptiondiff.find_all("span", id="correction") or\ souptitlediff.find_all("span", id="correction"): correction.state.append('in process') alert('internal', [request.root], context.working_group.members, internal_kind=InternalAlertKind.working_group_alert, subjects=[context], alert_kind='correction_added') else: context.text = old_text return {}
def update(self): result = {} amendment_viewer = get_current_registry().getUtility( IAmendmentViewer, 'amendment_viewer') souptextdiff, explanations = amendment_viewer.get_explanation_diff( self.context, self.request) amendment_viewer.add_details(explanations, self.context, self.request, souptextdiff, self.readonly_explanation_template) text_diff = html_diff_wrapper.soup_to_text(souptextdiff) not_published_ideas = [] if not self.request.moderate_ideas: not_published_ideas = [i for i in self.context.get_used_ideas() if 'published' not in i.state] values = {'context': self.context, 'explanationtext': text_diff, 'not_published_ideas': not_published_ideas} body = self.content(args=values, template=self.template)['body'] item = self.adapt_item(body, self.viewid) result['coordinates'] = {self.coordinates: [item]} return result
def _get_adapted_content(self, user, text): """Return the appropriate text to the user""" user_ = self.proposal.working_group.get_member(user) user_ = user_ or user soup = BeautifulSoup(text) corrections = soup.find_all("span", id='correction') # if user is self.author: # for correction in corrections: # self._adapt_correction(correction, True) # else: for correction in corrections: correction_data = self.corrections[correction["data-item"]] voters_favour = any((get_obj(v) is user_ for v in correction_data['favour'])) if voters_favour: self._adapt_correction(correction, True) continue voters_against = any((get_obj(v) is user_ for v in correction_data['against'])) if voters_against: self._adapt_correction(correction, False) return html_diff_wrapper.soup_to_text(soup)
def update(self): result = {} amendment_viewer = get_current_registry().getUtility( IAmendmentViewer, 'amendment_viewer') souptextdiff, explanations = amendment_viewer.get_explanation_diff( self.context, self.request) amendment_viewer.add_details(explanations, self.context, self.request, souptextdiff, self.readonly_explanation_template) text_diff = html_diff_wrapper.soup_to_text(souptextdiff) not_published_ideas = [] if not self.request.moderate_ideas: not_published_ideas = [ i for i in self.context.get_used_ideas() if 'published' not in i.state ] values = { 'context': self.context, 'explanationtext': text_diff, 'not_published_ideas': not_published_ideas } body = self.content(args=values, template=self.template)['body'] item = self.adapt_item(body, self.viewid) result['coordinates'] = {self.coordinates: [item]} return result
def get_text_amendment_diff_submitted(amendment, request): amendment_viewer = get_current_registry().getUtility( IAmendmentViewer, 'amendment_viewer') souptextdiff, explanations = amendment_viewer.get_explanation_diff( amendment, request) amendment_viewer.add_details(explanations, amendment, request, souptextdiff) return explanations, html_diff_wrapper.soup_to_text(souptextdiff)
def test_different_treatments_in_one_sentence(self): text1 = "fete de la science.Organiser des animations lors de la Fete de la science qui se deroule au mois d'octobre. Programme: - conferences - expositions - autres" text2 = "Fete de la science. Organiser des animations lors de la Fete de la science qui se deroule au mois du 24/09/2014 au 19/10/2014. Programme: - conferences - expositions - autres" soup, textdiff = html_diff_wrapper.render_html_diff( text1, text2, 'modif') spanids_data = [] spans = soup.find_all('span', {'id': "modif"}) descriminator = 1 for span in spans: span['data-item'] = str(descriminator) descriminator += 1 fselection = self._include_spanids(spans[0:2], "del", "ins") for span in fselection: spanids_data.append(span) lselection = self._include_spanids(spans[2:], "ins", "del") for span in lselection: spanids_data.append(span) html_diff_wrapper.unwrap_diff(spanids_data, soup) soup_to_text = html_diff_wrapper.soup_to_text(soup) self.assertEqual( soup_to_text, "Fete de la science. Organiser des animations lors de la Fete de la science qui se deroule au mois d'octobre. Programme: - conferences - expositions - autres" )
def _remove_css_item(self, context, item): soup = BeautifulSoup(context.text_diff) tag_item = soup.find('button', {'data-target': '#' + str(item)}) if tag_item and self.item_css_on in tag_item['class']: tag_item['class'].remove(self.item_css_on) tag_item['class'].append(self.item_css_off) context.text_diff = html_diff_wrapper.soup_to_text(soup)
def _remove_css_item(self, context, item): soup = BeautifulSoup(context.text_diff) tag_item = soup.find('button', {'data-target': '#'+str(item)}) if tag_item and self.item_css_on in tag_item['class']: tag_item['class'].remove(self.item_css_on) tag_item['class'].append(self.item_css_off) context.text_diff = html_diff_wrapper.soup_to_text(soup)
def get_text_amendment_diff_submitted(amendment, request): amendment_viewer = get_current_registry().getUtility( IAmendmentViewer, 'amendment_viewer') souptextdiff, explanations = amendment_viewer.get_explanation_diff( amendment, request) amendment_viewer.add_details(explanations, amendment, request, souptextdiff) return explanations, html_diff_wrapper.soup_to_text(souptextdiff)
def get_trimed_amendment_text(text): soup, source_tags = _prune_text(text, 'span', {'id': 'explanation'}) explanations_inline_tags = soup.find_all('span', {'class': 'explanation-inline'}) source_tags.extend(explanations_inline_tags) soup.body.clear() for tag in source_tags: soup.body.append(tag) return html_diff_wrapper.soup_to_text(soup)
def get_trimed_amendment_text(text): soup, source_tags = _prune_text(text, 'span', {'id': 'explanation'}) explanations_inline_tags = soup.find_all('span', {'class': 'explanation-inline'}) source_tags.extend(explanations_inline_tags) soup.body.clear() for tag in source_tags: soup.body.append(tag) return html_diff_wrapper.soup_to_text(soup)
def _edit_item(self, context, content, appstruct): soup = BeautifulSoup(getattr(context, content)) items_to_edit = soup.find_all( 'span', {'id': 'correction', 'data-item': appstruct['item']}) ins_to_edit = items_to_edit[0].find_all('ins')[0] soup_to_add = BeautifulSoup(appstruct['new_text']) new_ins = soup.new_tag("ins") soup_to_add.p.wrap(new_ins) soup_to_add.p.unwrap() new_ins = soup_to_add.ins ins_to_edit.replace_with(new_ins) setattr(context, content, html_diff_wrapper.soup_to_text(soup))
def test_partial_accept(self): text1 = "Organisation de conferences lors de la Fete de la science" text2 = "Organisation mon texte de la Fete de la autre texte science" soup_wrapped, textdiff = html_diff_wrapper.render_html_diff(text1, text2, 'modif') correction_tags = soup_wrapped.find_all('span', {'id': "modif"}) descriminator = 0 for correction_tag in correction_tags: correction_tag['data-item'] = str(descriminator) descriminator += 1 result = html_diff_wrapper.soup_to_text(soup_wrapped) #2 modifs self.assertEqual(result, 'Organisation <span data-item="0" id="modif"><del>de conferences lors</del><ins>mon texte</ins></span> de la Fete de la <span data-item="1" id="modif"><ins>autre texte </ins></span>science') items = ['0'] corrections = [] for item in items: corrections.extend(soup_wrapped.find_all('span', {'id':'modif', 'data-item': item})) #resfuse the first modif soup = html_diff_wrapper.include_diffs(soup_wrapped, corrections, "ins", "del", None) result = html_diff_wrapper.soup_to_text(soup) self.assertEqual(result, 'Organisation de conferences lors de la Fete de la <span data-item="1" id="modif"><ins>autre texte </ins></span>science') items = ['1'] corrections = [] for item in items: corrections.extend(soup_wrapped.find_all('span', {'id':'modif', 'data-item': item})) #accept the last modif soup = html_diff_wrapper.include_diffs(soup_wrapped, corrections, "del", "ins", None) result = html_diff_wrapper.soup_to_text(soup) #the text with only the last modif self.assertEqual(result, 'Organisation de conferences lors de la Fete de la autre texte science') soup_wrapped, textdiff = html_diff_wrapper.render_html_diff(text1, result, 'modif') result = html_diff_wrapper.soup_to_text(soup_wrapped) #1 modif (accepted modif) self.assertEqual(result, 'Organisation de conferences lors de la Fete de la <span id="modif"><ins>autre texte </ins></span>science')
def _edit_item(self, context, content, appstruct): soup = BeautifulSoup(getattr(context, content)) items_to_edit = soup.find_all('span', { 'id': 'correction', 'data-item': appstruct['item'] }) ins_to_edit = items_to_edit[0].find_all('ins')[0] soup_to_add = BeautifulSoup(appstruct['new_text']) new_ins = soup.new_tag("ins") soup_to_add.p.wrap(new_ins) soup_to_add.p.unwrap() new_ins = soup_to_add.ins ins_to_edit.replace_with(new_ins) setattr(context, content, html_diff_wrapper.soup_to_text(soup))
def get_trimed_proposal_text(text, amendments): merged_text = html_diff_wrapper.get_merged_diffs( text, amendments, {'id': 'modification', 'class': 'text-removed'}, {'id': 'modification', 'class': 'glyphicon glyphicon-plus text-added'}) soup, source_tags = _prune_text( merged_text, 'span', {'id': 'modification'}) soup.body.clear() for tag in source_tags: soup.body.append(tag) return html_diff_wrapper.soup_to_text(soup)
def get_trimed_proposal_text(text, amendments): merged_text = html_diff_wrapper.get_merged_diffs( text, amendments, { 'id': 'modification', 'class': 'text-removed' }, { 'id': 'modification', 'class': 'glyphicon glyphicon-plus text-added' }) soup, source_tags = _prune_text(merged_text, 'span', {'id': 'modification'}) soup.body.clear() for tag in source_tags: soup.body.append(tag) return html_diff_wrapper.soup_to_text(soup)
def _include_items(self, text, request, items, to_add=False): todel = "ins" toins = "del" if to_add: todel = "del" toins = "ins" soup = BeautifulSoup(text) corrections = [] for item in items: corrections.extend(soup.find_all('span', {'id': 'correction', 'data-item': item})) blocstodel = ('span', {'id': 'correction_actions'}) soup = html_diff_wrapper.include_diffs( soup, corrections, todel, toins, blocstodel) return html_diff_wrapper.soup_to_text(soup)
def _include_items(self, text, request, items, to_add=False): todel = "ins" toins = "del" if to_add: todel = "del" toins = "ins" soup = BeautifulSoup(text) corrections = [] for item in items: corrections.extend( soup.find_all('span', { 'id': 'correction', 'data-item': item })) blocstodel = ('span', {'id': 'correction_actions'}) soup = html_diff_wrapper.include_diffs(soup, corrections, todel, toins, blocstodel) return html_diff_wrapper.soup_to_text(soup)
def _get_amendment_text(self, context, group): items = [str(e['oid']) for e in group] soup = BeautifulSoup(context.text_diff) allexplanations = soup.find_all('span', {'id': 'explanation'}) explanations = [tag for tag in allexplanations if tag['data-item'] not in items] blocstodel = ('span', {'id': 'explanation_action'}) soup = html_diff_wrapper.include_diffs( soup, explanations, "ins", "del", blocstodel) explanations = [tag for tag in allexplanations if tag['data-item'] in items] soup = html_diff_wrapper.include_diffs( soup, explanations, "del", "ins", blocstodel) allmodal = [m for m in soup.find_all('div', {'class': 'modal'}) if m['id'].endswith("explanation_modal")] for modal in allmodal: modal.extract() text = html_diff_wrapper.soup_to_text(soup) return html_diff_wrapper.normalize_text(text)
def test_different_treatments_in_one_sentence(self): text1 = "fete de la science.Organiser des animations lors de la Fete de la science qui se deroule au mois d'octobre. Programme: - conferences - expositions - autres" text2 = "Fete de la science. Organiser des animations lors de la Fete de la science qui se deroule au mois du 24/09/2014 au 19/10/2014. Programme: - conferences - expositions - autres" soup, textdiff = html_diff_wrapper.render_html_diff(text1, text2, 'modif') spanids_data = [] spans = soup.find_all('span', {'id': "modif"}) descriminator = 1 for span in spans: span['data-item'] = str(descriminator) descriminator += 1 fselection = self._include_spanids(spans[0:2], "del", "ins") for span in fselection: spanids_data.append(span) lselection = self._include_spanids(spans[2:], "ins", "del") for span in lselection: spanids_data.append(span) html_diff_wrapper.unwrap_diff(spanids_data, soup) soup_to_text = html_diff_wrapper.soup_to_text(soup) self.assertEqual(soup_to_text, "Fete de la science. Organiser des animations lors de la Fete de la science qui se deroule au mois d'octobre. Programme: - conferences - expositions - autres")
def _get_amendment_text(self, context, group): items = [str(e['oid']) for e in group] soup = BeautifulSoup(context.text_diff) allexplanations = soup.find_all('span', {'id': 'explanation'}) explanations = [ tag for tag in allexplanations if tag['data-item'] not in items ] blocstodel = ('span', {'id': 'explanation_action'}) soup = html_diff_wrapper.include_diffs(soup, explanations, "ins", "del", blocstodel) explanations = [ tag for tag in allexplanations if tag['data-item'] in items ] soup = html_diff_wrapper.include_diffs(soup, explanations, "del", "ins", blocstodel) allmodal = [ m for m in soup.find_all('div', {'class': 'modal'}) if m['id'].endswith("explanation_modal") ] for modal in allmodal: modal.extract() text = html_diff_wrapper.soup_to_text(soup) return html_diff_wrapper.normalize_text(text)
def _entry_to_result(self, text1, text2, decision): soup_wrapped, textdiff = html_diff_wrapper.render_html_diff(text1, text2, 'modif') self._unwrap_spans('modif', decision, soup_wrapped) soup_to_text = html_diff_wrapper.soup_to_text(soup_wrapped) return soup_to_text
def _entry_to_result(self, text1, text2, decision): soup_wrapped, textdiff = html_diff_wrapper.render_html_diff( text1, text2, 'modif') self._unwrap_spans('modif', decision, soup_wrapped) soup_to_text = html_diff_wrapper.soup_to_text(soup_wrapped) return soup_to_text