justquick / django-activity-stream

Generate generic activity streams from the actions on your site. Users can follow any actors' activities for personalized streams.
http://django-activity-stream.rtfd.io/en/latest/
BSD 3-Clause "New" or "Revised" License
2.37k stars 482 forks source link

KeyError in gfk.py #71

Closed maximegaillard closed 12 years ago

maximegaillard commented 12 years ago

When using model_stream(Model) in views I get this error :

Exception Type: KeyError Exception Value: (15L, u'1') Exception Location: site-packages/actstream/gfk.py in fetch_generic_relations, line 79

This Model works with follow() and unfollow()

justquick commented 12 years ago

i need a lot more info about this bug as I cannot recreate it

any example source code and a dump of the actstream app would be helpful.

maximegaillard commented 12 years ago

The code example :

In my settings.py:

ACTSTREAM_ACTION_MODELS = ['auth.User']

My function to follow a stuff:

def followUser(request, userId):
    if not is_following_check(request.user, "user", userId):
        to_follow = User.objects.get(id=userId)
        follow(request.user, to_follow)

        return HttpResponse("success")
    else:
        return HttpResponse("error")

def is_following_check(user, who, whoId):

    if who == "user":

        to_check = User.objects.get(id=whoId)
        if is_following(user, to_check):
            return True
        else:
            return False

    else:
        return False

My function to get the stream :

def userStream(userId):
    user = User.objects.get(id=userId)
    user_activities = model_stream(user)

    return user_activities

And in the admin, I can see all the activities without any problem.

I found a "hack" to make it works in my dev environnement. In gfk.py, lines 70+:


for item in qs:
            for gfk in gfk_fields:
                if getattr(item, gfk.fk_field) != None:
                    ct_id_field = self.model._meta.get_field(gfk.ct_field).column
                    try:
                        setattr(item, gfk.name, data_map[(getattr(item, ct_id_field),
                                                            getattr(item, gfk.fk_field))])
                    except:
                        print "error gfk"
                        pass
        return qs
justquick commented 12 years ago

im not sure that just printing the error and ignoring that item is a good idea since this is a classic failure case. maybe resetting the actstream db might help if you can afford to do it? im also noticing in your example that you are using model_stream but you should probably be using user_stream. also in followUser you do not check to see whether the user is logged in. this might have something to do with it but i am going to make this problematic corner case a bit more obvious in a future release. im sorry but i cannot be of much more help because i do not have your setup in front of me to see what's happening.

justquick commented 12 years ago

it's a weird error ive just recently run into. there's a problem with your project setup. here are some tips which should help

Try v0.4.2 which might help but theres not much else i can do.