Exemplo n.º 1
0
    def create(self):

        response = self._response.copy()

        try:
            name = self.request.params['name']
            up_file = self.request.POST['file']
            banner = Banner(source=up_file.file, name=name, session=self.session)
            self.session.flush()

        except Exception as e:
            self.log.exception('Unknown error.')
            self.session.rollback()
            self.request.response.status = 500
            response['msg'] = str(e)

        else:
            self.session.commit()
            response['success'] = True
            response['dataset'] = [banner.to_dict()]
            response['dataset_length'] = len(response['dataset'])
            response['msg'] = self.request.translate("Banner created.")

        finally:
            return response
Exemplo n.º 2
0
 def test_no_resize(self):
     test_file = self._get_test_file('sample.png')
     Banner.set_sizes(full=None)
     source_size = PIL.Image.open(test_file).size
     b = Banner(source=test_file, session=self.session)
     banner_size = PIL.Image.open(b.path).size
     self.assertEqual(banner_size, source_size)
     self.session.rollback()
Exemplo n.º 3
0
 def test_resize(self):
     """ Banners are not streched anymore """
     full_size = (300, 400)
     real_size = (1, 1)
     test_file = self._get_test_file('sample.png')
     Banner.set_sizes(full=full_size)
     b = Banner(source=test_file, session=self.session)
     banner_size = PIL.Image.open(b.path).size
     self.assertEqual(banner_size, real_size)
     self.session.rollback()
Exemplo n.º 4
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__)
Exemplo n.º 5
0
    def delete(self):

        response = self._response.copy()

        try:
            banner = Banner.get(self.session, int(self.request.matchdict['id']))
            banner.delete()

        except (KeyError, ValueError, NoResultFound) as e:
            self.log.exception('Cannot get the banner to delete.')
            self.session.rollback()
            self.request.response.status = 400
            response['success'] = False
            response['msg'] = str(e)

        except Exception as e:
            self.log.exception('Unknown error.')
            self.session.rollback()
            self.request.response.status = 500
            response['msg'] = str(e)

        else:
            self.session.commit()
            response['success'] = True
            response['dataset'] = [banner.to_dict()]
            response['dataset_length'] = len(response['dataset'])
            response['msg'] = self.request.translate("Banner was deleted.")
            self.proxy.invalidate(url=banner.url)

        finally:
            return response
Exemplo n.º 6
0
    def update(self):

        response = self._response.copy()

        try:
            id_ = int(self.request.matchdict['id'])
            banner = Banner.get(self.session, id_)
            # FIXME: add code to update banner info.

        except KeyError as e:
            self.log.exception('Bad request.')
            self.session.rollback()
            self.request.response.status = 400
            response['success'] = False
            response['msg'] = str(e)

        except NoResultFound as e:
            self.log.exception('Cannot found banner.')
            self.session.rollback()
            self.request.response.status = 400
            response['success'] = False
            response['msg'] = str(e)

        except TypeError as e:
            self.log.exception('Invalid name')
            self.request.response.status = 400
            response['success'] = False
            response['msg'] = str(e)

        except Exception as e:
            self.log.exception('Unknown error.')
            self.session.rollback()
            self.request.response.status = 500
            response['msg'] = str(e)

        else:
            self.session.commit()
            response['success'] = True
            response['dataset'] = [banner.to_dict()]
            response['dataset_length'] = len(response['dataset'])
            response['msg'] = self.request.translate("Banner was updated.")
            self.proxy.invalidate(url=banner.url)

        finally:
            return response
Exemplo n.º 7
0
    def read(self):

        response = self._response.copy()

        try:
            items = [obj.to_dict()
                     for obj in Banner.all(self.session)]

        except Exception as e:
            self.log.exception('Unknown error.')
            self.session.rollback()
            self.request.response.status = 500
            response['msg'] = str(e)

        else:
            response['success'] = True
            response['dataset'] = items
            response['dataset_length'] = len(response['dataset'])
            response['msg'] = self.request.translate("Banners were found.")

        finally:
            return response
Exemplo n.º 8
0
    def test_default(self):
        self.assertEqual(None, Banner.get_default(self.session))
        b1 = Banner(source=self._get_test_file('sample.png'),
               name='banner.png', session=self.session)
        self.assertEqual(None, Banner.get_default(self.session))
        b2 = Banner(source=self._get_test_file('sample.png'),
               name='banner.png', session=self.session,
               default=True
        )
        self.assertTrue(b2.default)
        self.assertIs(b2, Banner.get_default(self.session))

        b1.default = True
        self.assertIs(b1, Banner.get_default(self.session))
        self.assertFalse(b2.default)

        self.session.rollback()
Exemplo n.º 9
0
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',
    })
Exemplo n.º 10
0
 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')
Exemplo n.º 11
0
    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()
Exemplo n.º 12
0
    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)
Exemplo n.º 13
0
 def test_set_size(self):
     size = (600, 300)
     Banner.set_sizes(full=size)
     self.assertEqual(Banner.full_size, size)