def eventregistration(request): context = RequestContext(request) registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and profile_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) # Assign units to user for unit in user_form.cleaned_data['units']: user.usersinunitoffering.add(unit) user.save() # Now sort out the UserProfile instance. # Since we need to set the user attribute ourselves, we set commit=False. # This delays saving the model until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user profile.role = "Student" # Now we save the UserProfile model instance. profile.save() # Update our variable to tell the template registration was successful. registered = True # Invalid form or forms - mistakes or something else? # Print problems to the terminal. # They'll also be shown to the user. else: print user_form.errors, profile_form.errors # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. else: user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context. return render_to_response( 'clatoolkit/eventregistration.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered, }, context)
def eventregistration(request): context = RequestContext(request) registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and profile_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) # Assign units to user for unit in user_form.cleaned_data['units']: user.usersinunitoffering.add(unit) user.save() # Now sort out the UserProfile instance. # Since we need to set the user attribute ourselves, we set commit=False. # This delays saving the model until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user profile.role = "Student" # Now we save the UserProfile model instance. profile.save() # Update our variable to tell the template registration was successful. registered = True # Invalid form or forms - mistakes or something else? # Print problems to the terminal. # They'll also be shown to the user. else: print user_form.errors, profile_form.errors # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. else: user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context. return render_to_response( 'clatoolkit/eventregistration.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered,}, context)
def register(request, unit_id): import requests # Like before, get the request's context. context = RequestContext(request) # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False try: unit = UnitOffering.objects.get(id=unit_id) except UnitOffering.DoesNotExist: raise Http404 platforms = unit.get_required_platforms() u = None if request.user.is_authenticated(): u = request.user # If it's a HTTP POST, we're interested in processing form data. if request.method == 'POST': # Attempt to grab information from the raw form information. # Note that we make use of both UserForm and UserProfileForm. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and profile_form.is_valid(): # Generate LRS Account user = user_form.cleaned_data['username'] email = user_form.cleaned_data['email'] lrs = unit.get_lrs() # Create a signature to authorise lrs account creation. # We don't want randoms creating accounts arbitrarly! hash = hmac.new(str(lrs.get_secret()), lrs.get_key(), sha1) # Return ascii formatted signature in base64 signature = binascii.b2a_base64(hash.digest())[:-1] payload = { "user": user, "mailbox": email, "client": lrs.app_name, "signature": signature } # TODO: Remove hardwired url (probs from client app model?) # print lrs.get_reg_lrs_account_url() r = requests.post(lrs.get_reg_lrs_account_url(), data=payload) if not (str(r.status_code) == '200' and r.content == 'success'): print 'Error: LRS account could not be created.' return HttpResponse(r.content) ### When an LRS account has been created, create the toolkit account. # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) user.save() m = UnitOfferingMembership(user=user, unit=unit, admin=False) m.save() # Now sort out the UserProfile instance. # Since we need to set the user attribute ourselves, we set commit=False. # This delays saving the model until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user profile.role = "Student" # Now we save the UserProfile model instance. profile.save() # Log in as the newly signed up user u = authenticate(username=user_form.cleaned_data["username"], password=user_form.cleaned_data["password"]) login(request, u) return HttpResponseRedirect("/") # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. else: user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context. return render_to_response( 'clatoolkit/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered, "course": unit, "req_platforms": platforms, "user": u }, context)
def register(request): # Like before, get the request's context. context = RequestContext(request) # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False # A boolean value used to determine if a unit should already be selected show_units = True selected_unit = 0 course = None # If it's a HTTP POST, we're interested in processing form data. if request.method == 'POST': #print request.POST # Attempt to grab information from the raw form information. # Note that we make use of both UserForm and UserProfileForm. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and profile_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) # Assign units to user selectedunit = request.GET.get('selectedunit', None) if selectedunit is not None: user.usersinunitoffering.add(selectedunit) else: for unit in user_form.cleaned_data['units']: user.usersinunitoffering.add(unit) user.save() # Now sort out the UserProfile instance. # Since we need to set the user attribute ourselves, we set commit=False. # This delays saving the model until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user profile.role = "Student" # Now we save the UserProfile model instance. profile.save() # Update our variable to tell the template registration was successful. registered = True # Invalid form or forms - mistakes or something else? # Print problems to the terminal. # They'll also be shown to the user. else: print user_form.errors, profile_form.errors course_code = request.POST.get('course_code', None) if course_code is not None: course = UnitOffering.objects.get(code=course_code) show_units = False selected_unit = course.id # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. else: print "loading forms" user_form = UserForm() profile_form = UserProfileForm() course_code = request.GET.get('course_code', None) if course_code is not None: course = UnitOffering.objects.get(code=course_code) show_units = False selected_unit = course.id # Render the template depending on the context. return render_to_response( 'clatoolkit/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered, 'show_units': show_units, 'selected_unit': selected_unit, "course": course}, context)
def register(request, unit_id): import requests # Like before, get the request's context. context = RequestContext(request) # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False try: unit = UnitOffering.objects.get(id=unit_id) except UnitOffering.DoesNotExist: raise Http404 platforms = unit.get_required_platforms() u = None if request.user.is_authenticated(): u = request.user # If it's a HTTP POST, we're interested in processing form data. if request.method == 'POST': # Attempt to grab information from the raw form information. # Note that we make use of both UserForm and UserProfileForm. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and profile_form.is_valid(): # Generate LRS Account user = user_form.cleaned_data['username'] email = user_form.cleaned_data['email'] lrs = unit.get_lrs() # Create a signature to authorise lrs account creation. # We don't want randoms creating accounts arbitrarly! hash = hmac.new(str(lrs.get_secret()), lrs.get_key(), sha1) # Return ascii formatted signature in base64 signature = binascii.b2a_base64(hash.digest())[:-1] payload = { "user": user, "mailbox": email, "client": lrs.app_name, "signature": signature } # TODO: Remove hardwired url (probs from client app model?) # print lrs.get_reg_lrs_account_url() r = requests.post(lrs.get_reg_lrs_account_url(), data=payload) if not (str(r.status_code) == '200' and r.content == 'success'): print 'Error: LRS account could not be created.' return HttpResponse(r.content) ### When an LRS account has been created, create the toolkit account. # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) user.save() m = UnitOfferingMembership(user=user, unit=unit, admin=False) m.save() # Now sort out the UserProfile instance. # Since we need to set the user attribute ourselves, we set commit=False. # This delays saving the model until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user profile.role = "Student" # Now we save the UserProfile model instance. profile.save() # Log in as the newly signed up user u = authenticate(username=user_form.cleaned_data["username"], password=user_form.cleaned_data["password"]) login(request, u) return HttpResponseRedirect("/") # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. else: user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context. return render_to_response( 'clatoolkit/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered, "course": unit, "req_platforms": platforms, "user": u}, context)
def register(request): # Like before, get the request's context. context = RequestContext(request) # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False # A boolean value used to determine if a unit should already be selected show_units = True selected_unit = 0 course = None # If it's a HTTP POST, we're interested in processing form data. if request.method == 'POST': #print request.POST # Attempt to grab information from the raw form information. # Note that we make use of both UserForm and UserProfileForm. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and profile_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) # Assign units to user selectedunit = request.GET.get('selectedunit', None) if selectedunit is not None: user.usersinunitoffering.add(selectedunit) else: for unit in user_form.cleaned_data['units']: user.usersinunitoffering.add(unit) user.save() # Now sort out the UserProfile instance. # Since we need to set the user attribute ourselves, we set commit=False. # This delays saving the model until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user profile.role = "Student" # Now we save the UserProfile model instance. profile.save() # Update our variable to tell the template registration was successful. registered = True # Invalid form or forms - mistakes or something else? # Print problems to the terminal. # They'll also be shown to the user. else: print user_form.errors, profile_form.errors course_code = request.POST.get('course_code', None) if course_code is not None: course = UnitOffering.objects.get(code=course_code) show_units = False selected_unit = course.id # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. else: print "loading forms" user_form = UserForm() profile_form = UserProfileForm() course_code = request.GET.get('course_code', None) if course_code is not None: course = UnitOffering.objects.get(code=course_code) show_units = False selected_unit = course.id # Render the template depending on the context. return render_to_response( 'clatoolkit/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered, 'show_units': show_units, 'selected_unit': selected_unit, "course": course }, context)
def register(request, course_code): # Like before, get the request's context. context = RequestContext(request) # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False unit = UnitOffering.objects.get(code=course_code) platforms = unit.get_required_platforms() u = None if request.user.is_authenticated(): u = request.user # If it's a HTTP POST, we're interested in processing form data. if request.method == 'POST': # Attempt to grab information from the raw form information. # Note that we make use of both UserForm and UserProfileForm. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and profile_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) user.save() m = UnitOfferingMembership(user=user, unit=unit, admin=False) m.save() # Now sort out the UserProfile instance. # Since we need to set the user attribute ourselves, we set commit=False. # This delays saving the model until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user profile.role = "Student" # Now we save the UserProfile model instance. profile.save() # Log in as the newly signed up user u = authenticate(username=user_form.cleaned_data["username"], password=user_form.cleaned_data["password"]) login(request, u) return HttpResponseRedirect("/") # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. else: user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context. return render_to_response( 'clatoolkit/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered, "course": unit, "req_platforms": platforms, "user": u}, context)