google-code-export / sct-project

Automatically exported from code.google.com/p/sct-project
1 stars 1 forks source link

Category latestPost doesn't filter hidden posts #186

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Removed (is_hidden=True) posts are still shown as last post for the category.

sphboard/models.py->get_latest_post:

def get_latest_post(self):
    return self.posts.latest( 'postdate' )

should be:

def get_latest_post(self):
    return self.posts.filter(is_hidden=False).latest( 'postdate' )

Original issue reported on code.google.com by pigle...@gmail.com on 20 Dec 2009 at 12:14

GoogleCodeExporter commented 9 years ago
There is also problem with is_hidden and _hasNewPosts and postCount. These 
functions 
should take is_hidden into account like:

    def _hasNewPosts(self, session, user):
        if hasattr(self, '__hasNewPosts'): return self.__hasNewPosts
        if not user.is_authenticated(): return False
        try:
            latestPost = Post.objects.filter( category = self, is_hidden=False 
).latest( 'postdate' )
        except Post.DoesNotExist:
            return False

    def postCount(self):
        return self.posts.filter(is_hidden=False).count()

Original comment by pigle...@gmail.com on 20 Dec 2009 at 9:58

GoogleCodeExporter commented 9 years ago
hmm.. something is very wrong here ..
when accessing Post.objects you don't have to filter for is_hidden, because 
this is
done by the PostManager:

class PostManager(models.Manager):
    """
    This custom manager makes sure that only visible posts are selected 
    (ie is_hidden has to be 0)
    """
    def get_query_set(self):
        return super(PostManager, self).get_query_set().filter(is_hidden = 0)

and - is_hidden is an IntegerField, no boolean - maybe this is your problem? .. 
as i
just saw that your delete-patch is setting is_hidden to True .. i somehow 
missed that :(
can you take a look at the database which value 'True' is? i mean .. is_hidden 
= 0
should still not match (i guess?)

(it kind of makes sense to have this as IntegerField, because i thought about 
using
it as indicator why it was hidden.. but i never used it (yet)) - theoretically 
we
could use '1' as "temporarily hidden" (e.g. when the post is just created so
attachments can be added) - and "deleted" ... but on the other hand this is 
what the
'status' is for :)

Original comment by herbert....@gmail.com on 20 Dec 2009 at 10:08

GoogleCodeExporter commented 9 years ago
Indeed, I've missed that it is IntegerField. But even if using is_hidden=True I 
have
is_hidden=1 in database column (checked with pgadminIII).

Original comment by pigle...@gmail.com on 20 Dec 2009 at 10:26

GoogleCodeExporter commented 9 years ago
I think this is the answer:
http://docs.djangoproject.com/en/dev/topics/db/managers/#controlling-automatic-m
anager-types

Original comment by pigle...@gmail.com on 20 Dec 2009 at 10:36

GoogleCodeExporter commented 9 years ago
i'm not sure if this is the answer.. because if you consider the latestPost 
example:

latestPost = Post.objects.filter( category = self ).latest( 'postdate' )

it directly accesses Post.objects - which is the PostManager? are you sure this 
also
returns hidden posts?

Original comment by herbert....@gmail.com on 20 Dec 2009 at 11:11

GoogleCodeExporter commented 9 years ago
You're right, sorry, I've messed up things there.
Post.objects.filter( category = self ).latest( 'postdate' ) works properly. 

I haven't read exactly that you're talking about Post.objects in your previous 
comment.
So Post.objects is good, the only problem is with related manager 'self.posts' 
in
get_latest_post and postCount

Original comment by pigle...@gmail.com on 20 Dec 2009 at 11:25

GoogleCodeExporter commented 9 years ago
ahh ok .. that makes sense.. have you tried out if use_for_related_fields = True
solves the problem? unfortunately i'm not sure what the "default manager" is .. 
if it
is the one called 'objects' .. or, like in other cases.. the one defined first..
somehow this is kind of messy :)

Original comment by herbert....@gmail.com on 20 Dec 2009 at 11:27

GoogleCodeExporter commented 9 years ago
I think that 'use_for_related_fields' can't be used there because PostManager
overrides get_query_set method and the docs says: do not filter away any 
results in
manager that is used for related fields. See: 

http://docs.djangoproject.com/en/dev/topics/db/managers/#do-not-filter-away-any-
results-in-this-type-of-manager-subclass

Original comment by pigle...@gmail.com on 20 Dec 2009 at 11:40

GoogleCodeExporter commented 9 years ago
hmm.. ok .. since i don't quite understand the reasoning behind that 
documentation
and haven't found anything in the archives, i have asked in the django mailing 
list:
http://groups.google.com/group/django-users/browse_thread/thread/b0fd7f2b8fd047a
7

from my point of view it would be much nicer to create the filter in one place,
instead of making sure that it is honored everywhere.. but i don't really want 
to
ignore the django documentation :) so let's hope they can help resolving the 
problem :/

Original comment by herbert....@gmail.com on 20 Dec 2009 at 11:58

GoogleCodeExporter commented 9 years ago
fixed by Issue 188

Original comment by herbert....@gmail.com on 4 Jan 2010 at 1:59