Example #1
0
def item_add(request, response_format='html'):
    "Add new knowledge item"
    items = Object.filter_permitted(manager=KnowledgeItem.objects, user=request.user.get_profile(), mode='r')
    
    if request.POST:
        if not 'cancel' in request.POST:
            item = KnowledgeItem()
            form = KnowledgeItemForm(request.user.get_profile(), None, request.POST, instance=item)
            if form.is_valid():
                item = form.save()
                item.set_user_from_request(request)
                return HttpResponseRedirect(reverse('knowledge_item_view', 
                                                    args=[item.folder.treepath, item.treepath]))
        else:
            return HttpResponseRedirect(reverse('knowledge'))
    else:   
        form = KnowledgeItemForm(request.user.get_profile(), None)
        
    context = _get_default_context(request)
    context.update({'items': items,
                    'form':form})
    
    return render_to_response('knowledge/item_add', context,
                              context_instance=RequestContext(request),
                              response_format=response_format)
Example #2
0
def item_add_folder(request, folderPath, response_format='html'):
    "Add new knowledge item to preselected folder"
    items = Object.filter_permitted(manager=KnowledgeItem.objects, user=request.user.get_profile(), mode='r')
    
    try:
        folder = KnowledgeFolder.by_path(folderPath)
        knowledgeType_id = folder.id
    except KnowledgeFolder.DoesNotExist:
        raise Http404    
            
    if request.POST:
        if not 'cancel' in request.POST:
            item = KnowledgeItem()
            form = KnowledgeItemForm(request.user.get_profile(), knowledgeType_id, request.POST, instance=item)
            if form.is_valid():
                item = form.save()
                item.set_user_from_request(request)
                return HttpResponseRedirect(reverse('knowledge_item_view', 
                                                    args=[item.folder.treepath, item.treepath]))
        else:
            return HttpResponseRedirect(reverse('knowledge'))
    else:   
        form = KnowledgeItemForm(request.user.get_profile(), knowledgeType_id)
        
    context = _get_default_context(request)
    context.update({'items': items,
                    'form':form,
                    'folder':folder})
    
    return render_to_response('knowledge/item_add_folder', context,
                              context_instance=RequestContext(request),
                              response_format=response_format)
Example #3
0
 def test_model_item(self):
     "Test item model"
     folder = KnowledgeFolder(name="test", treepath="test")
     folder.save()
     category = KnowledgeCategory(name="test", treepath="test")
     category.save()
     obj = KnowledgeItem(name="test", folder=folder, category=category, treepath="test")
     obj.save()
     self.assertEquals(folder, obj.folder)
     self.assertNotEquals(obj.id, None)
     obj.delete()
Example #4
0
    def setUp(self):
        "Initial Setup"
        if not self.prepared:
            # Clean up first
            Object.objects.all().delete()

            # Create objects
            try:
                self.group = Group.objects.get(name='test')
            except Group.DoesNotExist:
                Group.objects.all().delete()
                self.group = Group(name='test')
                self.group.save()

            try:
                self.user = DjangoUser.objects.get(username=self.username)
                self.user.set_password(self.password)
                try:
                    self.profile = self.user.get_profile()
                except Exception:
                    User.objects.all().delete()
                    self.user = DjangoUser(username=self.username, password='')
                    self.user.set_password(self.password)
                    self.user.save()
            except DjangoUser.DoesNotExist:
                User.objects.all().delete()
                self.user = DjangoUser(username=self.username, password='')
                self.user.set_password(self.password)
                self.user.save()

            try:
                perspective = Perspective.objects.get(name='default')
            except Perspective.DoesNotExist:
                Perspective.objects.all().delete()
                perspective = Perspective(name='default')
                perspective.set_default_user()
                perspective.save()
            ModuleSetting.set('default_perspective', perspective.id)

            self.folder = KnowledgeFolder(name='test', treepath='test')
            self.folder.set_default_user()
            self.folder.save()

            self.category = KnowledgeCategory(name='test', treepath='test')
            self.category.set_default_user()
            self.category.save()

            self.item = KnowledgeItem(name='test', folder=self.folder,
                                      category=self.category, treepath='test')
            self.item.set_default_user()
            self.item.save()

            # parent folder
            self.parent = KnowledgeFolder(name='test', treepath='test')
            self.parent.set_default_user()
            self.parent.save()

            self.client = Client()

            self.prepared = True
Example #5
0
    def setUp(self):
        "Initial Setup"
        if not self.prepared:
            self.group, created = Group.objects.get_or_create(name="test")
            duser, created = DjangoUser.objects.get_or_create(username=self.username)
            duser.set_password(self.password)
            duser.save()
            self.user, created = User.objects.get_or_create(user=duser)
            self.user.save()
            perspective, created = Perspective.objects.get_or_create(name="default")
            perspective.set_default_user()
            perspective.save()
            ModuleSetting.set("default_perspective", perspective.id)

            self.folder = KnowledgeFolder(name="test", treepath="test")
            self.folder.set_default_user()
            self.folder.save()

            self.category = KnowledgeCategory(name="test", treepath="test")
            self.category.set_default_user()
            self.category.save()

            self.item = KnowledgeItem(name="test", folder=self.folder, category=self.category, treepath="test")
            self.item.set_default_user()
            self.item.save()

            # parent folder
            self.parent = KnowledgeFolder(name="test", treepath="test")
            self.parent.set_default_user()
            self.parent.save()

            self.client = Client()

            self.prepared = True
Example #6
0
def item_view(request, folderPath, itemPath, response_format='html'):
    "Single knowledge item view page"

    try:
        item = KnowledgeItem.by_path(folderPath, itemPath)
    except KnowledgeItem.DoesNotExist:
        raise Http404
    if not item:
        raise Http404
    
    items = Object.filter_permitted(manager=KnowledgeItem.objects, user=request.user.get_profile(), mode='r')
    
    if not request.user.get_profile().has_permission(item):
        return user_denied(request, message="You don't have access to this Knowledge Item")
    
    context = _get_default_context(request)
    context.update({'items': items,
                    'item':item})

    return render_to_response('knowledge/item_view', context,
                              context_instance=RequestContext(request),
                              response_format=response_format)
Example #7
0
class KnowledgeViewsTest(TestCase):
    "Knowledge functional tests for views"

    username = "******"
    password = "******"
    prepared = False
    authentication_headers ={"CONTENT_TYPE": "application/json",
                             "HTTP_AUTHORIZATION" : "Basic YXBpX3Rlc3Q6YXBpX3Bhc3N3b3Jk" }
    content_type ='application/json'
    prepared = False

    def setUp(self):
        "Initial Setup"
        if not self.prepared:
            # Clean up first
            Object.objects.all().delete()

            # Create objects
            try:
                self.group = Group.objects.get(name='test')
            except Group.DoesNotExist:
                Group.objects.all().delete()
                self.group = Group(name='test')
                self.group.save()

            try:
                self.user = DjangoUser.objects.get(username=self.username)
                self.user.set_password(self.password)
                try:
                    self.profile = self.user.get_profile()
                except Exception:
                    User.objects.all().delete()
                    self.user = DjangoUser(username=self.username, password='')
                    self.user.set_password(self.password)
                    self.user.save()
            except DjangoUser.DoesNotExist:
                User.objects.all().delete()
                self.user = DjangoUser(username=self.username, password='')
                self.user.set_password(self.password)
                self.user.save()

            try:
                perspective = Perspective.objects.get(name='default')
            except Perspective.DoesNotExist:
                Perspective.objects.all().delete()
                perspective = Perspective(name='default')
                perspective.set_default_user()
                perspective.save()
            ModuleSetting.set('default_perspective', perspective.id)

            self.folder = KnowledgeFolder(name='test', treepath='test')
            self.folder.set_default_user()
            self.folder.save()

            self.category = KnowledgeCategory(name='test', treepath='test')
            self.category.set_default_user()
            self.category.save()

            self.item = KnowledgeItem(name='test', folder=self.folder,
                                      category=self.category, treepath='test')
            self.item.set_default_user()
            self.item.save()

            # parent folder
            self.parent = KnowledgeFolder(name='test', treepath='test')
            self.parent.set_default_user()
            self.parent.save()

            self.client = Client()

            self.prepared = True

    def test_unauthenticated_access(self):
        "Test index page at /api/knowledge/folders"
        response = self.client.get('/api/knowledge/folders')
        # Redirects as unauthenticated
        self.assertEquals(response.status_code, 401)

    def test_get_folders_list(self):
        """ Test index page api/knowledge/folders """
        response = self.client.get(path=reverse('api_knowledge_folders'), **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

    def test_get_folder(self):
        response = self.client.get(path=reverse('api_knowledge_folders', kwargs={'object_ptr': self.folder.id}), **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

    def test_update_folder(self):
        updates = {'name': 'Api folder update', 'parent': self.parent.id, 'details': '<p>api details</p>'}
        response = self.client.put(path=reverse('api_knowledge_folders', kwargs={'object_ptr': self.folder.id}),
                                   content_type=self.content_type,  data=json.dumps(updates), **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

        data = json.loads(response.content)
        self.assertEquals(updates["name"], data["name"])
        self.assertEquals(updates["parent"], data["parent"]["id"])
        self.assertEquals(updates["details"], data["details"])

    def test_get_categories_list(self):
        """ Test index page api/knowledge/categories """
        response = self.client.get(path=reverse('api_knowledge_categories'), **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

    def test_get_category(self):
        response = self.client.get(path=reverse('api_knowledge_categories', kwargs={'object_ptr': self.category.id}), **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

    def test_update_category(self):
        updates = {'name': 'Api catagory update', 'details': '<p>api details</p>'}
        response = self.client.put(path=reverse('api_knowledge_categories', kwargs={'object_ptr': self.category.id}),
                                   content_type=self.content_type,  data=json.dumps(updates), **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

        data = json.loads(response.content)
        self.assertEquals(updates["name"], data["name"])
        self.assertEquals(updates["details"], data["details"])

    def test_get_items_list(self):
        """ Test index page api/knowledge/items """
        response = self.client.get(path=reverse('api_knowledge_items'), **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

    def test_get_item(self):
        response = self.client.get(path=reverse('api_knowledge_items', kwargs={'object_ptr': self.item.id}), **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

    def test_update_item(self):
        updates = {'name': 'Api item update', 'folder': self.folder.id, 'category': self.category.id,
                   'body': '<p>api body</p>'}
        response = self.client.put(path=reverse('api_knowledge_items', kwargs={'object_ptr': self.item.id}),
                                   content_type=self.content_type,  data=json.dumps(updates), **self.authentication_headers)
        self.assertEquals(response.status_code, 400)
Example #8
0
class KnowledgeViewsTest(TestCase):
    "Knowledge functional tests for views"

    username = "******"
    password = "******"
    prepared = False

    def setUp(self):
        "Initial Setup"
        if not self.prepared:
            self.group, created = Group.objects.get_or_create(name="test")
            duser, created = DjangoUser.objects.get_or_create(username=self.username)
            duser.set_password(self.password)
            duser.save()
            self.user, created = User.objects.get_or_create(user=duser)
            self.user.save()
            perspective, created = Perspective.objects.get_or_create(name="default")
            perspective.set_default_user()
            perspective.save()
            ModuleSetting.set("default_perspective", perspective.id)

            self.folder = KnowledgeFolder(name="test", treepath="test")
            self.folder.set_default_user()
            self.folder.save()

            self.category = KnowledgeCategory(name="test", treepath="test")
            self.category.set_default_user()
            self.category.save()

            self.item = KnowledgeItem(name="test", folder=self.folder, category=self.category, treepath="test")
            self.item.set_default_user()
            self.item.save()

            # parent folder
            self.parent = KnowledgeFolder(name="test", treepath="test")
            self.parent.set_default_user()
            self.parent.save()

            self.client = Client()

            self.prepared = True

    ######################################
    # Testing views when user is logged in
    ######################################

    def test_index_login(self):
        "Test index page with login at /knowledge/"
        response = self.client.post("/accounts/login", {"username": self.username, "password": self.password})
        self.assertRedirects(response, "/")
        response = self.client.get(reverse("knowledge"))
        self.assertEquals(response.status_code, 200)

    def test_index_categories_login(self):
        "Test index page with login at /knowledge/categories/"
        response = self.client.post("/accounts/login", {"username": self.username, "password": self.password})
        self.assertRedirects(response, "/")
        response = self.client.get(reverse("knowledge_categories"))
        self.assertEquals(response.status_code, 200)

    # Knowledge folders

    def test_knowledge_folder_add(self):
        "Test index page with login at /knowledge/folder/add/"
        response = self.client.post("/accounts/login", {"username": self.username, "password": self.password})
        self.assertRedirects(response, "/")
        response = self.client.get(reverse("knowledge_folder_add"))
        self.assertEquals(response.status_code, 200)

    def test_knowledge_folder_add_typed(self):
        "Test index page with login at /knowledge/folder/add/<folderPath>"
        response = self.client.post("/accounts/login", {"username": self.username, "password": self.password})
        self.assertRedirects(response, "/")
        response = self.client.get(reverse("knowledge_folder_add_folder", args=[self.parent.treepath]))
        self.assertEquals(response.status_code, 200)

    def test_knowledge_folder_view(self):
        "Test index page with login at /knowledge/folder/view/<knowledgeType_id>"
        response = self.client.post("/accounts/login", {"username": self.username, "password": self.password})
        self.assertRedirects(response, "/")
        response = self.client.get(reverse("knowledge_folder_view", args=[self.folder.treepath]))
        self.assertEquals(response.status_code, 200)

    def test_knowledge_folder_edit(self):
        "Test index page with login at /knowledge/folder/edit/<knowledgeType_id>"
        response = self.client.post("/accounts/login", {"username": self.username, "password": self.password})
        self.assertRedirects(response, "/")
        response = self.client.get(reverse("knowledge_folder_edit", args=[self.folder.id]))
        self.assertEquals(response.status_code, 200)

    def test_knowledge_folder_delete(self):
        "Test index page with login at /knowledge/folder/delete/<knowledgeType_id>"
        response = self.client.post("/accounts/login", {"username": self.username, "password": self.password})
        self.assertRedirects(response, "/")
        response = self.client.get(reverse("knowledge_folder_delete", args=[self.folder.id]))
        self.assertEquals(response.status_code, 200)

    # Knowledge items

    def test_knowledge_item_add(self):
        "Test index page with login at /knowledge/item/add"
        response = self.client.post("/accounts/login", {"username": self.username, "password": self.password})
        self.assertRedirects(response, "/")
        response = self.client.get(reverse("knowledge_item_add"))
        self.assertEquals(response.status_code, 200)

    def test_knowledge_item_add_typed(self):
        "Test index page with login at /knowledge/item/add/<folderPath>"
        response = self.client.post("/accounts/login", {"username": self.username, "password": self.password})
        self.assertRedirects(response, "/")
        response = self.client.get(reverse("knowledge_item_add_folder", args=[self.folder.treepath]))
        self.assertEquals(response.status_code, 200)

    def test_knowledge_item_view(self):
        "Test index page with login at /knowledge/item/view/<knowledgeItem_id>"
        response = self.client.post("/accounts/login", {"username": self.username, "password": self.password})
        self.assertRedirects(response, "/")
        response = self.client.get(reverse("knowledge_item_view", args=[self.item.folder.treepath, self.item.treepath]))
        self.assertEquals(response.status_code, 200)

    def test_knowledge_item_edit(self):
        "Test index page with login at /knowledge/item/edit/<knowledgeItem_id>"
        response = self.client.post("/accounts/login", {"username": self.username, "password": self.password})
        self.assertRedirects(response, "/")
        response = self.client.get(reverse("knowledge_item_edit", args=[self.item.id]))
        self.assertEquals(response.status_code, 200)

    def test_knowledge_item_delete(self):
        "Test index page with login at /knowledge/item/delete/<knowledgeItem_id>"
        response = self.client.post("/accounts/login", {"username": self.username, "password": self.password})
        self.assertRedirects(response, "/")
        response = self.client.get(reverse("knowledge_item_delete", args=[self.item.id]))
        self.assertEquals(response.status_code, 200)

    # Knowledge categories

    def test_knowledge_category_add(self):
        "Test index page with login at /knowledge/category/add"
        response = self.client.post("/accounts/login", {"username": self.username, "password": self.password})
        self.assertRedirects(response, "/")
        response = self.client.get(reverse("knowledge_category_add"))
        self.assertEquals(response.status_code, 200)

    def test_knowledge_category_view(self):
        "Test index page with login at /knowledge/category/view/<knowledgeCategory_id>"
        response = self.client.post("/accounts/login", {"username": self.username, "password": self.password})
        self.assertRedirects(response, "/")
        response = self.client.get(reverse("knowledge_category_view", args=[self.category.treepath]))
        self.assertEquals(response.status_code, 200)

    def test_knowledge_category_edit(self):
        "Test index page with login at /knowledge/category/edit/<knowledgeCategory_id>"
        response = self.client.post("/accounts/login", {"username": self.username, "password": self.password})
        self.assertRedirects(response, "/")
        response = self.client.get(reverse("knowledge_category_edit", args=[self.category.id]))
        self.assertEquals(response.status_code, 200)

    def test_knowledge_category_delete(self):
        "Test index page with login at /knowledge/category/delete/<knowledgeCategory_id>"
        response = self.client.post("/accounts/login", {"username": self.username, "password": self.password})
        self.assertRedirects(response, "/")
        response = self.client.get(reverse("knowledge_category_delete", args=[self.category.id]))
        self.assertEquals(response.status_code, 200)

    ######################################
    # Testing views when user is not logged in
    ######################################

    def test_index(self):
        "Test index page at /knowledge/"
        response = self.client.get("/knowledge/")
        # Redirects as unauthenticated
        self.assertRedirects(response, reverse("user_login"))

    def test_index_categories_out(self):
        "Testing /knowledge/categories/"
        response = self.client.get(reverse("knowledge_categories"))
        self.assertRedirects(response, reverse("user_login"))

    # Knowledge folders

    def test_knowledge_folder_add_out(self):
        "Testing /knowledge/folder/add/"
        response = self.client.get(reverse("knowledge_folder_add"))
        self.assertRedirects(response, reverse("user_login"))

    def test_knowledge_folder_add_typed_out(self):
        "Testing /knowledge/folder/add/<folderPath>"
        response = self.client.get(reverse("knowledge_folder_add_folder", args=[self.parent.treepath]))
        self.assertRedirects(response, reverse("user_login"))

    def test_knowledge_folder_view_out(self):
        "Testing /knowledge/folder/view/<knowledgeType_id>"
        response = self.client.get(reverse("knowledge_folder_view", args=[self.folder.treepath]))
        self.assertRedirects(response, reverse("user_login"))

    def test_knowledge_folder_edit_out(self):
        "Testing /knowledge/folder/edit/<knowledgeType_id>"
        response = self.client.get(reverse("knowledge_folder_edit", args=[self.folder.id]))
        self.assertRedirects(response, reverse("user_login"))

    def test_knowledge_folder_delete_out(self):
        "Testing /knowledge/folder/delete/<knowledgeType_id>"
        response = self.client.get(reverse("knowledge_folder_delete", args=[self.folder.id]))
        self.assertRedirects(response, reverse("user_login"))

    # Knowledge items

    def test_knowledge_item_add_out(self):
        "Testing /knowledge/item/add"
        response = self.client.get(reverse("knowledge_item_add"))
        self.assertRedirects(response, reverse("user_login"))

    def test_knowledge_item_add_typed_out(self):
        "Testing /knowledge/item/add/<folderPath>"
        response = self.client.get(reverse("knowledge_item_add_folder", args=[self.folder.treepath]))
        self.assertRedirects(response, reverse("user_login"))

    def test_knowledge_item_view_out(self):
        "Testing /knowledge/item/view/<knowledgeItem_id>"
        response = self.client.get(reverse("knowledge_item_view", args=[self.item.folder.treepath, self.item.treepath]))
        self.assertRedirects(response, reverse("user_login"))

    def test_knowledge_item_edit_out(self):
        "Testing /knowledge/item/edit/<knowledgeItem_id>"
        response = self.client.get(reverse("knowledge_item_edit", args=[self.item.id]))
        self.assertRedirects(response, reverse("user_login"))

    def test_knowledge_item_delete_out(self):
        "Testing /knowledge/item/delete/<knowledgeItem_id>"
        response = self.client.get(reverse("knowledge_item_delete", args=[self.item.id]))
        self.assertRedirects(response, reverse("user_login"))

    # Knowledge categories

    def test_knowledge_category_add_out(self):
        "Testing /knowledge/category/add"
        response = self.client.get(reverse("knowledge_category_add"))
        self.assertRedirects(response, reverse("user_login"))

    def test_knowledge_category_view_out(self):
        "Testing /knowledge/category/view/<knowledgeCategory_id>"
        response = self.client.get(reverse("knowledge_category_view", args=[self.category.treepath]))
        self.assertRedirects(response, reverse("user_login"))

    def test_knowledge_category_edit_out(self):
        "Testing /knowledge/category/edit/<knowledgeCategory_id>"
        response = self.client.get(reverse("knowledge_category_edit", args=[self.category.id]))
        self.assertRedirects(response, reverse("user_login"))

    def test_knowledge_category_delete_out(self):
        "Testing /knowledge/category/delete/<knowledgeCategory_id>"
        response = self.client.get(reverse("knowledge_category_delete", args=[self.category.id]))
        self.assertRedirects(response, reverse("user_login"))