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.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
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)
class OrderAdmin(admin.ModelAdmin):
list_display = ('id','type','maker','taker','status','amount','currency','created_at','expires_at')
@ -10,7 +30,7 @@ class OrderAdmin(admin.ModelAdmin):
@admin.register(Profile)
class UserProfileAdmin(admin.ModelAdmin):
list_display = ('user','id','total_ratings','avg_rating','num_disputes','lost_disputes','avatar')
pass
admin.site.unregister(Group)
list_display = ('avatar_tag','user','id','total_ratings','avg_rating','num_disputes','lost_disputes')
list_display_links =['user']
readonly_fields = ['avatar_tag']
pass

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.dispatch import receiver
from django.utils.html import mark_safe
from pathlib import Path
#############################
@ -82,14 +84,14 @@ class Profile(models.Model):
# Ratings stored as a comma separated integer list
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
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
num_disputes = models.PositiveIntegerField(null=False, default=0)
lost_disputes = models.PositiveIntegerField(null=False, default=0)
# 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)
def create_user_profile(sender, instance, created, **kwargs):
@ -99,9 +101,16 @@ class Profile(models.Model):
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
def __str__(self):
return self.user.username
# Move avatar handling from views.py to here
# @receiver(pre_delete, sender=User)
# def _mymodel_delete(sender, instance, **kwargs):
# avatar_file = Path('frontend', instance.profile.avatar)
# avatar_file.unlink() # Unsafe if avatar does not exist.
# to display avatars in admin panel
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

@ -200,11 +200,12 @@ class BookView(APIView):
def get(self,request, format=None):
currency = request.GET.get('currency')
type = request.GET.get('type')
type = request.GET.get('type')
queryset = Order.objects.filter(currency=currency, type=type, status=0) # TODO status = 1 for orders that are Public
if len(queryset)== 0:
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 = []
for order in queryset:
data = OrderSerializer(order).data