Fix bug in publisher and author updates

select-best-date
Mike Benowitz 2019-08-07 09:46:24 -04:00
parent ffa3ed6d5b
commit 7ede495a31
2 changed files with 25 additions and 8 deletions

View File

@ -47,7 +47,7 @@ class CCE(Core, Base):
lccns = relationship('LCCN', backref='cce', cascade='all, delete-orphan')
authors = relationship('Author',
backref='cce', cascade='all, delete-orphan')
publishers = relationship('Publisher',
publishers = relationship('Publisher',
backref='cce', cascade='all, delete-orphan')
def __repr__(self):
@ -120,25 +120,27 @@ class CCE(Core, Base):
def updateAuthors(self, authors):
currentAuthors = [(a.name, a.primary) for a in self.authors]
newAuthors = filter(lambda x: x[0] is None, authors)
newAuthors = filter(lambda x: x[0] is not None, authors)
if newAuthors != currentAuthors:
self.authors = [
a for a in self.authors
if a.name in list(set(currentAuthors) & set(newAuthors))
if (a.name, a.primary) in
list(set(currentAuthors) & set(newAuthors))
]
for new in list(set(newAuthors) - set(currentAuthors)):
self.authors.append(Author(name=new[0], primary=new[1]))
def updatePublishers(self, publishers):
currentPublishers = [(a.name, a.claimant) for a in self.publishers]
currentPublishers = [(p.name, p.claimant) for p in self.publishers]
newPublishers = [
(p[0], True if p[1] == 'yes' else False)
for p in filter(lambda x: x[0] is None, publishers)
for p in filter(lambda x: x[0] is not None, publishers)
]
if newPublishers != currentPublishers:
self.authors = [
a for a in self.authors
if a.name in list(set(currentPublishers) & set(newPublishers))
self.publishers = [
p for p in self.publishers
if (p.name, p.claimant) in
list(set(currentPublishers) & set(newPublishers))
]
for new in list(set(newPublishers) - set(currentPublishers)):
self.publishers.append(Publisher(name=new[0], claimant=new[1]))

View File

@ -56,3 +56,18 @@ class TestModelCCE(object):
mockCCE.addLCCN([1, 2])
assert mockCCE.lccns[1].name == 'lccn2'
def test_updatePublishers(self, mockCCE):
mockPub = MagicMock()
mockPub.name = 'Removed, Name'
mockPub.claimant = True
mockCCE.publishers.append(mockPub)
newPublishers = [
(None, 'yes'),
('Test, Testing', 'yes'),
('Removed, Name', 'yes')
]
mockCCE.updatePublishers(newPublishers)
assert len(list(mockCCE.publishers)) == 2
assert list(mockCCE.publishers)[1].name == 'Test, Testing'