From 78886c8c9ca8f4474c7b5728af8d126db9c41b17 Mon Sep 17 00:00:00 2001 From: Reckless_Satoshi Date: Tue, 4 Jan 2022 07:58:10 -0800 Subject: [PATCH] Rework admin pannel and extended users --- api/admin.py | 30 +++++++++++++++++++++++++----- api/models.py | 23 ++++++++++++++++------- api/views.py | 3 ++- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/api/admin.py b/api/admin.py index 5bf592fe..dd761dd2 100644 --- a/api/admin.py +++ b/api/admin.py @@ -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) \ No newline at end of file + list_display = ('avatar_tag','user','id','total_ratings','avg_rating','num_disputes','lost_disputes') + list_display_links =['user'] + readonly_fields = ['avatar_tag'] + pass \ No newline at end of file diff --git a/api/models.py b/api/models.py index cde408f1..150c4c35 100644 --- a/api/models.py +++ b/api/models.py @@ -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. \ No newline at end of file + # 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('' % self.get_avatar()) diff --git a/api/views.py b/api/views.py index b4883db9..e7accd36 100644 --- a/api/views.py +++ b/api/views.py @@ -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