Skip to content

Skip login after registration (mostly for NYIF) - #174

Merged
tkeemon merged 5 commits into
appsembler/ficus/developfrom
appsembler/ficus/feature/nyif-reg-workflow
Oct 19, 2017
Merged

Skip login after registration (mostly for NYIF)#174
tkeemon merged 5 commits into
appsembler/ficus/developfrom
appsembler/ficus/feature/nyif-reg-workflow

Conversation

@tkeemon

@tkeemon tkeemon commented Oct 18, 2017

Copy link
Copy Markdown

I've added two major changes to the account registration workflow here:

  1. Try to import a custom model from our fork of edx-organizations, then create an object from that model if the account registration happens within a microsite.
  2. Add a new flag APPSEMBLER_FEATURES['SKIP_LOGIN_AFTER_REGISTRATION']. If set to True, users aren't automatically redirected to /dashboard after creating a new account.

The default behavior for each change is:

  1. If edx-organizations isn't installed, then the import fails gracefully.
  2. If SKIP_LOGIN_AFTER_REGISTRATION doesn't exist, then the normal registration workflow takes place and the user is logged in and redirected to /dashboard after registration.

Related commits:
https://github.com/noderabbit-team/edx-configs/commit/6df426793ef349659e503bf05aea95a9ed6d19d7

@tkeemon

tkeemon commented Oct 18, 2017

Copy link
Copy Markdown
Author

@OmarIthawi OmarIthawi changed the title Appsembler/ficus/feature/nyif reg workflow Skip login after registration (mostly for NYIF) Oct 18, 2017

@OmarIthawi OmarIthawi left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Only two changes to go!

Comment thread common/djangoapps/student/views.py Outdated
organization = request.site.organizations.first()
if organization:
UserOrganizationMapping.objects.get_or_create(user=user, organization=organization, is_active=False)
except:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

except: -> except NameError: to ensure that anything other than missing UserOrganizationMapping being catched (well, at least most of the errors).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tkeemon don't take my word for it. Please double check whether this works or not.

Generally it's advisable to avoid except: and except Exception: whenever possible.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OmarIthawi thanks for catching this. This is definitely a bad habit of mine.

Take a look and let me know if this addresses your concern.

Comment thread common/djangoapps/student/views.py Outdated
login(request, new_user)
request.session.set_expiry(0)

if not settings.APPSEMBLER_FEATURES.get('SKIP_LOGIN_AFTER_REGISTRATION',False):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...ION',False) -> ...ION', False) space after comma.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

#default behavior is to replace the existing backends with those in APPSEMBLER_FEATURES
AUTHENTICATION_BACKENDS = tuple(APPSEMBLER_FEATURES['LMS_AUTHENTICATION_BACKENDS'])

#attempt to import model from our custom fork of edx-organizations

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not specific to this PR at all. But we do need to DRY in the envs/*_appsembler.py files.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. And @johnbaldwin has raised this same point as well:

https://trello.com/c/wfj1jPTd/98-dry-up-awsappsembler-and-devstackappsembler-py-files

That's what I was attempting to do with the appsembler.py file. Initially, this file was supposed to be imported by both aws_appsembler.py and devstack_appsembler.py, so settings could be shared across both of production and devstack deployments.

But with this setup we were running into some problems. I can't remember specifically, but I think it was related to the fact that this file was called without importing common.py, so variables like INSTALLED_APPS didn't yet exist. And if we defined INSTALLED_APPS in appsembler.py, then it would nuke all of the values from common.py and aws.py when imported into aws_appsembler.py.

We can definitely do this, we just need to be careful about how we structure the imports.

@OmarIthawi

Copy link
Copy Markdown

@tkeemon somewhere the PEP8 there's a good practice:

Additionally, for all try/except clauses, limit the try clause to the absolute minimum amount of code necessary. Again, this avoids masking bugs.

Yes:

try:
    value = collection[key]
except KeyError:
    return key_not_found(key)
else:
    return handle_value(value)

No:

try:
    # Too broad!
    return handle_value(collection[key])
except KeyError:
    # Will also catch KeyError raised by handle_value()
    return key_not_found(key)

@OmarIthawi

Copy link
Copy Markdown

@tkeemon One additional read about the except: thingy:

@OmarIthawi OmarIthawi left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One suggestion and we're good to!

Comment thread common/djangoapps/student/views.py Outdated
pass
else:
if organization:
UserOrganizationMapping.objects.get_or_create(user=user, organization=organization, is_active=False)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks much better. I'd like to take it further:

Why not completely avoid the try/except? I think having if u'organizations.backends.OrganizationMemberBackend' in settings.AUTHENTICATION_BACKENDS: makes it safe to assume that all the rest will go well:

if u'organizations.backends.OrganizationMemberBackend' in settings.AUTHENTICATION_BACKENDS:
  organization = request.site.organizations.first()
  UserOrganizationMapping.objects.get_or_create(user=user, organization=organization, is_active=False)

Do we expect that organization = request.site.organizations.first() to be None? If so, let's keep the if organization: part.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. I'll remove the try/except. I do expect organization = request.site.organizations.first() to return None when the site in question is the default site, so I'm going to leave that last if statement in there.

Making that change and merging.

@tkeemon
tkeemon merged commit d06202e into appsembler/ficus/develop Oct 19, 2017
@bryanlandia
bryanlandia deleted the appsembler/ficus/feature/nyif-reg-workflow branch October 20, 2017 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants