mirror of
https://github.com/zulip/zulip.git
synced 2025-11-04 22:13:26 +00:00
[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:
@@ -19,6 +19,7 @@ autofocus('#id_username');
|
||||
|
||||
<input type="submit" value="Register" /><br />
|
||||
<input type="hidden" name="next" value="{{ next }}" />
|
||||
<input type="hidden" name="domain" value="humbughq.com" />
|
||||
</form>
|
||||
|
||||
{% endblock %}
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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 "<Realm: %s %s>" % (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 "<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."""
|
||||
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)
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user