示例#1
0
    def create(self, user, values):

        # Delete the gid which sets the default group when creating a
        # new user. The default group when creating a new user is always
        # the usergroup which gets automatically created on user
        # creation. So this value can (and must) be ignored.
        # Not removing the "gid" will otherwise cause an
        # IntegrityError while flushing the new user to the DB as the
        # usergroup with the given id is not persistent in the DB.
        if "gid" in values:
            del values["gid"]
        # Delete the sid which sets the default settings for the same
        # reasons than deleting the "gid" attribute.
        if "sid" in values:
            del values["sid"]

        new_user = BaseFactory.create(self, user, values)

        # Now create a a new Profile
        profile_property = get_prop_from_instance(new_user,
                                                  "profile",
                                                  include_relations=True)
        profile_class = profile_property.mapper.class_
        profile_factory = BaseFactory(profile_class)
        profile = profile_factory.create(user, {})
        new_user.profile.append(profile)

        # Create settings
        settings_factory = BaseFactory(UserSetting)
        settings = settings_factory.create(user, {})
        new_user.settings = settings

        # A usergroup
        usergroup_property = get_prop_from_instance(new_user,
                                                    "usergroup",
                                                    include_relations=True)
        usergroup_class = usergroup_property.mapper.class_
        usergroup_factory = BaseFactory(usergroup_class)
        usergroup = usergroup_factory.create(None, {})
        usergroup.name = new_user.login
        usergroup.members.append(new_user)
        # If no default group is set, then set the users group as
        # default group
        new_user.usergroup = values.get("usergroup") or usergroup
        return new_user
示例#2
0
 def create(self, user=None, values=None):
     # Hack! We have a not null constraint on the datafild. But the
     # value will be actually set in a savedata callback.
     if values is None:
         values = {}
     if "data" not in values:
         values["data"] = 'EMPTY'
     new_item = BaseFactory.create(self, user, values)
     return new_item
示例#3
0
    def test_import(self, import_item):
        from ringo.model.user import User
        from ringo.lib.imexport import JSONImporter
        from ringo.lib.sql import DBSession
        from ringo.model.base import BaseFactory

        imported_items = JSONImporter(User).perform(json.dumps(import_item))
        DBSession.flush()

        assert len(imported_items) == 1
        assert imported_items[0][1] == "CREATE"
        assert (imported_items[0][0] is BaseFactory(User).load(
            imported_items[0][0].id))
示例#4
0
    def test_update_import_byid(self, import_item):
        from ringo.model.user import User
        from ringo.lib.imexport import JSONImporter
        from ringo.lib.sql import DBSession
        from ringo.model.base import BaseFactory

        imported_items = JSONImporter(User).perform(json.dumps(import_item),
                                                    load_key="id")
        DBSession.flush()

        assert len(imported_items) == 1
        assert imported_items[0][1] == "UPDATE"
        assert (BaseFactory(User).load(
            import_item["id"]).login == import_item["login"])
示例#5
0
def test_item_export(expected_item):
    from ringo.model.base import BaseFactory
    from ringo.model.user import User
    from ringo.lib.imexport import JSONExporter

    user = BaseFactory(User).load(1)
    export = json.loads(JSONExporter(User, serialized=False).perform(user))

    assert sorted(export.keys()) == sorted(expected_item.keys())

    for key in [
            key for key in expected_item.keys()
            if key not in ["uuid", "last_login", "password"]
    ]:
        assert export[key] == expected_item[key]
示例#6
0
文件: user.py 项目: toirl/ringo
    def create(self, user, values):

        # Delete the gid which sets the default group when creating a
        # new user. The default group when creating a new user is always
        # the usergroup which gets automatically created on user
        # creation. So this value can (and must) be ignored.
        # Not removing the "gid" will otherwise cause an
        # IntegrityError while flushing the new user to the DB as the
        # usergroup with the given id is not persistent in the DB.
        if "gid" in values:
            del values["gid"]
        # Delete the sid which sets the default settings for the same
        # reasons than deleting the "gid" attribute.
        if "sid" in values:
            del values["sid"]

        new_user = BaseFactory.create(self, user, values)

        # Now create a a new Profile
        profile_property = get_prop_from_instance(new_user, "profile",
                                                  include_relations=True)
        profile_class = profile_property.mapper.class_
        profile_factory = BaseFactory(profile_class)
        profile = profile_factory.create(user, {})
        new_user.profile.append(profile)

        # Create settings
        settings_factory = BaseFactory(UserSetting)
        settings = settings_factory.create(user, {})
        new_user.settings = settings

        # A usergroup
        usergroup_property = get_prop_from_instance(new_user, "usergroup",
                                                    include_relations=True)
        usergroup_class = usergroup_property.mapper.class_
        usergroup_factory = BaseFactory(usergroup_class)
        usergroup = usergroup_factory.create(None, {})
        usergroup.name = new_user.login
        usergroup.members.append(new_user)
        # If no default group is set, then set the users group as
        # default group
        new_user.usergroup = values.get("usergroup") or usergroup
        return new_user
示例#7
0
 def create(self, user, values):
     new_item = BaseFactory.create(self, user, values)
     return new_item
示例#8
0
 def create(self, user, values):
     new_item = BaseFactory.create(self, user, values)
     return new_item
示例#9
0
 def create(self, user=None):
     new_item = BaseFactory.create(self, user)
     return new_item
示例#10
0
def get_base_list(clazz, request, user, table):
    """Helper function in views to get a BaseList instance for the
    given clazz. In contrast to the known "get_item_list" function
    which function will also handle searching, sorting and pagination
    based on the parameter privided within the current request.

    This function makes the search, pagination and sorting feature (as
    used in the advanced search) available in other external views.

    :clazz: Class of item which will be loaded.
    :request: Current request.
    :user: Current user. If None, than all items of a class will be loaded.
    :table: Name of the table configuration which is used for the listing.
    :returns: BaseList instance
    """
    # If the user enters the overview page of an item we assume that the
    # user actually leaves any context where a former set backurl is
    # relevant anymore. So delete it.
    backurl = request.session.get('%s.backurl' % clazz)
    if backurl:
        # Redirect to the configured backurl.
        del request.session['%s.backurl' % clazz]
        request.session.save()

    search = get_search(clazz, request)
    sorting = handle_sorting(clazz, request)
    pagination_page, pagination_size = handle_paginating(clazz, request)

    list_params = {}
    list_params["search"] = search
    list_params["sorting"] = sorting
    list_params["pagination"] = (pagination_page, pagination_size)

    # Try to do an optimized loading of items. If the loading succeeds
    # the loaded items will be used to build an item list. If for some
    # reasone the loading was'nt successfull items will be None and
    # loading etc will be done completely in application.
    if request.ringo.feature.dev_optimized_list_load:
        items, total = load_items(request, clazz, list_params)
        listing = get_item_list(request, clazz, user=user, items=items)
        # Ok no items are loaded. We will need to do sorting and filtering
        # on out own.
        if items is None:
            listing.sort(sorting[0], sorting[1])
            listing.filter(search, request, table)
    else:
        listing = get_item_list(request, clazz, user=user)
        listing.sort(sorting[0], sorting[1])
        listing.filter(search, request, table)

    total = len(listing.items)
    listing.paginate(total, pagination_page, pagination_size)

    # Only save the search if there are items
    if len(listing.items) > 0:
        request.session['%s.list.search' % clazz.__tablename__] = search
        if (request.params.get('form') == "search"):
            if "save" in request.params:
                query_name = request.params.get('save')
                user = BaseFactory(User).load(request.user.id)
                searches_dic = user.settings.get('searches', {})
                searches_dic_search = searches_dic.get(clazz.__tablename__, {})

                # Check if there is already a search saved with the name
                found = False
                for xxx in searches_dic_search.values():
                    if xxx[1] == query_name:
                        found = True
                        break
                if not found:
                    searches_dic_search[str(uuid.uuid1())] = (search, sorting,
                                                              query_name)
                searches_dic[clazz.__tablename__] = searches_dic_search
                user.settings.set('searches', searches_dic)
                request.db.flush()
            elif "delete" in request.params:
                query_key = request.params.get('delete')
                user = BaseFactory(User).load(request.user.id)
                searches_dic = user.settings.get('searches', {})
                searches_dic_search = searches_dic.get(clazz.__tablename__, {})
                try:
                    del searches_dic_search[query_key]
                except:
                    pass
                searches_dic[clazz.__tablename__] = searches_dic_search
                user.settings.set('searches', searches_dic)
                request.db.flush()
        request.session.save()
    return listing
示例#11
0
文件: model.py 项目: toirl/ringo_news
 def create(self, user=None, request=None):
     new_item = BaseFactory.create(self, user, request)
     return new_item