示例#1
0
def article_delete(user, article_slug, **kwargs):
    """Delete an article or forum post

    This doesn't actually delete but rather clears the auther, title
    and content fields and sets is_visible to False.  It isn't
    possible to clear the slug field because it'd break hyperlinks.
    Commenting is still possible if you have the hyperlink to the
    article saved.  To fully delete an article you must go in the
    backend.

    We mustn't allow users without staff privileges to edit an article
    once it's been converted to a news article.
    """
    if not (user and user.id):
        raise APIException(_("you're not logged in"))
    try:
        article = db.Article.objects.get(slug=article_slug, is_deleted=False)
    except db.Article.DoesNotExist:
        raise APIException(_("article not found"))
    _check_modify_article(user, article)
    if user != article.author:
        if not user.is_superuser:
            raise APIException(_("insufficient privileges"))
    article.author = None
    article.title = "[DELETED]"
    article.content = "[DELETED]"
    article.is_visible = False
    article.save()
    return []
示例#2
0
def message_send(user, to_username, content, **kwargs):
    """Send a private message"""
    if not (user and user.id):
        raise APIException(_("you're not logged in"))
    content = content.strip()
    if len(content) < 3:
        raise APIException(_("message too short"))
    try:
        to_user = db.User.objects.get(username=to_username, is_active=True)
    except db.User.DoesNotExist:
        raise APIException(_('user not found'))
    if user == to_user:
        raise APIException(_("you can't message yourself"))
    if not user.is_staff:
        hours24 = now() - timedelta(hours=24)
        rec = db.Message.objects.filter(from_user=user, published__gt=hours24)
        sentusers = set(m.to_user.username for m in rec)
        if len(sentusers) > settings.OWS_LIMIT_MSG_DAY:
            raise APIException(
                _("you can't send more than %(limit)s messages per day") %
                {"limit": settings.OWS_LIMIT_MSG_DAY})
    msg = db.Message.objects.create(from_user=user,
                                    to_user=to_user,
                                    content=content)
    db.Notification.send(
        to_user, user.get_absolute_url(),
        _("%(username)s sent you a message" % {"username": user.username}))
    html = render_to_string('occupywallst/message.html', {'message': msg})
    return [msg.as_dict({'html': html})]
示例#3
0
def comment_vote(user, comment, vote, **kwargs):
    """Increases comment karma by one

    If a user is logged in, we track their votes in the database.

    If a user is not logged in, we still allow them to vote but to
    prevent them from clicking the downvote arrow repeatedly we only
    allow an IP to vote once.  We track these votes in a
    non-persistant cache because we don't want to log IP addresses.
    """
    if not (user and user.id):
        raise APIException(_("not logged in"))
    if not isinstance(comment, db.Comment):
        try:
            comment = db.Comment.objects.get(id=comment, is_deleted=False)
        except db.Comment.DoesNotExist:
            raise APIException(_("comment not found"))
    if not settings.DEBUG and not user.is_staff:
        for tdelta, maxvotes in settings.OWS_LIMIT_VOTES:
            votes = (db.CommentVote.objects.filter(user=user,
                                                   time__gt=now() -
                                                   tdelta).count())
            if votes > maxvotes:
                raise APIException(_("you're voting too much"))
    if not user.userinfo.is_shadow_banned:
        if vote == "up":
            comment.upvote(user)
        elif vote == "down":
            comment.downvote(user)
        else:
            raise APIException(_("invalid vote"))
    return []
示例#4
0
def signup(request, username, password, email, **kwargs):
    """Create a new account

    - Username must have only letters/numbers and be 3-30 chars
    - Password must be 6-128 chars
    - Email is optional
    """
    raise APIException("disabled")
    if request.user.is_authenticated():
        raise APIException(_("you're already logged in"))
    check_username(username=username)
    if len(password) < 6:
        raise APIException(_("password must be at least six characters"))
    if len(password) > 128:
        raise APIException(_("password too long"))
    if email:
        if not email_re.match(email):
            raise APIException(_("invalid email address"))
    user = db.User()
    user.username = username
    user.set_password(password)
    user.email = email
    user.save()
    userinfo = db.UserInfo()
    userinfo.user = user
    userinfo.attendance = 'maybe'
    userinfo.save()
    user.userinfo = userinfo
    user.save()
    res = login(request, username, password)
    res[0]['conversion'] = render_to_string('occupywallst/conversion.html')
    return res
示例#5
0
def article_edit(user, article_slug, title, content, **kwargs):
    """Edit an article or forum post

    We mustn't allow users without staff privileges to edit an article
    once it's been flagged to allow HTML or converted to a news
    article.
    """
    if not (user and user.id):
        raise APIException(_("you're not logged in"))
    title = title.strip()
    content = content.strip()
    try:
        article = db.Article.objects.get(slug=article_slug, is_deleted=False)
    except db.Article.DoesNotExist:
        raise APIException(_("article not found"))
    if not user.is_staff:
        if article.author != user:
            raise APIException(_("you didn't post that"))
        if article.allow_html or not article.is_forum:
            raise APIException(_("insufficient privileges"))
    article.title = title
    article.content = content
    _check_post(user, article)
    article.save()
    return article_get(user, article_slug)
示例#6
0
def login(request, username, password, **kwargs):
    """Login user"""
    if request.user.is_authenticated():
        raise APIException(_("you're already logged in"))
    user = auth.authenticate(username=username, password=password)
    if not user:
        raise APIException(_("invalid username or password"))
    auth.login(request, user)
    return [user.userinfo.as_dict()]
示例#7
0
def check_username(username, check_if_taken=True, **kwargs):
    """Check if a username is valid and available"""
    if len(username) < 3:
        raise APIException(_("username too short"))
    if len(username) > 30:
        raise APIException(_("username too long"))
    if not re.match(r'[a-zA-Z0-9]{3,30}', username):
        raise APIException(_("bad username, use only letters/numbers"))
    if check_if_taken:
        if db.User.objects.filter(username__iexact=username).count():
            raise APIException(_("username is taken"))
    return []
示例#8
0
def _check_modify_comment(user, comment, mods_only=False):
    """Ensure user have permission to modify an comment

    Normal users can only edit their own comments.  Moderators and staff can
    modify all comments.
    """
    if not user.userinfo.can_moderate():
        if comment.user != user:
            raise APIException(_("you didn't post that"))
    if user.userinfo.can_moderate() and not user.is_staff:
        if comment.user.is_staff:
            raise APIException(_("insufficient privileges"))
示例#9
0
def ride_request_update(request_id, status, user=None, **kwargs):
    ride_request = (db.RideRequest.objects.filter(id=request_id)
                    .select_related("ride", "ride__user"))
    try:
        req = ride_request[0]
    except IndexError:
        raise APIException(_("request not found"))
    if req.ride.user == user:
        req.status = status
        req.save()
        return [{"id": req.id, "status": req.status}]
    else:
        raise APIException(_("insufficient permissions"))
示例#10
0
def message_delete(user, message_id, **kwargs):
    """Delete a message

    Both the sender and the receiver are able to delete messages.
    """
    if not (user and user.id):
        raise APIException(_("you're not logged in"))
    try:
        msg = db.Message.objects.get(id=message_id, is_deleted=False)
    except db.Message.DoesNotExist:
        raise APIException(_("message not found"))
    if user != msg.to_user and user != msg.from_user:
        raise APIException(_("you didn't send or receive that message"))
    msg.delete()
    return []
示例#11
0
def _check_modify_article(user, article):
    """Ensure user have permission to modify an article

    Normal users can only edit their own forum posts.  Moderators can edit all
    forum posts; however, only staff/admin users can edit news articles and
    articles that have been flagged to allow html.
    """
    if not user.is_staff:
        if not article.is_forum or article.allow_html:
            raise APIException(_("insufficient privileges"))
    if not user.userinfo.can_moderate():
        if article.author != user:
            raise APIException(_("you didn't post that"))
    if user.userinfo.can_moderate() and not user.is_staff:
        if article.author.is_staff:
            raise APIException(_("insufficient privileges"))
示例#12
0
def _limiter(last, seconds):
    limit = timedelta(seconds=seconds)
    since = (now() - last)
    if since < limit:
        raise APIException(
            _("please wait %(duration)s before making another post") %
            {"duration": timesince(now() - limit + since)})
示例#13
0
def carousel_get(user, carousel_id=None, **kwargs):
    """Fetch a list of photos in a carousel"""
    try:
        carousel = db.Carousel.objects.get(id=carousel_id)
    except db.Carousel.DoesNotExist:
        raise APIException(_("carousel not found"))
    return [photo.as_dict() for photo in carousel.photo_set.all()]
示例#14
0
def article_get_comments(user, article_slug=None, **kwargs):
    """Get all comments for an article"""
    try:
        article = db.Article.objects.get(slug=article_slug, is_deleted=False)
        comments = article.comment_set.all()
        return [comment.as_dict() for comment in comments]
    except db.Article.DoesNotExist:
        raise APIException(_("article not found"))
示例#15
0
def article_get_comment_votes(user, article_slug=None, **kwargs):
    """Get all votes for all comments for an article"""
    try:
        article = db.Article.objects.get(slug=article_slug, is_deleted=False)
        votes = db.CommentVote.objects.filter(
            comment__in=article.comment_set.all())
        return [vote.as_dict() for vote in votes]
    except db.Article.DoesNotExist:
        raise APIException(_("article not found"))
示例#16
0
def comment_delete(user, comment_id, **kwargs):
    """Delete a comment

    Also decrements article comment count.
    """
    if not (user and user.id):
        raise APIException(_("you're not logged in"))
    try:
        comment = db.Comment.objects.get(id=comment_id, is_deleted=False)
    except db.Comment.DoesNotExist:
        raise APIException(_("comment not found"))
    if not user.is_staff:
        if comment.user != user:
            raise APIException(_("you didn't post that"))
    comment.article.comment_count -= 1
    comment.article.save()
    comment.delete()
    return []
示例#17
0
def article_remove(user, article_slug, action, **kwargs):
    """Makes an article unlisted and invisible to search engines"""
    if not (user and user.id):
        raise APIException(_("you're not logged in"))
    if not user.is_staff:
        raise APIException(_("insufficient permissions"))
    try:
        article = db.Article.objects.get(slug=article_slug, is_deleted=False)
    except db.Article.DoesNotExist:
        raise APIException(_("article not found"))
    if action == 'remove':
        article.is_visible = False
    elif action == 'unremove':
        article.is_visible = True
    else:
        raise APIException(_("invalid action"))
    article.save()
    return []
示例#18
0
def comment_edit(user, comment_id, content, **kwargs):
    """Edit a comment's content"""
    if not (user and user.id):
        raise APIException(_("you're not logged in"))
    content = content.strip()
    if len(content) < 3:
        raise APIException(_("comment too short"))
    try:
        comment = db.Comment.objects.get(id=comment_id, is_deleted=False)
    except db.Comment.DoesNotExist:
        raise APIException(_("comment not found"))
    if not user.is_staff:
        if comment.user != user:
            raise APIException(_("you didn't post that"))
    comment.content = content
    _check_post(user, comment)
    comment.save()
    return comment_get(user, comment.id)
示例#19
0
def comment_delete(user, comment_id, **kwargs):
    """Delete a comment

    Also decrements article comment count.
    """
    if not (user and user.id):
        raise APIException(_("you're not logged in"))
    try:
        comment = db.Comment.objects.get(id=comment_id, is_deleted=False)
    except db.Comment.DoesNotExist:
        raise APIException(_("comment not found"))
    _check_modify_comment(user, comment)
    if user != comment.user:
        if not user.is_superuser:
            raise APIException(_("insufficient privileges"))
    comment.article.comment_count -= 1
    comment.article.save()
    comment.delete()
    return []
示例#20
0
def comment_edit(user, comment_id, content, **kwargs):
    """Edit a comment's content"""
    if not (user and user.id):
        raise APIException(_("you're not logged in"))
    content = content.strip()
    if len(content) < 3:
        raise APIException(_("comment too short"))
    try:
        comment = db.Comment.objects.get(id=comment_id, is_deleted=False)
    except db.Comment.DoesNotExist:
        raise APIException(_("comment not found"))
    _check_modify_comment(user, comment)
    if user != comment.user:
        if not user.is_superuser:
            raise APIException(_("insufficient privileges"))
    comment.content = content
    _check_post(user, comment)
    comment.save()
    return comment_get(user, comment.id)
示例#21
0
def forumlinks(user, after, count, **kwargs):
    """Used for continuous stream of forum post links"""
    after, count = int(after), int(count)
    if after < 0 or count <= 0:
        raise APIException(_("bad arguments"))
    articles = (db.Article.objects_as(user).select_related(
        "author", "author__userinfo").order_by('-killed'))
    for article in articles[after:after + count]:
        yield render_to_string('occupywallst/forumpost_synopsis.html', {
            'article': article,
            'user': user
        })
示例#22
0
def article_get(user, article_slug=None, read_more=False, **kwargs):
    """Get article information"""
    read_more = _to_bool(read_more)
    try:
        article = db.Article.objects.get(slug=article_slug, is_deleted=False)
    except db.Article.DoesNotExist:
        raise APIException(_("article not found"))
    html = render_to_string('occupywallst/article_content.html',
                            {'article': article,
                             'user': user,
                             'read_more': read_more})
    return [article.as_dict({'html': html})]
示例#23
0
def forumlinks(after, count, **kwargs):
    """Used for continuous stream of forum post links"""
    after, count = int(after), int(count)
    if after < 0 or count <= 0:
        raise APIException(_("bad arguments"))
    articles = (db.Article.objects
                .select_related("author")
                .filter(is_visible=True, is_deleted=False)
                .order_by('-killed'))
    for article in articles[after:after + count]:
        yield render_to_string('occupywallst/forumpost_synopsis.html',
                               {'article': article})
示例#24
0
def comment_remove(user, comment_id, action, **kwargs):
    """Allows moderator to remove a comment"""
    if not (user and user.id):
        raise APIException(_("you're not logged in"))
    if not user.is_staff:
        raise APIException(_("insufficient permissions"))
    try:
        comment = db.Comment.objects.get(id=comment_id, is_deleted=False)
    except db.Comment.DoesNotExist:
        raise APIException(_("comment not found"))
    if action == 'remove' and not comment.is_removed:
        comment.is_removed = True
        comment.article.comment_count -= 1
    elif action == 'unremove' and comment.is_removed:
        comment.is_removed = False
        comment.article.comment_count += 1
    else:
        raise APIException(_("invalid action"))
    comment.save()
    comment.article.save()
    return []
示例#25
0
def shadowban(user, username, action, **kwargs):
    """Ban a user through nefarious means"""
    if not (user and user.id):
        raise APIException(_("you're not logged in"))
    if not user.userinfo.can_moderate():
        raise APIException(_("insufficient permissions"))
    try:
        user2 = db.User.objects.get(username=username)
    except db.User.DoesNotExist:
        raise APIException(_("user not found"))
    if user2.userinfo.can_moderate():
        raise APIException(_("cannot ban privileged users"))
    if action == 'ban':
        if not user2.userinfo.is_shadow_banned:
            user2.userinfo.is_shadow_banned = True
    elif action == 'unban':
        if user2.userinfo.is_shadow_banned:
            user2.userinfo.is_shadow_banned = False
    else:
        raise APIException(_("invalid action"))
    user2.userinfo.save()
    return []
示例#26
0
def article_new(user, title, content, is_forum, **kwargs):
    """Create a news article or forum thread

    We mustn't allow users without staff privileges to post news
    articles.
    """
    is_forum = _to_bool(is_forum)
    if not (user and user.id):
        raise APIException(_("you're not logged in"))
    if not is_forum:
        if not user.is_staff:
            raise APIException(_("insufficient privileges"))
    title = title.strip()
    content = content.strip()
    slug = slugify(title)[:50]
    if db.Article.objects.filter(slug=slug).count():
        raise APIException(_("a thread with this title exists"))
    if not settings.DEBUG and not user.is_staff:
        last = user.article_set.order_by('-published')[:1]
        if last:
            _limiter(last[0].published, settings.OWS_LIMIT_THREAD)
        ip = _try_to_get_ip(kwargs)
        if ip:
            last = cache.get('api_article_new_' + ip)
            if last:
                _limiter(last, settings.OWS_LIMIT_THREAD)
    article = db.Article()
    article.author = user
    article.published = now()
    article.is_visible = True
    article.title = title
    article.slug = slug
    article.content = content
    article.is_forum = is_forum
    article.ip = _try_to_get_ip(kwargs)
    _check_post(user, article)
    article.save()
    return article_get(user, slug)
示例#27
0
def commentfeed(user, after, count, **kwargs):
    """Used for continuous stream of forum comments"""
    ip = _try_to_get_ip(kwargs)
    after, count = int(after), int(count)
    if after < 0 or count <= 0:
        raise APIException(_("bad arguments"))
    comments = (db.Comment.objects_as(user).select_related(
        "article", "user",
        "user__userinfo").order_by('-published'))[after:after + count + 10]
    comments = db.mangle_comments(comments, user, ip)
    for comment in comments[:count]:
        yield render_to_string('occupywallst/comment.html', {
            'comment': comment,
            'user': user,
            'can_reply': True,
            'extended': True
        })
示例#28
0
def _check_post(user, post):
    """Ensure user-submitted forum content is kosher"""
    if user.userinfo.can_moderate():
        return
    if len(post.content) < 3:
        raise APIException(_("content too short"))
    if len(post.content) > 10 * 1024:
        raise APIException(_("content too long"))
    if ((len(post.content) < 8 and 'bump' in post.content.lower())
            or (len(post.content) < 5 and '+1' in post.content.lower())):
        raise APIException(_("please don't bump threads"))
    if _too_many_caps(post.content):
        raise APIException(_("turn off bloody caps lock"))
    if hasattr(post, 'title'):
        if len(post.title) < 3:
            raise APIException(_("title too short"))
        if len(post.title) > 128:
            raise APIException(_("title too long"))
        if _too_many_caps(post.title):
            raise APIException(_("turn off bloody caps lock"))
        if db.SpamText.is_spam(post.title):
            post.is_removed = True
        if 'watch' in re.sub('[^a-z]', '', post.title.lower()):
            post.is_removed = True
        if 'episode' in re.sub('[^a-z]', '', post.title.lower()):
            post.is_removed = True
        if 'streaming' in re.sub('[^a-z]', '', post.title.lower()):
            post.is_removed = True
        if 'download' in re.sub('[^a-z]', '', post.title.lower()):
            post.is_removed = True
        if re.match(r's\d\de\d\d', post.title.lower()):
            post.is_removed = True
        if ' HD ' in post.title:
            post.is_removed = True
    if db.SpamText.is_spam(post.content):
        post.is_removed = True
    if post.is_removed:
        _train(post)
    if user.userinfo.is_shadow_banned:
        post.is_removed = True
    try:
        bclass = REDBAY.classify(post.full_text())
    except redis.ConnectionError:
        bclass = 'good'
    if bclass == 'bad' and user.userinfo.karma < settings.OWS_KARMA_THRESHOLD:
        post.is_removed = True
示例#29
0
def comment_get(user, comment_id=None, **kwargs):
    """Fetch a single comment information"""
    try:
        comment = db.Comment.objects.get(id=comment_id, is_deleted=False)
    except db.Comment.DoesNotExist:
        raise APIException(_("comment not found"))
    comment.upvoted = False
    comment.downvoted = False
    if user and user.id:
        try:
            vote = db.CommentVote.objects.get(comment=comment, user=user)
        except db.CommentVote.DoesNotExist:
            vote = None
        if vote:
            if vote.vote == 1:
                comment.upvoted = True
            elif vote.vote == -1:
                comment.downvoted = True
    html = render_to_string('occupywallst/comment.html',
                            {'comment': comment,
                             'user': user,
                             'can_reply': True})
    return [comment.as_dict({'html': html})]
示例#30
0
def _check_post(user, post):
    """Ensure user-submitted forum content is kosher"""
    if user.is_staff:
        return
    if len(post.content) < 3:
        raise APIException(_("content too short"))
    if len(post.content) > 10 * 1024:
        raise APIException(_("content too long"))
    if ((len(post.content) < 8 and 'bump' in post.content.lower()) or
        (len(post.content) < 5 and '+1' in post.content.lower())):
        raise APIException(_("please don't bump threads"))
    if _too_many_caps(post.content):
        raise APIException(_("turn off bloody caps lock"))
    if hasattr(post, 'title'):
        if len(post.title) < 3:
            raise APIException(_("title too short"))
        if len(post.title) > 255:
            raise APIException(_("title too long"))
        if _too_many_caps(post.title):
            raise APIException(_("turn off bloody caps lock"))
    if db.SpamText.is_spam(post.content):
        post.is_removed = True
    if user.userinfo.is_shadow_banned:
        post.is_removed = True