def sign_up(request): if request.method == 'POST': params = request.POST.copy() params['password'] = make_password(params['password']) params['created_at'] = timezone.now() params['updated_at'] = timezone.now() params['date_joined'] = timezone.now() params['last_login'] = timezone.now() form = UserForm(data = params, auto_id=True) if form.is_valid(): user = form.save() user.is_active = True user.save() form_msg = _("the user was successfully registered") return render(request, 'users/_sign_up_success.html', {'form_msg': form_msg}) else: form_errors = form.errors # form_cleaned = form.cleaned_data return render(request, 'users/_sign_up_errors.html', {'form_errors': form_errors}) else: form = UserForm(auto_id=True) context = {'form': form} return render(request, 'home/index.html', context)
def create(request): user = UserForm(request.POST) if user.is_valid(): user.save() return HttpResponse("Successfully create new account") else: return HttpResponse("Your input data is not valid </br>" + str(user._errors))
def register(request): # Redirect user if already logged in. if request.user: if request.user.is_authenticated(): return HttpResponseRedirect('/home') # 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() # 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 # Did the user provide a profile picture? # If so, we need to get it from the input form and put it in the UserProfile model. if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # Now we save the UserProfile model instance. profile.save() return HttpResponseRedirect('/home') # Invalid form or forms - mistakes or something else? # Print problems to the terminal. # They'll also be shown to the user. else: context_dict = { 'user_form': user_form, 'profile_form': profile_form, } return render(request, 'index.html', context_dict) # Not a HTTP POST, this should never happen else: context_dict = { 'user_form': UserForm(), 'profile_form': UserProfileForm(), 'error_messages': ['Invalid HTTP request.'] } return render(request,'index.html',context_dict)
def profile(request): if request.method == 'GET': form = UserForm(request.GET) user = form.data[u'user'] context = {'user': user, 'logged_in': True} if User.objects.filter(username=user): context['user_exists'] = "true" context['username'] = user context['user_stagecount'] = Stage.objects.filter( owner=user).count() context['user_stages'] = [] for stage in Stage.objects.filter(owner=user): context['user_stages'].append(stage) #context['user_result'] = "USER EXISTS" else: context['user_exists'] = "false" #context['user_result'] = "USER DOES NOT EXIST" return render(request, 'users/profile.html', context) else: context = {} return render(request, 'users/profile.html', context)
def personal_data(request): results = {} if request.is_ajax(): form = DoctorForm(request.POST, instance=request.user.userprofile.current_doctor) user_form = UserForm(request.POST, instance=request.user.userprofile.current_doctor.refer_userprofile.user) if form.is_valid() and user_form.is_valid(): form.save() user_form.save() results['return'] = True else: print results['errors'] = form.errors + user_form.errors results['return'] = False else: results['return'] = False return HttpResponse(json.dumps(results))
def get_login(request): context = {'message': ""} if request.method == 'POST': form = UserForm(request.POST) if u'logout' in form.data: logout(request) return redirect('/') username = form.data[u'username'] password = form.data[u'passwd'] if u'login' in form.data: user = authenticate(username=username, password=password) if user is not None and user.is_active: login(request, user) result = UsersModel.SUCCESS else: result = UsersModel.ERR_BAD_CREDENTIALS elif u'adduser' in form.data: if User.objects.filter(username=username): result = UsersModel.ERR_USER_EXISTS elif len(username ) == 0 or len(username) > UsersModel.MAX_USERNAME_LENGTH: result = UsersModel.ERR_BAD_USERNAME elif len(password) > UsersModel.MAX_PASSWORD_LENGTH: result = UsersModel.ERR_BAD_PASSWORD else: new_user = User.objects.create_user(username, '', password) new_user.save() new_user = authenticate(username=username, password=password) login(request, new_user) result = UsersModel.SUCCESS else: result = None # This should never be reached if result == UsersModel.SUCCESS: return redirect('/game') elif result == UsersModel.ERR_BAD_USERNAME: context['message'] = "ADD USER ERROR: Username is " + ( "empty" if len(username) == 0 else "too long") else: context['message'] = errors[result] elif request.user.is_authenticated(): return redirect('/') else: form = UserForm() context['form'] = form return render(request, 'static_pages/index.html', context)
def personal_data(request): results = {} if request.is_ajax(): user_form = UserForm(request.POST, instance=request.user) userprofile_form = UserProfileForm(request.POST, instance=request.user.userprofile) if user_form.is_valid() and userprofile_form.is_valid(): user_form.save() userprofile_form.save() results['return'] = True else: combo = userprofile_form.errors combo.update(user_form.errors) results['errors'] = combo results['return'] = False else: results['return'] = False return HttpResponse(json.dumps(results))
def post(self, request, *args, **kwargs): #user = self.instance.user user = EvoUser.objects.get(pk=request.user.id) form_class = self.get_form_class() params = request.POST.copy() params['password'] = user.password params['created_at'] = user.created_at params['updated_at'] = timezone.now() params['date_joined']= user.date_joined params['last_login'] = user.last_login form = UserForm(data = params, instance=user, auto_id=True) if form.is_valid(): user = form.save() form_msg = _("the user was successfully registered") return render(request, 'users/_edit_success.html', {'form_msg': form_msg}) else: form_errors = form.errors form_cleaned = form.cleaned_data return render(request, 'users/_edit_errors.html', {'form_errors': form_errors})
def user_settings(request): up = request.user.userprofile doc = up.current_doctor c = {'userprofile_id': request.user.userprofile.id, 'user_form': UserForm(instance=doc.refer_userprofile.user), 'personal_data_form': DoctorForm(instance=doc), 'settings_form': SettingsForm(instance=doc), 'avatar': doc.picture, 'text_form': TextForm(instance=doc), 'color_forms': [], 'password_change_form': PasswordChangeForm(user=request.user), 'invoice': doc.get_active_invoice(), 'collaborator_form': CollaboratorForm(), 'collaborators1': UserProfile.objects.filter(doctors=doc), 'collaborators2': Collaborator.objects.filter(doctor=doc), 'new_invoice': MiniInvoiceForm() if not doc.already_use_free_invoice() else NoFreeMiniInvoiceForm(), 'doctor_profile': doc.refer_userprofile} for st in settings.SLOT_TYPE: d = {'id': doc.get_colorslot(st[0]).id, 'name': st[1], 'form': ColorForm(instance=doc.get_colorslot(st[0]))} c['color_forms'].append(d) return render(request, 'config.tpl', c)
def search(request): if request.method == 'GET': form = UserForm(request.GET) query = form.data[u'query'] context = {} context = {'query': query} if User.objects.filter(username=query): context['user_exists'] = "true" context['username'] = query context['user_stagecount'] = Stage.objects.filter( owner=query).count() context['user_stages'] = [] for stage in Stage.objects.filter(owner=query): context['user_stages'].append(stage) #context['user_result'] = "USER EXISTS" else: context['user_exists'] = "false" #context['user_result'] = "USER DOES NOT EXIST" if Stage.objects.filter(name__icontains=query): context['stages_exists'] = "true" #context['stages_stagecount'] = Stage.objects.filter(name=query).count() context['stages_stagecount'] = Stage.objects.filter( name__icontains=query).count() context['stages_stages'] = [] for stage in Stage.objects.filter(name__icontains=query): context['stages_stages'].append(stage) else: context['stages_exists'] = "false" return render(request, 'users/search.html', context) else: context = {} return render(request, 'users/search.html', context)
def edit_user(request, username): profile = None try: user = User.objects.get(username=username) try: profile = user.profile except Profile.DoesNotExist: pass # ! Just go ahead ! except User.DoesNotExist: return redirect(home) if request.user.has_perm('auth.change_user') or request.user.username == username: if request.method == 'POST': form = UserForm(request.POST, instance=user) if profile: profile_form = ProfileForm(request.POST, instance = profile) else: profile_form = ProfileForm(request.POST) if form.is_valid(): updated_user = form.save() if profile_form.is_valid(): new_profile = profile_form.save(commit=False) new_profile.user = updated_user new_profile.save() return redirect(user_profile, username=username) else: form = UserForm(instance=user) if profile: profile_form = ProfileForm(instance=profile) else: profile_form = ProfileForm() context = {'form': form, 'username': username, 'profile_form': profile_form, } return render(request, "edit_user.html", context) return redirect(home)
def register_social(request): 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() # 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 # Did the user provide a profile picture? # If so, we need to get it from the input form and put it in the UserProfile model. if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # Now we save the UserProfile model instance. profile.save() user.backend = 'django.contrib.auth.backends.ModelBackend' login(request, user) backend = request.session['partial_pipeline']['backend'] return redirect('social:complete', backend=backend) # 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) context_dict = { 'user_form': user_form, 'profile_form': profile_form, 'backend': request.session['partial_pipeline']['backend'], } return render(request, 'register_social.html', context_dict) else: partial_user_data = request.session.get('partial_user_data') context_dict = { 'user_form': UserForm(initial=partial_user_data), 'profile_form': UserProfileForm(), 'backend': request.session['partial_pipeline']['backend'], } request.session.pop('partial_user_data') return render(request, 'register_social.html', context_dict)