def test_referred(self): tmpfile = self._create_tmp_file() newfile = File(name='testfile.txt', source=tmpfile, session=self.session) self.session.commit() source = self._get_test_file('sample.png') newimage = Image(source=source, name="original.png", session=self.session) self.session.commit() page = create_page(self.session) content = '<a href={f.url}>{f.name}</a><img src={img.url}/>' page.content = content.format(f=newfile, img=newimage) # Statements aren't needed: append will be performed by after_flush. #page.files.append(newfile) #page.images.append(newimage) self.session.commit() self.assertIn(page, newfile.pages) self.assertIn(page, newimage.pages) with self.assertRaises(ConstraintError): newfile.delete() with self.assertRaises(ConstraintError): newimage.delete() self.assertIn(page, newfile.pages) self.assertIn(page, newimage.pages) d = newfile.to_dict() self.assertNotIn('used_by', d) d = newfile.to_dict(ref_pages=True) self.assertIn('used_by', d)
def create(self): try: name = self.request.params["name"] up_file = self.request.POST["file"] obj = File(session=self.session, source=up_file.file, name=name) self.session.flush() except Exception as e: self.handle_exception(e) else: self.session.commit() self.res.update(obj.to_dict()) finally: return self.res