Django Rest Framework different image size for different account tiers 3 build in and possibility to make new

74 Views Asked by At
#a link to a thumbnail that's 200px in height - Basic
#a link to a thumbnail that's 400px in height - Basic and Intermediate
#a link to the originally uploaded image - Basic, Intermediate and Premium
class Profile(models.Model):

        MEMBERSHIP = (
            ('BASIC', 'Basic'),
            ('PREMIUM', 'Premium'),
            ('ENTERPRISE', 'Enterprise')
        )

        user = models.OneToOneField(User, on_delete=models.CASCADE)
        membership = models.CharField(max_length=10, choices=MEMBERSHIP, default='BASIC')

        def __str__(self):
            return f'{self.user.username} {self.membership} Profile'

The only way I know how to do 3 build in tiers are like above.

I don't know how to do more memberships with different image sizes that can be added from admin panel. I want to have them as one model and add it as necessary to make a new user.

2

There are 2 best solutions below

0
Mahmoud Nasser On

you could create a new model called Membership:

class Membership(models.Model):
  name = models.CharField(max_length=10, default='BASIC')
  thumbnail_max_width = models.IntgerField()
  thumbnail_max_height = models.IntgerField()


 class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    membership = models.ForigenKey(Membership)

    def __str__(self):
        return f'{self.user.username} {self.membership} Profile'

now you could create all the membership types you want from the admin panel

0
Iqbal Hussain On

Actually, to allow the user for different image size for different paid level you can add two image sizes for basic and premium.

class Profile(models.Model):

    MEMBERSHIP = (
        ('BASIC', 'Basic'),
        ('PREMIUM', 'Premium'),
        ('ENTERPRISE', 'Enterprise')
    )

    user = models.OneToOneField(User, on_delete=models.CASCADE)
    membership = models.CharField(max_length=10, choices=MEMBERSHIP, default='BASIC')
    basic_thumbnail_height = models.PositiveIntegerField(default=200)
    premium_thumbnail_height = models.PositiveIntegerField(default=400)

    def __str__(self):
        return f'{self.user.username} {self.membership} Profile'

    def thumbnail_link(self):
        if self.membership == 'BASIC':
            return f'https://something.com/thumbnail/{self.user.id}/{self.basic_thumbnail_height}'
        elif self.membership == 'PREMIUM':
            return f'https://something.com/thumbnail/{self.user.id}/{self.premium_thumbnail_height}'
        else:
            return f'https://something.com/original/{self.user.id}'