def prepare(self): super(CardWidget, self).prepare() user = m.get_user() if not self.package: self.package = select_random_package() allowed_tags = filter(lambda t: not t.banned, self.package.tags) count = 0 # Weird corner cases of obsoleted packages, etc. while (not self.package.xapian_summary or (user.anonymous and not allowed_tags)): self.package = select_random_package() allowed_tags = filter(lambda t: not t.banned, self.package.tags) # In development, sometimes the developer's DB has 0 packages with # any tags. This loop then spins infintely. This is included to # break out of that. count = count + 1 if count > 500: break if len(allowed_tags) >= self.N: picked_tags = random.sample(allowed_tags, self.N) else: picked_tags = allowed_tags self.tags = [TagWidget(tag=tag) for tag in picked_tags] if self.tags: self.tags[0].css_class += " selected"
def upcls(self): user = m.get_user() query = m.Vote.query.filter_by(user=user, tag=self.tag) if query.count() == 0: return "" if not query.one().like: return "" return "mod"
def textcls(self): user = m.get_user() query = m.Vote.query.filter_by(user=user, tag=self.tag) if query.count() == 0: return "" if query.one().like: return "up_text" return "down_text"
def prepare(self): super(CardWidget, self).prepare() user = m.get_user() tags_voted_for = [v.tag for v in user.votes] if not self.package: self.package = select_random_package(tags_voted_for) allowed_tags = filter(lambda t: not t.banned, self.package.tags) picked_tags = pick(allowed_tags, self.N, tags_voted_for) self.tags = [TagWidget(tag=tag) for tag in picked_tags] self.tags[0].css_class += " selected"
def vote(self, id, like): """ Handles the /vote path. Return JSON indicating results and stats. If the user has voted on this tag before, only allow a 'change' of votes; no "double-voting". If they have not, then register their new vote. """ like = asbool(like) tag = model.Tag.query.filter_by(id=id).one() user = model.get_user() # See if they've voted on this before. query = model.Vote.query.filter_by(user=user, tag=tag) if query.count() == 0: # They haven't. So register a new vote. if like: tag.like += 1 else: tag.dislike += 1 vote = model.Vote(like=like) vote.user = user vote.tag = tag model.DBSession.add(vote) else: # Otherwise, they've voted on this before. See if they're changing # their vote. vote = query.one() if vote.like == like: # They're casting the same vote, the same way. Ignore them. pass else: # Okay. Let them change their vote. if like: tag.like += 1 tag.dislike -= 1 else: tag.like -= 1 tag.dislike += 1 vote.like = like # Done changing vote. json = tag.__json__() json['user'] = { 'votes': user.total_votes, 'rank': user.rank, } return json
def add(self, label, package): """ Handles /add URL. Returns a JSON object indicating success or failure. - If the package does not exist. Fail. - If the label does not exist, create it. - If the label is not associated with the package, associate it. - Log a 'vote' for the current user on the new tag. """ json = dict(tag=label, package=package) query = model.TagLabel.query.filter_by(label=label) if query.count() == 0: model.DBSession.add(model.TagLabel(label=label)) label = query.one() query = model.Package.query.filter_by(name=package) if query.count() == 0: json['msg'] = "No such package '%s'" % package return json package = query.one() query = model.Tag.query.filter_by(label=label, package=package) if query.count() != 0: json['msg'] = "%s already tagged '%s'" % (package.name, label.label) return json tag = model.Tag(label=label, package=package) model.DBSession.add(tag) vote = model.Vote(like=True) vote.user = model.get_user() vote.tag = tag model.DBSession.add(vote) json['msg'] = "Success. '%s' added to package '%s'" % (label.label, package.name) return json
def add(self, label, package): """ Handles /add URL. Returns a JSON object indicating success or failure. - If the package does not exist. Fail. - If the label does not exist, create it. - If the label is not associated with the package, associate it. - Log a 'vote' for the current user on the new tag. """ json = dict(tag=label, package=package) query = model.TagLabel.query.filter_by(label=label) if query.count() == 0: model.DBSession.add(model.TagLabel(label=label)) label = query.one() query = model.Package.query.filter_by(name=package) if query.count() == 0: json['msg'] = "No such package '%s'" % package return json package = query.one() query = model.Tag.query.filter_by(label=label, package=package) if query.count() != 0: json['msg'] = "%s already tagged '%s'" % (package.name, label.label) return json tag = model.Tag(label=label, package=package) model.DBSession.add(tag) vote = model.Vote(like=True) vote.user = model.get_user() vote.tag = tag model.DBSession.add(vote) json['msg'] = "Success. '%s' added to package '%s'" % ( label.label, package.name) return json
def username(self): return m.get_user().username
def rank(self): user = m.get_user(self.username) return user.rank
def total_votes(self): user = m.get_user(self.username) return user.total_votes
def avatar_link(self, s=140, d='mm'): hash = hashlib.md5(m.get_user().email).hexdigest() return "http://www.gravatar.com/avatar/%s?s=%i&d=%s" % (hash, s, d)
def _notifications_on(self): user = m.get_user(self.username) return user.notifications_on and "true" or "false"
def notifications_on(self): user = m.get_user(self.username) return user.notifications_on and "checked='checked'" or ""
def gravatar_tag(self): return m.get_user().gravatar_md