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 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':'/'})
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 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()