Re: site and username unique together

2009-08-18 Thread Fredde

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 ":". 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  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
-~--~~~~--~~--~--~---



site and username unique together

2009-08-17 Thread Fredde

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 to 
http://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
-~--~~~~--~~--~--~---



One-to-many relationship (the opposite of many-to-one)

2009-08-13 Thread Fredde

Hi!

I am using the Site app to serve multiple sites with shared content in
the same django project. Now I want to be able to assign a owner
company for each site to display different copyright info, etc. So
what I would like to do is to add a ForeignKey to the site model
without having to create my own copy of the site app. Is there a one-
to-many relationship (the opposite of the ForeignKey that is many-to-
one) that I can add to the Company model?

What I would like to do is something like:

---
company.models.py:
---
from django.contrib.sites.models import Site

class Company(models.Model):
name = models.CharField(max_length = 30)
url = models.CharField(max_length = 100)
sites = models.OneToMany(Site, related_name = 'company')

---
In the views
---
from django.contrib.sites.models import Site

current_site = Site.objects.get_current()
company = current_site.company

---

I guess this could be done with a many-to-many relationship in the
Company model as well, but that is kind of ugly. Is this possible or
is there some other way to solve this. It cant be such a uncommon
scenario.

/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
-~--~~~~--~~--~--~---