~widelands-dev/widelands-website/trunk

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# -*- coding: utf-8
from django.utils.translation import ugettext_lazy as _
from django.contrib import admin
from pybb.models import Category, Forum, Topic, Post, Read, Attachment


def delete_selected(modeladmin, request, queryset):
    """Overwritten Django's default action to delete a post.

    This action uses the delete() method of the post model.
    This ensures also deleting a topic if neccesary, preventing
    index-errors if a topic has only one post.

    """
    for obj in queryset:
        obj.delete()
delete_selected.short_description = 'Delete selected posts'


def unhide_post(modeladmin, request, queryset):
    """Unhide post(s) and inform subscribers."""
    for obj in queryset:
        obj.unhide_post()
unhide_post.short_description = 'Unhide post and inform subscribers'


class CategoryAdmin(admin.ModelAdmin):
    list_display = ['name', 'position', 'forum_count']
    list_per_page = 20
    ordering = ['position']
    search_fields = ['name']


class ForumAdmin(admin.ModelAdmin):
    list_display = ['name', 'category', 'position', 'topic_count', 'moderator_group']
    list_per_page = 20
    ordering = ['category__position', 'position']
    search_fields = ['name', 'category__name']
    fieldsets = (
        (None, {
            'fields': ('category', 'name', 'updated', 'moderator_group',)
        }
        ),
        (_('Additional options'), {
            'description': 'Position is the position inside the category. \
            This has effect on ordering in forums overview and the navigation bar.',
            'fields': ('position', 'description')
        }
        ),
    )


class PostInline(admin.TabularInline):
    model = Post
    readonly_fields = ('user', 'markup', 'created',)
    exclude = ('created', 'updated', 'body',)
    ordering = ('-created',)


class TopicAdmin(admin.ModelAdmin):
    list_display = ['name', 'forum', 'created', 'head', 'is_hidden']
    list_per_page = 20
    ordering = ['-created']
    date_hierarchy = 'created'
    search_fields = ['name']
    inlines = [PostInline,]
    fieldsets = (
        (None, {
            'fields': ('forum', 'name', 'user', ('created', 'updated'))
        }),
        (_('Additional options'), {
            'classes': ('collapse',),
            'fields': (('views',), ('sticky', 'closed'),)
        }),
    )


class AttachmentInline(admin.StackedInline):
    model = Attachment
    extra = 0
    exclude = ('hash',)


class PostAdmin(admin.ModelAdmin):
    list_display = ['summary', 'topic', 'user', 'created', 'hidden']
    list_per_page = 20
    ordering = ['-created']
    date_hierarchy = 'created'
    search_fields = ['body', 'topic__name']
    actions = [delete_selected, unhide_post]
    inlines = [AttachmentInline,]
    fieldsets = (
        (None, {
            'fields': ('topic', 'user', 'markup', 'hidden')
        }
        ),
        (_('Date and Time'), {
            'classes': ('collapse',),
            'fields': (('created', 'updated'),)
        }
        ),
        (_('Message'), {
            'fields': ('body', 'body_html', 'body_text')
        }
        ),
    )


class ReadAdmin(admin.ModelAdmin):
    list_display = ['user', 'topic', 'time']
    list_per_page = 20
    ordering = ['-time']
    date_hierarchy = 'time'
    search_fields = ['user__username', 'topic__name']

admin.site.register(Category, CategoryAdmin)
admin.site.register(Forum, ForumAdmin)
admin.site.register(Topic, TopicAdmin)
admin.site.register(Post, PostAdmin)
admin.site.register(Read, ReadAdmin)