def test__reorder(self): links = CustomLinks(db.creator, self._creator['id']) original = self._ordered_ids() links.reorder() got = self._ordered_ids() self.assertEqual(got, original) self.assertEqual(self._order_nos(), [1, 2, 3]) link_ids = [original[2], original[1], original[0]] links.reorder(link_ids=link_ids) got = self._ordered_ids() self.assertEqual(got, link_ids) self.assertEqual(self._order_nos(), [1, 2, 3]) # Test that holes are closed. link_ids = [original[0], original[1], original[2]] links.reorder(link_ids=link_ids) for x in self._ordered_records(): x.update_record(order_no=db.creator_to_link.order_no * 2) db.commit() self.assertEqual(self._order_nos(), [2, 4, 6]) links.reorder(link_ids=link_ids) self.assertEqual(self._order_nos(), [1, 2, 3])
def order_no_handler(): """Handler for order_no sorting. request.args(0): string, link table name, eg creator_to_link request.args(1): integer, id of record in table. request.args(2): string, direction, 'up' or 'down' """ next_url = request.vars.next or URL(c='default', f='index') if not request.args(0): redirect(next_url, client_side=False) table = db[request.args(0)] if not request.args(1): redirect(next_url, client_side=False) record = db(table.id == request.args(1)).select(table.ALL).first() if not record: redirect(next_url, client_side=False) if not record.order_no: redirect(next_url, client_side=False) custom_links_table = db.book if request.args(0) == 'book_to_link' \ else db.creator filter_field = 'book_id' if request.args(0) == 'book_to_link' \ else 'creator_id' custom_links_id = record[filter_field] links = CustomLinks(custom_links_table, custom_links_id) links.move_link(request.args(1), request.args(2)) redirect(next_url, client_side=False)
def test__attach(self): links = CustomLinks(db.creator, self._creator['id']) form = crud.update(db.creator, self._creator['id']) links.attach(form, 'creator_wikipedia__row', edit_url='http://test.com') soup = BeautifulSoup(str(form)) trs = soup.findAll('tr') tr_ids = [x['id'] for x in trs] self.assertTrue('creator_wikipedia__row' in tr_ids) self.assertTrue('creator_custom_links__row' in tr_ids) self.assertEqual( tr_ids.index('creator_custom_links__row'), tr_ids.index('creator_wikipedia__row') + 1 )
def creator(): """Creator page request.args(0): integer, id of creator. """ if not request.args(0): redirect(URL(c='default', f='index')) creator_record = db(db.creator.id == request.args(0)).select( db.creator.ALL).first() if not creator_record: redirect(URL(c='default', f='index')) auth_user = db(db.auth_user.id == creator_record.auth_user_id).select( db.auth_user.ALL).first() if not auth_user: redirect(URL(c='default', f='index')) pre_links = [] if creator_record.tumblr: pre_links.append( A('tumblr', _href=creator_record.tumblr, _target='_blank')) if creator_record.wikipedia: pre_links.append( A('wikipedia', _href=creator_record.wikipedia, _target='_blank')) return dict( auth_user=auth_user, creator=creator_record, links=CustomLinks(db.creator, creator_record.id).represent(pre_links=pre_links), )
def test____init__(self): links = CustomLinks(db.creator, self._creator['id']) self.assertTrue(links) self.assertEqual(links.to_link_tablename, 'creator_to_link') self.assertEqual(links.to_link_table, db['creator_to_link']) self.assertEqual(links.join_to_link_fieldname, 'creator_id') self.assertEqual(links.join_to_link_field, db.creator_to_link['creator_id'])
def ondelete(table, record_id): """Callback for ondelete.""" db(table.id == record_id).delete() db.commit() if book_record: to_link_table = db.book_to_link filter_field = 'book_id' record = book_record links = CustomLinks(db.book, book_record.id) else: filter_field = 'creator_id' to_link_table = db.creator_to_link record = creator_record links = CustomLinks(db.creator, creator_record.id) # Delete any _to_links associated with the record. db(to_link_table.link_id == record_id).delete() db.commit() links.reorder()
def book(): """Book page controller request.args(0): id of book """ if not request.args(0): redirect(URL(c='default', f='index')) book_record = db(db.book.id == request.args(0)).select(db.book.ALL).first() if not book_record: redirect(URL(c='default', f='index')) creator = db(db.creator.id == book_record.creator_id).select( db.creator.ALL).first() auth_user = db(db.auth_user.id == creator.auth_user_id).select( db.auth_user.ALL).first() read_button = read_link(db, book_record, **dict( _class='btn btn-default', _type='button', )) pre_links = [] if creator.tumblr: pre_links.append(A('tumblr', _href=creator.tumblr, _target='_blank')) if creator.wikipedia: pre_links.append( A('wikipedia', _href=creator.wikipedia, _target='_blank')) return dict( auth_user=auth_user, book=book_record, cover_image=cover_image(db, book_record.id, size='original', img_attributes={'_class': 'img-responsive'}), creator=creator, creator_links=CustomLinks(db.creator, creator.id).represent(pre_links=pre_links), links=CustomLinks(db.book, book_record.id).represent(), page_count=db(db.book_page.book_id == book_record.id).count(), read_button=read_button, )
def test__represent(self): links = CustomLinks(db.creator, self._creator['id']) got = links.represent() soup = BeautifulSoup(str(got)) ul = soup.find('ul') self.assertEqual(ul['class'], 'inline') lis = ul.findAll('li') self.assertEqual(len(lis), 3) for count, li in enumerate(lis): anchor = li.find('a') self.assertEqual(anchor['title'], str(count)) # Test pre_links and post_links pre_links = [ A('1', _href='http://1.com', _title='pre_link 1'), A('2', _href='http://2.com', _title='pre_link 2'), ] post_links = [ A('1', _href='http://1.com', _title='post_link 1'), A('2', _href='http://2.com', _title='post_link 2'), ] links = CustomLinks(db.creator, self._creator['id']) got = links.represent(pre_links=pre_links, post_links=post_links) soup = BeautifulSoup(str(got)) ul = soup.find('ul') lis = ul.findAll('li') self.assertEqual(len(lis), 7) pres = lis[:2] for count, li in enumerate(pres, 1): anchor = li.find('a') self.assertEqual(anchor['title'], 'pre_link {c}'.format(c=count)) posts = lis[-2:] for count, li in enumerate(posts, 1): anchor = li.find('a') self.assertEqual(anchor['title'], 'post_link {c}'.format(c=count)) # Test no links links = CustomLinks(db.creator, self._creator_2['id']) self.assertEqual(links.represent(), None)
def test__links(self): links = CustomLinks(db.creator, self._creator['id']) got = links.links() self.assertEqual(len(got), 3) # Eg <a data-w2p_disable_with="default" # href="http://www.test_custom_links.com" target="_blank" # title="Test Custom Links">test_custom_links</a> for count, got_link in enumerate(got): soup = BeautifulSoup(str(got_link)) anchor = soup.find('a') self.assertEqual(anchor['href'], 'http://www.test_custom_links.com') self.assertEqual(anchor['title'], str(count)) links = CustomLinks(db.creator, self._creator_2['id']) self.assertEqual(links.links(), [])
def test__move_link(self): links = CustomLinks(db.creator, self._creator['id']) # Set up data original = self._ordered_ids() link_ids = [original[0], original[1], original[2]] links.reorder(link_ids=link_ids) got = self._ordered_ids() self.assertEqual(got, original) # Move top link up, should not change links.move_link(original[0], direction='up') got = self._ordered_ids() self.assertEqual(got, original) # Move bottom link down, should not change links.move_link(original[2], direction='down') got = self._ordered_ids() self.assertEqual(got, original) # Move top link down links.move_link(original[0], direction='down') got = self._ordered_ids() self.assertEqual(got, [original[1], original[0], original[2]]) # Move top link down again links.move_link(original[0], direction='down') got = self._ordered_ids() self.assertEqual(got, [original[1], original[2], original[0]]) # Move top link up links.move_link(original[0], direction='up') got = self._ordered_ids() self.assertEqual(got, [original[1], original[0], original[2]]) # Move top link up again, back to start links.move_link(original[0], direction='up') got = self._ordered_ids() self.assertEqual(got, original)