I have solved this. If anyone is interested, this is how I did...
Instead of having username and site as two separate fields, I combined
them. This will make the username unique for each site. The username
field will look like this "<site_id>:<username>". Since this isn't
very nice to display I wrote some methods in the User proxy SiteUser
that will extract the username and the site.

Here are the classes i wrote to make this work:

class SiteUserManager(UserManager):
        def create_user(self, username, email, password=None):
                return super(SiteUserManager, self).create_user('%d:%s' %
(settings.SITE_ID, username), email, password)

        def get_query_set(self):
                return super(SiteUserManager, self).get_query_set().filter
(username__startswith = '%d:' % settings.SITE_ID)

class SiteUser(User):
        objects = SiteUserManager()

        def get_site_id(self):
                return int(self.username.split(USERNAME_SEPARATOR, 1)[0])

        def get_site(self):
                return Site.objects.get(pk = self.get_site_id())

        def get_local_username(self):
                return self.username.split(':', 1)[1]

        def get_global_username(self):
                return '%...@%s' % (self.get_local_username(), 
self.get_site().domain)

        def __unicode__(self):
                return self.get_global_username()

        class Meta:
                proxy = True

class Backend(ModelBackend):
        def authenticate(self, username=None, password=None):
                try:
                        user = User.objects.get(username='%d:%s' % 
(settings.SITE_ID,
username))
                        if user.check_password(password):
                                return user
                except User.DoesNotExist:
                        return None

        def get_user(self, user_id):
                try:
                        return SiteUser.objects.get(pk=user_id)
                except SiteUser.DoesNotExist:
                        return None

/Fredrik

On 17 Aug, 10:31, Fredde <fredrik.k...@gmail.com> wrote:
> Hi!
>
> I am working on a project with multiple sites and are using the
> contrib.site app to separate them from eachother. The number of sites
> will be high and changing often, so I don't want to modify the Apache
> config and create a new VirtualHost/Location for each new site. I have
> solved this by writing a middleware that parses the url and changes
> the settings.SITE_ID based on that. Similar 
> tohttp://www.djangosnippets.org/snippets/1099/
>
> I'm planing to use contrib.auth for user login on each site, but I
> want to make the username unique together with the site. I tried
> something like this:
>
> class SiteUser(User):
>         site = ForeignKey(Site)
>
>         class Meta:
>                 unique_together = (('site', 'username'),)
>
> But this will not work so well, since the username field has to be in
> the same model as the unique_together meta attribute, and the username
> field is still unique across the sites. So I tried to make the User
> model abstract and removing the unique flag:
>
> User._meta.abstract = True
> User._meta.get_field('username')._unique = False
>
> But now the ManyToMany fields in User will complain that it's a
> abstract class, wich makes sence. And I guess you shouldn't modify en
> existing model anyway. Is it possible to do what I am trying to do
> without writing a new auth app? Are there any other problems with the
> multi site approach?
>
> /Fredrik
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to