def workspace_new(): # Step 1: Get a list of organizations this user owns existing = Workspace.query.filter(Workspace.userid.in_(g.user.organizations_owned_ids())).all() existing_ids = [e.userid for e in existing] # Step 2: Prune list to organizations without a workspace new_workspaces = [] for org in g.user.organizations_owned(): if org['userid'] not in existing_ids: new_workspaces.append((org['userid'], org['title'])) if not new_workspaces: return render_message( title=u"No organizations remaining", message=u"You do not have any organizations that do not yet have a workspace.") # Step 3: Ask user to select organization form = NewWorkspaceForm() form.workspace.choices = new_workspaces if form.validate_on_submit(): # Step 4: Make a workspace org = [org for org in g.user.organizations_owned() if org['userid'] == form.workspace.data][0] workspace = Workspace(name=org['name'], title=org['title'], userid=org['userid'], currency=form.currency.data, fullname=form.fullname.data, address=form.address.data, cin=form.cin.data,pan=form.pan.data,tin=form.tin.data,tan=form.tan.data) db.session.add(workspace) db.session.commit() flash("Created new workspace for %s" % workspace.title, "success") return render_redirect(url_for('workspace_view', workspace=workspace.name), code=303) return render_form(form=form, title="Create a new organization workspace", submit="Create", formid="workspace_new", cancel_url=url_for('index'), ajax=False)
def workspace_edit(workspace): form = NewWorkspaceForm() form.currency.choices = CURRENCIES if form.validate_on_submit(): form.populate_obj(workspace) db.session.add(workspace) db.session.commit() flash("Saved workspace %s" % workspace.title, "success") return render_redirect(url_for('workspace_view', workspace=workspace.name), code=303) return render_form(form=form, title="Edit organization workspace", submit="Save", formid="workspace_edit", cancel_url=url_for('workspace_view', workspace=workspace.name), ajax=False)