def adddashboard(request): if not request.is_ajax() or request.method != 'POST': #return method not allowed responsedata = { 'success': False, 'message': 'Non-ajax requests not allowed.' } return HttpResponse(json.dumps(responsedata), status=405) params = json.loads(request.body) try: dashboard = Dashboard(user=request.user, name=params['name']) dashboard.save() except (KeyError, ValueError) as e: print e responsedata = {'success': False, 'message': 'Internal server error.'} return HttpResponse(json.dumps(responsedata), status=500) responsedata = { 'success': True, 'message': 'Dashboard created.', 'uid': str(dashboard.uid) } return HttpResponse(json.dumps(responsedata))
def list(request): #print "found list" #print "Request: " + str(request) #print "User logged in: " + str(request.user.is_authenticated())#str(auth.user_logged_in) messages = [] error_messages = [] if (not request.user.is_authenticated()): return redirect(settings.BASE_URL) username = request.user.username #should check to make sure dashboard doesn't already exist if request.method == "POST": try: if request.POST[ "create_dashboard_submit"] == "Create Chat Dashboard": title = request.POST['title'].strip() dashboard = Dashboard(title=title, creator=username) permission = Dashboard_Permission( dashboard_title=title, user=username, privilege=Dashboard_Permissions.ADMIN) to_save = True for dash in Dashboard.objects: if dash.title == title: to_save = False if to_save: dashboard.save() permission.save() print "Created Dashboard: " + title else: messages.append( "Cannot create dashboard - the dashboard already exists" ) except: try: if request.POST["invite_user_submit"] == "Invite User": invite_user(request.POST["inviteemail"], username) except: pass user_dashboards = None try: user_dashboards = Dashboard_Permission.objects.filter(user=username) except: messages.append("You are not a user on any Dashboards.") all_dashboards = Dashboard.objects() template = loader.get_template('list.html') context = RequestContext( request, { 'all_dashboards': all_dashboards, 'user_dashboards': user_dashboards, 'messages': messages, 'error_messages': error_messages }) return HttpResponse(template.render(context))
def list(request): #print "found list" #print "Request: " + str(request) #print "User logged in: " + str(request.user.is_authenticated())#str(auth.user_logged_in) messages = [] error_messages = [] if (not request.user.is_authenticated()): return redirect(settings.BASE_URL) username = request.user.username #should check to make sure dashboard doesn't already exist if request.method == "POST": try: if request.POST["create_dashboard_submit"] == "Create Chat Dashboard": title = request.POST['title'].strip() dashboard = Dashboard(title=title, creator=username) permission = Dashboard_Permission(dashboard_title=title, user=username, privilege=Dashboard_Permissions.ADMIN) to_save = True for dash in Dashboard.objects: if dash.title == title: to_save = False if to_save: dashboard.save() permission.save() print "Created Dashboard: " + title else: messages.append("Cannot create dashboard - the dashboard already exists") except: try: if request.POST["invite_user_submit"] == "Invite User": invite_user(request.POST["inviteemail"],username) except: pass user_dashboards = None try: user_dashboards = Dashboard_Permission.objects.filter(user=username) except: messages.append("You are not a user on any Dashboards.") all_dashboards = Dashboard.objects() template = loader.get_template('list.html') context = RequestContext(request, { 'all_dashboards': all_dashboards, 'user_dashboards': user_dashboards, 'messages': messages, 'error_messages': error_messages }) return HttpResponse(template.render(context))
class DashboardsViewTest(MockedCloudAuthenticatedTestCase): def setUp(self): super(DashboardsViewTest, self).setUp() self.dash1 = Dashboard(widgets='{"test":"widget"}', owner=self.existing_user) self.dash1.save() self.dash2 = Dashboard(widgets='{"test2":"widget2"}', owner=self.existing_user) self.dash2.save() self.factory = RequestFactory() def test_dashboard_list_read(self): resp = self.client.get(reverse('dashboard-list')) self.assertEqual(resp.status_code, 200) json_resp = json.loads(resp.content) self.assertTrue(type(json_resp) is list) self.assertTrue(len(json_resp) == 2) self.assertEqual(json_resp[0]['widgets'], self.dash1.widgets) def test_dashboard_detail_read(self): resp = self.client.get( reverse('dashboard-list') + '/{}'.format(self.dash1.pk)) self.assertEqual(resp.status_code, 200) json_resp = json.loads(resp.content) self.assertTrue(type(json_resp) is not list) self.assertEqual(json_resp['widgets'], self.dash1.widgets) def test_unauthenticated_dashboard_read(self): self.client.logout() resp = self.client.get(reverse('dashboard-list')) self.assertEqual(resp.status_code, 403) def test_dashboard_create(self): old_dash_count = Dashboard.objects.filter( owner=self.existing_user).count() body = {'widgets': '{"test3":"widget3"}'} resp = self.client.post(reverse('dashboard-list'), data=body) self.assertEqual(resp.status_code, 201) new_dash_count = Dashboard.objects.filter( owner=self.existing_user).count() self.assertEqual(old_dash_count + 1, new_dash_count) def test_dashboard_bad_create(self): old_dash_count = Dashboard.objects.filter( owner=self.existing_user).count() bad_body = {'widgets': '{"badjsson'} resp = self.client.post(reverse('dashboard-list'), data=bad_body) self.assertEqual(resp.status_code, 400) new_dash_count = Dashboard.objects.filter( owner=self.existing_user).count() self.assertEqual(old_dash_count, new_dash_count) def test_dashboard_update(self): dashes = self.client.get(reverse('dashboard-list')) dash_loc = dashes.data[0]['url'] new_widget = {"widgets": {"new": "widget"}} resp = self.client.put(dash_loc, new_widget) self.assertEqual(resp.status_code, 200) widget = self.client.get(dash_loc) self.assertEqual(widget.data['widgets'], new_widget['widgets']) def test_dashboard_delete(self): old_dash_count = Dashboard.objects.filter( owner=self.existing_user).count() dashes = self.client.get(reverse('dashboard-list')) dash_loc = dashes.data[0]['url'] resp = self.client.delete(dash_loc) self.assertEqual(resp.status_code, 204) new_dash_count = Dashboard.objects.filter( owner=self.existing_user).count() self.assertEqual(old_dash_count - 1, new_dash_count)
class DashboardsViewTest(MockedCloudAuthenticatedTestCase): def setUp(self): super(DashboardsViewTest, self).setUp() self.dash1 = Dashboard(widgets='{"test":"widget"}', owner=self.existing_user) self.dash1.save() self.dash2 = Dashboard(widgets='{"test2":"widget2"}', owner=self.existing_user) self.dash2.save() self.factory = RequestFactory() def test_dashboard_list_read(self): resp = self.client.get(reverse('dashboard-list')) self.assertEqual(resp.status_code, 200) json_resp = json.loads(resp.content) self.assertTrue(type(json_resp) is list) self.assertTrue(len(json_resp) == 2) self.assertEqual(json_resp[0]['widgets'], self.dash1.widgets) def test_dashboard_detail_read(self): resp = self.client.get(reverse('dashboard-list')+'/{}'.format(self.dash1.pk)) self.assertEqual(resp.status_code, 200) json_resp = json.loads(resp.content) self.assertTrue(type(json_resp) is not list) self.assertEqual(json_resp['widgets'], self.dash1.widgets) def test_unauthenticated_dashboard_read(self): self.client.logout() resp = self.client.get(reverse('dashboard-list')) self.assertEqual(resp.status_code, 403) def test_dashboard_create(self): old_dash_count = Dashboard.objects.filter(owner = self.existing_user).count() body = {'widgets':'{"test3":"widget3"}'} resp = self.client.post(reverse('dashboard-list'), data=body) self.assertEqual(resp.status_code, 201) new_dash_count = Dashboard.objects.filter(owner = self.existing_user).count() self.assertEqual(old_dash_count + 1, new_dash_count) def test_dashboard_bad_create(self): old_dash_count = Dashboard.objects.filter(owner = self.existing_user).count() bad_body = {'widgets':'{"badjsson'} resp = self.client.post(reverse('dashboard-list'), data=bad_body) self.assertEqual(resp.status_code, 400) new_dash_count = Dashboard.objects.filter(owner = self.existing_user).count() self.assertEqual(old_dash_count, new_dash_count) def test_dashboard_update(self): dashes = self.client.get(reverse('dashboard-list')) dash_loc = dashes.data[0]['url'] new_widget = {"widgets":{"new":"widget"}} resp = self.client.put(dash_loc, new_widget) self.assertEqual(resp.status_code, 200) widget = self.client.get(dash_loc) self.assertEqual(widget.data['widgets'], new_widget['widgets']) def test_dashboard_delete(self): old_dash_count = Dashboard.objects.filter(owner = self.existing_user).count() dashes = self.client.get(reverse('dashboard-list')) dash_loc = dashes.data[0]['url'] resp = self.client.delete(dash_loc) self.assertEqual(resp.status_code, 204) new_dash_count = Dashboard.objects.filter(owner = self.existing_user).count() self.assertEqual(old_dash_count-1, new_dash_count)