Improve admin panel to list projects in user detail panel

remotes/origin/issue/4795/notification_even_they_are_disabled
David Barragán Merino 2016-04-18 15:14:22 +02:00
parent 4174334982
commit 960cbfd55a
1 changed files with 77 additions and 7 deletions

View File

@ -15,12 +15,14 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.utils.translation import ugettext_lazy as _ from django.apps import apps
from django.contrib import admin from django.contrib import admin
from django.contrib.auth.models import Group, Permission from django.contrib.auth.models import Group, Permission
from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin
from django.utils.translation import ugettext_lazy as _
from .models import Role, User from .models import Role, User
from .forms import UserChangeForm, UserCreationForm from .forms import UserChangeForm, UserCreationForm
@ -28,6 +30,75 @@ from .forms import UserChangeForm, UserCreationForm
admin.site.unregister(Group) admin.site.unregister(Group)
## Inlines
class MembershipsInline(admin.TabularInline):
model = apps.get_model("projects", "Membership")
fk_name = "user"
verbose_name = _("Project Member")
verbose_name_plural = _("Project Members")
fields = ("project_id", "project_name", "project_slug", "project_is_private",
"project_owner", "is_admin")
readonly_fields = ("project_id", "project_name", "project_slug", "project_is_private",
"project_owner", "is_admin")
show_change_link = True
extra = 0
def project_id(self, obj):
return obj.project.id if obj.project else None
project_id.short_description = _("id")
def project_name(self, obj):
return obj.project.name if obj.project else None
project_name.short_description = _("name")
def project_slug(self, obj):
return obj.project.slug if obj.project else None
project_slug.short_description = _("slug")
def project_is_private(self, obj):
return obj.project.is_private if obj.project else None
project_is_private.short_description = _("is private")
project_is_private.boolean = True
def project_owner(self, obj):
if obj.project and obj.project.owner:
#return obj.project.owner.get_full_name()
return "{} (@{})".format(obj.project.owner.get_full_name(), obj.project.owner.username)
return None
project_owner.short_description = _("owner")
def has_add_permission(self, *args):
return False
def has_delete_permission(self, *args):
return False
class OwnedProjectsInline(admin.TabularInline):
model = apps.get_model("projects", "Project")
fk_name = "owner"
verbose_name = _("Project Ownership")
verbose_name_plural = _("Project Ownerships")
fields = ("id", "name", "slug", "is_private")
readonly_fields = ("id", "name", "slug", "is_private")
show_change_link = True
extra = 0
def has_add_permission(self, *args):
return False
def has_delete_permission(self, *args):
return False
class RoleInline(admin.TabularInline):
model = Role
extra = 0
## Admin panels
class RoleAdmin(admin.ModelAdmin): class RoleAdmin(admin.ModelAdmin):
list_display = ["name"] list_display = ["name"]
filter_horizontal = ("permissions",) filter_horizontal = ("permissions",)
@ -42,9 +113,6 @@ class RoleAdmin(admin.ModelAdmin):
db_field, request=request, **kwargs) db_field, request=request, **kwargs)
# admin.site.register(Role, RoleAdmin)
class UserAdmin(DjangoUserAdmin): class UserAdmin(DjangoUserAdmin):
fieldsets = ( fieldsets = (
(None, {"fields": ("username", "password")}), (None, {"fields": ("username", "password")}),
@ -63,10 +131,12 @@ class UserAdmin(DjangoUserAdmin):
search_fields = ("username", "full_name", "email") search_fields = ("username", "full_name", "email")
ordering = ("username",) ordering = ("username",)
filter_horizontal = () filter_horizontal = ()
inlines = [
OwnedProjectsInline,
MembershipsInline
]
class RoleInline(admin.TabularInline):
model = Role
extra = 0
admin.site.register(User, UserAdmin) admin.site.register(User, UserAdmin)