Exemplo n.º 1
0
    def add_favorite(self, sharedfile):
        """
        Add a sharedfile as a favorite for the user.

        Will return False when one can't favorite a shared file:
         - it's deleted
         - it belongs to current user
         - already favorited

        Will return True if favoriting succeeds.
        """
        if sharedfile.deleted:
            return False
        if sharedfile.user_id == self.id:
            return False

        existing_favorite = models.favorite.Favorite.get('user_id = %s and sharedfile_id=%s' % (self.id, sharedfile.id))
        if existing_favorite:
            if existing_favorite.deleted == 0:
                return False
            existing_favorite.deleted = 0
            existing_favorite.save()
        else:
            favorite = models.favorite.Favorite(user_id=self.id, sharedfile_id=sharedfile.id)
            try:
                favorite.save()
            # This can only happen in a race condition, when request gets
            # sent twice (like during double click).  We just assume it worked
            # the first time and return a True.
            except IntegrityError:
                return True
            notification.Notification.new_favorite(self, sharedfile)

        calculate_likes.delay_or_run(sharedfile.id)
        return True
Exemplo n.º 2
0
    def remove_favorite(self, sharedfile):
        """
        Remove a favorite. If there is no favorite or if it's already been remove, return False.
        """
        existing_favorite = models.favorite.Favorite.get('user_id= %s and sharedfile_id = %s and deleted=0' % (self.id, sharedfile.id))
        if not existing_favorite:
            return False
        if existing_favorite.deleted:
            return False
        existing_favorite.deleted = 1
        existing_favorite.save()

        calculate_likes.delay_or_run(sharedfile.id)
        return True