|
@@ -1,12 +1,13 @@
|
|
|
from django.contrib import admin
|
|
|
-from django.contrib.auth.models import User, Group
|
|
|
from django.contrib.contenttypes.admin import GenericStackedInline, GenericTabularInline
|
|
|
from django.db.models import Q
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
+from django.contrib.auth.models import User as AuthUser, Group
|
|
|
from django.contrib.auth.admin import UserAdmin as AuthUserAdmin
|
|
|
+from django.http import HttpResponseRedirect
|
|
|
|
|
|
-from .forms import AdhesionForm
|
|
|
-from .models import ProxyUser, Corporation, Adhesion
|
|
|
+from .forms import AdhesionForm, UserCreationForm
|
|
|
+from .models import User, Corporation, Adhesion
|
|
|
from accounts.models import Profile
|
|
|
from services.models import Service
|
|
|
from banking.admin import PaymentInline
|
|
@@ -71,27 +72,41 @@ class UserIsAdherentFilter(admin.SimpleListFilter):
|
|
|
class UserAdmin(AuthUserAdmin):
|
|
|
list_display = AuthUserAdmin.list_display + ('adherent_id',)
|
|
|
list_filter = (AuthUserAdmin.list_filter[2], UserIsAdherentFilter) # [2] = actif
|
|
|
- inlines = (ProfileInline, AdhesionInline,)
|
|
|
|
|
|
def adherent_id(self, user):
|
|
|
- adherent = user.profile.adhesion
|
|
|
+ adherent = user.adhesion
|
|
|
if adherent:
|
|
|
return adherent.id
|
|
|
adherent_id.short_description = 'Numéro d’adhérent'
|
|
|
|
|
|
+ def get_form(self, request, obj=None, **kwargs):
|
|
|
+ if obj:
|
|
|
+ UserAdmin.inlines = (ProfileInline, AdhesionInline,)
|
|
|
+ return super().get_form(request, obj, **kwargs)
|
|
|
+ else:
|
|
|
+ UserAdmin.inlines = (AdhesionInline,)
|
|
|
+ return UserCreationForm
|
|
|
+
|
|
|
def get_fieldsets(self, request, obj=None):
|
|
|
- return (
|
|
|
- AuthUserAdmin.fieldsets[0],
|
|
|
- AuthUserAdmin.fieldsets[1],
|
|
|
- (AuthUserAdmin.fieldsets[2][0], {
|
|
|
- 'classes': ('collapse',),
|
|
|
- 'fields': ('is_active', 'is_staff', 'is_superuser',), # removing groups and user_permissions
|
|
|
- }),
|
|
|
- (AuthUserAdmin.fieldsets[3][0], {
|
|
|
- 'classes': ('collapse',),
|
|
|
- 'fields': AuthUserAdmin.fieldsets[3][1]['fields'],
|
|
|
- }),
|
|
|
- )
|
|
|
+ if obj:
|
|
|
+ return (
|
|
|
+ AuthUserAdmin.fieldsets[0],
|
|
|
+ AuthUserAdmin.fieldsets[1],
|
|
|
+ (AuthUserAdmin.fieldsets[2][0], {
|
|
|
+ 'classes': ('collapse',),
|
|
|
+ 'fields': ('is_active', 'is_staff', 'is_superuser',), # removing groups and user_permissions
|
|
|
+ }),
|
|
|
+ (AuthUserAdmin.fieldsets[3][0], {
|
|
|
+ 'classes': ('collapse',),
|
|
|
+ 'fields': AuthUserAdmin.fieldsets[3][1]['fields'],
|
|
|
+ }),
|
|
|
+ )
|
|
|
+ else:
|
|
|
+ return (
|
|
|
+ (None, {
|
|
|
+ 'fields': ('first_name', 'last_name', 'username',),
|
|
|
+ }),
|
|
|
+ )
|
|
|
|
|
|
def get_actions(self, request):
|
|
|
actions = super().get_actions(request)
|
|
@@ -100,7 +115,7 @@ class UserAdmin(AuthUserAdmin):
|
|
|
return actions
|
|
|
|
|
|
def has_delete_permission(self, request, obj=None):
|
|
|
- return obj and not obj.profile.adhesion
|
|
|
+ return obj and not obj.adhesion
|
|
|
|
|
|
|
|
|
class AdherentTypeFilter(admin.SimpleListFilter):
|
|
@@ -185,8 +200,8 @@ class CorporationAdmin(admin.ModelAdmin):
|
|
|
return obj and not obj.adhesion
|
|
|
|
|
|
|
|
|
-admin.site.unregister(User)
|
|
|
+admin.site.unregister(AuthUser)
|
|
|
admin.site.unregister(Group)
|
|
|
-admin.site.register(ProxyUser, UserAdmin)
|
|
|
+admin.site.register(User, UserAdmin)
|
|
|
admin.site.register(Corporation, CorporationAdmin)
|
|
|
admin.site.register(Adhesion, AdhesionAdmin)
|