Exemplo n.º 1
0
    def test_should_update_user_permission(self):
        user_permission = UserPermission(self.manager, 1, ['q1', 'q2'])
        user_permission.save()

        #Remove all questionnaires
        update_user_permission(self.manager, 1, [])
        questionnaires = get_questionnaires_for_user(1, self.manager)
        self.assertEqual(len(questionnaires), 0)

        #Again add some questionnaires
        form_model_id = self._create_sample_questionnaire()
        update_user_permission(self.manager, 1, [form_model_id])
        questionnaires = get_questionnaires_for_user(1, self.manager)
        self.assertEqual(questionnaires[0]['_id'], form_model_id)
        self.assertEqual(questionnaires[0].get('is_project_manager'), True)
Exemplo n.º 2
0
def populate_user_permission(database_name):
    try:
        start = time.time()
        organization_setting = OrganizationSetting.objects.get(
            document_store=database_name)
        org_id = organization_setting.organization_id
        project_managers = User.objects.filter(ngouserprofile__org_id=org_id, groups__name__in=["Project Managers"])\
            .values_list('id', flat=True)
        dbm = get_db_manager(database_name)
        project_ids = [
            row.key for row in dbm.database.query(map_all_questionnaire_id)
        ]
        for user_id in project_managers:
            update_user_permission(dbm, user_id, project_ids)
        mark_as_completed(database_name)
        logger.info(
            'Time taken (seconds) for migrating {database_name} : {timetaken}'.
            format(database_name=database_name,
                   timetaken=(time.time() - start)))
    except Exception as e:
        self.logger.exception(
            'Unexpected error while migrating user permission')
        raise
Exemplo n.º 3
0
def edit_user_profile(request, user_id=None):
    user = User.objects.get(id=user_id)
    current_user = request.user

    if user is None:
        data = {"errors": "User not found"}
        return HttpResponse(json.dumps(data),
                            mimetype="application/json",
                            status=404)
    if not current_user.has_higher_privileges_than(user):
        return HttpResponseRedirect(django_settings.ACCESS_DENIED_PAGE)
    else:
        profile = user.get_profile()
    if request.method == 'GET':
        if profile.mobile_phone == 'Not Assigned':
            profile.mobile_phone = ''
        org = get_organization(request)
        manager = get_database_manager(user)
        questionnaire_list = get_questionnaires_for_user(user.id, manager)
        questionnaire_map = make_questionnaire_map(questionnaire_list)
        form_data = dict(title=profile.title,
                         id=user_id,
                         full_name=profile.user.first_name,
                         username=profile.user.username,
                         mobile_phone=profile.mobile_phone,
                         role=user.groups.all()[0].name,
                         questionnaires=questionnaire_map)
        return render_to_response("accountmanagement/account/edit_user.html", {
            'form_data': json.dumps(form_data),
            'is_pro_sms': org.is_pro_sms,
            'current_lang': get_language()
        },
                                  context_instance=(RequestContext(request)))

    if request.method == 'POST':
        post_parameters = request.POST
        role = post_parameters['role']
        ngo_user = NGOUserProfile.objects.get(user=user)
        manager = get_database_manager(user)
        reporter_id = ngo_user.reporter_id
        previous_role = user.groups.all()[0].name
        org = get_organization(request)
        form = EditUserProfileForm(organization=org,
                                   reporter_id=profile.reporter_id,
                                   data=request.POST)
        message = ""
        _edit_user_success = False
        if form.is_valid():
            _update_user_and_profile(form, user.username, role)

            name = post_parameters["full_name"]

            if role == 'Project Managers':
                selected_questionnaires = post_parameters.getlist(
                    'selected_questionnaires[]')
                selected_questionnaire_names = post_parameters.getlist(
                    'selected_questionnaire_names[]')
                if selected_questionnaires is None or len(
                        selected_questionnaires) < 1:
                    selected_questionnaires = []

                dissociate_user_as_datasender_with_projects(
                    reporter_id, user, previous_role, selected_questionnaires)
                make_user_data_sender_for_projects(manager,
                                                   selected_questionnaires,
                                                   reporter_id)
                update_user_permission(manager,
                                       user_id=user.id,
                                       project_ids=selected_questionnaires)
                UserActivityLog().log(request,
                                      action=UPDATED_USER,
                                      detail=activity_log_detail(
                                          name, friendly_name(role),
                                          selected_questionnaire_names))
            elif role == 'Extended Users':
                if previous_role != 'Extended Users':
                    associate_user_with_all_projects_of_organisation(
                        manager, reporter_id)
                    update_user_permission(manager,
                                           user_id=user.id,
                                           project_ids=[])
                UserActivityLog().log(request,
                                      action=UPDATED_USER,
                                      detail=activity_log_detail(
                                          name, friendly_name(role)))

            message = _('Profile has been updated successfully')
            _edit_user_success = True
        data = dict(edit_user_success=_edit_user_success,
                    errors=form.errors,
                    message=message)
        return HttpResponse(json.dumps(data),
                            mimetype="application/json",
                            status=200)