def restore(self, user): group_dashboard_mappings = DomainMapping.objects.get_group_dashboard_mappings( self.default_group.id) group_dashboard_clone_mappings = DomainMapping.objects. \ get_group_dashboard_clone_mappings(group_dashboard_mappings) user_dashboards_for_group = Dashboard.objects.filter(pk__in=list( group_dashboard_clone_mappings.values_list("src_id", flat=True)), user=user) for user_dashboard in user_dashboards_for_group: Dashboard.restore(user_dashboard) group_dashboard_ids = group_dashboard_mappings.values_list("dest_id", flat=True) user_cloned_dashboards_ids_for_group = DomainMapping.objects.filter( src_id__in=list( user_dashboards_for_group.values_list("id", flat=True)), src_type=MappingType.dashboard, relationship_type=RelationshipType.cloneOf, dest_type=MappingType.dashboard).values_list("dest_id", flat=True) # Create missing dashboards # TODO: extract this into a method for reuse. Same logic exists in people/models.py # TODO: modify this to use .exclude(). This was originally refactored, because of issues # using .difference() on querysets with mysql. missing_dashboard_ids = [] for group_dashboard_id in group_dashboard_ids: if group_dashboard_id not in user_cloned_dashboards_ids_for_group: missing_dashboard_ids.append(group_dashboard_id) Dashboard.create_missing_dashboards_for_user(user, missing_dashboard_ids)
def sync_dashboards(self): from stacks.models import StackGroups, Stack from domain_mappings.models import DomainMapping import uuid group_ids = list(self.groups.values_list("id", flat=True)) # Default groups that this user is in, represents user's direct assignment to stacks default_group_ids = [] for stacks in self.get_directly_assigned_stacks(): default_group_ids.append(stacks.default_group.id) # Get default groups from groups assigned to stacks stack_ids_assigned_through_group = [] for stack in self.get_group_assigned_stacks(): stack_ids_assigned_through_group.append(stack.id) default_group_ids_from_stack_groups_assignment = Stack.objects.filter(pk__in=stack_ids_assigned_through_group) \ .values_list("default_group_id", flat=True) # List of all default groups from stacks assigned to user and remove any duplicates default_group_ids_for_stacks = list( set(default_group_ids + list(default_group_ids_from_stack_groups_assignment))) group_dashboard_ids = DomainMapping.objects.filter( src_id__in=default_group_ids_for_stacks, src_type=MappingType.group, relationship_type=RelationshipType.owns, dest_type=MappingType.dashboard).values_list("dest_id", flat=True) user_dashboards = Dashboard.objects.filter(user=self) user_cloned_dashboards_ids = DomainMapping.objects.filter( src_id__in=list(user_dashboards.values_list("id", flat=True)), src_type=MappingType.dashboard, relationship_type=RelationshipType.cloneOf, dest_type=MappingType.dashboard).values_list("dest_id", flat=True) # Create missing dashboards # TODO: extract this into a method for reuse. Same logic exists in stacks/models.py # TODO: modify this to use .exclude(). This was originally refactored, because of issues # using .difference() on querysets with mysql. missing_dashboard_ids = [] for group_dashboard_id in group_dashboard_ids: if group_dashboard_id not in user_cloned_dashboards_ids: missing_dashboard_ids.append(group_dashboard_id) Dashboard.create_missing_dashboards_for_user(self, missing_dashboard_ids)
def test_correct_option_is_selected(self): dashboard = Dashboard(category=self.category, data_provider=EmptyDashboard) form = DashboardForm(instance=dashboard) widget = form['data_provider'].as_widget() selected_option = 'option value="dashboards.dashboards.EmptyDashboard" selected="selected' self.assertTrue(selected_option in widget)
def put(request): try: DashboardValidator.validate_params_for_update(request.query_params) dashboard = Dashboard.objects( uuid=request.query_params['uuid']).first() dashboard.update_with(request.query_params).save() return JsonResponse({'message': "Dashboard successfully updated"}, status=200) except InvalidDashboardParametersException as e: return JsonResponse({'error_message': e.message}, status=400)
def create_dashboard_from_query_params(query_params): dashboard = Dashboard() dashboard.name = query_params['name'] dashboard.description = query_params['description'] dashboard.template = query_params['template'] dashboard.monitored_object_id = query_params['monitored-object-uuid'] dashboard.transmitter_id = query_params['transmitter-uuid'] dashboard.uuid = uuid.uuid4() for widget in DashboardFactory.__get_default_widgets_for( template=query_params['template']): dashboard.widgets.append(widget) return dashboard
def put(request): try: Dashboard.objects( uuid=request.query_params['dashboard-uuid'], widgets__uuid=request.query_params['widget-uuid']).update_one( set__widgets__S__name=request.query_params['name'], set__widgets__S__description=request. query_params['description'], set__widgets__S__width=request.query_params['width']) return JsonResponse({'message': "Widget successfully updated"}, status=200) except InvalidWidgetParametersException as e: return JsonResponse( { 'message': "The query should contains the following parameters: " "<dashboard-uuid>, <widget-uuid>" }, status=400) except Exception as e: return JsonResponse({'message': e.message}, status=500)
def post(request): try: widget = DashboardWidgetFactory.create_widget_from_query_params( WidgetValidator.validate_params_for_creation( request.query_params)) dashboard = Dashboard.objects( uuid=request.query_params['dashboard-uuid']).first() dashboard.widgets.append(widget) dashboard.save() return Response(status=status.HTTP_201_CREATED) except InvalidDashboardParametersException as e: return JsonResponse({'error_message': e.message}, status=400)
def get(request): if "uuid" not in request.query_params.keys(): return render_to_response( 'dashboards/dashboards-index.html', { 'content_title': "Dashboards", 'dashboards': Dashboard.objects.all(), 'monitored_objects': MonitoredObject.objects.all(), 'transmitters': Transmitter.objects.all() }, RequestContext(request)) else: dashboard = Dashboard.objects( uuid=request.query_params["uuid"]).first() dashboard_rows = DashboardRowsFactory.create_dashboard_rows_from( dashboard.widgets) return render_to_response( 'dashboards/dashboard.html', { 'content_title': dashboard.name + "(" + MonitoredObject.objects( uuid=dashboard.monitored_object_id).first().name + ")", 'dashboards': dashboard, 'dashboard_rows': dashboard_rows, 'widget_types': DashboardWidget.TYPES.keys(), 'measure_units': DashboardWidget.MEASURE_UNITS.keys(), 'dashboard_transmitter': Transmitter.objects(uuid=dashboard.transmitter_id), 'transmitters': Transmitter.objects.all(), 'sensors': map( lambda sensor_uuid: Sensor.objects(uuid=sensor_uuid ).first(), MonitoredObject.objects( uuid=dashboard.monitored_object_id).first(). sensor_ids), 'local_sensors': LocalSensor.objects.all(), 'monitored_object': MonitoredObject.objects( uuid=dashboard.monitored_object_id).first() }, RequestContext(request))
def delete(request): try: if 'dashboard-uuid' in request.query_params.keys( ) and 'widget-uuid' in request.query_params.keys(): dashboard = Dashboard.objects( uuid=request.query_params['dashboard-uuid']) dashboard.update_one(pull__widgets__uuid=DashboardWidget( uuid=request.query_params['widget-uuid']).uuid) return JsonResponse({'message': "Widget successfully deleted"}, status=200) else: return JsonResponse( { 'message': "The query should contains the following parameters: " "<dashboard-uuid>, <widget-uuid>" }, status=400) except Exception as e: return JsonResponse({'message': e.message}, status=500)
def delete(request): """ Delete a dashboard. --- parameters: - name: uuid description: The dashboard's uuid. type: string required: true """ try: if 'uuid' in request.query_params.keys(): dashboard = Dashboard.objects( uuid=request.query_params['uuid']) dashboard.delete() return JsonResponse( {'message': "Dashboard successfully deleted"}, status=200) else: return JsonResponse({}, status=400) except Exception as e: return JsonResponse({'error_message': e.message}, status=500)
def test_provider_class_does_not_exist(self): with self.assertRaises(ValidationError): dashboard = Dashboard( category=self.category, data_provider='i.do.not.exist') dashboard.full_clean()
def test_provider_is_of_wrong_type(self): with self.assertRaises(ValidationError): dashboard = Dashboard( category=self.category, data_provider=IAmNotADashboardProvider) dashboard.full_clean()
def test_provider_class_does_not_exist(self): with self.assertRaises(ValidationError): dashboard = Dashboard(category=self.category, data_provider='i.do.not.exist') dashboard.full_clean()
def test_provider_is_of_wrong_type(self): with self.assertRaises(ValidationError): dashboard = Dashboard(category=self.category, data_provider=IAmNotADashboardProvider) dashboard.full_clean()
def upload(request): #check whether user id is in request.session dictionary-like object; i.e. user's session is running if request.session.get('member_id'): lines = list() #define state variables as global variables; state variables store the user's form entries and selections #to be later retrieved using form = UploadForm(initial={'title': title}) global author; global title; global status; global type; global resource; global file_name; global upload_date; print "before: " + title if request.method == "POST": form = UploadForm(request.POST, request.FILES) if form.is_valid(): cd = form.cleaned_data handle_uploaded_file(request.FILES['file']) u1 = User.objects.filter(id=request.session['member_id']) print u1 print "and "+ str(len(u1)) if u1 and len(u1) == 1: dashboard = Dashboard(file_field = request.FILES['file']) #upload file directly into db and wait until #user decides to publish it (see def publish(request) above dashboard.author = User(id = u1[0].id) dashboard.title = cd['title'] dashboard.status = cd['status'] dashboard.type = cd['type'] dashboard.resource = cd['resource'] #to keep versioning split name of the file file_split = str(cd['file']).split('.') dashboard.file_name = file_split[0] + version_prefix + '.' + file_split[1] dashboard.upload_date = datetime.datetime.now() #check whether dashboard already exists and alert user if Dashboard.objects.filter(author=request.session['member_id']).filter(title__contains=dashboard.title): author = User(id = u1[0].id) title = cd['title'] status = cd['status'] type = cd['type'] resource = cd['resource'] file_name = cd['file'] upload_date = datetime.datetime.now() lines.append("Duplicate dashboard title. Please click 'back' or 'Upload dashboard' " "and select a different name for dashboard") return render_to_response('base_upload.html', {'form':form, 'lines':lines}, RequestContext(request)) #if there are no duplicates of dashboard - save it in the database dashboard.save() return HttpResponseRedirect('/contact/thanks/') else: return HttpResponseRedirect('/contact/error_message/') else: form = UploadForm(initial={'title': title, 'status':status, 'type':type, 'resource':resource, 'file':file_name}) return render_to_response('base_upload.html', {'form':form, 'lines':lines}, RequestContext(request)) else: #redirect template to 'top' frame. Otherwise it will remain in the same frame and each new log in will created ' #nested cascade of frames return TemplateResponse(request, 'redirect_template.html', {'redirect_url':'/'})