Move new user/class creation into their own functions.

(imported from commit 192533c4e2bc55a26e7ea70a32e7d1f64d2ed8c3)
This commit is contained in:
Tim Abbott
2012-09-27 14:46:42 -04:00
parent 0f8f60f30d
commit 8ffd91a6ee
2 changed files with 34 additions and 31 deletions

View File

@@ -106,6 +106,29 @@ def create_user(email, password, realm, full_name, short_name):
user.save() user.save()
UserProfile.create(user, realm, full_name, short_name) UserProfile.create(user, realm, full_name, short_name)
def create_user_if_needed(realm, email, password, full_name, short_name):
try:
return User.objects.get(email=email)
except User.DoesNotExist:
# forge a user for this person
create_user(email, password, realm,
full_name, short_name)
user = User.objects.get(email=email)
return user
def create_class_if_needed(realm, class_name):
try:
return ZephyrClass.objects.get(name=class_name, realm=realm)
except ZephyrClass.DoesNotExist:
new_class = ZephyrClass()
new_class.name = class_name
new_class.realm = realm
new_class.save()
recipient = Recipient(type_id=new_class.id, type=Recipient.CLASS)
recipient.save()
return new_class
class ZephyrClass(models.Model): class ZephyrClass(models.Model):
name = models.CharField(max_length=30, db_index=True) name = models.CharField(max_length=30, db_index=True)
realm = models.ForeignKey(Realm, db_index=True) realm = models.ForeignKey(Realm, db_index=True)

View File

@@ -11,7 +11,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, get_display_recipient, get_huddle, Realm, UserMessage, \ Recipient, get_display_recipient, get_huddle, Realm, UserMessage, \
create_user, do_send_zephyr, mit_sync_table create_user, do_send_zephyr, mit_sync_table, create_user_if_needed, \
create_class_if_needed
from zephyr.forms import RegistrationForm from zephyr.forms import RegistrationForm
from zephyr.decorator import asynchronous from zephyr.decorator import asynchronous
@@ -178,14 +179,9 @@ def forge_zephyr(request):
if "time" not in request.POST: if "time" not in request.POST:
return json_error("Missing time") return json_error("Missing time")
try: user = create_user_if_needed(user_profile.realm, email, "test",
user = User.objects.get(email=email)
except User.DoesNotExist:
# forge a user for this person
create_user(email, "test", user_profile.realm,
strip_html(request.POST['fullname']), strip_html(request.POST['fullname']),
strip_html(request.POST['shortname'])) strip_html(request.POST['shortname']))
user = User.objects.get(email=email)
if (request.POST['type'] == 'personal' and ',' in request.POST['recipient']): if (request.POST['type'] == 'personal' and ',' in request.POST['recipient']):
# Huddle message, need to make sure we're not syncing it twice! # Huddle message, need to make sure we're not syncing it twice!
@@ -198,11 +194,7 @@ def forge_zephyr(request):
# Now confirm all the other recipients exist in our system # Now confirm all the other recipients exist in our system
for user_email in request.POST["recipient"].split(","): for user_email in request.POST["recipient"].split(","):
try: create_user_if_needed(user_profile.realm, user_email, "test",
User.objects.get(email=user_email)
except User.DoesNotExist:
# forge a user for this person
create_user(user_email, "test", user_profile.realm,
user_email.split('@')[0], user_email.split('@')[0],
user_email.split('@')[0]) user_email.split('@')[0])
@@ -224,21 +216,9 @@ def zephyr_backend(request, sender):
if "instance" not in request.POST: if "instance" not in request.POST:
return json_error("Missing instance") return json_error("Missing instance")
class_name = strip_html(request.POST['class']).strip() zephyr_class = create_class_if_needed(user_profile.realm,
my_classes = ZephyrClass.objects.filter(name=class_name, realm=user_profile.realm) strip_html(request.POST['class']).strip())
if my_classes: recipient = Recipient.objects.get(type_id=zephyr_class.id, type=Recipient.CLASS)
my_class = my_classes[0]
else:
my_class = ZephyrClass()
my_class.name = class_name
my_class.realm = user_profile.realm
my_class.save()
recipient = Recipient(type_id=my_class.id, type=Recipient.CLASS)
recipient.save()
try:
recipient = Recipient.objects.get(type_id=my_class.id, type=Recipient.CLASS)
except Recipient.DoesNotExist:
return json_error("Invalid class")
elif zephyr_type_name == 'personal': elif zephyr_type_name == 'personal':
if "recipient" not in request.POST: if "recipient" not in request.POST:
return json_error("Missing recipient") return json_error("Missing recipient")