Rework admin pannel and extended users

This commit is contained in:
Reckless_Satoshi 2022-01-04 07:58:10 -08:00
parent 30dfd09f58
commit 78886c8c9c
No known key found for this signature in database
GPG Key ID: 9C4585B561315571
3 changed files with 43 additions and 13 deletions

View File

@ -1,7 +1,27 @@
from django.contrib import admin from django.contrib import admin
from django.contrib.auth.models import Group from django.db import models
from django.contrib.auth.models import Group, User
from django.contrib.auth.admin import UserAdmin
from .models import Order, Profile from .models import Order, Profile
admin.site.unregister(Group)
admin.site.unregister(User)
class ProfileInline(admin.StackedInline):
model = Profile
can_delete = False
fields = ('avatar_tag',)
readonly_fields = ['avatar_tag']
# extended users with avatars
@admin.register(User)
class EUserAdmin(UserAdmin):
inlines = [ProfileInline]
list_display = ('avatar_tag',) + UserAdmin.list_display
list_display_links = ['username']
def avatar_tag(self, obj):
return obj.profile.avatar_tag()
@admin.register(Order) @admin.register(Order)
class OrderAdmin(admin.ModelAdmin): class OrderAdmin(admin.ModelAdmin):
list_display = ('id','type','maker','taker','status','amount','currency','created_at','expires_at') list_display = ('id','type','maker','taker','status','amount','currency','created_at','expires_at')
@ -10,7 +30,7 @@ class OrderAdmin(admin.ModelAdmin):
@admin.register(Profile) @admin.register(Profile)
class UserProfileAdmin(admin.ModelAdmin): class UserProfileAdmin(admin.ModelAdmin):
list_display = ('user','id','total_ratings','avg_rating','num_disputes','lost_disputes','avatar') list_display = ('avatar_tag','user','id','total_ratings','avg_rating','num_disputes','lost_disputes')
list_display_links =['user']
readonly_fields = ['avatar_tag']
pass pass
admin.site.unregister(Group)

View File

@ -4,6 +4,8 @@ from django.core.validators import MaxValueValidator, MinValueValidator, validat
from django.db.models.signals import post_save, pre_delete from django.db.models.signals import post_save, pre_delete
from django.dispatch import receiver from django.dispatch import receiver
from django.utils.html import mark_safe
from pathlib import Path from pathlib import Path
############################# #############################
@ -82,14 +84,14 @@ class Profile(models.Model):
# Ratings stored as a comma separated integer list # Ratings stored as a comma separated integer list
total_ratings = models.PositiveIntegerField(null=False, default=0) total_ratings = models.PositiveIntegerField(null=False, default=0)
latest_ratings = models.CharField(max_length=999, null=True, default=None, validators=[validate_comma_separated_integer_list]) # Will only store latest ratings latest_ratings = models.CharField(max_length=999, null=True, default=None, validators=[validate_comma_separated_integer_list]) # Will only store latest ratings
avg_rating = models.DecimalField(max_digits=3, decimal_places=1, default=None, null=True, validators=[MinValueValidator(0), MaxValueValidator(100)]) avg_rating = models.DecimalField(max_digits=4, decimal_places=1, default=None, null=True, validators=[MinValueValidator(0), MaxValueValidator(100)])
# Disputes # Disputes
num_disputes = models.PositiveIntegerField(null=False, default=0) num_disputes = models.PositiveIntegerField(null=False, default=0)
lost_disputes = models.PositiveIntegerField(null=False, default=0) lost_disputes = models.PositiveIntegerField(null=False, default=0)
# RoboHash # RoboHash
avatar = models.ImageField(default="static/assets/avatars/unknown.png") avatar = models.ImageField(default="static/assets/avatars/unknown.png", verbose_name='Avatar')
@receiver(post_save, sender=User) @receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs): def create_user_profile(sender, instance, created, **kwargs):
@ -100,8 +102,15 @@ class Profile(models.Model):
def save_user_profile(sender, instance, **kwargs): def save_user_profile(sender, instance, **kwargs):
instance.profile.save() instance.profile.save()
# Move avatar handling from views.py to here def __str__(self):
# @receiver(pre_delete, sender=User) return self.user.username
# def _mymodel_delete(sender, instance, **kwargs):
# avatar_file = Path('frontend', instance.profile.avatar) # to display avatars in admin panel
# avatar_file.unlink() # Unsafe if avatar does not exist. def get_avatar(self):
if not self.avatar:
return 'static/assets/avatars/unknown.png'
return self.avatar.url
# method to create a fake table field in read only mode
def avatar_tag(self):
return mark_safe('<img src="%s" width="50" height="50" />' % self.get_avatar())

View File

@ -205,6 +205,7 @@ class BookView(APIView):
if len(queryset)== 0: if len(queryset)== 0:
return Response({'not_found':'No orders found, be the first to make one'}, status=status.HTTP_404_NOT_FOUND) return Response({'not_found':'No orders found, be the first to make one'}, status=status.HTTP_404_NOT_FOUND)
queryset = queryset.order_by('created_at')
book_data = [] book_data = []
for order in queryset: for order in queryset:
data = OrderSerializer(order).data data = OrderSerializer(order).data