Exemple #1
0
def post_moderate(request, pid, status):
    "General moderation function"
    user = request.user
    post = models.Post.objects.get(id=pid)
     
    # remap the status to valid
    status = dict(close=POST_CLOSED, open=POST_OPEN, delete=POST_DELETED).get(status)
    if not status:
        messages.error('Invalid post moderation action')
        return html.redirect( post.get_absolute_url() )    
    
    url = models.post_moderate(request=request, user=user, post=post, status=status)
    return html.redirect( url )    
def comment_delete(request, pid):
    
    user = request.user
    post = models.Post.objects.get(id=pid)
    
    # two conditions where a comment destruction may occur
    permit  = user.can_moderate or (user == post.author )
    if not permit:
        return ajax_error('Permission denied')

    status = POST_DELETED if (post.status != POST_DELETED) else POST_OPEN    
    url = models.post_moderate(request=request, post=post, user=user, status=status)
    
    if url == "/":
        return ajax_success("destroyed") # this is required by the UI
    else:
        return ajax_success("The comment status set to %s" % post.get_status_display() )
Exemple #3
0
def comment_delete(request, pid):

    user = request.user
    post = models.Post.objects.get(id=pid)

    # two conditions where a comment destruction may occur
    permit = user.can_moderate or (user == post.author)
    if not permit:
        return ajax_error('Permission denied')

    status = POST_DELETED if (post.status != POST_DELETED) else POST_OPEN
    url = models.post_moderate(request=request,
                               post=post,
                               user=user,
                               status=status)

    if url == "/":
        return ajax_success("destroyed")  # this is required by the UI
    else:
        return ajax_success("The comment status set to %s" %
                            post.get_status_display())
Exemple #4
0
def insert_post_revisions(fname, limit, users, posts):
    """
    Inserts post revisions. Also responsible for parsing out closed/deleted 
    states from the post history log
    """
    gc.collect()

    # no limits are necessary since it is limited by posts and users already
    rows = xml_reader(fname, limit=None)
    revs = {}  # Dictionary for fast GUID lookup
    ords = []  # Ensures order doesn't get mixed up

    # Stack overflow splits up modifications to title, tags, and content
    # as separate rows in the post history XML distinguished by type
    # We need to first go through and collect them together
    # based on the GUID they set on them.

    # keep revisions to valid posts/users
    rows = filter(checkfunc('UserId', users), rows)
    rows = filter(checkfunc('PostId', posts), rows)

    revs = {}
    glist = []  # maintains order between revisions
    alist = []  # action list

    for row in rows:
        guid = row['RevisionGUID']
        date = parse_time(row['CreationDate'])
        post = posts[row['PostId']]
        author = users[row['UserId']]
        rtype = row['PostHistoryTypeId']
        if guid not in revs:
            glist.append(guid)

        # we will update a revision to contain all changes
        rev = revs.get(guid, {
            'post': post,
            'lastedit_user': author,
            'lastedit_date': date
        })

        if rtype in ['1', '4']:  # Title modification
            rev['title'] = row['Text']
        if rtype in ['2', '5']:  # Body modification
            rev['content'] = row['Text']
        if rtype in ['3', '6']:  # Tag modification
            rev['tag_string'] = parse_tag_string(row['Text'])

        if rtype in ['10', '11', '12', '13']:  # Moderator actions
            actions = {
                '10': const.POST_CLOSED,
                '11': const.POST_OPEN,
                '12': const.POST_DELETED,
                '13': const.POST_OPEN
            }
            # this is defined in the models
            alist.append((post, actions[rtype], author, date))

        revs[guid] = rev

    print "*** inserting %s revisions" % len(glist)
    with transaction.commit_on_success():
        for (i, guid) in enumerate(glist):
            data = revs[guid]
            post = data['post']
            del data['post']
            if USE_DB:
                if (i % 1000 == 0):
                    print "*** commit at %s" % i
                    transaction.commit()

                for key, value in data.items():
                    setattr(post, key, value)

                post.save()

    print "*** inserting %s moderator actions" % len(alist)
    with transaction.commit_on_success():
        for post, status, user, date in alist:
            if USE_DB and post.id:
                models.post_moderate(request=None,
                                     post=post,
                                     status=status,
                                     user=user,
                                     date=date)

    # some posts may have been removed
    for (key, post) in posts.items():
        if not post.id:
            del posts[key]
Exemple #5
0
def insert_post_revisions(fname, limit, users, posts):
    """
    Inserts post revisions. Also responsible for parsing out closed/deleted 
    states from the post history log
    """
    gc.collect()

    # no limits are necessary since it is limited by posts and users already
    rows = xml_reader(fname, limit=None)
    revs = {} # Dictionary for fast GUID lookup
    ords = [] # Ensures order doesn't get mixed up
    
    # Stack overflow splits up modifications to title, tags, and content
    # as separate rows in the post history XML distinguished by type
    # We need to first go through and collect them together
    # based on the GUID they set on them.
       
    # keep revisions to valid posts/users
    rows = filter(checkfunc('UserId', users), rows)
    rows = filter(checkfunc('PostId', posts), rows)
    
    revs  = {}
    glist = [] # maintains order between revisions
    alist = [] # action list

    for row in rows:
        guid = row['RevisionGUID']
        date = parse_time(row['CreationDate'])
        post = posts[ row['PostId'] ]
        author = users[ row['UserId'] ]
        rtype  = row['PostHistoryTypeId']
        if guid not in revs:
            glist.append(guid)

        # we will update a revision to contain all changes
        rev = revs.get(guid, {'post':post, 'lastedit_user':author, 'lastedit_date':date})

        if rtype in ['1', '4']: # Title modification
            rev['title'] = row['Text']
        if rtype in ['2', '5']: # Body modification
            rev['content'] = row['Text']                     
        if rtype in ['3', '6']: # Tag modification
            rev['tag_string'] = parse_tag_string(row['Text'])

        if rtype in ['10','11','12','13']: # Moderator actions
            actions = {'10':const.POST_CLOSED, '11':const.POST_OPEN,
                       '12':const.POST_DELETED, '13':const.POST_OPEN}
            # this is defined in the models
            alist.append( (post, actions[rtype], author, date) )

        revs[guid] = rev

    print "*** inserting %s revisions" % len(glist)
    with transaction.commit_on_success():
        for (i, guid) in enumerate(glist):
            data = revs[guid]
            post = data['post']
            del data['post']
            if USE_DB:
                if (i % 1000 == 0):
                    print "*** commit at %s" % i
                    transaction.commit()
                    
                for key, value in data.items():
                    setattr(post, key, value)
                    
                post.save()
    
                

    print "*** inserting %s moderator actions" % len(alist)
    with transaction.commit_on_success():
        for post, status, user, date in alist:
            if USE_DB and post.id:
                models.post_moderate(request=None, post=post, status=status, user=user, date=date)
                
    # some posts may have been removed
    for (key, post) in posts.items():
        if not post.id:
            del posts[key]