def wikiEdit(u: str, pn: str): """ Edit a wiki page @param u = user id @param pn = page name """ npn = mark.normaliseTagWpn(pn) # normalised page name wp = wikidb.getWikiPage(u, npn, create=True) wf = WikiForm(source=wp.source) if request.method == 'POST': wf = wf.populateFromRequest(request) wp.source = wf.source wp.save() return redirect(form("/wiki/{u}/{pn}", u=u, pn=pn)) #//if tem = jinjaEnv.get_template("wikiEdit.html") h = tem.render( userName=htmlEsc(u), pn=htmlEsc(pn), wp=wp, wf=wf, exists=bool(wp), canAlter=True, nav=wikiPageNav(u, npn), ) return h
def askH(self) -> str: """ return the question as html """ h = form(""" <p class='question'><i class='fa fa-question-circle-o'></i> <tt>[{qid}]</tt> {qtext}</p>""", qid=htmlEsc(self.qid), qtext=htmlEsc(self.qtext)) return h
def calcGroupTable(userName: str) -> str: """ caslculate the group table for the current user. Returns html. """ h = """<table class='bz-report-table'> <tr> <th>Group</th> <th>Questions<br>Answered</th> <th>Questions<br>Unanswered</th> <th>Details</th> </tr> """ groups = questionManager.getGroups() for group in groups: dpr("group.id=%r", group.id) numQs = len(group.questions) numAnswered = len(answeredQs(currentUserName(), group.questions)) numUnanswered = numQs - numAnswered if numUnanswered == 0: detailsH = form( "View <a href='/results/{groupId}' class='green'>" "<i class='fa fa-star'></i> " "Your Results</a>", groupId=htmlEsc(group.id)) else: detailsH = ("<i class='fa fa-lock'></i> " "<i>answer all questions to unlock details</i>") h += form( """ <tr> <td><a href="/group/{groupId}">{groupName}</a></td> <td style='text-align:right;'> <a href="/answered/{groupId}">{numAnswered}</a></td> <td style='text-align:right;'> <a href="/ask/{groupId}">{numUnanswered}</a>/{numTotal}</td> <td>{details}</td> </tr> """, groupId=htmlEsc(group.id), numQs=numQs, groupName=htmlEsc(group.title), numAnswered=numAnswered, numUnanswered=numUnanswered, numTotal=numQs, details=detailsH, ) #//for h += "</table>\n" return h
def user(id): if id=='NEW': doc = userdb.User() dpr("doc=%r", doc) else: doc = userdb.User.getDoc(id) dpr("doc=%r", doc) msg = "" if request.method=='POST': doc = doc.populateFromRequest(request) if doc.isValid(): if request.form['delete']=="1": doc.delete() msg = "Deleted user" else: doc.save() msg = "Saved user" #//if tem = jinjaEnv.get_template("user.html") h = tem.render( doc = doc, id = htmlEsc(id), msg = ht.goodMessageBox(msg), ) return h #--------------------------------------------------------------------- #end
def answerH(self, ans) -> str: """ return the querstion as html with the answer selected in (ans). """ userAnswerText = self.answerText(ans) h = form(""" <p class='question'><i class='fa fa-question-circle-o'></i> <tt>[{qid}]</tt> {qtext}</p> <tt>[{ansVal}]</tt> {ansText} <br> """, qid=htmlEsc(self.qid), qtext=htmlEsc(self.qtext), ansVal=htmlEsc(ans), ansText=htmlEsc(userAnswerText)) return h
def wikiPageNav(u: str, npn: str) -> str: """ Return html for navigation for a wiki page """ pageIconFa = "fa-home" if npn == "home" else "fa-file-text-o" pageIcon = form("<i class='fa {fa}'></i>", fa=pageIconFa) h = form("""<span class='nav-instance'> <i class='fa fa-bank'></i> {siteLocation}</span> <span class='nav-user'> <i class='fa fa-user'></i> {u}</span> <span class='nav-wiki'> <i class='fa fa-database'></i> {pageIcon} {pn}</span> """, siteLocation=config.SITE_LOCATION, u=htmlEsc(u), pageIcon=pageIcon, pn=htmlEsc(npn)) return h
def wiki(u: str, pn: str): """ Display a wiki page @param u = user id @param pn = page name """ npn = mark.normaliseTagWpn(pn) # normalised page name wp = wikidb.getWikiPage(u, npn) tem = jinjaEnv.get_template("wikiPage.html") h = tem.render( userName=htmlEsc(u), pn=htmlEsc(pn), wp=wp, exists=bool(wp), canAlter=True, nav=wikiPageNav(u, npn), ) return h
def answerChoiceRB(qid: str, ansClass: str, ansVal: str, ansText: str) -> str: """ return html for a radio button for an answer to a question. qid = question ID ansClass = CSS class for this answer ansVal = value for answer in the database ansText = value for answer to be displayed """ h = form("""\ <input type=radio id="{qid}" name="{qid}" value="{ansVal}"> <span class='answer {ansClass}'> <tt>[{ansVal}]</tt> {ans} </span><br> """, qid=htmlEsc(qid), ansClass=ansClass, ansVal=htmlEsc(ansVal), ans=htmlEsc(ansText)) return h
def pollTable() -> str: partyCols = "" for p in POLL_PARTIES: pa = party.Party.docs[p] partyCols += form("<th>{partyName}<br>{logo}</th>", partyName = pa.getNameH(), logo = pa.logo()) #//for h = form("""<table class='bz-report-table'> <tr> <th>End<br>Date</th> <th>Days<br>to Poll</th> <th>Polling<br>Org</th> <th>Sample<br>Size</th> {partyCols} </tr> """, partyCols = partyCols) allPolls = sorted(Poll.polls, key=lambda po: po.dateInt) for po in allPolls: partyCells = "" for p in POLL_PARTIES: pa = party.Party.docs[p] partyCells += ("<td style='text-align:right;color:%s'" ">%.1f</td>\n" % (pa.col, po.__dict__[p],)) h += form("""<tr> <td>{date}</td> <td style='text-align:right'>{dateInt}</td> <td>{org}</td> <td style='text-align:right'>{sample}</td> {partyCells} </tr>""", date = htmlEsc(po.date), dateInt = po.dateInt, org = htmlEsc(po.org), sample = htmlEsc(str(po.sample)), partyCells = partyCells) #//for h += "</table>" return h
def tag(t): lf = TagFormatter(t) tem = jinjaEnv.get_template("tag.html") h = tem.render( t = t, lf = lf, qh = htmlEsc(repr(lf.q)), ) return h
def answered(groupId): group = questionManager.getGroup(groupId) if not group: return permission.http403("Group does not exist") cun = currentUserName() ansQs = answeredQs(cun, group.questions) numQs = len(group.questions) numAns = len(ansQs) ansH = calcAnsH(cun, ansQs) tem = jinjaEnv.get_template("answered.html") h = tem.render( group=group, groupId=htmlEsc(group.id), groupTitle=htmlEsc(group.title), numQs=numQs, numAns=numAns, ansH=calcAnsH(cun, ansQs), ) return h
def group(groupId): g = questionManager.getGroup(groupId) if not g: return permission.http403("Group does not exist") cun = currentUserName() numQs = len(g.questions) numAnswered = 0 # number of questions this user has answered if cun: numAnswered = len(answeredQs(cun, g.questions)) numUnanswered = numQs - numAnswered tem = jinjaEnv.get_template("group.html") h = tem.render( group=g, groupId=htmlEsc(g.id), groupTitle=htmlEsc(g.title), numQs=numQs, numAnswered=numAnswered, numUnanswered=numUnanswered, ) return h
def getResultTable(th: TheTestForm) -> str: """ return an html table with the contents of (tf) """ h = """<table class='bz-report-table'> <tr> <th colspan=3>Field</th> <th colspan=3>Value</th> </tr> <tr> <th>Py Name</th> <th>Screen Name</th> <th>Type</th> <th>Py Value</th> <th>Screen Value</th> <th>Type</th> </tr> """ for fn in th.fieldNames(): fi = th.getFieldInfo(fn) cn = fi.__class__.__name__ v = th[fn] h += form( """<tr> <td><code>{fn}</code></td> <td><b>{screenName}</b></td> <td><tt>{type}</tt></td> <td><code>{r}</code></td> <td>{s}</td> <td><tt>{vt}</tt></td> <tr>""", screenName=fi.title, fn=fn, type=cn, s=th.asReadableH(fn), r=htmlEsc(repr(v)), vt=htmlEsc(v.__class__.__name__), ) #//for h += "</table>" return h
def ask(groupId, numQs=DEFAULT_NUM_QS): numQs = butil.exValue(lambda: int(numQs), DEFAULT_NUM_QS) dpr("groupId=%r, numQs=%r", groupId, numQs) group = questionManager.getGroup(groupId) if not group: return http403("Group does not exist") cun = currentUserName() if request.method == 'POST': rf = dict(request.form) dpr("request form data: rf=%r", rf) # in (rf) keys are question ids, values are question values. # if the user hasn't answered a question, it is missing from the # dictionary for qid, ansVal in rf.items(): q = getQ(group.questions, qid) if q: models.saveAnswer(cun, q.qid, ansVal) #//for #//if unansweredQs = getUnansweredQs(group, cun) qsToAsk = unansweredQs[:numQs] qListH = getQuestionsH(qsToAsk) numAns = len(group.questions) - len(unansweredQs) tem = jinjaEnv.get_template("ask.html") h = tem.render( group=group, groupTitle=htmlEsc(group.title), groupId=htmlEsc(group.id), numQ=len(group.questions), # questions in this group numAns=numAns, # q's answered numUnanswered=len(unansweredQs), # q's unanswered numAsk=len(qsToAsk), # q's to ask on this page qs=qListH, ) return h
def messSource(id): m = models.Message.getDoc(id) starredByH = ", ".join(u for u in m.starredBy_ids) tem = jinjaEnv.get_template("messSource.html") h = tem.render( m=m, id=id, ms=m.viewH(), messSource=htmlEsc(m.source), starredBy=starredByH, ) return h
def wikiNav(u: str) -> str: """ Return html for navigation for a wiki """ h = form("""<span class='nav-instance'> <i class='fa fa-bank'></i> {siteLocation}</span> <span class='nav-user'> <i class='fa fa-user'></i> {u}</span> </span> <span class='nav-wiki'> <i class='fa fa-database'></i> </span> """, siteLocation=config.SITE_LOCATION, u=htmlEsc(u)) return h
def goodMessageBox(msg: str, escapeForHtml: bool = True) -> str: """ If a message is blank, leave it blank. Otherwise, wrap it up in css that makes it look like a pretty warning message. @param msg = message to be displayed @param escapeForHtml = if True, the msg is deemed to contain a non-html string, which needs to be escaped for HTML (because it might contain chars such as "&" or "<"). Set to False if #msg contains HTML. """ if not msg: return "" if escapeForHtml: msgH = butil.htmlEsc(msg) else: msgH = msg h = form("<div class='message-box'>" "{msg}</div>", msg=msgH) return h
def wikiIndex(u: str): """ display a list of all the pages in a wiki @param u = user id """ wps = wikidb.getWikiPages(u) wikiIndexH = "" for wp in wps: wikiIndexH += form("<br>{a}\n", a=wp.a()) #//for tem = jinjaEnv.get_template("wikiIndex.html") h = tem.render( userName=htmlEsc(u), nav=wikiNav(u), wikiIndexH=wikiIndexH, ) return h
def foosTable(pag: paginate.Paginator) -> str: """ a table of foos """ h = """<table class='bz-report-table'> <tr> <th>Id</th> <th>Name</th> <th>Description</th> <th>Favourite<br>Drink</th> <th>Fruits<br>Liked</th> <th>Ticky<br>Box</th> </tr> """ fs = Foo.find( skip=pag.skip, # skip this number of docs before returning some limit=pag.numShow, # max number of docs to return sort='name') for f in fs: h += form( """<tr> <td style='background:#fed'><tt>{id}</tt></td> <td>{name}</td> <td>{description}</td> <td>{favouriteDrink}</td> <td>{fruitsLiked}</td> <td>{tickyBox}</td> </tr>""", id=htmlEsc(f.id()), name=f.a(), description=f.asReadableH('description'), favouriteDrink=f.asReadableH('favouriteDrink'), fruitsLiked=f.asReadableH('fruitsLiked'), tickyBox=f.asReadableH('tickyBox'), ) #//for f h += "</table>" return h
def messRep(id=None): if id: isReply = True m = models.Message.getDoc(id) mh = m.viewH() else: isReply = False m = None mh = "" hasPreview = False previewH = "" tags = None mf = MessageForm() if request.method == 'POST': mf = mf.populateFromRequest(request) messRepButton = request.form['messRepButton'] dpr("messRepButton=%r", messRepButton) if mf.isValid(): if messRepButton == 'preview': #>>>>> preview message previewH, tags = mark.render(mf.message) hasPreview = True else: #>>>>> create message dpr("create new message") previewH, tags = mark.render(mf.message) newM = models.Message(source=mf.message, html=previewH, tags=tags, author_id=permission.currentUserName()) if isReply: newM.replyTo_id = id newM.save() models.notifyTags(tags) dpr("newM=%r", newM) dpr("tags=%r", tags) u = "/mess/" + newM.id() dpr("u=%r", u) if isReply: al = models.Alert(user_id=m.author_id, alertType='reply', message_id=m._id, doer_id=newM.author_id, reply_id=newM._id) al.save() return redirect(u, code=303) #//if valid #//if POST tem = jinjaEnv.get_template("messRep.html") h = tem.render(id=id, isReply=isReply, m=m, mh=mh, mf=mf, msg="", hasPreview=hasPreview, previewH=previewH, tagsH=htmlEsc(repr(tags))) return h
def orNone(s): if s: return htmlEsc(s) else: return "<span class='unemphasized'>None</span>"