Example #1
0
    def read(self, request):
        user = get_user_authentication(request)

        data_list = {'reloadShowcase': False}
        try:
            data_list['reloadShowcase'] = sync_base_workspaces(user)
            # updated user workspaces
            workspaces = WorkSpace.objects.filter(users=user)

            if not data_list['reloadShowcase'] and workspaces.count() == 0:
                # There is no workspace for the user

                cloned_workspace = None

                # it's the first time the user has logged in.
                # try to assign a default workspace according to user category
                try:
                    categories = getCategories(user)
                    if len(categories) > 0:
                        #take the first one which has a default workspace
                        for category in categories:
                            try:
                                default_workspace = Category.objects.get(category_id=getCategoryId(category)).default_workspace
                                # duplicate the workspace for the user
                                cloned_workspace = cloneWorkspace(default_workspace.id, user)
                                linkWorkspace(user, cloned_workspace.id, default_workspace.workspace.creator)
                                setActiveWorkspace(user, cloned_workspace)
                                data_list['reloadShowcase'] = True
                                break
                            except Category.DoesNotExist:
                                # the user category doesn't have a default workspace
                                # try with other categories
                                continue

                except Exception, e:
                    pass

                if not cloned_workspace:
                    # create an empty workspace
                    createEmptyWorkSpace(_('WorkSpace'), user)

            # Now we can fetch all the workspaces of an user
            workspaces = WorkSpace.objects.filter(users__id=user.id)

            # if there is no active workspace
            if UserWorkSpace.objects.filter(user=user, active=True).count() == 0:
                # set the first workspace as active
                setActiveWorkspace(user, workspaces.all()[0])