Ejemplo n.º 1
0
def _preload_items(env, entries):
    by_stream = {}
    for entry in entries:
        by_stream.setdefault(entry.stream_name, [])
        by_stream[entry.stream_name].append(entry.object_id)

    by_stream_and_id = {}
    for stream_name, ids in by_stream.items():

        name = stream_name.split(':')[0]
        if name not in env.streams:
            continue
        stream = env.streams[name]
        lang = decode_stream_uid(stream_name)[1].get('lang')

        stream_env = VersionedStorage(version="admin", lang=lang)
        stream_env._storage._parent_storage = env
        # XXX only for multi db! will fail on single db
        stream_env.models = env.models_.admin
        if lang:
            stream_env.models = getattr(stream_env.models, lang)

        M = stream.get_model(stream_env)
        items = stream.item_query(stream_env).filter(M.id.in_(ids)).all()
        for item in items:
            by_stream_and_id[(stream_name, str(item.id))] = item
    return by_stream_and_id
Ejemplo n.º 2
0
def _expand(env, obj, for_item=None):
    # XXX all this is like a hack!
    stream_name = obj.stream_name.split(':')[0]
    if stream_name not in env.streams:
        return {}
    stream = env.streams[stream_name]
    lang = decode_stream_uid(obj.stream_name)[1].get('lang')
    if for_item is None:
        stream_env = VersionedStorage(version="admin", lang=lang)
        stream_env._storage._parent_storage = env
        # XXX only for multi db! will fail on single db
        stream_env.models = env.models.admin
        if lang:
            stream_env.models = getattr(stream_env.models, lang)
    else:
        stream_env = env

    assert 'r' in stream.get_permissions(stream_env)
    if for_item is None:
        item = stream.item_query(stream_env).filter_by(id=obj.object_id).first()
    else:
        item = for_item

    if item is not None:
        item_title = getattr(item, 'title', unicode(item))
    else:
        item_title = u"<{}: удалённый или недоступный объект: {}>".format(
                                stream.title, obj.object_id)

    return {"obj": obj,
            "stream": stream,
            "item": item,
            "item_title": item_title,
            "lang": lang,
            "type": stream.edit_log_action.log_type_title(obj)}
Ejemplo n.º 3
0
def _preload_items(env, entries):
    by_stream = {}
    for entry in entries:
        by_stream.setdefault(entry.stream_name, [])
        by_stream[entry.stream_name].append(entry.object_id)

    by_stream_and_id = {}
    for stream_name, ids in by_stream.items():

        name = stream_name.split(':')[0]
        if name not in env.streams:
            continue
        stream = env.streams[name]
        lang = decode_stream_uid(stream_name)[1].get('lang')

        stream_env = VersionedStorage(version="admin", lang=lang)
        stream_env._storage._parent_storage = env
        # XXX only for multi db! will fail on single db
        stream_env.models = env.models_.admin
        if lang:
            stream_env.models = getattr(stream_env.models, lang)

        M = stream.get_model(stream_env)
        items = stream.item_query(stream_env).filter(M.id.in_(ids)).all()
        for item in items:
            by_stream_and_id[(stream_name, str(item.id))] = item
    return by_stream_and_id
Ejemplo n.º 4
0
    def test_as_dict(self):
        'VersionedStorage as_dict method'
        vs = VersionedStorage(a=1)
        a = vs._storage
        b = vs._push(b=2)
        c = vs._push(c=3, b=4)

        self.assertEqual(a.as_dict(), {'a': 1})
        self.assertEqual(b.as_dict(), {'a': 1, 'b': 2})
        self.assertEqual(c.as_dict(), {'a': 1, 'b': 4, 'c': 3})
Ejemplo n.º 5
0
    def test_as_dict(self):
        'VersionedStorage as_dict method'
        vs = VersionedStorage(a=1)
        a = vs._storage
        b = vs._push(b=2)
        c = vs._push(c=3, b=4)

        self.assertEqual(a.as_dict(), {'a': 1})
        self.assertEqual(b.as_dict(), {'a': 1, 'b': 2})
        self.assertEqual(c.as_dict(), {'a': 1, 'b': 4, 'c': 3})
Ejemplo n.º 6
0
    def test_hasattr(self):
        'VersionedStorage __getattr__ method'
        vs = VersionedStorage(a=1)
        vs._push(b=2)
        vs._push(c=3, b=4)

        self.assert_(hasattr(vs, 'a'))
        self.assert_(hasattr(vs, 'b'))
        self.assert_(hasattr(vs, 'c'))
        self.assert_(not hasattr(vs, 'd'))
Ejemplo n.º 7
0
    def env(self):
        DIR = jnj.__file__
        DIR = path.dirname(path.abspath(DIR))
        TEMPLATES = [path.join(DIR, 'templates')]

        jinja_loader = TemplateEngine(TEMPLATES)
        template_loader = Template(engines={'html': jinja_loader}, *TEMPLATES)
        env = VersionedStorage()
        env.template = BoundTemplate(env, template_loader)
        return env
Ejemplo n.º 8
0
    def env(self):
        DIR = jnj.__file__
        DIR = path.dirname(path.abspath(DIR))
        TEMPLATES = [path.join(DIR, "templates")]

        jinja_loader = TemplateEngine(TEMPLATES)
        template_loader = Template(engines={"html": jinja_loader}, *TEMPLATES)
        env = VersionedStorage()
        env.template = BoundTemplate(env, template_loader)
        return env
Ejemplo n.º 9
0
    def test_List(self):
        'cases handle'

        @F
        def h1(env, data, nh):
            self.assertEqual(env.count, 0)
            env.count = env.count + 1
            return nh(env, data)

        @F
        def h2(env, data, nh):
            self.assertEqual(env.count, 0)
            env.count = env.count + 1
            return nh(env, data)

        @F
        def h3(env, data, nh):
            self.assertEqual(env.count, 0)
            env.count = env.count + 1
            return nh(env, data)

        chain = web.cases(h1, h2, h3)
        env = VersionedStorage(count=0)
        self.assert_(chain(env, VS()) is None)
        self.assertEqual(env.count, 0)
Ejemplo n.º 10
0
 def __call__(self, environ, start_response):
     '''
     WSGI interface method.
     Creates webob and iktomi wrappers and calls `handle` method.
     '''
     # validating Host header to prevent problems with url parsing
     if not is_host_valid(environ['HTTP_HOST']):
         logger.warning('Unusual header "Host: {}", return HTTPNotFound'\
                        .format(environ['HTTP_HOST']))
         return HTTPNotFound()(environ, start_response)
     request = Request(environ, charset='utf-8')
     env = VersionedStorage(self.env_class, request=request, root=self.root)
     data = VersionedStorage()
     response = self.handle(env, data)
     try:
         result = response(environ, start_response)
     except Exception:
         self.handle_error(env)
         result = HTTPInternalServerError()(environ, start_response)
     return result
Ejemplo n.º 11
0
    def log_entry(self, env, data):
        #insure_is_xhr(env)

        if getattr(env, 'version', None) == 'front':
            raise HTTPNotFound()

        EditLog = env.edit_log_model
        log = EditLog.query_for_item(env.db, data.item)\
                     .filter_by(id=data.log_id)\
                     .first()

        if log is None:
            raise HTTPNotFound()

        rel_stream_name, params = decode_stream_uid(log.stream_name)
        if rel_stream_name in env.streams:
            rel_stream = env.streams[rel_stream_name]
        else:
            # Deleted or renamed stream
            raise NotImplementedError("Deleted or renamed stream",
                                      rel_stream_name)
        rel_env = VersionedStorage(**params)
        rel_env._storage._parent_storage = env

        form_cls = rel_stream.config.ItemForm

        item_cp = type(data.item)(id=data.item.id)

        form1 = form_cls.load_initial(env, item_cp, initial={})
        form2 = form_cls.load_initial(env, item_cp, initial={})
        form1.model = form2.model = rel_stream.get_model(env)

        form1.accept(log.before)
        form2.accept(log.after)

        # reset permissions
        form1.permissions = form2.permissions = frozenset('r')
        form1.fields = [field(parent=form1) for field in form1.fields]
        form2.fields = [field(parent=form2) for field in form2.fields]

        diff = form1.get_diff(form2)
        diffs = diff['children'] if diff is not None else []

        return env.render_to_response(
            'edit_log/item',
            dict(form1=form1,
                 form2=form2,
                 diffs=diffs,
                 log=log,
                 stream=self.stream,
                 item=data.item,
                 log_type=self.log_type_title(log)))
Ejemplo n.º 12
0
def expand_stream(env, obj):
    stream_name, params = decode_stream_uid(obj.stream_name)

    stream_env = VersionedStorage()
    stream_env._storage._parent_storage = env

    if 'lang' in params:
        stream_env.models = getattr(env.models, params['lang'])
        stream_env.lang = params['lang']
    if stream_name not in env.streams:
        # Deleted or renamed stream
        raise NotImplementedError
    stream = env.streams[stream_name]
    item = stream.item_query(stream_env)\
                 .filter_by(id=obj.object_id).first()
    if item is not None:
        stream_title = stream.title
        if 'lang' in params:
            stream_title = I18nLabel(stream_title, params['lang'])
        url = stream.url_for(stream_env, 'item', 
                             item=obj.object_id, **params)
    return (url, stream_title, stream, obj, item)
Ejemplo n.º 13
0
def expand_stream(env, obj):
    stream_name, params = decode_stream_uid(obj.stream_name)

    stream_env = VersionedStorage()
    stream_env._storage._parent_storage = env

    if 'lang' in params:
        stream_env.models = getattr(env.models, params['lang'])
        stream_env.lang = params['lang']
    if stream_name not in env.streams:
        # Deleted or renamed stream
        raise NotImplementedError
    stream = env.streams[stream_name]
    item = stream.item_query(stream_env)\
                 .filter_by(id=obj.object_id).first()
    if item is not None:
        stream_title = stream.title
        if 'lang' in params:
            stream_title = I18nLabel(stream_title, params['lang'])
        url = stream.url_for(stream_env, 'item',
                             item=obj.object_id, **params)
        return (url, stream_title, stream, obj, item)
Ejemplo n.º 14
0
def _expand(env, obj, for_item=None):
    # XXX all this is like a hack!
    stream_name = obj.stream_name.split(':')[0]
    if stream_name not in env.streams:
        return {}
    stream = env.streams[stream_name]
    lang = decode_stream_uid(obj.stream_name)[1].get('lang')
    if for_item is None:
        stream_env = VersionedStorage(version="admin", lang=lang)
        stream_env._storage._parent_storage = env
        # XXX only for multi db! will fail on single db
        stream_env.models = env.models.admin
        if lang:
            stream_env.models = getattr(stream_env.models, lang)
    else:
        stream_env = env

    assert 'r' in stream.get_permissions(stream_env)
    if for_item is None:
        item = stream.item_query(stream_env).filter_by(
            id=obj.object_id).first()
    else:
        item = for_item

    if item is not None:
        item_title = getattr(item, 'title', unicode(item))
    else:
        item_title = u"<{}: удалённый или недоступный объект: {}>".format(
            stream.title, obj.object_id)

    return {
        "obj": obj,
        "stream": stream,
        "item": item,
        "item_title": item_title,
        "lang": lang,
        "type": stream.edit_log_action.log_type_title(obj)
    }
Ejemplo n.º 15
0
    def command_generate(self):
        from front.environment import FrontEnvironment, environment
        from front.urls import app
        env = VersionedStorage(FrontEnvironment,
                               Request.blank('/'),
                               Reverse.from_handler(app))
        for lang in self.langs:
            env.lang = Lang(env, lang)
            env.models = getattr(models.front, lang)
            for error in self.errors:
                handler = environment | (
                        lambda env, data: env.render_to_string('errors/' + error, {}))
                tmpl = handler(env, VersionedStorage()).encode('utf-8')
                out = os.path.join(env.cfg.STATIC, lang, error+'.html')

                if os.path.isfile(out):
                    with open(out, 'r') as f:
                        existing = f.read()
                        if existing == tmpl:
                            continue

                with open(out, 'w') as f:
                    f.write(tmpl)
                logger.info("Rendered %s/%s.html", lang, error)
Ejemplo n.º 16
0
    def test_storage_properties(self):
        class Env(StorageFrame):
            @storage_cached_property
            def storage_cached(self):
                return self.value

            @storage_cached_property
            def storage_cached_2(self):
                return self.value

            @storage_property
            def storage(self):
                return self.value

            @storage_method
            def method(self):
                return self.value

        vs = VersionedStorage(Env)
        vs._push(value=4)
        self.assertEqual(vs.storage_cached, 4)
        self.assertEqual(vs.storage, 4)
        self.assertEqual(vs.method(), 4)

        vs._push(value=1)
        self.assertEqual(vs.storage_cached, 4)
        self.assertEqual(vs.storage_cached_2, 1)  # XXX is this right?
        self.assertEqual(vs.storage, 1)
        self.assertEqual(vs.method(), 1)

        vs._pop()
        vs._pop()
        self.assertEqual(vs.storage_cached, 4)
        self.assertEqual(vs.storage_cached_2, 1)  # XXX is this right?
        self.assertRaises(AttributeError, lambda: vs.storage)
        self.assertRaises(AttributeError, vs.method)
Ejemplo n.º 17
0
Archivo: app.py Proyecto: motor23/cms34
 def __call__(self, environ, start_response):
     '''
     WSGI interface method.
     Creates webob and iktomi wrappers and calls `handle` method.
     '''
     request = self.create_request(environ)
     env = self.create_environment(request)
     data = VersionedStorage()
     response = self.handle(env, data)
     try:
         result = response(environ, start_response)
     except Exception:
         self.handle_error(env)
         result = HTTPInternalServerError()(environ, start_response)
     return result
Ejemplo n.º 18
0
    def test_hasattr(self):
        'VersionedStorage __getattr__ method'
        vs = VersionedStorage(a=1)
        vs._push(b=2)
        vs._push(c=3, b=4)

        self.assert_(hasattr(vs, 'a'))
        self.assert_(hasattr(vs, 'b'))
        self.assert_(hasattr(vs, 'c'))
        self.assert_(not hasattr(vs, 'd'))
Ejemplo n.º 19
0
    def test_storage_properties(self):
        class Env(StorageFrame):

            @storage_cached_property
            def storage_cached(self):
                return self.value

            @storage_cached_property
            def storage_cached_2(self):
                return self.value

            @storage_property
            def storage(self):
                return self.value

            @storage_method
            def method(self):
                return self.value

        vs = VersionedStorage(Env)
        vs._push(value=4)
        self.assertEqual(vs.storage_cached, 4)
        self.assertEqual(vs.storage, 4)
        self.assertEqual(vs.method(), 4)

        vs._push(value=1)
        self.assertEqual(vs.storage_cached, 4)
        self.assertEqual(vs.storage_cached_2, 1) # XXX is this right?
        self.assertEqual(vs.storage, 1)
        self.assertEqual(vs.method(), 1)

        vs._pop()
        vs._pop()
        self.assertEqual(vs.storage_cached, 4)
        self.assertEqual(vs.storage_cached_2, 1) # XXX is this right?
        self.assertRaises(AttributeError, lambda: vs.storage)
        self.assertRaises(AttributeError, vs.method)
Ejemplo n.º 20
0
    def get_front_item_form(self, env, item):
        front_models = _AdminReplicated.front
        if getattr(env, "lang", None):
            front_models = getattr(front_models, env.lang)

        front_env = VersionedStorage(db=env.db,
                                     models=front_models,
                                     user=env.user,
                                     version='front')
        front_env._storage._parent_storage = env

        form_cls = self.stream.config.ItemForm(front_env, {}, item._front_item)
        form = form_cls.load_initial(env,
                                     item._front_item,
                                     initial={},
                                     permissions='r')
        form.model = self.stream.get_model(env)
        return form
def call_with_front_env(env, data, nxt):

    request = Request.blank(env._route_state.path+'?'+env.request.query_string,
                            charset='utf-8')
    front_env = VersionedStorage(wsgi_app.env_class, request, wsgi_app.root)
    # env.root is created in wsgi_app, we do not wrap front app in one,
    # so we have to create env.root manually
    front_env.root = env.root.front(version=env.version, lang=env.lang)\
                             .bind_to_env(front_env)
    front_env.models = env.models
    # we wave set language explicitly here, because language detection is based
    # on subdomains, and we do not have ones in admin
    front_env.langs = [Lang(front_env, n) for n in ['en', 'ru']]
    front_env.lang = [x for x in front_env.langs if x == env.lang][0]
    front_env.matched_domain = ''
    front_env.db = env.db
    front_data = VersionedStorage()
    front_env.data = front_data

    # XXX hack to make cfg configurable
    _cfg = front_env.cfg
    front_env.cfg = VersionedStorage()
    front_env.cfg._storage._parent_storage = _cfg
    _cfg._parent_storage = None

    front_env.cfg.STATIC_URL = env.cfg.FRONT_STATIC_URL
    front_env.cfg.PREVIEW = True
    front_env.cfg.DOMAINS = [env.request.host.split(':', 1)[0]]
    old_query = front_env.db._query_cls
    try:
        query_cls = AdminPublicQuery# if env.version == 'admin' else PublicQuery
        # XXX hack!
        front_env.db._query_cls = query_cls
        return nxt(front_env, front_data)
    finally:
        front_env.db._query_cls = old_query
Ejemplo n.º 22
0
 def get_data(self):
     return VersionedStorage()
Ejemplo n.º 23
0
    def test_setattr(self):
        'VersionedStorage setattr and push/pop'
        vs = VersionedStorage(a=1)
        vs._push()
        vs.b = 2
        self.assertEqual(vs.as_dict(), {'a': 1, 'b': 2})

        vs._push()
        vs.c = 3
        vs.b = 4
        self.assertEqual(vs.as_dict(), {'a': 1, 'b': 4, 'c': 3})

        vs._pop()
        self.assertEqual(vs.as_dict(), {'a': 1, 'b': 2})

        vs._pop()
        self.assertEqual(vs.as_dict(), {'a': 1})
Ejemplo n.º 24
0
    def test_push_pop(self):
        'VersionedStorage push/pop'
        vs = VersionedStorage(a=1)
        self.assertEqual(vs.as_dict(), {'a': 1})

        vs._push(b=2)
        self.assertEqual(vs.as_dict(), {'a': 1, 'b': 2})

        vs._push(c=3, b=4)
        self.assertEqual(vs.as_dict(), {'a': 1, 'b': 4, 'c': 3})

        vs._pop()
        self.assertEqual(vs.as_dict(), {'a': 1, 'b': 2})

        vs._pop()
        self.assertEqual(vs.as_dict(), {'a': 1})
Ejemplo n.º 25
0
    def test_push_pop(self):
        'VersionedStorage push/pop'
        vs = VersionedStorage(a=1)
        self.assertEqual(vs.as_dict(), {'a': 1})

        vs._push(b=2)
        self.assertEqual(vs.as_dict(), {'a': 1, 'b': 2})

        vs._push(c=3, b=4)
        self.assertEqual(vs.as_dict(), {'a': 1, 'b': 4, 'c': 3})

        vs._pop()
        self.assertEqual(vs.as_dict(), {'a': 1, 'b': 2})

        vs._pop()
        self.assertEqual(vs.as_dict(), {'a': 1})
Ejemplo n.º 26
0
 def create(cls, *args, **kwargs):
     return VersionedStorage(cls, *args, **kwargs)
Ejemplo n.º 27
0
    def test_setattr(self):
        'VersionedStorage setattr and push/pop'
        vs = VersionedStorage(a=1)
        vs._push()
        vs.b = 2
        self.assertEqual(vs.as_dict(), {'a': 1, 'b': 2})

        vs._push()
        vs.c = 3
        vs.b = 4
        self.assertEqual(vs.as_dict(), {'a': 1, 'b': 4, 'c': 3})

        vs._pop()
        self.assertEqual(vs.as_dict(), {'a': 1, 'b': 2})

        vs._pop()
        self.assertEqual(vs.as_dict(), {'a': 1})
Ejemplo n.º 28
0
 def env(self):
     # XXX hack
     env = super(LangLonerMenu, self).env
     vs = VersionedStorage(lang=self.lang)
     vs._storage._parent_storage = env
     return vs
Ejemplo n.º 29
0
    def publish_queue(self, env, data):
        #insure_is_xhr(env)

        changed = []
        for stream in self.streams.values():
            if isinstance(stream, I18nPublishStream):
                for lang, lang_name in stream.langs:
                    subenv = VersionedStorage()
                    subenv._parent_storage = env
                    subenv.models = getattr(env.models.admin, lang)
                    subenv.version = 'admin'
                    subenv.lang = lang

                    items = stream.item_query(subenv)\
                                  .filter_by(has_unpublished_changes=True)
                    if hasattr(stream.config.Model, 'state'):
                        items = items.filter_by(state=stream.config.Model.PUBLIC)
                    items = items.all()
                    changed += [(stream, subenv, item) for item in items]
            elif isinstance(stream, PublishStream):
                subenv = VersionedStorage()
                subenv._parent_storage = env
                subenv.models = env.models.admin
                subenv.version = 'admin'
                items = stream.item_query(subenv)\
                              .filter_by(has_unpublished_changes=True)
                if hasattr(stream.config.Model, 'state'):
                    items = items.filter_by(state=stream.config.Model.PUBLIC)
                items = items.all()
                changed += [(stream, subenv, item) for item in items]
        changed.sort(key=lambda x: x[2].updated_dt)
        return env.render_to_response('publish_queue', dict(
            changed = changed,
            menu = env.current_location,
            title = u'Очередь публикации',
        ))
Ejemplo n.º 30
0
 def env_data(self, wsgi_app, path):
     request = Request.blank(path)
     env = VersionedStorage(wsgi_app.env_class, request, wsgi_app.root)
     data = VersionedStorage()
     return env, data
Ejemplo n.º 31
0
 def get_env(self, request):
     return VersionedStorage(self.env_class, request=request, app=self)