def add_ref(self, tref): try: new_oref = Ref(tref) except InputError as e: raise ManuscriptError(e) for oref in self.get_ref_objects(): if oref.overlaps(new_oref): raise ManuscriptError( f'Overlap between contained refs {oref} and {new_oref}') self.contained_refs.append(tref) self.expanded_refs.extend(self.get_expanded_refs_for_source(new_oref))
def _validate(self): super(ManuscriptPage, self)._validate() # check that the manuscript this page is part of exists in the database if self.get_manuscript() is None: raise ManuscriptError("Manuscript missing in database") for tref in self.contained_refs: if not Ref.is_ref(tref): raise ManuscriptError(f'{tref} is not a valid Ref') test_refs = self.get_ref_objects() while test_refs: current_ref = test_refs.pop() for tr in test_refs: if current_ref.overlaps(tr): raise ManuscriptError( f'Overlap between contained refs {tr} and {current_ref}' ) if not len(test_refs): break
def _pre_save(self): self.expanded_refs = list(set( self.expanded_refs)) # clear out duplicates # make sure we're not adding duplicates manuscript_id, page_id = getattr(self, 'manuscript_slug', None), getattr(self, 'page_id', None) if manuscript_id is None or page_id is None: # important to check for None explicitly, page_id=0 is valid raise ManuscriptError('No manuscript_id or page_id') if self.is_new(): duplicate = ManuscriptPage().load({ 'manuscript_id': manuscript_id, 'page_id': page_id }) if duplicate: raise DuplicateRecordError( "Record already exists. Please update existing instead of adding new." )
def get_ref_objects(self): try: return [Ref(tref) for tref in self.contained_refs] except InputError: raise ManuscriptError( 'bad ref associated with this Manuscript Page')