Example #1
0
class Image(models.Model):
    '''
    Class that defines Image attributes
    '''
    image = models.CloudinaryField('image')
    img_name = models.CharField(max_length=60)
    caption = models.CharField(max_length=100)
    profile = models.ForeignKey(User, on_delete=models.CASCADE, default="")
    likes = models.ManyToManyField(User, related_name="img_post")
    comments = models.CharField(max_length=30)

    def __str__(self):
        return self.img_name

    def save_image(self):
        self.save()

    def delete_image(self):
        self.delete()

    @classmethod
    def update_caption(cls, id, caption):
        cls.objects.filter(id=id).update(caption=caption)
        updated_caption = cls.objects.get(id=id)
        return updated_image

    def like_count(self):
        return self.likes.count()

    @classmethod
    def get_images(cls, profile):
        return cls.objects.filter(profile=profile)
Example #2
0
class Profile(models.Model):
    First_Name = models.CharField(max_length=50)
    Last_Name = models.CharField(max_length=50)
    Email = models.EmailField(max_length=50)
    bio = HTMLField()
    profile_pic = models.CloudinaryField()
    user = models.OneToOneField(User,
                                on_delete=models.CASCADE,
                                primary_key=True)
Example #3
0
class Profile(models.Model):
    '''
    Class that defines the Profile attributes
    '''
    user = models.OneToOneField(User, on_delete=models.CASCADE, default="")
    profile_photo = models.CloudinaryField('image')
    bio = models.CharField(max_length=200, blank=True)

    def __str__(self):
        return self.user.username

    @classmethod
    def search_user(cls, search_term):
        return User.objects.filter(username__icontains=search_term)

    @receiver(post_save, sender=User)
    def create_profile(sender, instance, created, **kwargs):
        if created:
            Profile.objects.create(user=instance)

    @receiver(post_save, sender=User)
    def save_profile(sender, instance, **kwargs):
        instance.profile.save()