From 6a2b32f00ccc8bf2f3cc57ab6689491539f969ef Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 22 Mar 2021 11:17:48 +0000 Subject: [PATCH 1/4] fixed company name search in custom user admin --- core/admin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/admin.py b/core/admin.py index 3e21527..72bb44d 100644 --- a/core/admin.py +++ b/core/admin.py @@ -8,6 +8,6 @@ from . import models class UserAdmin(admin.ModelAdmin): list_display = ('email', 'full_name', 'role', 'company', 'email_verified', 'is_active', 'is_staff', 'created', 'last_visit') list_filter = ('is_active', 'is_staff', 'email_verified') - search_fields = ('email', 'full_name', 'company') + search_fields = ('email', 'full_name', 'company__short_name') admin.site.register(models.CustomUser, UserAdmin) From a13d6d1f364a17ec00c88ec59b4095a6f9091e10 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 25 Mar 2021 10:08:22 +0000 Subject: [PATCH 2/4] changes to core.admin, useradmin still not showing password correctly --- core/admin.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/core/admin.py b/core/admin.py index 72bb44d..f13f27a 100644 --- a/core/admin.py +++ b/core/admin.py @@ -6,8 +6,22 @@ from . import models class UserAdmin(admin.ModelAdmin): + model = models.CustomUser + list_display = ('email', 'full_name', 'role', 'company', 'email_verified', 'is_active', 'is_staff', 'created', 'last_visit') list_filter = ('is_active', 'is_staff', 'email_verified') search_fields = ('email', 'full_name', 'company__short_name') + fieldsets = ( + (None, {'fields': ('email', 'password','full_name', 'role', 'notify','provider','email_verified','company','is_active')}), + ('Permissions', {'fields': ('is_staff', 'groups')}), + ) + + add_fieldsets = ( + (None, { + 'classes': ('wide',), + 'fields': ('email', 'password1', 'password2', 'is_staff', 'is_active')} + ), + ) + admin.site.register(models.CustomUser, UserAdmin) From 86aaa0ef2e4e7e61e03a1e94d717e66605bbfaa1 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 25 Mar 2021 10:16:08 +0000 Subject: [PATCH 3/4] admin user forms handle passwords correctly --- core/admin.py | 10 +++++++++- core/forms.py | 27 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 core/forms.py diff --git a/core/admin.py b/core/admin.py index f13f27a..11522ac 100644 --- a/core/admin.py +++ b/core/admin.py @@ -1,12 +1,20 @@ from django.contrib import admin +from .forms import CustomUserChangeForm, CustomUserCreationForm from . import models +from .forms import CustomUserChangeForm, CustomUserCreationForm # Register your models here. +from django.contrib.auth import get_user_model + +User = get_user_model() + class UserAdmin(admin.ModelAdmin): - model = models.CustomUser + add_form = CustomUserCreationForm + form = CustomUserChangeForm + model = User list_display = ('email', 'full_name', 'role', 'company', 'email_verified', 'is_active', 'is_staff', 'created', 'last_visit') list_filter = ('is_active', 'is_staff', 'email_verified') diff --git a/core/forms.py b/core/forms.py new file mode 100644 index 0000000..ddf7731 --- /dev/null +++ b/core/forms.py @@ -0,0 +1,27 @@ +from django import forms +from django.contrib.auth.forms import UserChangeForm, UserCreationForm +from django.forms import ModelForm +from django.contrib.auth import get_user_model + +User = get_user_model() + + +class CustomUserCreationForm(UserCreationForm): + + class Meta(UserCreationForm.Meta): + + model = User + + UserCreationForm.Meta.fields = ('email',) + fields = UserCreationForm.Meta.fields + ('full_name','role', 'notify', 'provider', 'email_verified', 'company', 'is_active', 'is_staff') + UserCreationForm.Meta.fields = ('password1','password2',) + + +class CustomUserChangeForm(UserChangeForm): + + class Meta(UserChangeForm.Meta): + model = User + UserChangeForm.Meta.fields = ('email',) + fields = UserChangeForm.Meta.fields + ('full_name','role', 'notify', 'provider', 'email_verified', 'company', 'is_active', 'is_staff') + UserChangeForm.Meta.fields = ('password1','password2',) + From 2629292c64375503119eba3d63f3b47dd170159f Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 26 Mar 2021 10:16:19 +0000 Subject: [PATCH 4/4] added product id to image path --- core/management/commands/addtestdata.py | 4 ++-- products/models.py | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/core/management/commands/addtestdata.py b/core/management/commands/addtestdata.py index 35d7bf9..3b91f22 100644 --- a/core/management/commands/addtestdata.py +++ b/core/management/commands/addtestdata.py @@ -35,11 +35,11 @@ class Command(BaseCommand): def handle(self, *args, **kwargs): print("Create fake data to populate database\n") - + ''' print("Deleting existing Product and Company instances") Product.objects.all().delete() Company.objects.all().delete() - + ''' # start faker fake = Faker() Faker.seed(0) diff --git a/products/models.py b/products/models.py index 7ee89fd..196cc8c 100644 --- a/products/models.py +++ b/products/models.py @@ -34,6 +34,12 @@ class AttributeTag(TagTreeModel): # autocomplete_view = 'myapp.views.hobbies_autocomplete' +def product_image_path(instance, filename): + """Add the instance's id to the path + """ + return f"products/{instance.id}/{filename}" + + class Product(models.Model): SYNCHRONIZED = 'SYNCHRONIZED' @@ -50,7 +56,7 @@ class Product(models.Model): sku = models.CharField('Referencia de producto', max_length=255, null=True, blank=True ) name = models.CharField('Nombre del producto', max_length=500, null=True, blank=True) description = models.TextField('Descripción', null=True, blank=True) - image = models.ImageField('Imagen del producto', upload_to='products/', null=True, blank=True) + image = models.ImageField('Imagen del producto', upload_to=product_image_path, null=True, blank=True) url = models.URLField('URL del producto', null=True, blank=True, max_length=1000) price = models.DecimalField('Precio', max_digits=10, decimal_places=2, null=True, blank=True) shipping_cost = models.DecimalField('Gastos de envío', max_digits=10, decimal_places=2, null=True, blank=True)