def remove_from_group(self, item): typecheck(item, HasUserID) while item in self.__items: self.__items.remove(item) self._p_changed = 1
def stop_watching_item(self, item): """Remove item from watch list.""" typecheck(item, Watchable) typecheck(item, Persistent) if not item.watchable(): return if self.__items.has_key(item._p_oid): del self.__items[item._p_oid]
def set_user_id(self, user_id): typecheck(user_id, str) if not self.valid_user_id(user_id): raise ValueError( "Invalid user ID %s: can only contain letters, numbers, " "dashes, and underscores." % user_id) self.user_id = user_id
def watch_item(self, item): """Add item to watch list. Records current time as last-seen.""" typecheck(item, Watchable) typecheck(item, Persistent) if not item.watchable(): return if not self.__items.has_key(item._p_oid): self.__items[item._p_oid] = datetime.utcnow()
def set_owning_group(self, group): """Set group as sole group owner of this resource. Can use add_owning_group to add additional groups. """ typecheck(group, HasUserID) self.groups = [] self.groups.append(group) self._p_changed = 1
def vote(self, user, vote): """Record a vote. vote: ['for', 'against', 'abstain']. Raises VotingClosed if voting is over. If VotingClosed is raised, caller must inform VoteableQueue.voting_closed() """ typecheck(vote, str) self._check_vote_status(user, vote) vote_list = self._get_vote_list(vote) vote_list.append(user) self._p_changed = 1
def watchable_seen(self, obj): """Called when a watchable item is seen, even if it's not in my watchable list.""" typecheck(obj, Watchable) typecheck(obj, Persistent) if not obj.watchable(): return if self.__items.has_key(obj._p_oid): self.__items[obj._p_oid] = datetime.utcnow() self.add_footprint(obj)
def _check_vote_status(self, user, vote): typecheck(user, User) if self.get_state() != 'voting': raise InvalidState, "not in voting state." if self.__votes_cast.has_key(user): raise KeyError, "already voted." if datetime.utcnow() > self.__end_voting: self.close_voting() raise VotingClosed, "voting is over." self.__votes_cast[user] = vote self._p_changed = 1
def notify_authored_item(self, item): """Notice that I authored an item: BlogItem or WikiPage.""" from blog import BlogItem from group import Group from wiki import WikiPage typecheck(item, (BlogItem, WikiPage)) if isinstance(item, BlogItem): # only notice new BlogItems contained in Groups; otherwise # would catch a WikiPage's comment BlogItem. IHasBlog.notify_new_item # is called when an item is added to self.blog if isinstance(item.blog.ihb, Group): self.get_activity().add_recent_blog_item(item) elif isinstance(item, WikiPage): self.get_activity().add_recent_wiki_page(item)
def add_karma(self, source, karma, credit_bank=True): """Add karma from source. Will call self.notify_karma_changed() if it exists. """ typecheck(source, HasKarmaBank) assert hasattr(source, 'get_user_id') source_uid = source.get_user_id() self.__karma[source_uid] = self.__karma.get(source_uid, 0) + karma self.__score += karma if credit_bank: if karma > 0: if hasattr(self, '_karma_subpoint'): self._karma_subpoint(karma * HasKarmaBank.subpoints_for_receive_karma) self.notify_karma_changed()
def give_karma(self, to, karma): """Give karma to a user or item of content.""" typecheck(to, HasKarma) if not self.can_give_karma(karma): raise NoKarmaToGive to.add_karma(self, karma) if karma < 0: self.add_karma_bank(karma) self.__minus_given -= karma # sum is positive self._record_minus_given(to) else: self.add_karma_bank(-karma) self.__plus_given += karma self._record_plus_given(to) if self.get_karma_bank_balance() < self.activity_credit_max: self._karma_subpoint(self.subpoints_for_karma)
def blog_edit_item(item, **kwargs): typecheck(item, qon.blog.BlogItem) if kwargs.has_key('summary'): new_summary = _unicode_fix(kwargs['summary']) if item.title == 'comment': if not item.history: # start the history with the original message item.history = "---- Original Content from %s ----\n%s\n" % \ (format_datetime(item.date), item.get_summary()) # append the diff from the current state to the new state to the history import difflib prev = item.get_summary().splitlines() current = new_summary.splitlines() diffs = list(difflib.unified_diff(prev, current, n=1, lineterm='')) date = format_datetime(datetime.utcnow()) item.history = "%s\n---- Edit on %s ----\n%s\n" % (item.history, date, '\n'.join(diffs[2:])) # change the summary item.set_summary(new_summary) del kwargs['summary'] _set_attributes(item, **kwargs) item.modified = datetime.utcnow() item._p_changed = 1 transaction_commit(None, 'EditBlogItem') # moved from blog/form.ptl/EditBlogItemForm.commit() if item.title == 'comment' or item.not_watchable: # XXX hack to find comment assert(item.parent_blogitem) qon.search.searchengine.notify_edited_blog_comment(item) else: # handle edited blog entries qon.search.searchengine.notify_edited_blog_item(item)
def notify_authored_comment(self, item, parent): """Notice that I authored a comment belonging to parent.""" from blog import BlogItem from group import Group from wiki import WikiPage if not item: return typecheck(item, BlogItem) typecheck(parent, BlogItem) activity = self.get_activity() container = parent.blog.ihb typecheck(container, (Group, WikiPage, User)) if isinstance(container, Group): activity.add_recent_blog_comment(item, parent) elif isinstance(container, WikiPage): activity.add_recent_wiki_comment(item, container) # note not parent elif isinstance(container, User): activity.add_recent_personal_comments(item, parent)
def is_watching(self, item): """Returns true if item is in watch list.""" typecheck(item, Watchable) typecheck(item, Persistent) return item._p_oid in self.__items
def last_seen(self, item): """Return datetime this item was last seen.""" typecheck(item, Watchable) typecheck(item, Persistent) return self.__items.get(item._p_oid, never)
def is_member_of_group(self, item): typecheck(item, HasUserID) return item in self.__items
def file_rename_directory(dir, name): name = _unicode_fix(name) typecheck(dir, qon.file.QonDir) dir.filename = name transaction_commit(None, 'RenameDir') # moved from file.ptl/RenameDirForm.commit()
def set_member_since(self, dt): typecheck(dt, datetime) self.__member_since = dt
def add_member(self, user): typecheck(user, HasUserID) if user not in self.__members: self.__members.append(user) self._p_changed = 1