Skip to content

Commit

Permalink
Merge pull request #572 from pipermerriam/piper/issue-560-use-organiz…
Browse files Browse the repository at this point in the history
…ation-context-for-label-endpoints

Handle users with multiple organizations correctly with labels
  • Loading branch information
mmclark committed Dec 17, 2015
2 parents 9fd1e74 + 182cd69 commit 26db0c1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
1 change: 1 addition & 0 deletions seed/static/seed/js/controllers/menu_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ angular.module('BE.seed.controller.menu', [])
organization_service.get_organizations().then(function (data) {
// resolve promise
$scope.organizations_count = data.organizations.length;
$scope.menu.user.organizations = data.organizations;
});
};
init();
Expand Down
14 changes: 12 additions & 2 deletions seed/static/seed/js/services/label_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ angular.module('BE.seed.service.label',
$http({
method: 'POST',
'url': window.BE.urls.label_list,
'data': label
'data': label,
'params': {
'organization_id': user_service.get_organization().id
}
}).success(function(data, status, headers, config) {
if(data){
data = update_label_w_local_props(data);
Expand Down Expand Up @@ -129,6 +132,9 @@ angular.module('BE.seed.service.label',
method: 'PUT',
'url': window.BE.urls.label_list + label.id + '/',
'data': label,
'params': {
'organization_id': user_service.get_organization().id
}
}).success(function(data, status, headers, config) {
if(data){
data = update_label_w_local_props(data);
Expand All @@ -153,6 +159,9 @@ angular.module('BE.seed.service.label',
$http({
method: 'DELETE',
'url': window.BE.urls.label_list + label.id + '/',
'params': {
'organization_id': user_service.get_organization().id
}
}).success(function(data, status, headers, config) {
defer.resolve(data);
}).error(function(data, status, headers, config) {
Expand Down Expand Up @@ -188,7 +197,8 @@ angular.module('BE.seed.service.label',
'url': window.BE.urls.update_building_labels,
'params': _.extend({
'selected_buildings' : selected_buildings,
'select_all_checkbox': select_all_checkbox
'select_all_checkbox': select_all_checkbox,
'organization_id': user_service.get_organization().id
}, search_params),
'data': {
'add_label_ids': add_label_ids,
Expand Down
32 changes: 29 additions & 3 deletions seed/views/labels.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.core.exceptions import ObjectDoesNotExist

from rest_framework import viewsets
from rest_framework import generics
from rest_framework import response
Expand Down Expand Up @@ -29,13 +31,25 @@ class LabelViewSet(DecoratorMixin(drf_api_endpoint),
queryset = Label.objects.none()
filter_backends = (LabelFilterBackend,)

_organization = None

def get_organization(self):
if self._organization is None:
try:
self._organization = self.request.user.orgs.get(
pk=self.request.query_params["organization_id"],
)
except (KeyError, ObjectDoesNotExist):
self._organization = self.request.user.orgs.all()[0]
return self._organization

def get_queryset(self):
return Label.objects.filter(
super_organization__in=self.request.user.orgs.all()
super_organization=self.get_organization()
).order_by("name").distinct()

def get_serializer(self, *args, **kwargs):
kwargs['super_organization'] = self.request.user.orgs.first()
kwargs['super_organization'] = self.get_organization()
building_snapshots = BuildingFilterBackend().filter_queryset(
request=self.request,
queryset=BuildingSnapshot.objects.all(),
Expand All @@ -50,6 +64,18 @@ class UpdateBuildingLabelsAPIView(generics.GenericAPIView):
queryset = BuildingSnapshot.objects.all()
serializer_class = UpdateBuildingLabelsSerializer

_organization = None

def get_organization(self):
if self._organization is None:
try:
self._organization = self.request.user.orgs.get(
pk=self.request.query_params["organization_id"],
)
except ObjectDoesNotExist:
self._organization = self.request.user.orgs.all()[0]
return self._organization

def put(self, *args, **kwargs):
"""
Updates label assignments to buildings.
Expand Down Expand Up @@ -81,7 +107,7 @@ def put(self, *args, **kwargs):
serializer = self.get_serializer(
data=self.request.data,
queryset=queryset,
super_organization=self.request.user.orgs.first(),
super_organization=self.get_organization(),
)
serializer.is_valid(raise_exception=True)

Expand Down

0 comments on commit 26db0c1

Please sign in to comment.