James Bennett wrote:
> On Mon, Aug 17, 2009 at 10:57 AM, Jonas Obrist<ojiido...@gmail.com> wrote:
>   
>> Thanks for your opinion on this, didn't know django had this feature
>> once. But I just can't get it out of my head that there's gotta be a
>> better solution than this profile-extending... It just seems ridiculous
>> to me that half of the user properties is in one table and the other
>> half in the other one. And I don't like the template variables looking
>> like {{ request.user.user.someproperty }} and {{
>> request.user.some_other_property }}. Especially since my designer isn't
>> a coder and got all confused over which was where.
>>
>> So any thoughts on implementing my idea in a better way? To me (at least
>> in my code) my version seems pretty elegant... And unless I find a
>> better solution I'll continue using it for my own projects.
>>     
>
> Well, now that you've described the actual problem you're trying to
> solve, here's my five-minutes'-worth-of-coding solution:
>
> from django.contrib.auth.models import AnonymousUser
> from django.core.context_processors import PermWrapper
>
> class MergedUser(object):
>     def __init__(self, user):
>         self.user = user
>         self.profile = self.user.get_profile()
>
>     def __getattr(self, name):
>         if hasattr(self.user, name):
>             return getattr(self.user, name)
>         else:
>             return getattr(self.profile, name)
>
> def merged_user(request):
>     if hasattr(request, 'user'):
>         user = MergedUser(request.user)
>     else:
>         user = AnonymousUser()
>     return {
>         'user': user,
>         'messages': user.get_and_delete_messages(),
>         'perms': PermWrapper(user),
>     }
>
> Drop this somewhere, disable the auth context processor and enable the
> 'merged_user' processor above. Authenticated users will appear in
> template context as an object which transparently passes attribute
> access to either the User object or the related profile object,
> depending on where the required attribute actually exists.
>
>
>   

This is actually pretty much exactly what I had before, it was horribly 
slow! And somehow not nice since you always have to wrap the users 
around and can't just use request.user...


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to