diff --git a/bots/markpages.py b/bots/markpages.py index 7fae7c8..9be668a 100644 --- a/bots/markpages.py +++ b/bots/markpages.py @@ -142,7 +142,12 @@ class MarkPagesBot( CurrentPageBot ): # sets 'current_page' on each treat() other template in leading section """ # The notice to add - notice = self.current_page.redfam.generate_disc_notice_template() + self.disc_notice = \ + self.current_page.redfam.generate_disc_notice_template() + + # Check if it is already present in wikicode + if self.disc_notice_present(): + return False # Find the right place to insert notice template # Therfore we need the first section (if there is one) @@ -156,16 +161,43 @@ class MarkPagesBot( CurrentPageBot ): # sets 'current_page' on each treat() # If there is one, add notice after this if ltemplate: - self.current_wikicode.insert_after(ltemplate, notice ) + self.current_wikicode.insert_after(ltemplate, self.disc_notice) # To have it in its own line we need to add a linbreak before - self.current_wikicode.insert_before(notice, "\n" ) + self.current_wikicode.insert_before(self.disc_notice, "\n" ) # If there is no template, add before first element on page else: - self.current_wikicode.insert( 0, notice ) + self.current_wikicode.insert( 0, self.disc_notice ) # If there is no leadsec (and therefore no template in it, we will add # before the first element else: - self.current_wikicode.insert( 0, notice ) + self.current_wikicode.insert( 0, self.disc_notice ) + + # Notice was added + return True + + def disc_notice_present(self): + """ + Checks if disc notice which shall be added is already present. + """ + # Iterate over Templates with same name (if any) to search equal + # Link to decide if they are the same + for present_notice in self.current_wikicode.ifilter_templates( + matches=self.disc_notice.name ): + + # Get reddisc page.title of notice to add + add_notice_link_tile = self.disc_notice.get( + "Diskussion").partition("#")[0] + # Get reddisc page.title of possible present notice + present_notice_link_tile = present_notice.get( + "Diskussion").partition("#")[0] + + # If those are equal, notice is already present + if add_notice_link_tile == present_notice_link_tile: + return True + + # If nothing is found, loop will run till its end + else: + return False