replace ImageFields to URLFields in User model, refactored serializers that included this fields
This commit is contained in:
parent
e4eedd8af2
commit
1e650b9b1f
|
|
@ -15,11 +15,13 @@ class UserAdmin(BaseUserAdmin):
|
|||
list_filter = ('is_active', 'is_staff', 'is_superuser', 'email_confirmed',
|
||||
'groups',)
|
||||
search_fields = ('email', 'first_name', 'last_name')
|
||||
readonly_fields = ('last_login', 'date_joined',)
|
||||
readonly_fields = ('last_login', 'date_joined', 'image_preview', 'cropped_image_preview')
|
||||
fieldsets = (
|
||||
(None, {'fields': ('email', 'password',)}),
|
||||
(_('Personal info'), {
|
||||
'fields': ('username', 'first_name', 'last_name', 'image')}),
|
||||
'fields': ('username', 'first_name', 'last_name',
|
||||
'image_url', 'image_preview',
|
||||
'cropped_image_url', 'cropped_image_preview',)}),
|
||||
(_('Subscription'), {
|
||||
'fields': (
|
||||
'newsletter',
|
||||
|
|
@ -46,3 +48,15 @@ class UserAdmin(BaseUserAdmin):
|
|||
return obj.get_short_name()
|
||||
|
||||
short_name.short_description = _('Name')
|
||||
|
||||
def image_preview(self, obj):
|
||||
"""Get user image preview"""
|
||||
return obj.image_tag
|
||||
|
||||
image_preview.short_description = 'Image preview'
|
||||
|
||||
def cropped_image_preview(self, obj):
|
||||
"""Get user cropped image preview"""
|
||||
return obj.cropped_image_tag
|
||||
|
||||
cropped_image_preview.short_description = 'Cropped image preview'
|
||||
|
|
|
|||
21
apps/account/migrations/0007_auto_20190912_1323.py
Normal file
21
apps/account/migrations/0007_auto_20190912_1323.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
# Generated by Django 2.2.4 on 2019-09-12 13:23
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('account', '0006_delete_resetpasswordtoken'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='user',
|
||||
name='cropped_image',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='user',
|
||||
name='image',
|
||||
),
|
||||
]
|
||||
23
apps/account/migrations/0008_auto_20190912_1325.py
Normal file
23
apps/account/migrations/0008_auto_20190912_1325.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
# Generated by Django 2.2.4 on 2019-09-12 13:25
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('account', '0007_auto_20190912_1323'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='user',
|
||||
name='cropped_image_url',
|
||||
field=models.URLField(blank=True, default=None, null=True, verbose_name='Cropped image URL path'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='user',
|
||||
name='image_url',
|
||||
field=models.URLField(blank=True, default=None, null=True, verbose_name='Image URL path'),
|
||||
),
|
||||
]
|
||||
|
|
@ -7,13 +7,12 @@ from django.db import models
|
|||
from django.template.loader import render_to_string
|
||||
from django.utils import timezone
|
||||
from django.utils.encoding import force_bytes
|
||||
from django.utils.html import mark_safe
|
||||
from django.utils.http import urlsafe_base64_encode
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from easy_thumbnails.fields import ThumbnailerImageField
|
||||
from rest_framework.authtoken.models import Token
|
||||
|
||||
from authorization.models import Application
|
||||
from utils.methods import image_path
|
||||
from utils.models import GMTokenGenerator
|
||||
from utils.models import ImageMixin, ProjectBaseMixin, PlatformMixin
|
||||
from utils.tokens import GMRefreshToken
|
||||
|
|
@ -53,11 +52,12 @@ class UserQuerySet(models.QuerySet):
|
|||
oauth2_provider_refreshtoken__expires__gt=timezone.now())
|
||||
|
||||
|
||||
class User(ImageMixin, AbstractUser):
|
||||
class User(AbstractUser):
|
||||
"""Base user model."""
|
||||
cropped_image = ThumbnailerImageField(upload_to=image_path,
|
||||
blank=True, null=True, default=None,
|
||||
verbose_name=_('Crop image'))
|
||||
image_url = models.URLField(verbose_name=_('Image URL path'),
|
||||
blank=True, null=True, default=None)
|
||||
cropped_image_url = models.URLField(verbose_name=_('Cropped image URL path'),
|
||||
blank=True, null=True, default=None)
|
||||
email = models.EmailField(_('email address'), blank=True,
|
||||
null=True, default=None)
|
||||
email_confirmed = models.BooleanField(_('email status'), default=False)
|
||||
|
|
@ -161,6 +161,14 @@ class User(ImageMixin, AbstractUser):
|
|||
'uidb64': self.get_user_uidb64,
|
||||
'site_name': settings.SITE_NAME}
|
||||
|
||||
@property
|
||||
def image_tag(self):
|
||||
return mark_safe(f'<img src="{self.image_url}" />')
|
||||
|
||||
@property
|
||||
def cropped_image_tag(self):
|
||||
return mark_safe(f'<img src="{self.cropped_image_url}" />')
|
||||
|
||||
def reset_password_template(self, country_code):
|
||||
"""Get reset password template"""
|
||||
context = {'token': self.reset_password_token,
|
||||
|
|
|
|||
|
|
@ -24,8 +24,8 @@ class UserSerializer(serializers.ModelSerializer):
|
|||
required=False)
|
||||
first_name = serializers.CharField(required=False, write_only=True)
|
||||
last_name = serializers.CharField(required=False, write_only=True)
|
||||
image = serializers.ImageField(required=False)
|
||||
cropped_image = serializers.ImageField(required=False)
|
||||
image_url = serializers.URLField(required=False)
|
||||
cropped_image_url = serializers.URLField(required=False)
|
||||
email = serializers.EmailField(
|
||||
validators=(rest_validators.UniqueValidator(queryset=models.User.objects.all()),),
|
||||
required=False)
|
||||
|
|
@ -38,8 +38,8 @@ class UserSerializer(serializers.ModelSerializer):
|
|||
'first_name',
|
||||
'last_name',
|
||||
'fullname',
|
||||
'cropped_image',
|
||||
'image',
|
||||
'cropped_image_url',
|
||||
'image_url',
|
||||
'email',
|
||||
'email_confirmed',
|
||||
'newsletter',
|
||||
|
|
|
|||
|
|
@ -9,7 +9,8 @@ class CommentSerializer(serializers.ModelSerializer):
|
|||
nickname = serializers.CharField(read_only=True,
|
||||
source='user.username')
|
||||
is_mine = serializers.BooleanField(read_only=True)
|
||||
profile_pic = serializers.SerializerMethodField()
|
||||
profile_pic = serializers.URLField(read_only=True,
|
||||
source='user.cropped_image_url')
|
||||
|
||||
class Meta:
|
||||
"""Serializer for model Comment"""
|
||||
|
|
@ -24,7 +25,3 @@ class CommentSerializer(serializers.ModelSerializer):
|
|||
'nickname',
|
||||
'profile_pic'
|
||||
]
|
||||
|
||||
def get_profile_pic(self, obj):
|
||||
"""Get profile picture URL"""
|
||||
return obj.user.get_full_image_url(request=self.context.get('request'))
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ from . import models, serializers
|
|||
|
||||
class ImageUploadView(generics.CreateAPIView):
|
||||
"""Upload image to gallery"""
|
||||
permission_classes = (IsAuthenticatedAndTokenIsValid, )
|
||||
model = models.Image
|
||||
queryset = models.Image.objects.all()
|
||||
serializer_class = serializers.ImageSerializer
|
||||
permission_classes = (IsAuthenticatedAndTokenIsValid, )
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user