Source

Ochs / news / admin.py

Full commit
import os

from django import forms
from django.db import models
from django.forms import widgets
from django.forms.formsets import formset_factory
from django.contrib import admin
from django.utils.functional import curry
from django.forms.models import inlineformset_factory
from django.conf import settings
from django.forms.util import ErrorList
from django.forms.models import BaseInlineFormSet

from django.contrib.contenttypes.generic import GenericTabularInline

from cms.admin.pageadmin import PageAdmin
from cms.models.pagemodel import Page

from widgets import WYMEditor

from staff import util
from staff.models import EVERYONE, NON_CONTRIBUTOR, PUBLISH, CONTRIBUTOR
from poll.models import Poll
from news.models import Story, Byline, StoryLink, StoryNotes, Poll, Choice, DRAFT
from lib import scrapers
from ochs_admin import admin_site

class OchsPageAdmin(PageAdmin):
    def has_add_permission(self, request):
        return util.check_role(request.user, PUBLISH)
    has_recover_permission = has_add_permission

    def has_change_permission(self, request, obj=None):
        return util.check_role(request.user, PUBLISH)
    has_delete_permission = has_change_permission
#admin_site.site.unregister(Page)
admin_site.site.register(Page, OchsPageAdmin)

class ChoiceInline(admin.TabularInline):
    model = Choice
    extra = 3

class PollAdmin(admin.ModelAdmin):
    inlines = [ChoiceInline,]
    raw_id_fields = ('story',)

    def has_add_permission(self, request):
        return util.check_role(request.user, EVERYONE)

    def has_change_permission(self, request, obj=None):
        if not obj:
            return True
        if request.user.is_superuser or not util.check_role(request.user,
CONTRIBUTOR):
            return True
        else:
            return False
    has_delete_permission = has_change_permission
admin_site.site.register(Poll, PollAdmin)

def storynotes_form_factory(request, obj=None):
    return type("StoryNotesAdminForm", (forms.ModelForm,), {})

class StoryNotesAdminInline(admin.TabularInline):
    model = StoryNotes
    extra = 1

class StoryBylineInline(GenericTabularInline):
    model = Byline

class StoryLinkInline(admin.StackedInline):
    model = StoryLink

class StoryAdmin(admin.ModelAdmin):
    prepopulated_fields = {"slug": ("headline",)}
    inlines = [StoryBylineInline, StoryLinkInline, StoryNotesAdminInline]
    formfield_overrides = {
        models.TextField: {'widget': WYMEditor()}
    }

    class Media:
        js = [os.path.join(settings.CMS_MEDIA_URL, path) for path in (
            'js/lib/jquery.js',
            'js/lib/jquery.query.js',
            'js/lib/ui.core.js',
            'js/lib/ui.dialog.js',
            'wymeditor/jquery.wymeditor.js',
            'wymeditor/plugins/resizable/jquery.wymeditor.resizable.js',
            'wymeditor/plugins/hovertools/jquery.wymeditor.hovertools.js',
            'js/wymeditor.placeholdereditor.js',
            'js/placeholder_editor_registry.js',
        )]
        js += ['/media/wymeditor/plugins/jquery.wymeditor.filebrowser.js']
        css = {
            'all': [os.path.join(settings.CMS_MEDIA_URL, path) for path in (
                        'css/jquery/cupertino/jquery-ui.css',
                    )],
        }

    def save_model(self, request, obj, form, change):
        if obj.status != DRAFT and util.check_role(request.user, CONTRIBUTOR):
            obj.status = DRAFT
        obj.save()

    def queryset(self, request):
        if not request.user.is_superuser and util.check_role(request.user, CONTRIBUTOR):
            qs = Story.objects.filter(byline__user=request.user).order_by('-publish_date')
        else:
            qs = super(StoryAdmin, self).queryset(request)
        return qs

    def get_form(self, request, obj=None, **kwargs):
        if util.check_role(request.user, CONTRIBUTOR):
            self.exclude = ('status', 'publish_date', 'enable_comments', 'breaking_news')
        return super(StoryAdmin, self).get_form(request, obj, **kwargs)

    def has_add_permission(self, request):
        return util.check_role(request.user, EVERYONE)

    def has_change_permission(self, request, obj=None):
        if not obj:
            return True
        if request.user.is_superuser or not util.check_role(request.user,
CONTRIBUTOR):
            return True
        else:
            return False
    has_delete_permission = has_change_permission

admin_site.site.register(Story, StoryAdmin)