def register(request): if not settings.OPPIA_ALLOW_SELF_REGISTRATION: raise Http404 if request.method == 'POST': # if form submitted... form = RegisterForm(request.POST) if form.is_valid(): # All validation rules pass # Create new user username = form.cleaned_data.get("username") email = form.cleaned_data.get("email") password = form.cleaned_data.get("password") first_name = form.cleaned_data.get("first_name") last_name = form.cleaned_data.get("last_name") user = User.objects.create_user(username, email, password) user.first_name = first_name user.last_name = last_name user.save() user_profile = UserProfile() user_profile.user = user user_profile.sex = form.cleaned_data.get("sex") user_profile.age_range = form.cleaned_data.get("age_range") user_profile.role = form.cleaned_data.get("role") user_profile.location = form.cleaned_data.get("location") user_profile.save() u = authenticate(username=username, password=password) if u is not None and u.is_active: login(request, u) return HttpResponseRedirect('thanks/') return HttpResponseRedirect('thanks/') # Redirect after POST else: form = RegisterForm(initial={'next': filter_redirect(request.GET), }) return render(request, 'oppia/form.html', {'form': form, 'title': _(u'Register'), })
def obj_create(self, bundle, **kwargs): if not settings.OPPIA_ALLOW_SELF_REGISTRATION: raise BadRequest(_(u'Registration is disabled on this server.')) required = [ 'username', 'password', 'passwordagain', 'email', 'firstname', 'lastname', 'sex', 'age_range', 'role', 'location' ] check_required_params(bundle, required) data = { 'username': bundle.data['username'], 'password': bundle.data['password'], 'password_again': bundle.data['passwordagain'], 'email': bundle.data['email'], 'sex': bundle.data['sex'], 'age_range': bundle.data['age_range'], 'role': bundle.data['role'], 'location': bundle.data['location'], 'first_name': bundle.data['firstname'], 'last_name': bundle.data['lastname'], } rf = RegisterForm(data) if not rf.is_valid(): str = "" for key, value in rf.errors.items(): for error in value: str += error + "\n" raise BadRequest(str) else: username = bundle.data['username'] password = bundle.data['password'] email = bundle.data['email'] first_name = bundle.data['firstname'] last_name = bundle.data['lastname'] try: bundle.obj = User.objects.create_user(username, email, password) bundle.obj.first_name = first_name bundle.obj.last_name = last_name bundle.obj.save() user_profile = UserProfile() user_profile.user = bundle.obj user_profile.sex = bundle.data['sex'] user_profile.age_range = bundle.data['age_range'] user_profile.role = bundle.data['role'] user_profile.location = bundle.data['location'] user_profile.save() u = authenticate(username=username, password=password) if u is not None and u.is_active: login(bundle.request, u) # Add to tracker tracker = Tracker() tracker.user = u tracker.type = 'register' tracker.ip = bundle.request.META.get( 'REMOTE_ADDR', oppia.api.DEFAULT_IP_ADDRESS) tracker.agent = bundle.request.META.get( 'HTTP_USER_AGENT', 'unknown') tracker.save() key = ApiKey.objects.get(user=u) bundle.data['api_key'] = key.key except IntegrityError: raise BadRequest( _(u'Username "%s" already in use, please select another' % username)) del bundle.data['passwordagain'] del bundle.data['password'] del bundle.data['firstname'] del bundle.data['lastname'] del bundle.data['age_range'] del bundle.data['role'] del bundle.data['location'] return bundle
def obj_create(self, bundle, **kwargs): if not settings.OPPIA_ALLOW_SELF_REGISTRATION: raise BadRequest(_(u'Registration is disabled on this server.')) required = ['username', 'password', 'passwordagain', 'email', 'firstname', 'lastname', 'sex', 'age_range', 'role', 'location'] check_required_params(bundle, required) data = {'username': bundle.data['username'], 'password': bundle.data['password'], 'password_again': bundle.data['passwordagain'], 'email': bundle.data['email'], 'sex': bundle.data['sex'], 'age_range': bundle.data['age_range'], 'role': bundle.data['role'], 'location': bundle.data['location'], 'first_name': bundle.data['firstname'], 'last_name': bundle.data['lastname'], } rf = RegisterForm(data) if not rf.is_valid(): str = "" for key, value in rf.errors.items(): for error in value: str += error + "\n" raise BadRequest(str) else: username = bundle.data['username'] password = bundle.data['password'] email = bundle.data['email'] first_name = bundle.data['firstname'] last_name = bundle.data['lastname'] try: bundle.obj = User.objects.create_user(username, email, password) bundle.obj.first_name = first_name bundle.obj.last_name = last_name bundle.obj.save() user_profile = UserProfile() user_profile.user = bundle.obj user_profile.sex = bundle.data['sex'] user_profile.age_range = bundle.data['age_range'] user_profile.role = bundle.data['role'] user_profile.location = bundle.data['location'] user_profile.save() u = authenticate(username=username, password=password) if u is not None and u.is_active: login(bundle.request, u) # Add to tracker tracker = Tracker() tracker.user = u tracker.type = 'register' tracker.ip = bundle.request.META.get('REMOTE_ADDR', oppia.api.DEFAULT_IP_ADDRESS) tracker.agent = bundle.request.META.get('HTTP_USER_AGENT', 'unknown') tracker.save() key = ApiKey.objects.get(user=u) bundle.data['api_key'] = key.key except IntegrityError: raise BadRequest(_(u'Username "%s" already in use, please select another' % username)) del bundle.data['passwordagain'] del bundle.data['password'] del bundle.data['firstname'] del bundle.data['lastname'] del bundle.data['age_range'] del bundle.data['role'] del bundle.data['location'] return bundle
def edit(request, user_id=0): if user_id != 0: if can_edit_user(request, user_id): view_user = User.objects.get(pk=user_id) else: raise exceptions.PermissionDenied else: view_user = request.user key = ApiKey.objects.get(user=view_user) if request.method == 'POST': form = ProfileForm(request.POST) build_form_options(form) if form.is_valid(): # update basic data email = form.cleaned_data.get("email") first_name = form.cleaned_data.get("first_name") last_name = form.cleaned_data.get("last_name") view_user.email = email view_user.first_name = first_name view_user.last_name = last_name view_user.save() try: user_profile = UserProfile.objects.get(user=view_user) except UserProfile.DoesNotExist: user_profile = UserProfile() user_profile.user = view_user user_profile.sex = form.cleaned_data.get("sex") user_profile.age_range = form.cleaned_data.get("age_range") user_profile.role = form.cleaned_data.get("role") user_profile.location = form.cleaned_data.get("location") user_profile.save() messages.success(request, _(u"Profile updated")) # if password should be changed password = form.cleaned_data.get("password") if password: view_user.set_password(password) view_user.save() messages.success(request, _(u"Password updated")) else: try: user_profile = UserProfile.objects.get(user=view_user) except UserProfile.DoesNotExist: user_profile = UserProfile() form = ProfileForm(initial={'username':view_user.username, 'email':view_user.email, 'first_name':view_user.first_name, 'last_name':view_user.last_name, 'sex': user_profile.sex, 'age_range': user_profile.age_range, 'role': user_profile.role, 'location': user_profile.location, 'api_key': key.key}) build_form_options(form) return render(request, 'oppia/profile/profile.html', {'form': form,})