File size: 8,063 Bytes
ef287e1
 
 
4e138bb
 
 
 
 
 
 
 
 
 
ef287e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.utils.html import format_html
from .models import User, Product, Transaction, Budget, Ad, AIInsight


@admin.register(AIInsight)
class AIInsightAdmin(admin.ModelAdmin):
    """Administration des insights IA"""
    list_display = ['user', 'created_at', 'context_hash']
    list_filter = ['created_at', 'user']
    search_fields = ['user__email', 'content']
    readonly_fields = ['created_at', 'context_hash']


@admin.register(User)
class UserAdmin(BaseUserAdmin):
    """Administration des utilisateurs"""
    
    list_display = [
        'email', 'first_name', 'last_name', 'account_type',
        'is_premium', 'is_staff', 'date_joined'
    ]
    list_filter = ['account_type', 'is_premium', 'is_staff', 'is_active']
    search_fields = ['email', 'first_name', 'last_name', 'business_name']
    ordering = ['-date_joined']
    
    fieldsets = (
        ('Informations de connexion', {
            'fields': ('email', 'password')
        }),
        ('Informations personnelles', {
            'fields': ('first_name', 'last_name', 'phone_number', 'avatar')
        }),
        ('Type de compte', {
            'fields': ('account_type', 'is_premium')
        }),
        ('Informations Business', {
            'fields': ('business_name', 'sector', 'location', 'ifu', 'business_logo'),
            'classes': ('collapse',)
        }),
        ('Paramètres', {
            'fields': ('currency', 'language', 'dark_mode')
        }),
        ('Permissions', {
            'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions'),
            'classes': ('collapse',)
        }),
        ('Dates importantes', {
            'fields': ('last_login', 'date_joined', 'business_agreed_at'),
            'classes': ('collapse',)
        }),
    )
    
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'password1', 'password2', 'first_name', 'last_name', 'account_type'),
        }),
    )


@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    """Administration des produits"""
    
    list_display = [
        'name', 'user', 'price', 'unit', 'category',
        'stock_status', 'image_preview', 'created_at'
    ]
    list_filter = ['category', 'stock_status', 'created_at']
    search_fields = ['name', 'description', 'user__email']
    ordering = ['-created_at']
    readonly_fields = ['created_at', 'updated_at', 'image_preview']
    
    fieldsets = (
        ('Informations de base', {
            'fields': ('user', 'name', 'description')
        }),
        ('Tarification', {
            'fields': ('price', 'unit')
        }),
        ('Catégorisation', {
            'fields': ('category', 'stock_status')
        }),
        ('Image', {
            'fields': ('image', 'image_preview')
        }),
        ('Dates', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )
    
    def image_preview(self, obj):
        if obj.image:
            return format_html(
                '<img src="{}" style="max-height: 100px; max-width: 100px;" />',
                obj.image.url
            )
        return "Pas d'image"
    image_preview.short_description = "Aperçu"


@admin.register(Transaction)
class TransactionAdmin(admin.ModelAdmin):
    """Administration des transactions"""
    
    list_display = [
        'name', 'user', 'amount', 'currency', 'type',
        'category', 'date', 'created_at'
    ]
    list_filter = ['type', 'category', 'date', 'created_at']
    search_fields = ['name', 'user__email', 'category']
    ordering = ['-date']
    readonly_fields = ['created_at', 'updated_at']
    date_hierarchy = 'date'
    
    fieldsets = (
        ('Utilisateur', {
            'fields': ('user',)
        }),
        ('Transaction', {
            'fields': ('name', 'amount', 'currency', 'type', 'category', 'date')
        }),
        ('Dates système', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )
    
    def get_queryset(self, request):
        qs = super().get_queryset(request)
        return qs.select_related('user')


@admin.register(Budget)
class BudgetAdmin(admin.ModelAdmin):
    """Administration des budgets"""
    
    list_display = [
        'category', 'user', 'limit', 'spent_display',
        'percentage_display', 'color_preview', 'created_at'
    ]
    list_filter = ['created_at']
    search_fields = ['category', 'user__email']
    ordering = ['-created_at']
    readonly_fields = ['created_at', 'updated_at', 'spent_display', 'percentage_display']
    
    fieldsets = (
        ('Utilisateur', {
            'fields': ('user',)
        }),
        ('Budget', {
            'fields': ('category', 'limit', 'color')
        }),
        ('Statistiques', {
            'fields': ('spent_display', 'percentage_display'),
            'classes': ('collapse',)
        }),
        ('Dates', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )
    
    def spent_display(self, obj):
        return f"{obj.get_spent_amount()} FCFA"
    spent_display.short_description = "Montant dépensé"
    
    def percentage_display(self, obj):
        spent = obj.get_spent_amount()
        if obj.limit > 0:
            percentage = (spent / obj.limit) * 100
            color = 'green' if percentage < 80 else 'orange' if percentage < 100 else 'red'
            return format_html(
                '<span style="color: {};">{}</span>',
                color, f'{percentage:.1f}%'
            )
        return "0%"
    percentage_display.short_description = "Pourcentage"
    
    def color_preview(self, obj):
        return format_html(
            '<div style="width: 30px; height: 30px; background-color: {}; border: 1px solid #ccc;"></div>',
            obj.color
        )
    color_preview.short_description = "Couleur"


@admin.register(Ad)
class AdAdmin(admin.ModelAdmin):
    """Administration des annonces"""
    
    list_display = [
        'product_name', 'owner_name', 'location',
        'is_verified', 'image_preview', 'created_at'
    ]
    list_filter = ['is_verified', 'created_at']
    search_fields = ['product_name', 'owner_name', 'description', 'location']
    ordering = ['-created_at']
    readonly_fields = ['created_at', 'updated_at', 'image_preview']
    
    fieldsets = (
        ('Informations de base', {
            'fields': ('user', 'product_name', 'owner_name', 'description')
        }),
        ('Localisation & Contact', {
            'fields': ('location', 'whatsapp', 'website')
        }),
        ('Image', {
            'fields': ('image', 'image_preview')
        }),
        ('Modération', {
            'fields': ('is_verified',)
        }),
        ('Dates', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )
    
    actions = ['verify_ads', 'unverify_ads']
    
    def verify_ads(self, request, queryset):
        count = queryset.update(is_verified=True)
        self.message_user(request, f"{count} annonce(s) vérifiée(s).")
    verify_ads.short_description = "Vérifier les annonces sélectionnées"
    
    def unverify_ads(self, request, queryset):
        count = queryset.update(is_verified=False)
        self.message_user(request, f"{count} annonce(s) dé-vérifiée(s).")
    unverify_ads.short_description = "Retirer la vérification"
    
    def image_preview(self, obj):
        if obj.image:
            return format_html(
                '<img src="{}" style="max-height: 100px; max-width: 100px;" />',
                obj.image.url
            )
        return "Pas d'image"
    image_preview.short_description = "Aperçu"


# Personnalisation du site admin
admin.site.site_header = "Akompta AI Administration"
admin.site.site_title = "Akompta Admin"
admin.site.index_title = "Bienvenue sur l'administration Akompta"