def test_todojoreadstore(self): """Testing todojoreadstore() function""" log.info("Testng h.todojoreadstore ...") # Testing the list samp = { 'identifier': 'id1', 'label': 'lab1', 'items': [['0', 'hello'], ['0.1', 'world']] } jsonstr = h.todojoreadstore(samp['items'], lambda x: {x[0]: x[1]}, id='id1', label='lab1') samp['items'] = [{x[0]: x[1]} for x in samp['items']] assert_equal(samp, json.loads(jsonstr), 'Mismatch in converting list items to dojoreadstore') # Testing the dictionary samp = { 'identifier': 'id1', 'label': 'lab1', 'items': { '0': 'hello', '0.1': 'world' } } jsonstr = h.todojoreadstore(samp['items'], lambda k, v: {k: v}, id='id1', label='lab1') samp['items'] = [{k: samp['items'][k]} for k in samp['items']] assert_equal(samp, json.loads(jsonstr), 'Mismatch in converting dict items to dojoreadstore')
def _json_mountlist(self): # JSON """JSON: { id : 'id', label: 'mount_id', items: [ { 'id' : m.id, 'name' : m.name, 'content' : m.content, 'repospath' : m.repospath, 'href' : href, 'createdon' : m.created_on }, ... ] }""" from zeta.config.environment import vcscomp def format_item(m): href = self.url_projmountsname(c.project.projectname, m[0]) d = { 'id': m[0], 'name': m[1], 'content': m[2], 'repospath': m[3], 'href': href, 'created_on': m[4], } return d return h.todojoreadstore(vcscomp.projmounts(c.project), format_item, id='id', label='mount_id')
def _json_myprojects( self, environ ) : # JSON """JSON : { id : '' label : '' item : [ { projectnames : myprojectnames } ] }""" fn = lambda v : { 'projectnames': v } return h.todojoreadstore( [ h.myprojects(c.authuser) ], fn )
def _json_wikicomments(self): # JSON """JSON: { wiki_comment_id : 'wiki_comment_id', wiki_comment_id : 'wiki_comment_id', items: [ { wiki_comment_id : wcmt.id, version_id : wcmt.version_id, commentby : wcmt.commentby.username , text : wcmt.text, html : wcmt.texthtml, commentbyicon : usericon, commentbyurl : userurl, datestr : wcmt.created_on }, ... ] }""" from zeta.config.environment import wikicomp def format_item(qres): s = h.utc_2_usertz(qres[4], c.authuser.timezone).strftime('%d %b %Y, %r') d = { 'wiki_comment_id': qres[0], 'version_id': qres[1], 'commentby': qres[5], 'text': qres[2], 'html': qres[3], 'commentbyicon': '', 'commentbyurl': self.url_user(qres[5]), 'datestr': s } return d return h.todojoreadstore(wikicomp.wikicomments(c.wiki.id), format_item, id='wiki_comment_id', label='wiki_comment_id')
def _json_vcslist(self): # JSON """JSON: { id : 'id', label: 'vcs_id', items: [ { 'id' : v.id, 'name' : v.name, 'rooturl' : v.rooturl, 'href' : href, 'vcs_typename': v.type.vcs_typename }, ... ] }""" def format_item(v): href = self.url_vcsbrowse(c.project.projectname, str(v.id)) d = { 'id': v.id, 'name': v.name, 'rooturl': v.rooturl, 'href': href, 'vcs_typename': v.type.vcs_typename, } return d return h.todojoreadstore(c.project.vcslist, format_item, id='id', label='vcs_id')
def _json_mstnlist( self, environ ) : # JSON """JSON: { id : 'id', label: 'id', items: [ { id : milestone.id, milestone_name: milestone.milestone_name, due_date : milestone.due_date , description : milestone.description, status : milestone.status, closing_remark: milestone.closing_remark }, ... ] }""" mstns = [] for mstn in getattr( c.project, 'milestones', [] ) : status = ( mstn.completed and 'completed' ) or \ ( mstn.cancelled and 'cancelled' ) or '' dd = [] if mstn.due_date : due_date = mstn.due_date.astimezone( h.timezone( c.authuser.timezone )) dd = [ due_date.year, due_date.month, due_date.day ] mstns.append([ mstn.id, mstn.milestone_name, dd, mstn.description, status, mstn.closing_remark ]) fn = lambda v : { 'id' : v[0], 'milestone_name' : v[1], 'due_date' : v[2], 'description' : v[3], 'status' : v[4], 'closing_remark' : v[5] } return h.todojoreadstore( mstns, fn, id='id', label='id' )
def _json_projectnames( self, environ ) : # JSON """JSON : { id : '' label : '' item : [ { projectnames : projectnames } ] }""" from zeta.config.environment import projcomp fn = lambda v : { 'projectnames': v } return h.todojoreadstore( [ projcomp.projectnames ], fn )
def _json_userstatus( self ) : # JSON """JSON: { id : 'status', label: 'status', items: [ { status: status, usernames: usernames }, ... ] }""" from zeta.config.environment import userscomp fn = lambda k, v : { 'status' : k, 'usernames' : v } x = userscomp.userstatus return h.todojoreadstore( x, fn, id='status', label='status' )
def _json_usernames( self, users=[] ) : # JSON """JSON: { id : '', label: '', items: [ { usernames: usernames } ] }""" from zeta.config.environment import userscomp usernames = [ u.username for u in users ] if users else sorted( userscomp.usernames ) return h.todojoreadstore( [ usernames ], lambda v : { 'usernames': v } )
def _json_projectstatus( self, environ ) : # JSON """JSON: { id : 'status', label: 'status', items: [ { status: status, projectnames: projectnames }, ... ] }""" from zeta.config.environment import projcomp fn = lambda k, v : { 'status' : k, 'projectnames' : v } return h.todojoreadstore( projcomp.projectstatus, fn, id='status', label='status' )
def _json_userperms( self ) : # JSON """JSON: { id : 'username', label: 'username', items: [ { username: username, permissions: permissions, x_permissions: ^permissions }, ... ] }""" from zeta.config.environment import userscomp fn = lambda k, v : { 'username' : k, 'permissions' : v[0], 'x_permissions' : v[1] }, x = userscomp.userpermission_map() return h.todojoreadstore( x, fn, id='username', label='username' )
def _json_prjperms( self, environ ) : # JSON """JSON: { id : 'username', label: 'username', items: [ { username : projuser, permsids : [[ pup.id, permgroup ] ... ], x_permission: [ permission, ... ] }, ... ] }""" from zeta.config.environment import projcomp fn = lambda k, v : { 'username' : k, 'permsids' : v[0], 'x_permissions' : v[1] } x = projcomp.projectuserperms( project=c.project ) return h.todojoreadstore( x, fn, id='username', label='username' )
def _json_teamperms( self, environ ) : # JSON """JSON: { id : 'team', label: 'team', items: [ { team : team_typename, permsids : [[ ptp.id, permgroup ] ... ], x_permission: [ permission, ... ] }, ... ] }""" from zeta.config.environment import projcomp teamperms = projcomp.teamperms( project=c.project ) fn = lambda k, v : { 'team' : k, 'permsids' : v[0], 'x_permissions' : v[1] } return h.todojoreadstore( teamperms, fn, id='team', label='team' )
def _json_verlist( self, environ ) : # JSON """JSON: { id : 'id', label: 'id', items: [ { id : version.id, version_name: version.version_name, description : version.description }, ... ] }""" vers = [ [ ver.id, ver.version_name, ver.description ] for ver in getattr( c.project, 'versions', [] ) ] fn = lambda v : { 'id' : v[0], 'version_name' : v[1], 'description' : v[2] } return h.todojoreadstore( vers, fn, id='id', label='id' )
def _json_tckrcomments(self): # JSON """JSON: { id : 'ticket_comment_id', label: 'ticket_comment_id', items: [ { ticket_comment_id : tcmt.id, commentby : tcmt.commentby.username , text : tcmt.text, html : tcmt.texthtml, commentbyicon : usericon, commentbyurl : userurl, datestr : tcmt.created_on }, ... ] }""" from zeta.config.environment import tckcomp def format_item(qres): d = { 'ticket_comment_id': qres[0], 'commentby': qres[4], 'text': qres[1], 'html': qres[2], 'commentbyicon': '', 'commentbyurl': self.url_user(qres[4]), 'datestr': h.utc_2_usertz(qres[3], c.authuser.timezone).strftime('%d %b %Y, %r'), } return d tcomments = tckcomp.tckrcomments(c.ticket.id) items = [] while tcomments: tcomment = tcomments.pop(0) d_tcmt = format_item(tcomment) d_tcmt.setdefault( 'replies', [format_item(rtcomment) for rtcomment in tcomment[-1]]) items.append(d_tcmt) return h.todojoreadstore(items, lambda v: v, id='ticket_comment_id', label='ticket_comment_id')
def _json_projectteams( self, environ ) : # JSON """JSON: { id : 'team', label: 'team', items: [ { team : team_typename, usersids : [[ pt.id, pt.user.username ], ... ], x_usernames : [ username, ... ] }, ... ] }""" from zeta.config.environment import projcomp teams = projcomp.projectteams( project=c.project ) teams.pop( projcomp.team_nomember ) # Prune nomember team fn = lambda k, v : { 'team' : k, 'usersids' : v[0], 'x_usernames' : v[1] } return h.todojoreadstore( teams, fn, id='team', label='team' )
def _json_wikilist(self): # JSON-GRID """JSON: { id : 'id', label: 'wiki_id', items: [ { id : wiki.id, wikiurl : wiki.wikiurl , pagename : wurl, summary : wiki.summary , sourceurl : wiki.sourceurl, wiki_typename : wiki.type.wiki_typename , latest_version : wiki.latest_version , last_modified : wcnt.created_on , author : wcnt.author, upvotes : upvotes , downvotes : downvotes } ... ] }""" from zeta.config.environment import wikicomp def format_item(w): wcnt = wikicomp.get_content(w) votes = wikicomp.countvotes(votes=w.votes) lastmod = h.utc_2_usertz( wcnt.created_on, c.authuser.timezone).strftime('%d %b %Y') if wcnt else 'N/A' author = wcnt.author if wcnt else 'N/A' d = { 'id': w.id, 'wikiurl': w.wikiurl, 'pagename': c.wurl, 'summary': w.summary, 'sourceurl': w.sourceurl, 'wiki_typename': w.type.wiki_typename, 'latest_version': str(w.latest_version), 'last_modified': lastmod, 'author': author, 'upvotes': votes.get('up', 0), 'downvotes': votes.get('down', 0) } return d return h.todojoreadstore(c.project.wikis, format_item, id='id', label='wiki_id')
def _json_wikircomments(self): # JSON """JSON: { wiki_comment_id : 'wiki_comment_id', wiki_comment_id : 'wiki_comment_id', items: [ { wiki_comment_id : wcmt.id, version_id : wcmt.version_id, commentby : wcmt.commentby.username , text : wcmt.text, html : wcmt.texthtml, commentbyicon : usericon, commentbyurl : userurl, datestr : wcmt.created_on }, ... ] }""" from zeta.config.environment import wikicomp def format_item(qres): s = h.utc_2_usertz(qres[4], c.authuser.timezone).strftime('%d %b %Y, %r') d = { 'wiki_comment_id': qres[0], 'version_id': qres[1], 'commentby': qres[5], 'text': qres[2], 'html': qres[3], 'commentbyicon': '', 'commentbyurl': self.url_user(qres[5]), 'datestr': s } return d wcomments = wikicomp.wikircomments(c.wiki.id) items = [] while wcomments: wcomment = wcomments.pop(0) d_wcmt = format_item(wcomment) d_wcmt.setdefault( 'replies', [format_item(rwcomment) for rwcomment in wcomment[-1]]) items.append(d_wcmt) return h.todojoreadstore(items, lambda v: v, id='wiki_comment_id', label='wiki_comment_id')
def _json_dirlist(self): # JSON """JSON : { id: 'dirlist', label: 'dirlist', items: [{ 'dirlist': '', 'dirs' : [ [ dirname, listurl, repos_path ], ... ], 'files': [ [ created_revision, mime_type, path, author, size, timestamp, repos_path, basename, url ], ... ] }]""" c.vrep = va.open_repository(c.vcs) listing = {} fn = lambda f: listing.setdefault(f[1], []).append(f) map(fn, c.vrep.list(c.repopath, revno=c.revno)) fn = lambda f: [ basename(f[2]), self.url_vcsbrowse(c.project.projectname, c.vcs.id, repopath=f[2], revno=c.revno, jsonobj='dirlist', view='js'), f[6][2:] if f[6][:2] == './' else f[6].lstrip('/') ] listing['text/directory'] = map(fn, listing.get('text/directory', [])) fn = lambda f: f + [ basename(f[2]), self.url_vcsbrowse(c.project.projectname, c.vcs.id, filepath=f[6].lstrip('/'), revno=c.revno) ] listing['text/file'] = map(fn, listing.get('text/file', [])) return h.todojoreadstore([{ 'dirlist': 0, 'dirs': listing['text/directory'], 'files': listing['text/file'] }], lambda v: v, id='dirlist', label='dirlist')
def _json_userconns( self ) : # JSON """JSON: { id : '', label: '', items: [ touserrels, fromuserrels, potrels ] } touserrels : { type : [ (tousr, rel.id, approved), ... ], ... } fromuserrels: { type : [ (fromsr, rel.id, approved), ... ], ... } potrels : { type : [ [ username, ...], ... }""" from zeta.config.environment import userscomp json = [] touserrels, fromuserrels, potrels = userscomp.get_connections( c.authuser ) if c.authusername != 'anonymous' else ( [], [], [] ) fn = lambda rt, val : ( rt, sorted( val, key=lambda x : x[0] ) ) touserrels = dict( map( fn, touserrels.iteritems() )) fromuserrels = dict( map( fn, fromuserrels.iteritems() )) potrels = dict([ ( rt, sorted( potrels[rt] ) ) for rt in potrels ]) return h.todojoreadstore( [ [ touserrels, fromuserrels, potrels ] ], lambda v : { 'rels' : v } )
def _json_tckcomments(self): # JSON """JSON: { id : 'ticket_comment_id', label : 'ticket_comment_id', items: [ { ticket_comment_id : tcmt.id, commentby : tcmt.commentby.username, text : tcmt.text, html : tcmt.texthtml, commentbyicon : usericon, commentbyurl : userurl, datestr : tcmt.created_on }, ... ] }""" from zeta.config.environment import tckcomp def format_item(qres): d = { 'ticket_comment_id': qres[0], 'commentby': qres[4], 'text': qres[1], 'html': qres[2], 'commentbyicon': '', 'commentbyurl': self.url_user(qres[4]), 'datestr': h.utc_2_usertz(qres[3], c.authuser.timezone).strftime('%d %b %Y, %r'), } return d return h.todojoreadstore(tckcomp.tckcomments(c.ticket.id), format_item, id='ticket_comment_id', label='ticket_comment_id')
def _json_revwlist(self): # JSON """JSON: { id : 'id', label: 'review_id', items: [ { 'id' : r.id, 'href' : href, 'resource_url' : r.resource_url, 'reviewset' : r.reviewset.name, 'rshref' : href, 'version' : r.version, 'author' : r.author.username, 'moderator' : r.moderator.username, 'comments' : comments, 'olderby' : olderby } ... ] }""" from zeta.config.environment import revcomp def format_item(tup): p = c.project.projectname olderby = dt.datetime.utcnow().toordinal() - tup[7].toordinal() d = { 'id': tup[0], 'href': self.url_revwid(p, tup[0]), 'resource_url': tup[1], 'reviewset': tup[2] or '', 'rshref': self.url_rsetid(p, tup[3]) if tup[3] else '', 'version': tup[4], 'author': tup[5], 'moderator': tup[6], 'comments': revcomp.countcomments(tup[0]), 'olderby': h.olderby(olderby), } return d return h.todojoreadstore(revcomp.reviewlist(c.project).values(), format_item, id='id', label='vcs_id')
def _json_ticketlist(self): # JSON-GRID """Fetch the json object with caching, under `projectname` JSON: { id : 'id', label: 'ticket_id', items: [ { id : tck.id, projectname : tck.project.projectname, ts_id : ts.id , ticketurl : url_for(ticket) , summary : tck.summary, tck_typename : tck.type.typename, tck_severityname : tck.severity.tck_severityname, tck_statusname : tck.status.tck_statusname, due_date : ts.due_date , owner : ts.owner.username, promptuser : tck.promptuser.username, component_id : component.id, componentname : component.componentname, milestone_id : milestone.id, milestone_name : milestone.milestone_name, version_id : version.id,, version_name : version.version_name, upvotes : upvotes , downvotes : downvotes, age : age }, ... ] }""" from zeta.config.environment import tckcomp def format_item(tup): due_date = tup[8].astimezone(h.timezone( c.authuser.timezone)) if tup[8] else None ymd = [due_date.year, due_date.month, due_date.day ] if due_date else [] age = h.olderby(dt.datetime.utcnow().toordinal() - tup[3].toordinal()) d = { 'id': tup[0], 'projectname': tup[1], 'ticketurl': self.url_ticket(c.project.projectname, tup[0]), 'summary': tup[2], 'tck_typename': tup[4], 'tck_severityname': tup[5], 'tck_statusname': tup[6], 'ts_id': tup[7], 'due_date': ymd, 'owner': tup[9], 'promptuser': tup[10], 'component_id': tup[11], 'componentname': tup[12], 'milestone_id': tup[13], 'milestone_name': tup[14], 'version_id': tup[15], 'version_name': tup[16], 'upvotes': tup[17], 'downvotes': tup[18], 'age': age, } d = tckcomp.computefilters(d, tckfilters=c.tckfilters)[0] return (d.get(c.stdfilter, True) and d) if c.stdfilter else d c.tckfilters = h.compile_tckfilters(tckfilters) savfilters = dict([(tf.id, h.json.loads(tf.filterbyjson)) for tf in tckcomp.get_ticketfilter(user=c.authuser) ]) filters = savfilters.get(c.savfilter, {}) tcklist = tckcomp.ticketlist(project=c.project, filters=filters) tcklist = sorted(tcklist.values(), key=lambda l: l[0], reverse=True) _tl = h.todojoreadstore(tcklist, format_item, id='id', label='ticket_id') return _tl
def _revwrcomments(self): """JSON: { review_comment_id : 'review_comment_id', review_comment_id : 'review_comment_id', items: [ { review_comment_id : rcmt.id, position : rcmt.position, text : rcmt.text, html : rcmt.texthtml, commentby : rcmt.commentby.username , commentbyurl : userurl, nature : rcmt.nature.naturename, action : rcmt.action.actionname, approved : rcmt.approved replies : [] datestr : rcmt.created_on }, ... ] }""" from zeta.config.environment import revcomp def format_item(tup): d = { 'review_comment_id': tup[0], 'position': tup[1], 'text': tup[2], 'html': tup[3], 'commentby': tup[6], 'commentbyurl': self.url_user(tup[6]), 'nature': tup[7] or '', 'action': tup[8] or '', 'approved': tup[4], 'datestr': h.utc_2_usertz(tup[5], c.authuser.timezone).strftime('%d %b %Y'), } return d c.position = int(c.position) if c.position else None rcomments = revcomp.reviewrcomments(c.review).values() items = [] while rcomments: rcomment = rcomments.pop(0) d_rcmt = format_item(rcomment) d_rcmt.setdefault( 'replies', [format_item(rrcomment) for rrcomment in rcomment[-1]]) if c.position != None and d_rcmt['position'] != c.position: continue items.append(d_rcmt) json = h.todojoreadstore(items, lambda v: v, id='review_comment_id', label='review_comment') return json, items