def x_numActiveAlerts() -> str: cun = currentUserName() if not cun: return json.dumps([0]) numAlerts = Alert.count({'user_id': cun, 'live': True}) dpr("numAlerts=%r", numAlerts) return json.dumps([numAlerts])
def xFollow(id: str, status: str): """ @param id = the followee user who the currently-logged-in user is following or unfollowing @param status = following status, 0=unfollow, 1=follow """ makeFollowing = (status == "1") cun = currentUserName() dpr("id=%r status=%r cun=%r", id, status, cun) if not cun: return "{}" cu = User.getDoc(cun) if not cu: return "{}" followee = User.getDoc(id) if not followee: return "{}" ai = models.getAccountInfo(cun) followingSet = set(ai.following_ids) if makeFollowing: followingSet2 = followingSet | set([id]) else: followingSet2 = followingSet - set([id]) if followingSet2 != followingSet: ai.following_ids = list(followingSet2) ai.save() return "{}"
def starH(self) -> str: """ The HTML for a star underneath a message """ h = "" c = "" if self.numStars >= 1: h = form("{} ", self.numStars) cun = currentUserName() if not cun or cun == self.author_id: #>>>>> not logged in, or author h += "<i class='fa fa-star-o fa-lg'></i> " else: #>>>>> user is not message author # has this user starred the message? starred = cun in self.starredBy_ids if starred: h += "<i class='starred fa fa-star fa-lg'></i> " c = "starred" else: h += form( """<i onclick='starClicked("{mid}")' """ "class='can_star fa fa-star-o fa-lg'></i> ", mid=self._id) c = "can_star" #//if h2 = form("<span class='{c}'>{h}</span>", c=c, h=h) return h2
def replyA(self) -> str: """ html containing the link to reply to this message. If not logged in this is empty. """ cun = currentUserName() if not cun: return "" h = form("- <a href='/messRep/{id}'>reply</a> ", id=self.id()) return h
def editA(self) -> str: """ html containing the link to edit this message. This is empty unless the logged-in user is the author of the message. """ cun = currentUserName() dpr("cun=%r self.author_id=%r", cun, self.author_id) if (not cun) or self.author_id != cun: return "" h = form("- <a href='/editMess/{id}'>edit</a> ", id=self.id()) return h
def x_star(id): cun = permission.currentUserName() if not cun: return m = models.Message.getDoc(id) if not m: return if cun not in m.starredBy_ids: m.starredBy_ids += [cun] m.save() al = models.Alert(user_id=m.author_id, alertType='star', message_id=m._id, doer_id=cun) al.save() return "{}"
def alerts_replies() -> str: """ alerts for replies to the current user's posts """ cun = permission.currentUserName() q = {'user_id': cun, 'alertType': 'reply', 'live': True} if request.method == 'POST': models.Alert.col().update_many(q, {'$set': {'live': False}}) #//for count = models.Alert.count(q) tem = jinjaEnv.get_template("alerts_replies.html") h = tem.render(tabLine=alertTabLine("replies"), count=count, messages=getMessages(q)) 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 alerts_historic() -> str: """ the current user's current alerts """ cun = permission.currentUserName() q = {'user_id': cun, 'live': False} count = models.Alert.count(q) pag = paginate.Paginator(count) tem = jinjaEnv.get_template("alerts_historic.html") h = tem.render( tabLine=alertTabLine("historic"), count=count, pag=pag, table=alertsTable(q, pag), ) return h
def alerts_stars() -> str: """ alerts for stars to the current user's posts """ cun = permission.currentUserName() q = {'user_id': cun, 'alertType': 'star', 'live': True} if request.method == 'POST': models.Alert.col().update_many(q, {'$set': {'live': False}}) #//for numStars = models.Alert.count(q) tem = jinjaEnv.get_template("alerts_stars.html") h = tem.render( tabLine=alertTabLine("stars"), numStars=numStars, messages=getStarredMessages(q), ) return h
def demographics(): ud = models.getUserDemographics(currentUserName()) msg = "" if request.method=='POST': ud = ud.populateFromRequest(request) if ud.isValid(): ud.save() msg = "Saved user demographics" #//if tem = jinjaEnv.get_template("demographics.html") h = tem.render( ud = ud, msg = ht.goodMessageBox(msg), ) return h
def blog(id): user = User.getDoc(id) ai = models.getAccountInfo(id) lf = BlogFormatter(id) numPosts = models.Message.count({'author_id': id}) numHeadPosts = models.Message.count({ 'author_id': id, 'replyTo_id': { '$in': [None, ''] }, }) numFollowing = len(ai.following_ids) numFollowers = models.AccountInfo.count({'following_ids': id}) cun = currentUserName() if not cun: # not logged in, so no follow button followButton = "" else: if models.follows(cun, id): # follows, so unfollow button followButton = "unfollow" else: # doesn't currently follow, so follow button followButton = "follow" dpr("followButton=%r", followButton) tem = jinjaEnv.get_template("blog.html") h = tem.render( id=id, idJson=json.dumps(id), user=user, ai=ai, blogTitle=ai.asReadableH('title'), name=ai.asReadableH('realName'), bio=ai.bioHtml, numPosts=numPosts, numHeadPosts=numHeadPosts, numFollowing=numFollowing, numFollowers=numFollowers, followButton=followButton, lf=lf, ) return h
def wikiPage(u: str, folder: str, filename: str): """ return a wiki page. If it doesn't exist, return a placeholder. @param u = user name @param folder = the path to the page (not inculding filename) @param filename = the filename """ wp = wikidb.getWikiPage(u, folder, filename) tem = jinjaEnv.get_template("wikiPage.html") canAlter = wikidb.canAlter(currentUserName(), u, folder, filename) h = tem.render( nav=wikiNavigation(u, folder, filename), userName=u, folder=folder, filename=filename, wp=wp, exists=bool(wp), canAlter=canAlter, ) 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 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 review(): groupTable = calcGroupTable(permission.currentUserName()) tem = jinjaEnv.get_template("review.html") h = tem.render(groupTable=groupTable, ) return h