def createWorkSpace(workspaceName, user): cloned_workspace = None #try to assign a new workspace according to user category try: categories = getCategories(user) # take the first one which has a new workspace for category in categories: try: new_workspace = Category.objects.get(category_id=getCategoryId(category)).new_workspace if new_workspace != None: cloned_workspace, _junk = buildWorkspaceFromTemplate(new_workspace.template, user) cloned_workspace.name = workspaceName cloned_workspace.save() setActiveWorkspace(user, cloned_workspace) break except Category.DoesNotExist: #the user category doesn't have a new workspace #try with other categories continue except: pass if not cloned_workspace: #create an empty workspace return createEmptyWorkSpace(workspaceName, user) # Returning created Ids return cloned_workspace
def sync_base_workspaces(user): from workspace.mashupTemplateParser import buildWorkspaceFromTemplate packageLinker = PackageLinker() reload_showcase = False managers = get_workspace_managers() workspaces_by_manager = {} workspaces_by_ref = {} for manager in managers: workspaces_by_manager[manager.get_id()] = [] workspaces_by_ref[manager.get_id()] = {} workspaces = UserWorkSpace.objects.filter(user=user) for workspace in workspaces: if workspace.manager != '': workspaces_by_manager[workspace.manager].append(workspace.reason_ref) workspaces_by_ref[workspace.manager][workspace.reason_ref] = workspace for manager in managers: current_workspaces = workspaces_by_manager[manager.get_id()] result = manager.update_base_workspaces(user, current_workspaces) for workspace_to_remove in result[0]: user_workspace = workspaces_by_ref[manager.get_id()][workspace_to_remove] workspace = user_workspace.workspace user_workspace.delete() if workspace.userworkspace_set.count() == 0: workspace.delete() for workspace_to_add in result[1]: from_workspace = workspace_to_add[1] if isinstance(from_workspace, WorkSpace): user_workspace = packageLinker.link_workspace(from_workspace, user, from_workspace.creator) elif isinstance(from_workspace, PublishedWorkSpace): _junk, user_workspace = buildWorkspaceFromTemplate(from_workspace.template, user) else: # TODO warning continue user_workspace.manager = manager.get_id() user_workspace.reason_ref = workspace_to_add[0] user_workspace.save() reload_showcase = True return reload_showcase
def read(self, request, workspace_id): user = get_user_authentication(request) published_workspace = get_object_or_404(PublishedWorkSpace, id=workspace_id) workspace, _junk = buildWorkspaceFromTemplate(published_workspace.template, user) activate = request.GET.get('active') == "true" if not activate: workspaces = UserWorkSpace.objects.filter(user__id=user.id, active=True) if workspaces.count() == 0: # there aren't any active workspace yet activate = True # Mark the mashup as the active workspace if it's requested. For example, solutions if activate: setActiveWorkspace(user, workspace) workspace_data = get_global_workspace_data(workspace, user) return HttpResponse(json_encode(workspace_data.get_data()), mimetype='application/json; charset=UTF-8')