Пример #1
0
def delete_all_tags(i_am_sure=False):
    if not i_am_sure:
        return

    # globally
    del get_tags_database().tags
    get_tags_database().tags = OOBTree.OOBTree()

    # global reverse mapping
    del get_tagged_item_database().root
    get_tagged_item_database().root = OOBTree.OOBTree()

    # for each group, ensure tags are empty
    for group_id, group in get_group_database().root.iteritems():
        del group.tags
        group.tags = OOBTree.OOBTree()
    #

    for user_id, user in get_user_database().root.iteritems():
        if hasattr(user, "tags"):
            if len(user.tags) > 0:
                del user.tags
                user.tags = OOBTree.OOBTree()
    #
    get_transaction().commit()
Пример #2
0
def format_atom_tags_feed (tags):
    """Return an Atom Feed for this tag."""

    if type(tags) is list:
        tag = ",".join(tags)
    else:
        tag = tags

    # get the most recent items for this tag
    tags_db = get_tags_database()
    dates_n_items = tags_db.get_items_and_dates(tags)
    dates_n_items.sort()
    dates_n_items = dates_n_items[:-25]
    dates_n_items.reverse()
    most_recent = dates_n_items[0][0]

    feed = qon.atom.Feed()
    feed.title = "Most recent items tagged with %s" % tag
    feed.url = qon.ui.blocks.util.full_url('/home/tags/%s' % tag)
    feed.set_modified(most_recent)
    try2 = """
    # for each item, use ui blocks to format the entries
    for date, item_oid in dates_n_items:
        item = get_oid(item_oid)

        if type(item) is qon.wiki.Wiki:
            # which page is this? most recent?
            wiki_entry = blocks.wiki.format_atom_page(0)
            feed.entries.append(wiki_entry)
        elif type(item) is qon.user.User:
            user_entry = blocks.blog.format_atom_item(item)
            feed.entries.append(user_entry)
        elif type(item) is qon.blog.Blog:
            comment_entry = blocks.blog.format_atom_item(item)
            #blog_entry = blocks.blog.format_atom_item(item)
            feed.entries.append(comment_entry)
    """
    # fill the feed up with items?
    try1 = """
    for date, item in dats_n_items:
        #entry = _create_page_entry(page, add_group_title)
        path_to_item = qon.ui.blocks.util.full_url('/home/tags/%s' % tag)
        entry = qon.atom.Entry(path_to_item)
        entry.title = xml_escape("title here")
        entry.url = path_to_item
        entry.feed = path_to_item + 'atom.xml'
        entry_feed_title = "" #
        entry.id = qon.ui.blocks.util.atom_id(item)
        entry.set_modified(date)
        entry.set_issued(date)
        entry.set_created(item.date)
        author = qon.atom.Person(xml_escape(item.author.display_name()))
        author.url = qon.ui.blocks.util.full_url(qon.ui.blocks.user.path_to_user(item.author))
        entry.author = author

        entry.content = xml_escape(page.get_cached_html2())

        feed.entries.append(entry)
    """
    return feed
Пример #3
0
    def get_tags_n_counts(self, limit=80):
        """ Suitable for a tag cloud, returns [ (tag, count), ...] 
        sorted alphabetically by tag.

        count is the number of people who have used the tag.
        """
        if limit == 0:
            return []

        tags_db = get_tags_database();
  
        counts_n_tags = []
        for tag, items in self.tags.iteritems():
            items_tagged = len(items)
            unique_users = {}
            for item, users in items.iteritems():
                for user_id in users:
                    if not tags_db.is_tagger_naughty(user_id):
                        unique_users[user_id] = 1
            if unique_users:
                counts_n_tags.append( (len(unique_users), items_tagged, tag) )

        # number of items would look like
        #counts_n_tags = [(len(items.keys()), tag) for (tag, items) in self.tags.iteritems()]

        counts_n_tags.sort()
        # now the popular tags are at the end of counts_n_tags
        tags_n_counts = [ (tag, count)for count, items_tagged, tag in counts_n_tags[-limit:] ]
        tags_n_counts.sort()
        # now we have the most popular, alphabetically
        return tags_n_counts
Пример #4
0
def tag_item (tags, user, item_oid, group, comment = None, is_user=False):
    """ all tags are applied through this function, which keeps
    the various databases consistent with each other."""
    user_id = user.get_user_id()

    tags = qon.tags.standardize_tags(tags)

    # clear out any removed tags
    tags_db = get_tags_database()
    tidb = get_tagged_item_database()
    if is_user:
        user_db = get_user_database()

    # what gets removed? what gets added?
    old_tags = tidb.get_tags(item_oid, user_id)
    tags_to_remove = [tag for tag in old_tags if tag not in tags]
    tags_to_add = [tag for tag in tags if tag not in old_tags]

    if tags_to_remove:
        # remove user from removed tags
        tags_db.remove_tags(tags_to_remove, item_oid, user_id)
    
        if group:
            group.remove_tags(tags_to_remove, item_oid, user_id)
    
        # remove the tag from the user's list too.
        user.remove_tags(tags_to_remove, item_oid)

        if is_user:
            user_db.remove_tags(tags_to_remove, item_oid, user_id)
        #
    #

    if tags_to_add:
        # add to the global database
        tags_db.tag_item(user.get_user_id(), item_oid, tags, comment)
    
        # group gets its tag information
        if group:
            group.tag_item(user_id, item_oid, tags_to_add, comment)
    
        # update the user's tag cloud
        user.tag_item(tags, item_oid)

        if is_user:
            user_db.tag_item(user_id, item_oid, tags_to_add, comment)
        #
    #
    get_transaction().commit()
Пример #5
0
def tag_karma_give_bad(fr, to, message=None, message_anon=True, karma=qon.karma.HasKarma.bad_karma):
    message = _unicode_fix(message)
    
    if to.can_get_karma_from(fr):
        try:
            tags_db = get_tags_database()
            fr.give_karma(to.tag_karma, karma)
            to.tag_karma.calc_karma_score()
            if to.tag_karma.get_karma_score() <= qon.tags.tagger_naughty_threshold:
                tags_db.set_tagger_naughty(to.get_user_id())
            #
            _karma_send_message(fr, to, 'negative', '%d' % karma, message, message_anon)
            transaction_commit(fr, 'KarmaGiveBad')            
            qon.search.searchengine.notify_karma_given(to)
        except qon.karma.NoKarmaToGive:
            pass
Пример #6
0
 def get_item_taggers(self, item_oid, specific_tags=None):
     tags_db = get_tags_database()
     if specific_tags:
         if type(specific_tags) != list:
             specific_tags = [specific_tags]
         taggers = []
         for user_id, attributes in self[item_oid].iteritems():
             common_tags = [tag for tag in specific_tags if tag in attributes.tags]
             if common_tags and not tags_db.is_tagger_naughty(user_id):
                 taggers.append(user_id)
             #
         #
         return taggers
     else:
         if self.has_key(item_oid):
             return self[item_oid].keys()
         else:
             return []
Пример #7
0
    def get_tags(self, item_oid, user_id=None):
        """ given an item, return all the tags, and the number 
        of times that tag was applied. 
        returns [tag, tag,... ] 
        """
        tags = {}

        if self.has_key(item_oid):
            if user_id:
                if self[item_oid].has_key(user_id):
                    for tag in self[item_oid][user_id].tags:
                        tags[tag] = 1
            else:
                # for all users
                tags_db = get_tags_database()
                for user_id, attribs in self[item_oid].iteritems():
                    if not tags_db.is_tagger_naughty(user_id):
                        for tag in attribs.tags:
                            tags[tag] = 1
                #
            #
        #
        return tags.keys()
Пример #8
0
 def get_item_popularity(self, item_oid, specific_tags=None):
     """More popular items are tagged by more people.
     returns: number of people who have tagged this item."""
     if not self.has_key(item_oid):
         return 0
     tags_db = get_tags_database()
     if specific_tags:
         if type(specific_tags) != list:
             specific_tags = [specific_tags]
         count = 0
         for user_id, attributes in self[item_oid].iteritems():
             if not tags_db.is_tagger_naughty(user_id):
                 common_tags = [tag for tag in specific_tags if tag in attributes.tags]
                 if common_tags:
                     count += 1
                 #
             #
         #
         return count
     else:
         if self.has_key(item_oid):
             return len(self[item_oid].keys())
         else:
             return 0
Пример #9
0
    def get_tags_n_counts(self, item_oid, user_id=None):
        """ given an item, return all the tags, and the number 
        of times that tag was applied. 
        returns [ (tag, count),... ] 
        """
        tags = []
        all_tags = []

        tags_db = get_tags_database()

        if self.has_key(item_oid):
            if user_id:
                if self[item_oid].has_key(user_id):
                    tags.extend(self[item_oid][user_id].tags)
            else:
                # for all users (except the naughty taggers)
                for user_id, attribs in self[item_oid].iteritems():
                    if not tags_db.is_tagger_naughty(user_id):
                        tags.extend(attribs.tags)

            # [ (tag, count),... ]
            tags = unique_word_counts(tags)

        return tags
Пример #10
0
def publish_stats():
    """Call after update_stats to publish to a wiki page."""
    
    _stats_group = 'community-general'
    _stats_page = 'user_statistics'
    _stats_author = 'admin'
    
    from datetime import datetime
    from qon.ui.blocks.util import format_datetime_utc_ymd
    
    group_db = get_group_database()
    group = group_db.get_group(_stats_group)
    if group:
        wiki = group.get_wiki()
        try:
            page = wiki.pages[_stats_page]
        except KeyError:
            return
        
        # build new text to publish stats
        # date, num_users, num_groups, num_topics, num_comments, num_pages, num_revisions,
        # total_bank, total_user_pos, total_user_neg, total_topic_pos, total_topic_pos,
        # total_comment_pos, total_comment_neg, total_page_pos, total_page_neg
        # jimc: added: total PMs, group PMs, group PM recipients total increases from 15 to 18
        # added total tags, total tagged items for 20 total fields
        stats_fmt = ['%d' for i in range(20)]       # fields
        stats_fmt = ','.join(stats_fmt)             # comma-separated
        
        # indent and date is first field
        stats_fmt = '    %s,' % format_datetime_utc_ymd(datetime.utcnow()) + stats_fmt + '\n'
        
        # fill in stats
        list_db = get_list_database()
        group_stats = list_db.group_stats(force=True, ignore_out_of_date=False)
        #group_stats = list_db.group_stats(ignore_out_of_date=True)

        tidb = get_tagged_item_database()
        tags_db = get_tags_database()
        total_items_tagged = len(tidb)
        total_tags = len(tags_db.tags)

        stats = stats_fmt % (
            group_stats['users'],
            group_stats['groups'],
            group_stats['topics'],
            group_stats['comments'],
            group_stats['pages'],
            group_stats['revisions'],
            list_db.karma_total_bank(),
            list_db.karma_total_user()[0],
            list_db.karma_total_user()[1],
            list_db.karma_total_topic()[0],
            list_db.karma_total_topic()[1],
            list_db.karma_total_comment()[0],
            list_db.karma_total_comment()[1],
            list_db.karma_total_page()[0],
            list_db.karma_total_page()[1],
            # total pms, for groups, and number of group pm recipients
            list_db.total_users_pms(),
            group_stats['total_group_pms'],
            group_stats['total_group_pm_recipients'],
            total_tags,
            total_items_tagged,
            )
        
        # author is admin user
        author = get_user_database().get_user(_stats_author)
        
        # get current revision
        raw = page.versions[-1].get_raw()
        
        # append stats line
        raw += stats
        
        # set new revision - will commit
        qon.api.wiki_edit_page(wiki, page, page.name, author, page.versions[-1].title, raw)