def build_short_dict(self): """ Build the short dict. If everything is working, this method should not make a single database call.""" item_cache_key = "_%s_dict_%s_" % ( self.content_type.model_class().item_name, "%s") object_dict = get_dictionary_with_cache_priority( item_cache_key, self.content_type.model_class(), self.object_pk, "get_short_dict") album_cache_key = "_album_dict_%s_" album_dict = get_dictionary_with_cache_priority( album_cache_key, "mwa.Album", object_dict["album_id"], "get_short_dict") return { "comment": self.comment, "id": self.id, "likes_count": self.get_likes_count(), "object": { "id": self.object_pk, "type": self.content_type.model_class().item_type, "owner_id": object_dict["owner_id"] }, "submit_date": self.submit_date.isoformat(), "user": self.get_owner_dict(), "album": { "id": album_dict["id"], "owner_id": album_dict["owner_id"], } }
def build_short_dict(self): """ Build the short dict. If everything is working, this method should not make a single database call.""" item_cache_key = "_%s_dict_%s_" % (self.content_type.model_class().item_name, "%s") object_dict = get_dictionary_with_cache_priority( item_cache_key, self.content_type.model_class(), self.object_pk, "get_short_dict" ) album_cache_key = "_album_dict_%s_" album_dict = get_dictionary_with_cache_priority( album_cache_key, "mwa.Album", object_dict["album_id"], "get_short_dict" ) return { "comment": self.comment, "id": self.id, "likes_count": self.get_likes_count(), "object": { "id": self.object_pk, "type": self.content_type.model_class().item_type, "owner_id": object_dict["owner_id"] }, "submit_date": self.submit_date.isoformat(), "user": self.get_owner_dict(), "album": { "id": album_dict["id"], "owner_id": album_dict["owner_id"], } }
def get_like_list(self, user, skip_permission_check=False): from myproject.like.models import Like """ Get a list of likes for this comment. If the user has no read permission, get None. @user: The user whose point of view will be used when fetching likes. @skip_permission_check: If you already checked the permissions, set this to True in order to avoid a redundant database call. This method builds the like list manually, meaning likes themselves are not cached. Therefore there might be a lot of cache calls in this method. Change it if it effects performance. """ if not skip_permission_check: item_cache_key = "_%s_dict_%s_" % (self.content_type.model_class().item_name, "%s") object_dict = get_dictionary_with_cache_priority( item_cache_key, self.content_type.model_class(), self.object_pk, "get_short_dict" ) album_dict = get_dictionary_with_cache_priority( "_album_dict_%d_", "mwa.Album", object_dict["album_id"], "get_short_dict" ) privacy = album_dict.pop("privacy") # If the user is not the owner or anonymous, this method launches a query status = self.get_user_status(user, privacy, album_dict["owner_id"]) if privacy[status] & settings.PERMISSIONS["read"] == 0: # User cannot read this item, return None return False # one query here likes = Like.objects.filter( resource_type=3, resource_id=self.pk, ).only("id", "user") item_dict = { "type": 3, "id": self.id, "owner_id": self.user_id } like_list = [] for l in likes: liker_dict = get_dictionary_with_cache_priority( "_user_%d_owner_dict_", get_user_model(), l.user_id, "get_owner_dict", expected_field_list=["first_name", "last_name"] ) t_dict = { "user": liker_dict, "item": item_dict, "id": l.id, "can_delete": user.id == l.user_id } like_list.append(t_dict) return like_list
def get_like_list(self, user, skip_permission_check=False): from myproject.like.models import Like """ Get a list of likes for this comment. If the user has no read permission, get None. @user: The user whose point of view will be used when fetching likes. @skip_permission_check: If you already checked the permissions, set this to True in order to avoid a redundant database call. This method builds the like list manually, meaning likes themselves are not cached. Therefore there might be a lot of cache calls in this method. Change it if it effects performance. """ if not skip_permission_check: item_cache_key = "_%s_dict_%s_" % ( self.content_type.model_class().item_name, "%s") object_dict = get_dictionary_with_cache_priority( item_cache_key, self.content_type.model_class(), self.object_pk, "get_short_dict") album_dict = get_dictionary_with_cache_priority( "_album_dict_%d_", "mwa.Album", object_dict["album_id"], "get_short_dict") privacy = album_dict.pop("privacy") # If the user is not the owner or anonymous, this method launches a query status = self.get_user_status(user, privacy, album_dict["owner_id"]) if privacy[status] & settings.PERMISSIONS["read"] == 0: # User cannot read this item, return None return False # one query here likes = Like.objects.filter( resource_type=3, resource_id=self.pk, ).only("id", "user") item_dict = {"type": 3, "id": self.id, "owner_id": self.user_id} like_list = [] for l in likes: liker_dict = get_dictionary_with_cache_priority( "_user_%d_owner_dict_", get_user_model(), l.user_id, "get_owner_dict", expected_field_list=["first_name", "last_name"]) t_dict = { "user": liker_dict, "item": item_dict, "id": l.id, "can_delete": user.id == l.user_id } like_list.append(t_dict) return like_list