def register_view(request): """ Handle user registration :param request: Request to handle """ context = {} if request.method == 'POST': # handle submitted registration forms # TODO refactor RegisterForm creation to somewhere else form = RegisterForm(request.POST) if form.is_valid(): data = form.cleaned_data # TODO verify user was actually created try: create_new_user(data) except IntegrityError: form.add_error('username', 'Username taken.') else: user = authenticate(username=data.get('username'), password=data.get('password')) login(request, user) return redirect('index') else: # display blank form form = RegisterForm() context['form'] = form # assemble page title title_elements = [_('REGISTRATION')] create_page_title(context, title_elements) return render(request, 'register.html', context)
def generate_user_form(user, form_data=None): form = RegisterForm(form_data) if form_data else RegisterForm() up = user.userprofile form.fields.pop('username') for field in form.fields.values(): field.required = False if form_data and form.is_valid(): form_data = form.cleaned_data if form_data.get('password'): user.set_password(form_data.get('password')) if form_data.get('email'): user.email = form_data.get('email') user.save() for field in ['dj_name', 'location']: if form_data.get(field): setattr(up, field, form_data.get(field)) if form_data.get('playside'): up.play_side = form_data.get('playside') if form_data.get('best_techniques'): up.best_techniques = form_data.get('best_techniques') up.save() data = { 'dj_name': up.dj_name, 'playside': up.play_side, 'email': user.email, 'location': up.location, 'best_techniques': up.best_techniques } new_form = RegisterForm(data) new_form.fields.pop('username') for field in new_form.fields.values(): field.required = False for error in form.errors.items(): print(error) new_form.add_error(error[0], error[1]) return new_form
def generate_user_form(user, form_data=None): form = RegisterForm(form_data) if form_data else RegisterForm() up = user.userprofile form.fields.pop('username') for field in form.fields.values(): field.required = False if form_data and form.is_valid(): form_data = form.cleaned_data if form_data.get('password'): user.set_password(form_data.get('password')) if form_data.get('email'): user.email = form_data.get('email') user.save() for field in ['dj_name', 'dancer_name', 'location']: if form_data.get(field): setattr(up, field, form_data.get(field)) if form_data.get('playside'): up.play_side = form_data.get('playside') if form_data.get('best_techniques'): up.best_techniques = form_data.get('best_techniques') up.save() data = { 'dj_name': up.dj_name, 'dancer_name': up.dancer_name, 'playside': up.play_side, 'email': user.email, 'location': up.location, 'best_techniques': up.best_techniques } new_form = RegisterForm(data) new_form.fields.pop('username') for field in new_form.fields.values(): field.required = False for error in form.errors.items(): print(error) new_form.add_error(error[0], error[1]) return new_form