Monday, August 1, 2011

GSoC 2011 GUI Overhaul: Document List for Admin

Hi everyone,

Lets talk about document list. Carol asked this on issue 1257. To implement such list, I borrowed the view from accepted_orgs.py. Lets name the list "DocumentList" and the page "DocumentListPage". I only added two visible cols, the title and shortname, for DocumentList. When the row is clicked, it will go to the edit document page view. I also created the test for document list inside test_document.py. The test will make sure, the list will be shown to the host (based on checkAccess method). Okay here's the class for DocumentList and DocumentListPage:

class DocumentList(Template):
"""Template for list of documents.
"""

def __init__(self, request, data):
self.request = request
self.data = data
r = data.redirect

list_config = lists.ListConfiguration()
list_config.addColumn('title', 'Title',
lambda e, *args: e.title.strip())
list_config.addSimpleColumn('link_id', 'Link ID', hidden=True)
list_config.addColumn('short_name', 'Short Name',
lambda e, *args: e.short_name.strip())
list_config.setRowAction(
lambda e, *args: r.document(e).urlOf('edit_gsoc_document'))

list_config.setDefaultPagination(False)
list_config.setDefaultSort('title')

self._list_config = list_config

def context(self):
description = 'List of documents for %s' % (
self.data.program.name)

list = lists.ListConfigurationResponse(
self.data, self._list_config, 0, description)

return {
'lists': [list],
}

def getListData(self):
idx = lists.getListIndex(self.request)
if idx == 0:
fields = {'scope': self.data.program}
response_builder = lists.QueryContentResponseBuilder(
self.request, self._list_config, document_logic, fields)
return response_builder.build()
else:
return None

def templatePath(self):
return 'v2/modules/gsoc/document/_document_list.html'


class DocumentListPage(RequestHandler):
"""View for the list documents page.
"""

def templatePath(self):
return 'v2/modules/gsoc/document/document_list.html'

def djangoURLPatterns(self):
return [
url(r'documents/%s$' % url_patterns.PROGRAM, self,
name='list_gsoc_documents'),
]

def checkAccess(self):
self.check.isHost()

def jsonContext(self):
list_content = DocumentList(self.request, self.data).getListData()

if not list_content:
raise AccessViolation(
'You do not have access to this data')
return list_content.content()

def context(self):
return {
'page_name': "Documents for %s" % self.data.program.name,
'document_list': DocumentList(self.request, self.data),
'program_select': ProgramSelect(self.data, 'list_gsoc_documents'),
}

And here's the test for document list:
class ListDocumentTest(DjangoTestCase):
"""Test document list page.
"""

def setUp(self):
self.init()
self.data.createHost()

def testListDocument(self):
url = '/gsoc/documents/' + self.gsoc.key().name()
response = self.client.get(url)
self.assertGSoCTemplatesUsed(response)

response = self.getListResponse(url, 0)
self.assertIsJsonResponse(response)
data = response.context['data']['']
self.assertEqual(1, len(data))

No comments:

Post a Comment