def test_published_projects_no_projects(self): """Test CACHE USERS published_projects returns empty list if the user has not created any project""" user = UserFactory.create() projects_published = cached_users.published_projects(user.id) assert projects_published == [], projects_published
def test_published_projects_only_returns_published(self): """Test CACHE USERS published_projects does not return draft or another user's projects""" user = UserFactory.create() another_user_published_project = ProjectFactory.create(published=True) draft_project = ProjectFactory.create(owner=user, published=False) projects_published = cached_users.published_projects(user.id) assert len(projects_published) == 0, projects_published
def test_published_projects_returns_published(self): """Test CACHE USERS published_projects returns a list with the projects that are published by the user""" user = UserFactory.create() published_project = ProjectFactory.create(owner=user, published=True) projects_published = cached_users.published_projects(user.id) assert len(projects_published) == 1, projects_published assert projects_published[0]['short_name'] == published_project.short_name, projects_published
def test_published_projects_returns_fields(self): """Test CACHE USERS published_projects returns the info of the projects with the required fields""" user = UserFactory.create() published_project = ProjectFactory.create(owner=user, published=True) fields = ('id', 'name', 'short_name', 'owner_id', 'description', 'overall_progress', 'n_tasks', 'n_volunteers', 'info') projects_published = cached_users.published_projects(user.id) for field in fields: assert field in projects_published[0].keys(), field
def test_published_projects_only_returns_published(self): """Test CACHE USERS published_projects does not return hidden, draft or another user's projects""" user = UserFactory.create() another_user_published_project = ProjectFactory.create() TaskFactory.create(project=another_user_published_project) draft_project = ProjectFactory.create(info={}) hidden_project = ProjectFactory.create(owner=user, hidden=1) TaskFactory.create(project=hidden_project) projects_published = cached_users.published_projects(user.id) assert len(projects_published) == 0, projects_published
def _get_user_projects(user_id, opts=None): projects_published = cached_users.published_projects(user_id, opts) projects_draft = cached_users.draft_projects(user_id) return projects_published, projects_draft
def _get_user_projects(user_id): projects_published = cached_users.published_projects(user_id) projects_draft = cached_users.draft_projects(user_id) return projects_published, projects_draft
def delete_user(name, confirmed): """ Deletes a user on pybossa - Only admins will be able to delete other users. - Does not let delete admin users. Admin users will have to remove the user from the admin lists before they can delete then - Marks all the task_runs of the specific user as anonymous - Changes the ownership of all the projects owned by the user to the current_user TODO: Clean this feature up and push this feature to pybossa core """ """ Get the user object and contributed projects object from cache to enable global helper functions to render it in a uniform way. But Obtain the results from the non-memoized functions to get the latest state """ target_user = cached_users.get_user_summary(name) if current_user.admin and target_user != None and current_user.id != target_user['id'] : user_page_redirect = request.args.get('user_page_redirect') if not user_page_redirect: user_page_redirect = 1 if confirmed == "unconfirmed": published_projects = cached_users.published_projects(target_user['id']) draft_projects = cached_users.draft_projects(target_user['id']) owned_projects = published_projects + draft_projects return render_template('geotagx/users/delete_confirmation.html', \ target_user = target_user, owned_projects = owned_projects, user_page_redirect=user_page_redirect ) elif confirmed == "confirmed": """ Retrieval of the User object necessary as the target_user object obtained from `cached_users.get_user_summary` doesnot expose the `admin` check that is necessary to prevent the user from deleting other admin users, and also the SQLAlchemy `delete` function """ user_object = User.query.filter_by(id=target_user['id']).first() if user_object.admin: # It is not allowed to delete other admin users abort(404) """ Mark all task runs by the user as anonymous Mark the user_ip field in the task_run by the username instead to retain user identity for analytics """ task_runs = TaskRun.query.filter_by(user_id=target_user['id']).all() for task_run in task_runs: task_run.user_id = None task_run.user_ip = "deleted_user_"+target_user['name'] db.session.commit() """ Change the ownership of all projects owned by the target user to that of the current user """ projects = Project.query.filter_by(owner_id=target_user['id']).all() for project in projects: project.owner_id = current_user.id db.session.commit() """ Clean cached data about the project """ cached_projects.clean_project(project.id) """ Delete the user from the database """ db.session.delete(user_object) db.session.commit() """ Clean user data from the cache Force Update current_user's data in the cache """ cached_users.delete_user_summary(target_user['id']) cached_users.delete_user_summary(current_user.id) flash("User <strong>"+target_user['name']+"</strong> has been successfully deleted, and all the projects owned by the user have been transferred to you.", 'success') return redirect(url_for('geotagx-admin.manage_users', page=user_page_redirect)) else: abort(404) else: abort(404)