def _show_public_profile(user, form, can_update): if current_user.id == user.id: user_dict = cached_users.get_user_summary(user.name) else: user_dict = cached_users.public_get_user_summary(user.name) if current_user.admin: user_dict['email_addr'] = user.email_addr projects_contributed = cached_users.public_projects_contributed_cached( user.id) projects_created = cached_users.public_published_projects_cached(user.id) total_projects_contributed = '{} / {}'.format( cached_users.n_projects_contributed(user.id), n_published()) percentage_tasks_completed = user_dict['n_answers'] * 100 / ( n_total_tasks() or 1) if current_user.is_authenticated() and current_user.admin: draft_projects = cached_users.draft_projects(user.id) projects_created.extend(draft_projects) title = "%s · User Profile" % user_dict['fullname'] response = dict(template='/account/public_profile.html', title=title, user=user_dict, projects=projects_contributed, projects_created=projects_created, total_projects_contributed=total_projects_contributed, percentage_tasks_completed=percentage_tasks_completed, form=form, can_update=can_update, private_instance=bool(data_access_levels), upref_mdata_enabled=bool(app_settings.upref_mdata)) return handle_content_type(response)
def _show_public_profile(user, form): user_dict = cached_users.public_get_user_summary(user.name) projects_contributed = cached_users.public_projects_contributed_cached( user.id) projects_created = cached_users.public_published_projects_cached(user.id) can_update = False if (user.restrict is False and current_user.is_authenticated() and current_user.admin): draft_projects = cached_users.draft_projects(user.id) projects_created.extend(draft_projects) can_update = True if user.restrict is False: title = "%s · User Profile" % user_dict['fullname'] else: title = "User data is restricted" projects_contributed = [] projects_created = [] form = None response = dict(template='/account/public_profile.html', title=title, user=user_dict, projects=projects_contributed, projects_created=projects_created, form=form, can_update=can_update, input_form=False) return handle_content_type(response)
def _show_public_profile(user): user_dict = cached_users.public_get_user_summary(user.name) md = cached_users.get_metadata(user.name) form = MetadataForm(**md) projects_contributed = cached_users.projects_contributed_cached(user.id) projects_created = cached_users.published_projects_cached(user.id) total_projects_contributed = '{} / {}'.format(cached_users.n_projects_contributed(user.id), n_published()) percentage_tasks_completed = user_dict['n_answers'] * 100 / (n_total_tasks() or 1) if current_user.is_authenticated() and current_user.admin: draft_projects = cached_users.draft_projects(user.id) projects_created.extend(draft_projects) title = "%s · User Profile" % user_dict['fullname'] response = dict(template='/account/public_profile.html', title=title, user=user_dict, projects=projects_contributed, form=form, projects_created=projects_created, metadata=md, can_update=can_update_user_info(current_user, user), total_projects_contributed=total_projects_contributed, percentage_tasks_completed=percentage_tasks_completed) return handle_content_type(response)
def _show_public_profile(user, form): user_dict = cached_users.public_get_user_summary(user.name) projects_contributed = cached_users.public_projects_contributed_cached(user.id) projects_created = cached_users.public_published_projects_cached(user.id) can_update = False if (user.restrict is False and current_user.is_authenticated() and current_user.admin): draft_projects = cached_users.draft_projects(user.id) projects_created.extend(draft_projects) can_update = True if user.restrict is False: title = "%s · User Profile" % user_dict['fullname'] else: title = "User data is restricted" projects_contributed = [] projects_created = [] form = None response = dict(template='/account/public_profile.html', title=title, user=user_dict, projects=projects_contributed, projects_created=projects_created, form=form, can_update=can_update, input_form=False) return handle_content_type(response)
def test_draft_projects_no_projects(self): """Test CACHE USERS draft_projects returns an empty list if the user has no draft projects""" user = UserFactory.create() published_project = ProjectFactory.create(owner=user, published=True) draft_projects = cached_users.draft_projects(user.id) assert len(draft_projects) == 0, draft_projects
def test_draft_projects_return_drafts(self): """Test CACHE USERS draft_projects returns draft belonging to the user""" user = UserFactory.create() draft_project = ProjectFactory.create(owner=user, info={}) draft_projects = cached_users.draft_projects(user.id) assert len(draft_projects) == 1, draft_projects assert draft_projects[0]['short_name'] == draft_project.short_name, draft_projects
def test_draft_projects_no_projects(self): """Test CACHE USERS draft_projects returns an empty list if the user has no draft projects""" user = UserFactory.create() published_project = ProjectFactory.create(owner=user) draft_projects = cached_users.draft_projects(user.id) assert len(draft_projects) == 0, draft_projects
def add_metadata(name): """ Admin can save metadata for selected user. Regular user can save their own metadata. Redirects to public profile page for selected user. """ user = user_repo.get_by_name(name=name) (can_update, disabled_fields) = can_update_user_info(current_user, user) if not can_update: abort(403) form_data = get_form_data(request, user, disabled_fields) form = UserPrefMetadataForm(form_data, can_update=(can_update, disabled_fields)) form.set_upref_mdata_choices() if not form.validate(): if current_user.id == user.id: user_dict = cached_users.get_user_summary(user.name) else: user_dict = cached_users.public_get_user_summary(user.name) projects_contributed = cached_users.projects_contributed_cached( user.id) projects_created = cached_users.published_projects_cached(user.id) total_projects_contributed = '{} / {}'.format( cached_users.n_projects_contributed(user.id), n_published()) percentage_tasks_completed = user_dict['n_answers'] * 100 / ( n_total_tasks() or 1) if current_user.is_authenticated and current_user.admin: draft_projects = cached_users.draft_projects(user.id) projects_created.extend(draft_projects) title = "%s · User Profile" % user.name flash("Please fix the errors", 'message') return render_template( '/account/public_profile.html', title=title, user=user, projects=projects_contributed, projects_created=projects_created, total_projects_contributed=total_projects_contributed, percentage_tasks_completed=percentage_tasks_completed, form=form, input_form=True, can_update=can_update, upref_mdata_enabled=bool(app_settings.upref_mdata)) user_pref, metadata = get_user_pref_and_metadata(name, form) user.info['metadata'] = metadata ensure_data_access_assignment_from_form(user.info, form) user.user_pref = user_pref user_repo.update(user) cached_users.delete_user_pref_metadata(user.name) cached_users.delete_user_access_levels_by_id(user.id) delete_memoized(get_user_preferences, user.id) flash("Input saved successfully", "info") return redirect(url_for('account.profile', name=name))
def test_draft_projects_return_drafts(self): """Test CACHE USERS draft_projects returns draft belonging to the user""" user = UserFactory.create() draft_project = ProjectFactory.create(owner=user, published=False) draft_projects = cached_users.draft_projects(user.id) assert len(draft_projects) == 1, draft_projects assert draft_projects[0]['short_name'] == draft_project.short_name, draft_projects
def test_draft_projects_hidden(self): """Test CACHE USERS draft_projects returns a project that belongs to the user and is a draft, even it's marked as hidden""" user = UserFactory.create() hidden_draft_project = ProjectFactory.create(owner=user, hidden=1, info={}) draft_projects = cached_users.draft_projects(user.id) assert len(draft_projects) == 1, draft_projects
def test_draft_projects_only_returns_drafts(self): """Test CACHE USERS draft_projects does not return any pubished projects or drafts that belong to another user""" user = UserFactory.create() published_project = ProjectFactory.create(owner=user, published=True) other_users_draft_project = ProjectFactory.create(published=False) draft_projects = cached_users.draft_projects(user.id) assert len(draft_projects) == 0, draft_projects
def test_draft_projects_only_returns_drafts(self): """Test CACHE USERS draft_projects does not return any projects that are not draft (published) or drafts that belong to another user""" user = UserFactory.create() published_project = ProjectFactory.create(owner=user) TaskFactory.create(project=published_project) other_users_draft_project = ProjectFactory.create(info={}) draft_projects = cached_users.draft_projects(user.id) assert len(draft_projects) == 0, draft_projects
def test_draft_projects_returns_fields(self): """Test CACHE USERS draft_projects returns the info of the projects with the required fields""" user = UserFactory.create() draft_project = ProjectFactory.create(owner=user, info={}) fields = ('id', 'name', 'short_name', 'owner_id', 'description', 'overall_progress', 'n_tasks', 'n_volunteers', 'info') draft_project = cached_users.draft_projects(user.id) for field in fields: assert field in draft_project[0].keys(), field
def test_draft_projects_returns_fields(self): """Test CACHE USERS draft_projects returns the info of the projects with the required fields""" user = UserFactory.create() draft_project = ProjectFactory.create(owner=user, published=False) fields = ('id', 'name', 'short_name', 'owner_id', 'description', 'overall_progress', 'n_tasks', 'n_volunteers', 'info') draft_project = cached_users.draft_projects(user.id) for field in fields: assert field in draft_project[0].keys(), field
def _show_public_profile(user): user_dict = cached_users.get_user_summary(user.name) projects_contributed = cached_users.projects_contributed_cached(user.id) projects_created = cached_users.published_projects_cached(user.id) if current_user.is_authenticated() and current_user.admin: draft_projects = cached_users.draft_projects(user.id) projects_created.extend(draft_projects) title = "%s · User Profile" % user_dict['fullname'] return render_template('/account/public_profile.html', title=title, user=user_dict, projects=projects_contributed, projects_created=projects_created)
def add_metadata(name): """ Admin can add metadata for selected user Redirects to public profile page for selected user. """ user = user_repo.get_by_name(name=name) if not can_update_user_info(current_user, user): abort(403) form = MetadataForm(request.form) if not any(value for value in form.data.values()): user.info['metadata'] = {} user.user_pref = {} elif form.validate(): metadata = dict(admin=current_user.name, time_stamp=time.ctime(), user_type=form.user_type.data, start_time=form.start_time.data, end_time=form.end_time.data, review=form.review.data, timezone=form.timezone.data, profile_name=user.name) user.info['metadata'] = metadata user_pref = {} if form.languages.data: user_pref["languages"] = form.languages.data if form.locations.data: user_pref["locations"] = form.locations.data user.user_pref = user_pref else: projects_contributed = cached_users.projects_contributed_cached(user.id) projects_created = cached_users.published_projects_cached(user.id) metadata = cached_users.get_metadata(user.name) if current_user.is_authenticated() and current_user.admin: draft_projects = cached_users.draft_projects(user.id) projects_created.extend(draft_projects) title = "%s · User Profile" % user.name flash("Please fix the errors", 'message') return render_template('/account/public_profile.html', title=title, user=user, metadata=metadata, projects=projects_contributed, form=form, projects_created=projects_created, input_form=True) user_repo.update(user) cached_users.delete_user_metadata(user.name) delete_memoized(get_user_preferences, user.id) flash("Input saved successfully", "info") return redirect(url_for('account.profile', name=name))
def add_metadata(name): """ Admin can save metadata for selected user Redirects to public profile page for selected user. """ user = user_repo.get_by_name(name=name) form = UserPrefMetadataForm(request.form) form.set_upref_mdata_choices() if not form.validate(): if current_user.id == user.id: user_dict = cached_users.get_user_summary(user.name) else: user_dict = cached_users.public_get_user_summary(user.name) projects_contributed = cached_users.projects_contributed_cached( user.id) projects_created = cached_users.published_projects_cached(user.id) if current_user.is_authenticated() and current_user.admin: draft_projects = cached_users.draft_projects(user.id) projects_created.extend(draft_projects) title = "%s · User Profile" % user.name flash("Please fix the errors", 'message') can_update = current_user.admin return render_template('/account/public_profile.html', title=title, user=user_dict, projects=projects_contributed, projects_created=projects_created, form=form, can_update=can_update, input_form=True) user_pref, metadata = get_user_pref_and_metadata(name, form) user.info['metadata'] = metadata user.user_pref = user_pref user_repo.update(user) cached_users.delete_user_pref_metadata(user.name) flash("Input saved successfully", "info") return redirect(url_for('account.profile', name=name))
def add_metadata(name): """ Admin can save metadata for selected user Redirects to public profile page for selected user. """ user = user_repo.get_by_name(name=name) form = UserPrefMetadataForm(request.form) form.set_upref_mdata_choices() if not form.validate(): if current_user.id == user.id: user_dict = cached_users.get_user_summary(user.name) else: user_dict = cached_users.public_get_user_summary(user.name) projects_contributed = cached_users.projects_contributed_cached(user.id) projects_created = cached_users.published_projects_cached(user.id) if current_user.is_authenticated() and current_user.admin: draft_projects = cached_users.draft_projects(user.id) projects_created.extend(draft_projects) title = "%s · User Profile" % user.name flash("Please fix the errors", 'message') can_update = current_user.admin return render_template('/account/public_profile.html', title=title, user=user_dict, projects=projects_contributed, projects_created=projects_created, form=form, can_update=can_update, input_form=True) user_pref, metadata = get_user_pref_and_metadata(name, form) user.info['metadata'] = metadata user.user_pref = user_pref user_repo.update(user) cached_users.delete_user_pref_metadata(user.name) flash("Input saved successfully", "info") return redirect(url_for('account.profile', name=name))
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)
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