Пример #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
Пример #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)}
Пример #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
Пример #4
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)))
Пример #5
0
    def log_entry(self, env, data):
        insure_is_xhr(env)

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

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

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

        form_cls = rel_stream.config.ItemForm

        form1 = form_cls.load_initial(env, data.item, initial={})
        form2 = form_cls.load_initial(env, data.item, initial={})
        form1.accept(log.before)
        form2.accept(log.after)

        fields = sum([x.field_names for x in form1.fields], [])
        changed_fields = []
        for field in fields:
            if not field:
                continue
            data1 = self.get_field_data(form1, field)
            data2 = self.get_field_data(form2, field)
            if data1 != data2:
                changed_fields.append(field)

        # XXX
        form1 = form_cls.load_initial(env, data.item.__class__(),
                                      initial=form1.python_data,
                                      permissions='r')
        form2 = form_cls.load_initial(env, data.item.__class__(),
                                      initial=form2.python_data,
                                      permissions='r')
        return env.render_to_response('edit_log/item',
                                      dict(form1=form1,
                                           form2=form2,
                                           changed_fields=changed_fields,
                                           log=log,
                                           stream=self.stream,
                                           item=data.item,
                                           log_type=self.log_type_title(log)))
Пример #6
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)))
Пример #7
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)
    }
Пример #8
0
 def get_lang(obj):
     return decode_stream_uid(obj.stream_name)[1].get('lang')