from django.contrib import admin

from newsreader.news.collection.models import CollectionRule


class CollectionRuleAdmin(admin.ModelAdmin):
    fields = ("url", "name", "timezone", "category", "favicon", "user")

    list_display = ("name", "type_display", "category", "url", "last_run", "succeeded")
    list_filter = ("user",)

    def save_model(self, request, obj, form, change):
        if not change:
            obj.user = request.user
        obj.save()

    def type_display(self, rule):
        return rule.get_type_display()


admin.site.register(CollectionRule, CollectionRuleAdmin)
