Commit f912ffe8 by Brian Wilson

add persistence of testcenter_user registration

parent bc40a7f1
...@@ -189,10 +189,25 @@ class TestCenterUser(models.Model): ...@@ -189,10 +189,25 @@ class TestCenterUser(models.Model):
# Company # Company
company_name = models.CharField(max_length=50, blank=True) company_name = models.CharField(max_length=50, blank=True)
@staticmethod
def user_provided_fields():
return [ 'first_name', 'middle_name', 'last_name', 'suffix', 'salutation',
'address_1', 'address_2', 'address_3', 'city', 'state', 'postal_code', 'country',
'phone', 'extension', 'phone_country_code', 'fax', 'fax_country_code', 'company_name']
@property @property
def email(self): def email(self):
return self.user.email return self.user.email
class TestCenterRegistration(models.Model):
testcenter_user = models.ForeignKey(TestCenterUser, unique=True, default=None)
created_at = models.DateTimeField(auto_now_add=True, db_index=True)
updated_at = models.DateTimeField(auto_now=True, db_index=True)
accommodation_request = models.CharField(max_length=1024)
# TODO: this should be an enumeration:
accommodation_code = models.CharField(max_length=64)
def unique_id_for_user(user): def unique_id_for_user(user):
""" """
Return a unique id for a user, suitable for inserting into Return a unique id for a user, suitable for inserting into
......
...@@ -26,7 +26,7 @@ from bs4 import BeautifulSoup ...@@ -26,7 +26,7 @@ from bs4 import BeautifulSoup
from django.core.cache import cache from django.core.cache import cache
from django_future.csrf import ensure_csrf_cookie, csrf_exempt from django_future.csrf import ensure_csrf_cookie, csrf_exempt
from student.models import (Registration, UserProfile, TestCenterUser, from student.models import (Registration, UserProfile, TestCenterUser, TestCenterRegistration,
PendingNameChange, PendingEmailChange, PendingNameChange, PendingEmailChange,
CourseEnrollment, unique_id_for_user) CourseEnrollment, unique_id_for_user)
...@@ -614,7 +614,7 @@ def begin_test_registration(request, course_id): ...@@ -614,7 +614,7 @@ def begin_test_registration(request, course_id):
log.error("User {0} enrolled in non-existent course {1}" log.error("User {0} enrolled in non-existent course {1}"
.format(user.username, course_id)) .format(user.username, course_id))
# placeholder for possible messages... # TODO: placeholder for possible messages...
message = "" message = ""
if not user.is_active: if not user.is_active:
message = render_to_string('registration/activate_account_notice.html', {'email': user.email}) message = render_to_string('registration/activate_account_notice.html', {'email': user.email})
...@@ -635,20 +635,48 @@ def _do_create_or_update_test_center_user(post_vars): ...@@ -635,20 +635,48 @@ def _do_create_or_update_test_center_user(post_vars):
Returns a tuple (User, UserProfile, TestCenterUser). Returns a tuple (User, UserProfile, TestCenterUser).
Note: this function is also used for creating test users.
""" """
user = User(username=post_vars['username'],
email=post_vars['email'], # first determine if we need to create a new TestCenterUser, or if we are making any update
is_active=False) # to an existing TestCenterUser.
user.set_password(post_vars['password']) username=post_vars['username']
registration = Registration() user = User.objects.get(username=username)
needs_saving = False
try:
testcenter_user = TestCenterUser.objects.get(user=user)
# found a TestCenterUser, so check to see if it has changed
needs_updating = any([testcenter_user.__getattribute__(fieldname) != post_vars[fieldname]
for fieldname in TestCenterUser.user_provided_fields()])
if needs_updating:
# TODO: what do we set a timestamp to, in order to get now()?
testcenter_user.user_updated_at = datetime.datetime.now()
# Now do the update:
for fieldname in TestCenterUser.user_provided_fields():
testcenter_user.__setattr__(fieldname, post_vars[fieldname])
needs_saving = True
except TestCenterUser.DoesNotExist:
# did not find the TestCenterUser, so create a new one
testcenter_user = TestCenterUser(user=user)
# testcenter_user.user = user
for fieldname in TestCenterUser.user_provided_fields():
testcenter_user.__setattr__(fieldname, post_vars[fieldname])
# testcenter_user.candidate_id remains unset
testcenter_user.client_candidate_id = 'edx' + '123456' # some unique value
testcenter_user.user_updated_at = datetime.datetime.now()
needs_saving = True
# additional validation occurs at save time, so handle exceptions
# TODO: Rearrange so that if part of the process fails, the whole process fails. # TODO: Rearrange so that if part of the process fails, the whole process fails.
# Right now, we can have e.g. no registration e-mail sent out and a zombie account # Right now, we can have e.g. no registration e-mail sent out and a zombie account
if needs_saving:
try: try:
user.save() testcenter_user.save()
except IntegrityError: except IntegrityError:
js = {'success': False} js = {'success': False}
# Figure out the cause of the integrity error # TODO: Figure out the cause of the integrity error
if len(User.objects.filter(username=post_vars['username'])) > 0: if len(User.objects.filter(username=post_vars['username'])) > 0:
js['value'] = "An account with this username already exists." js['value'] = "An account with this username already exists."
js['field'] = 'username' js['field'] = 'username'
...@@ -661,25 +689,13 @@ def _do_create_or_update_test_center_user(post_vars): ...@@ -661,25 +689,13 @@ def _do_create_or_update_test_center_user(post_vars):
raise raise
registration.register(user)
profile = UserProfile(user=user) registration = TestCenterRegistration(testcenter_user = testcenter_user)
profile.name = post_vars['name'] # registration.register(user)
profile.level_of_education = post_vars.get('level_of_education')
profile.gender = post_vars.get('gender')
profile.mailing_address = post_vars.get('mailing_address')
profile.goals = post_vars.get('goals')
try: registration.accommodation_request = post_vars['accommodations']
profile.year_of_birth = int(post_vars['year_of_birth'])
except (ValueError, KeyError): return (user, testcenter_user, registration)
profile.year_of_birth = None # If they give us garbage, just ignore it instead
# of asking them to put an integer.
try:
profile.save()
except Exception:
log.exception("UserProfile creation failed for user {0}.".format(user.id))
return (user, profile, registration)
@ensure_csrf_cookie @ensure_csrf_cookie
def create_test_registration(request, post_override=None): def create_test_registration(request, post_override=None):
...@@ -718,7 +734,7 @@ def create_test_registration(request, post_override=None): ...@@ -718,7 +734,7 @@ def create_test_registration(request, post_override=None):
return ret return ret
(user, profile, testcenter_user, testcenter_registration) = ret (user, testcenter_user, testcenter_registration) = ret
# only do the following if there is accommodation text to send, # only do the following if there is accommodation text to send,
...@@ -743,17 +759,6 @@ def create_test_registration(request, post_override=None): ...@@ -743,17 +759,6 @@ def create_test_registration(request, post_override=None):
return HttpResponse(json.dumps(js)) return HttpResponse(json.dumps(js))
if DoExternalAuth:
eamap.user = login_user
eamap.dtsignup = datetime.datetime.now()
eamap.save()
log.debug('Updated ExternalAuthMap for %s to be %s' % (post_vars['username'],eamap))
if settings.MITX_FEATURES.get('BYPASS_ACTIVATION_EMAIL_FOR_EXTAUTH'):
log.debug('bypassing activation email')
login_user.is_active = True
login_user.save()
# statsd.increment("common.student.account_created") # statsd.increment("common.student.account_created")
js = {'success': True} js = {'success': True}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment