Moves get_object_from_name into util.

This commit is contained in:
Christopher Neugebauer 2016-09-20 11:24:50 +10:00
parent 6e4d2fab16
commit 6611546a35
2 changed files with 20 additions and 9 deletions

View file

@ -1,4 +1,5 @@
import string import string
import sys
from django.utils.crypto import get_random_string from django.utils.crypto import get_random_string
@ -55,3 +56,19 @@ def lazy(function, *args, **kwargs):
return retval[0] return retval[0]
return evaluate return evaluate
def get_object_from_name(name):
''' Returns the named object.
Arguments:
name (str): A string of form `package.subpackage.etc.module.property`.
This function will import `package.subpackage.etc.module` and
return `property` from that module.
'''
dot = name.rindex(".")
mod_name, property_name = name[:dot], name[dot + 1:]
__import__(mod_name)
return getattr(sys.modules[mod_name], property_name)

View file

@ -1,4 +1,5 @@
import sys import sys
import util
from registrasion import forms from registrasion import forms
from registrasion import util from registrasion import util
@ -60,13 +61,6 @@ class GuidedRegistrationSection(_GuidedRegistrationSection):
pass pass
def get_object(name):
dot = name.rindex(".")
mod_name, form_name = name[:dot], name[dot + 1:]
__import__(mod_name)
return getattr(sys.modules[mod_name], form_name)
@login_required @login_required
def guided_registration(request): def guided_registration(request):
''' Goes through the registration process in order, making sure user sees ''' Goes through the registration process in order, making sure user sees
@ -277,11 +271,11 @@ def edit_profile(request):
# Define the attendee profile form, or get a default. # Define the attendee profile form, or get a default.
try: try:
ProfileForm = get_object(settings.ATTENDEE_PROFILE_FORM) ProfileForm = util.get_object_from_name(settings.ATTENDEE_PROFILE_FORM)
except: except:
class ProfileForm(django_forms.ModelForm): class ProfileForm(django_forms.ModelForm):
class Meta: class Meta:
model = get_object(settings.ATTENDEE_PROFILE_MODEL) model = util.get_object_from_name(settings.ATTENDEE_PROFILE_MODEL)
exclude = ["attendee"] exclude = ["attendee"]