summaryrefslogtreecommitdiffstats
path: root/askbot/api.py
blob: 9f37995e9933ac5ecf15438a89e2492db431b924 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""place for the API calls into askbot
at this point most of the useful functions are still
in the askbot.models module, but
api must become a place to manupulate the data in the askbot application
so that other implementations of the data storage could be possible
"""
from django.db.models import Q
from askbot import models
from askbot import const

def get_info_on_moderation_items(user):
    """returns a dictionary with
    counts of new and seen moderation items for a given user
    if user is not a moderator or admin, returns None
    """
    if user.is_anonymous():
        return None
    if not(user.is_moderator() or user.is_administrator()):
        return None

    content_types = (
        const.TYPE_ACTIVITY_MARK_OFFENSIVE,
        const.TYPE_ACTIVITY_MODERATED_NEW_POST,
        const.TYPE_ACTIVITY_MODERATED_POST_EDIT,
    )

    messages = models.ActivityAuditStatus.objects.filter(
        activity__activity_type__in = content_types,
        user = user
    )

    seen_count = messages.filter(
                    status = models.ActivityAuditStatus.STATUS_SEEN
                ).count()
    new_count = messages.filter(
                    status = models.ActivityAuditStatus.STATUS_NEW
                ).count()
    return {
        'seen_count': seen_count,
        'new_count': new_count
    }

def get_admin(seed_user_id = None):
    """returns user objects with id == seed_user_id
    if the user with that id is not an administrator,
    the function will try to find another admin or moderator
    who has the smallest user id

    if the user is not found, or there are no moderators/admins
    User.DoesNotExist will be raised

    The reason this function is here and not on a manager of
    the user object is because we still patch the django-auth User table
    and it's probably better not to patch the manager
    """

    if seed_user_id:
        user = models.User.objects.get(id = seed_user_id)#let it raise error here
        if user.is_administrator() or user.is_moderator():
            return user
    try:
        return models.User.objects.filter(
                        Q(is_superuser=True) | Q(status='m')
                    ).order_by('id')[0]
    except IndexError:
        raise models.User.DoesNotExist(
                """Please add a moderator or an administrator to the forum first
                there don't seem to be any"""
            )