# -*- coding: utf-8 -*-

from models import *
from django.contrib import admin
from django.utils.html import format_html
from django.utils.translation import ugettext_lazy as _


class ProjectAdmin(admin.ModelAdmin):
    fieldsets = [
        (_(u"Проект"), {"fields": [
            "name", "description", "logo", "user"
        ]}),
    ]
    raw_id_fields = ("user",)
    list_display = ("id", "project_icon", "name", "description", "date_creation", "user")
    list_display_links = ("id", "project_icon", "name",)
    search_fields = ["name", "description", "user"]
    list_filter = ("date_creation",)
    list_per_page = 25
    date_hierarchy = 'date_creation'

    def project_icon(self, obj):
        if obj.logo:
            return format_html( "<img src='%s' width=32 height=32 />" % obj.logo.url )
        elif not obj.logo and obj.invatar:
            return format_html( obj.invatar )
        else:
            return "---"
    project_icon.short_description = u"Иконка"


class IterationAdmin(admin.ModelAdmin):
    fieldsets = [
        (_(u"Итерация"), {"fields": [
            "title", "description", "project", ("date_begin", "date_end")
        ]}),
    ]
    raw_id_fields = ("project",)
    list_display = ("id", "project", "title", "description", "date_begin", "date_end")
    list_display_links = ("id", "project", "title")
    search_fields = ["project", "title", "description", "project__name"]
    list_filter = ("date_begin", "date_end")
    list_per_page = 50


class TaskAdmin(admin.ModelAdmin):
    fieldsets = [
        (_(u"Задача"), {"fields": [
            ("title", "description"), ("priority", "type"),
            "file_attache", "iteration", "user", "is_complete"
        ]}),
    ]
    readonly_fields = ("dt_creation",)
    raw_id_fields = ("iteration", "user")
    list_display = (
        "id", "iteration", "title", "description", "priority", "type", "dt_creation", "is_complete", "dt_completion"
    )
    list_display_links = ("id", "iteration", "title", "description")
    list_editable = ("priority", "type", "is_complete")
    search_fields = ["iteration", "title", "description", "iteration__title"]
    list_filter = ("priority", "type", "is_complete", "dt_completion", "dt_creation")
    date_hierarchy = 'dt_creation'
    list_per_page = 100


class CommentAdmin(admin.ModelAdmin):
    fieldsets = [
        (_(u"Комментарий"), {"fields": [
            "parent", "text", "pub_date", "task", "user"
        ]}),
    ]
    raw_id_fields = ("task", "user")
    list_display = ("id", "parent", "text", "pub_date")
    list_display_links = ("id", "parent", "text", "pub_date")
    search_fields = ["text"]
    list_filter = ("pub_date",)
    list_per_page = 100
    date_hierarchy = 'pub_date'


admin.site.register(Task, TaskAdmin)
admin.site.register(Comment, CommentAdmin)
admin.site.register(Project, ProjectAdmin)
admin.site.register(Iteration, IterationAdmin)

# EOF
