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 list(self): num_files = File.count(self.session) self.res = dict(datalen=num_files, data=[]) for f in File.all(self.session): d = f.to_dict(ref_pages=True) self.res["data"].append(d) return self.res
def test_delete(self): tmpfile = self._create_tmp_file() f = File(name='testfile.txt', source=tmpfile, session=self.session) self.session.commit() f.delete() self.session.commit() self.assertEqual(len(File.all(self.session)), 0)
def setUp(self): super(FileTestsBase, self).setUp() self.tempdir = tempfile.mkdtemp() self.static = os.path.join(self.tempdir, 'static') os.mkdir(self.static) self.datadir = os.path.realpath(os.path.join(os.path.dirname(__file__), "data")) Banner.initialize(base=os.path.join(self.static, 'banners'), private=self.tempdir) File.initialize(base=os.path.join(self.static, 'files'), private=self.tempdir) Image.initialize(base=os.path.join(self.static, 'images'), private=self.tempdir) self.log = getLogger(__name__)
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
def delete(self): try: id_ = int(self.request.params["id"]) obj = File.get(self.session, id_) obj.delete() self.session.flush() except Exception as e: self.handle_exception(e) else: self.session.commit() finally: return self.res
def add_assets(config): """ Setup search paths for static files and for templates """ # Use SqlSoup to access database to avoid requiring the entities to be # mapped, which is not the case when we are in a multiprocess environment. engine = engine_from_config(config.registry.settings, "sqlalchemy.") db = SqlSoup(engine) tname = db.settings.filter(db.settings.name == u'theme_name').one().value theme = db.themes.filter(db.themes.name == tname).one() log.info("Adding static view for aybu") #config.add_static_view('favicon.ico/', 'aybu.website:static/favicon.ico') config.add_static_view('static', 'aybu.website:static/') log.info("Preparing static search path for %s", theme) themes_inheritance_chain = [] themes_paths = [pkg_resources.resource_filename('aybu.website', 'templates')] while theme: themes_inheritance_chain.insert(0, theme) if theme.parent_name: theme = db.themes.filter(db.themes.name == theme.parent_name).one() else: theme = None for theme in themes_inheritance_chain: log.info('-- Adding %s' % (theme.name)) theme_static_spec = 'aybu.themes.%s:/static/' % theme.name log.info("Adding '%s' as override for static files", theme_static_spec) config.override_asset( to_override='aybu.website:static/', override_with=theme_static_spec ) """ favicon = '%sfavicon.ico' % (theme_static_spec) log.info("Adding '%s' as override for favicon", favicon) config.override_asset( to_override = 'aybu.website:static/favicon.ico', override_with = favicon ) """ theme_templates_spec = 'aybu.themes.%s:/templates/' % theme.name log.info("Adding '%s' as override for templates", theme_templates_spec) config.override_asset( to_override='aybu.website:templates/', override_with=theme_templates_spec ) theme_path = pkg_resources.\ resource_filename('aybu.themes.%s' % (theme.name), 'templates') log.info("Adding '%s' to mako directories", theme_path) themes_paths.insert(0, theme_path) log.info('-- Adding Instance') settings = config.get_settings() try: instance_name = settings['instance'] instance_module_name = "aybu.instances.%s" % (instance_name) if instance_name is None or instance_name == '': raise KeyError() else: instance_static_spec = '%s:/static/' % instance_module_name log.info("Adding '%s' as override for static files", instance_static_spec) config.override_asset( to_override='aybu.website:static/', override_with=instance_static_spec ) """ favicon = '%sfavicon.ico' % (instance_static_spec) log.info("Adding '%s' as override for favicon", favicon) config.override_asset( to_override = 'aybu.website:static/favicon.ico', override_with = favicon ) """ instance_templates_spec = '%s:/templates/' % instance_module_name log.info("Adding '%s' as override for templates", instance_templates_spec) config.override_asset( to_override='aybu.website:templates/', override_with=instance_templates_spec ) instance_template_path = pkg_resources.\ resource_filename(instance_module_name, 'templates/') log.info("Adding '%s' to mako directories", instance_template_path) themes_paths.insert(0, instance_template_path) instance_static_path = os.path.realpath( pkg_resources.\ resource_filename(instance_module_name, 'static/') ) upload_path = os.path.join(instance_static_path, 'uploads') if os.path.isdir(upload_path): if not os.access(upload_path, os.W_OK): log.critical("*" * 79) log.critical("Instance upload dir '%s' is not writable", upload_path) log.critical('Uploads will NOT work') log.critical("*" * 79) else: log.critical("*" * 79) log.critical("Instance upload dir '%s' does not exists", upload_path) log.critical('Uploads will NOT work') log.critical("*" * 79) # Setup Pufferfish entities file_base = os.path.join(upload_path, "files") image_base = os.path.join(upload_path, "images") banner_base = os.path.join(upload_path, "banners") logo_base = os.path.join(upload_path, "logo") prefix = 'static' File.initialize(base=file_base, private=instance_static_path, url_prefix=prefix) Image.initialize(base=image_base, private=instance_static_path, url_prefix=prefix) Banner.initialize(base=banner_base, private=instance_static_path, url_prefix=prefix) Logo.initialize(base=logo_base, private=instance_static_path, url_prefix=prefix) Background.initialize(base=logo_base, private=instance_static_path, url_prefix=prefix) img_fsize = int( db.settings.filter( db.settings.name == u'image_full_size').one().value ) Image.set_sizes(full=(img_fsize, img_fsize * 3), thumbs=dict(thumb=(120, 120))) banner_width = int(db.settings.filter( db.settings.name == u'banner_width').one().value) banner_height = int(db.settings.filter( db.settings.name == u'banner_height').one().value) logo_height = int(db.settings.filter( db.settings.name == u'logo_height').one().value) logo_width = int(db.settings.filter( db.settings.name == u'logo_width').one().value) Banner.set_sizes(full=(banner_width, banner_height)) Logo.set_sizes(full=(logo_width, logo_height)) Background.set_sizes(full=(2560, 2560)) for dir_ in (file_base, image_base, banner_base, logo_base): try: os.mkdir(dir_) except OSError as e: if e.errno != 17: log.exception("Cannot create directory %s", dir_) raise e except KeyError as e: log.critical("*" * 79) log.critical("No instance") log.critical('Uploads and instance templates/static will NOT work') log.critical("*" * 79) raise e config.add_settings({ 'mako.directories': themes_paths, 'mako.strict_undefined': 'true', })
def setUp(self): super(NodeTests, self).setUp() Banner.initialize(base='/static', private='/static/private') File.initialize(base='/static', private='/static/private') Image.initialize(base='/static', private='/static/private')
def command(self): if not self.args or len(self.args) < 2: msg = 'You must give a configuration file and an archive name.' raise BadCommand(msg) file_name = self.args[0] if not file_name.startswith("/"): file_name = os.path.join(os.getcwd(), file_name) archive_name = self.args[1] if not archive_name.startswith("/"): archive_name = os.path.join(os.getcwd(), archive_name) if not archive_name.endswith(".tar.gz"): archive_name = '{}.tar.gz'.format(archive_name) # Setup logging via the logging module's fileConfig function # with the specified 'config_file', if applicable. self.logging_file_config(file_name.split('#')[0]) config = appconfig('config:{}'.format(file_name)) engine = engine_from_config(config, 'sqlalchemy.') Base.metadata.bind = engine Base.metadata.create_all() src = pkg_resources.resource_filename('aybu.instances.%s' % config['instance'], 'static') log.info('Using %s for static files', src) instance_static_path = os.path.realpath(src) upload_path = os.path.join(instance_static_path, 'uploads') file_base = os.path.join(upload_path, "files") image_base = os.path.join(upload_path, "images") banner_base = os.path.join(upload_path, "banners") logo_base = os.path.join(upload_path, "logo") prefix = 'static' File.initialize(base=file_base, private=instance_static_path, url_prefix=prefix) Image.initialize(base=image_base, private=instance_static_path, url_prefix=prefix) Banner.initialize(base=banner_base, private=instance_static_path, url_prefix=prefix) Logo.initialize(base=logo_base, private=instance_static_path, url_prefix=prefix) Session = sessionmaker(bind=engine) session = Session() init_session_events(session) tar = tarfile.open(archive_name, 'w:gz') try: data = export(session) for obj in data: if obj['__class__'] in ('File', 'Image', 'Banner', 'Logo'): arcname = '{}/{}'.format(obj['id'], obj['name']) tar.add(obj['source'], arcname=arcname) obj['source'] = arcname filename = 'data.json' with open(filename, 'w') as file_: data = json.dumps(data, encoding='utf-8') print data file_.write(data) tar.add(filename) os.remove(filename) except Exception as e: log.exception('Cannot export data from database.') session.rollback() raise e finally: session.close() tar.close()
def command(self): if not self.args or len(self.args) < 2: msg = 'You must give a configuration file and an archive name.' raise BadCommand(msg) file_name = self.args[0] if not file_name.startswith("/"): file_name = os.path.join(os.getcwd(), file_name) archive_name = self.args[1] if not archive_name.startswith("/"): archive_name = os.path.join(os.getcwd(), archive_name) if not archive_name.endswith(".tar.gz"): archive_name = '{}.tar.gz'.format(archive_name) # Setup logging via the logging module's fileConfig function # with the specified 'config_file', if applicable. self.logging_file_config(file_name.split('#')[0]) config = appconfig('config:{}'.format(file_name)) engine = engine_from_config(config, 'sqlalchemy.') Base.metadata.bind = engine Base.metadata.drop_all() Base.metadata.create_all() src = pkg_resources.resource_filename('aybu.instances.%s' % config['instance'], 'static') log.info('Using %s for static files', src) instance_static_path = os.path.realpath(src) upload_path = os.path.join(instance_static_path, 'uploads') file_base = os.path.join(upload_path, "files") image_base = os.path.join(upload_path, "images") banner_base = os.path.join(upload_path, "banners") logo_base = os.path.join(upload_path, "logo") prefix = 'static' File.initialize(base=file_base, private=instance_static_path, url_prefix=prefix) Image.initialize(base=image_base, private=instance_static_path, url_prefix=prefix) Banner.initialize(base=banner_base, private=instance_static_path, url_prefix=prefix) Logo.initialize(base=logo_base, private=instance_static_path, url_prefix=prefix) Session = sessionmaker(bind=engine) session = Session() init_session_events(session) base_path = tempfile.mkdtemp() tar = tarfile.open(archive_name, 'r') tar.extractall(path=base_path) tar.close() json_data = os.path.join(base_path, 'data.json') data = json.load(open(json_data, 'r'), encoding='utf-8') try: for obj in data: if obj['__class__'] in ('File', 'Image', 'Banner', 'Logo'): file_ = open(os.path.join(base_path, obj['source'])) obj['source'] = file_.read() file_.close() import_(session, data) except Exception as e: log.exception('Error in import') session.rollback() raise e else: session.commit() finally: session.close() shutil.rmtree(base_path)