diff --git a/templates/zephyr/register.html b/templates/zephyr/register.html index a0a3858491..19d584b3c2 100644 --- a/templates/zephyr/register.html +++ b/templates/zephyr/register.html @@ -19,6 +19,7 @@ autofocus('#id_username');
+ {% endblock %} diff --git a/zephyr/management/commands/populate_db.py b/zephyr/management/commands/populate_db.py index c164eb958c..f25a136f92 100644 --- a/zephyr/management/commands/populate_db.py +++ b/zephyr/management/commands/populate_db.py @@ -3,7 +3,7 @@ from django.utils.timezone import utc from django.contrib.auth.models import User from zephyr.models import Zephyr, UserProfile, ZephyrClass, Recipient, \ - Subscription, Huddle, get_huddle + Subscription, Huddle, get_huddle, Realm, create_user_profile import datetime import random @@ -50,15 +50,21 @@ class Command(BaseCommand): self.stderr.write("Error! More than 100% of messages allocated.\n") return - for klass in [Zephyr, ZephyrClass, UserProfile, User, Recipient, Subscription, Huddle]: + for klass in [Zephyr, ZephyrClass, UserProfile, User, Recipient, + Realm, Subscription, Huddle]: klass.objects.all().delete() + # Create a test realm + realm = Realm(domain="humbughq.com") + realm.save() + # Create test Users (UserProfiles are automatically created, # as are subscriptions to the ability to receive personals). usernames = ["othello", "iago", "prospero", "cordelia", "hamlet"] for username in usernames: - u = User.objects.create_user(username=username, password=username) - u.save() + user = User.objects.create_user(username=username, password=username) + user.save() + create_user_profile(user, realm) users = [user.id for user in User.objects.all()] # Create public classes. @@ -130,7 +136,7 @@ class Command(BaseCommand): new_zephyr.recipient = Recipient.objects.get(type="huddle", type_id=random.choice(recipient_huddles)) elif (randkey <= random_max * (options["percent_huddles"] + options["percent_personals"]) / 100.): zephyr_type = "personal" - personals_pair = random.choice(personals_personals_pairs) + personals_pair = random.choice(personals_pairs) random.shuffle(personals_pair) elif (randkey <= random_max * 1.0): zephyr_type = "class" diff --git a/zephyr/models.py b/zephyr/models.py index 77a788bc52..c97de0d6da 100644 --- a/zephyr/models.py +++ b/zephyr/models.py @@ -24,9 +24,18 @@ def get_display_recipient(recipient): callback_table = {} +class Realm(models.Model): + domain = models.CharField(max_length=40) + + def __repr__(self): + return "" % (self.domain, self.id) + def __str__(self): + return self.__repr__() + class UserProfile(models.Model): user = models.OneToOneField(User) pointer = models.IntegerField() + realm = models.ForeignKey(Realm) # The user receives this message def receive(self, message): @@ -49,19 +58,19 @@ class UserProfile(models.Model): callback_table.setdefault(self.user.id, []).append(cb) def __repr__(self): - return "" % (self.user.username,) + return "" % (self.user.username, self.realm) + def __str__(self): + return self.__repr__() -def create_user_profile(**kwargs): +def create_user_profile(user, realm): """When creating a new user, make a profile for him or her.""" - u = kwargs["instance"] - if not UserProfile.objects.filter(user=u): - profile = UserProfile(user=u, pointer=-1) + if not UserProfile.objects.filter(user=user): + profile = UserProfile(user=user, pointer=-1, realm_id=realm.id) profile.save() # Auto-sub to the ability to receive personals. recipient = Recipient(type_id=profile.pk, type="personal") recipient.save() Subscription(userprofile_id=profile, recipient_id=recipient).save() -post_save.connect(create_user_profile, sender=User) class ZephyrClass(models.Model): name = models.CharField(max_length=30) diff --git a/zephyr/tests.py b/zephyr/tests.py index e9194a7ff3..88ebf1fded 100644 --- a/zephyr/tests.py +++ b/zephyr/tests.py @@ -17,7 +17,7 @@ class AuthedTestCase(TestCase): def register(self, username, password): return self.client.post('/accounts/register/', - {'username':username, 'password':password}) + {'username':username, 'password':password, 'domain':'humbughq.com'}) def send_zephyr(self, sender_name, recipient_name, zephyr_type): sender = UserProfile.objects.get(user=User.objects.get(username=sender_name)) diff --git a/zephyr/tests/generate-fixtures b/zephyr/tests/generate-fixtures index 18c8cb7c75..4cb0bda3b0 100755 --- a/zephyr/tests/generate-fixtures +++ b/zephyr/tests/generate-fixtures @@ -1,3 +1,3 @@ #!/bin/sh mkdir -p zephyr/fixtures -python manage.py dumpdata auth.User zephyr.UserProfile zephyr.ZephyrClass zephyr.Recipient zephyr.Subscription zephyr.Zephyr > zephyr/fixtures/zephyrs.json +python manage.py dumpdata auth.User zephyr.UserProfile zephyr.ZephyrClass zephyr.Recipient zephyr.Subscription zephyr.Zephyr zephyr.Huddle zephyr.Realm > zephyr/fixtures/zephyrs.json diff --git a/zephyr/views.py b/zephyr/views.py index 62cbfc5e59..f7f94c058d 100644 --- a/zephyr/views.py +++ b/zephyr/views.py @@ -9,7 +9,8 @@ from django.utils.timezone import utc from django.contrib.auth.models import User from zephyr.models import Zephyr, UserProfile, ZephyrClass, Subscription, \ - Recipient, filter_by_subscriptions, get_display_recipient, get_huddle + Recipient, filter_by_subscriptions, get_display_recipient, get_huddle, \ + create_user_profile, Realm from zephyr.forms import RegistrationForm import tornado.web @@ -35,10 +36,16 @@ def register(request): if form.is_valid(): username = request.POST['username'] password = request.POST['password'] - u = User.objects.create_user(username=username, password=password) - u.save() - user = authenticate(username=username, password=password) - login(request, user) + domain = request.POST['domain'] + realm = Realm.objects.filter(domain=domain) + if not realm: + realm = Realm(domain=domain) + else: + realm = Realm.objects.get(domain=domain) + user = User.objects.create_user(username=username, password=password) + user.save() + create_user_profile(user, realm) + login(request, authenticate(username=username, password=password)) return HttpResponseRedirect(reverse('zephyr.views.home')) else: form = RegistrationForm()