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 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 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.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)