[schema] Add realms to the schema.

Note that realms aren't actually used for anything, aren't prompted
for, etc, so this is mostly just a schema change and the refactoring
needed to allow setting the realm in the future.

(imported from commit b8b483dcc9601b288702284879e9c99707a50a5c)
This commit is contained in:
Tim Abbott
2012-09-05 15:49:56 -04:00
parent 4dd1567426
commit 38b30e5997
6 changed files with 41 additions and 18 deletions

View File

@@ -19,6 +19,7 @@ autofocus('#id_username');
<input type="submit" value="Register" /><br /> <input type="submit" value="Register" /><br />
<input type="hidden" name="next" value="{{ next }}" /> <input type="hidden" name="next" value="{{ next }}" />
<input type="hidden" name="domain" value="humbughq.com" />
</form> </form>
{% endblock %} {% endblock %}

View File

@@ -3,7 +3,7 @@ from django.utils.timezone import utc
from django.contrib.auth.models import User from django.contrib.auth.models import User
from zephyr.models import Zephyr, UserProfile, ZephyrClass, Recipient, \ from zephyr.models import Zephyr, UserProfile, ZephyrClass, Recipient, \
Subscription, Huddle, get_huddle Subscription, Huddle, get_huddle, Realm, create_user_profile
import datetime import datetime
import random import random
@@ -50,15 +50,21 @@ class Command(BaseCommand):
self.stderr.write("Error! More than 100% of messages allocated.\n") self.stderr.write("Error! More than 100% of messages allocated.\n")
return 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() klass.objects.all().delete()
# Create a test realm
realm = Realm(domain="humbughq.com")
realm.save()
# Create test Users (UserProfiles are automatically created, # Create test Users (UserProfiles are automatically created,
# as are subscriptions to the ability to receive personals). # as are subscriptions to the ability to receive personals).
usernames = ["othello", "iago", "prospero", "cordelia", "hamlet"] usernames = ["othello", "iago", "prospero", "cordelia", "hamlet"]
for username in usernames: for username in usernames:
u = User.objects.create_user(username=username, password=username) user = User.objects.create_user(username=username, password=username)
u.save() user.save()
create_user_profile(user, realm)
users = [user.id for user in User.objects.all()] users = [user.id for user in User.objects.all()]
# Create public classes. # Create public classes.
@@ -130,7 +136,7 @@ class Command(BaseCommand):
new_zephyr.recipient = Recipient.objects.get(type="huddle", type_id=random.choice(recipient_huddles)) 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.): elif (randkey <= random_max * (options["percent_huddles"] + options["percent_personals"]) / 100.):
zephyr_type = "personal" zephyr_type = "personal"
personals_pair = random.choice(personals_personals_pairs) personals_pair = random.choice(personals_pairs)
random.shuffle(personals_pair) random.shuffle(personals_pair)
elif (randkey <= random_max * 1.0): elif (randkey <= random_max * 1.0):
zephyr_type = "class" zephyr_type = "class"

View File

@@ -24,9 +24,18 @@ def get_display_recipient(recipient):
callback_table = {} callback_table = {}
class Realm(models.Model):
domain = models.CharField(max_length=40)
def __repr__(self):
return "<Realm: %s %s>" % (self.domain, self.id)
def __str__(self):
return self.__repr__()
class UserProfile(models.Model): class UserProfile(models.Model):
user = models.OneToOneField(User) user = models.OneToOneField(User)
pointer = models.IntegerField() pointer = models.IntegerField()
realm = models.ForeignKey(Realm)
# The user receives this message # The user receives this message
def receive(self, message): def receive(self, message):
@@ -49,19 +58,19 @@ class UserProfile(models.Model):
callback_table.setdefault(self.user.id, []).append(cb) callback_table.setdefault(self.user.id, []).append(cb)
def __repr__(self): def __repr__(self):
return "<UserProfile: %s>" % (self.user.username,) return "<UserProfile: %s %s>" % (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.""" """When creating a new user, make a profile for him or her."""
u = kwargs["instance"] if not UserProfile.objects.filter(user=user):
if not UserProfile.objects.filter(user=u): profile = UserProfile(user=user, pointer=-1, realm_id=realm.id)
profile = UserProfile(user=u, pointer=-1)
profile.save() profile.save()
# Auto-sub to the ability to receive personals. # Auto-sub to the ability to receive personals.
recipient = Recipient(type_id=profile.pk, type="personal") recipient = Recipient(type_id=profile.pk, type="personal")
recipient.save() recipient.save()
Subscription(userprofile_id=profile, recipient_id=recipient).save() Subscription(userprofile_id=profile, recipient_id=recipient).save()
post_save.connect(create_user_profile, sender=User)
class ZephyrClass(models.Model): class ZephyrClass(models.Model):
name = models.CharField(max_length=30) name = models.CharField(max_length=30)

View File

@@ -17,7 +17,7 @@ class AuthedTestCase(TestCase):
def register(self, username, password): def register(self, username, password):
return self.client.post('/accounts/register/', 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): def send_zephyr(self, sender_name, recipient_name, zephyr_type):
sender = UserProfile.objects.get(user=User.objects.get(username=sender_name)) sender = UserProfile.objects.get(user=User.objects.get(username=sender_name))

View File

@@ -1,3 +1,3 @@
#!/bin/sh #!/bin/sh
mkdir -p zephyr/fixtures 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

View File

@@ -9,7 +9,8 @@ from django.utils.timezone import utc
from django.contrib.auth.models import User from django.contrib.auth.models import User
from zephyr.models import Zephyr, UserProfile, ZephyrClass, Subscription, \ 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 from zephyr.forms import RegistrationForm
import tornado.web import tornado.web
@@ -35,10 +36,16 @@ def register(request):
if form.is_valid(): if form.is_valid():
username = request.POST['username'] username = request.POST['username']
password = request.POST['password'] password = request.POST['password']
u = User.objects.create_user(username=username, password=password) domain = request.POST['domain']
u.save() realm = Realm.objects.filter(domain=domain)
user = authenticate(username=username, password=password) if not realm:
login(request, user) 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')) return HttpResponseRedirect(reverse('zephyr.views.home'))
else: else:
form = RegistrationForm() form = RegistrationForm()