Beispiel #1
0
def _get_default_org(user):
    """Gets the default org for a user and returns the id, name, and
    role_level. If no default organization is set for the user, the first
    organization the user has access to is set as default if it exists.

    :param user: the user to get the default org
    :returns: tuple (Organization id, Organization name, OrganizationUser role)
    """
    org = user.default_organization
    # check if user is still in the org, i.e. s/he wasn't removed from his/her
    # default org or didn't have a set org and try to set the first one
    if (
        not org
        or not OrganizationUser.objects.filter(
            organization=org, user=user
        ).exists()
    ):
        org = user.orgs.first()
        user.default_organization = org
        user.save()
    if org:
        org_id = org.pk
        org_name = org.name
        ou = user.organizationuser_set.filter(organization=org).first()
        # parent org owner has no role (None) yet has access to the sub-org
        org_user_role = _get_js_role(ou.role_level) if ou else ""
        return org_id, org_name, org_user_role
    else:
        return "", "", ""
 def test__get_js_role(self):
     self.assertEquals(_get_js_role(ROLE_OWNER), 'owner')
     self.assertEquals(_get_js_role(ROLE_MEMBER), 'member')
     self.assertEquals(_get_js_role(ROLE_VIEWER), 'viewer')
Beispiel #3
0
 def test__get_js_role(self):
     self.assertEquals(_get_js_role(ROLE_OWNER), 'owner')
     self.assertEquals(_get_js_role(ROLE_MEMBER), 'member')
     self.assertEquals(_get_js_role(ROLE_VIEWER), 'viewer')
Beispiel #4
0
def get_building(request):
    """
    Retrieves a building. If user doesn't belong to the building's org,
    fields will be masked to only those shared within the parent org's
    structure.

    :GET: Expects building_id and organization_id in query string.

    Returns::

        {
             'status': 'success or error',
             'message': 'error message, if any',
             'building': {'id': the building's id,
                          other fields this user has access to...
             },
             'imported_buildings': [ A list of buildings imported to create
                                     this building's record, in the same
                                     format as 'building'
                                   ],
             'compliance_projects': [ A list of compliance projects this
                                      building has been involved in
                                    ],
             'user_role': role of user in this org,
             'user_org_id': the org id this user belongs to
        }

    TODO:  Docstring needs elaboration on compliance_projects.
    """
    building_id = request.GET.get('building_id')
    organization_id = request.GET.get('organization_id')
    org = Organization.objects.get(pk=organization_id)
    building = BuildingSnapshot.objects.get(pk=building_id)
    user_orgs = request.user.orgs.all()
    parent_org = user_orgs[0].get_parent()

    if (
        building.super_organization in user_orgs
        or parent_org in user_orgs
    ):
        exportable_field_names = None  # show all
    else:
        # User isn't in the parent org or the building's org,
        # so only show shared fields.
        exportable_fields = parent_org.exportable_fields
        exportable_field_names = exportable_fields.values_list('name',
                                                               flat=True)

    building_dict = building.to_dict(exportable_field_names)

    ancestors = get_ancestors(building)
    imported_buildings_list = []
    for b in ancestors:
        d = b.to_dict(exportable_field_names)
        # get deleted import file names without throwing an error
        imp_file = ImportFile.raw_objects.get(pk=b.import_file_id)
        d['import_file_name'] = imp_file.filename_only
        # do not show deleted import file sources
        if not imp_file.deleted:
            imported_buildings_list.append(d)
    imported_buildings_list.sort(key=lambda x: x['source_type'])

    projects = get_compliance_projects(building, org)
    ou = request.user.organizationuser_set.filter(
        organization=building.super_organization
    ).first()

    return {
        'status': 'success',
        'building': building_dict,
        'imported_buildings': imported_buildings_list,
        'compliance_projects': projects,
        'user_role': _get_js_role(ou.role_level) if ou else "",
        'user_org_id': ou.organization.pk if ou else "",
    }
 def test__get_js_role(self):
     self.assertEquals(_get_js_role(ROLE_OWNER), "owner")
     self.assertEquals(_get_js_role(ROLE_MEMBER), "member")
     self.assertEquals(_get_js_role(ROLE_VIEWER), "viewer")