# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from django.utils.translation import ugettext_lazy as _
from django.contrib import admin
import logging
from django.utils.html import format_html, escape
from django.contrib.auth.admin import UserAdmin

from account.models import Team

LOG = logging.getLogger(__name__)

class TeamInline(admin.StackedInline):
    model = Team.users.through
    can_delete = False
    extra = 1
    max_num = 1
    verbose_name = _("Team")
    verbose_name_plural = _("Teams")


class UserAdmin(UserAdmin):
    
    inlines = (TeamInline, )
    
    list_display = ('username', 'email', 'get_team_for_user')
    list_filter = ('is_staff', 'is_superuser', 'is_active', 'groups')
    search_fields = ('username', 'first_name', 'last_name', 'email')
    ordering = ('username',)
    
    fieldsets_basic = (
        (None, {'fields': ('username', 'password')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name', 'email', 'is_active')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )
    
    fieldsets_advanced = (
        (None, {'fields': ('username', 'password')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name', 'email', 'is_staff', 'is_active', 'is_superuser')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )
    
    def get_team_for_user(self, user):
        teams = user.team_set.all()
        team_html = []
        if teams:
            team_html.append("<ul>")
            for team in teams:
                team_html.append("<li>%s</li>" % team.name)
            team_html.append("</ul>")
            return format_html("".join(team_html))
        else:
            return "N/A"

    
    get_team_for_user.short_description = "Team(s)"

    def get_fieldsets(self, request, obj=None):
        if not obj:
            return self.add_fieldsets
        else:
            if request.user.is_superuser:
                #return super(UserAdmin, self).get_fieldsets(request, obj=obj)
                return self.fieldsets_advanced
            else:
                return self.fieldsets_basic

@@ -1004,16 +1004,28 @@
 _active'
+, 'is_staff'
 )%7D),%0A   
@@ -1273,26 +1273,26 @@
 'is_
-staff
+active
 ', 'is_
-active
+staff
 ', '
@@ -2176,8 +2176,314 @@
 s_basic%0A
+%0A    def get_readonly_fields(self, request, obj=None):%0A        %22%22%22%0A        if user is not superuser, than is_staff field should be readonly%0A        %22%22%22%0A        if obj: #In edit mode%0A            if request.user.is_superuser:%0A                return ()%0A            else:%0A                return ('is_staff',)%0A%0A

fix bug when unpriviliged user updates a user attribute