def get_all(self): try: db_config = read_db_config() conn = MySQLConnection(**db_config) cursor = conn.cursor() cursor.callproc('getAllUsers') for result in cursor.stored_results(): user = result.fetchall() users = [] for x in user: u = User() u.id = x[0] u.password = x[1] u.last_login = x[2] u.is_superuser = x[3] u.username = x[4] u.first_name = x[5] u.last_name = x[6] u.email = x[7] u.is_staff = x[8] u.is_active = x[9] u.date_joined = x[10] users.append(u) conn.commit() cursor.close() conn.close() except Error as error: print(error) except Exception as e: print(e) return users
def get_byusername(self, username): u = None try: db_config = read_db_config() conn = MySQLConnection(**db_config) cursor = conn.cursor() args = [username] cursor.callproc('getUserByUserName', args) for result in cursor.stored_results(): user = result.fetchall() for x in user: u = User() u.id = x[0] u.password = x[1] u.last_login = x[2] u.is_superuser = x[3] u.username = x[4] u.first_name = x[5] u.last_name = x[6] u.email = x[7] u.is_staff = x[8] u.is_active = x[9] u.date_joined = x[10] conn.commit() cursor.close() conn.close() except Error as error: print(error) except Exception as e: print(e) return u
def post(self,request): user_id = request.session['user_id'] username = request.session['username'] info_form = CustomerInfoForm(request.POST) Cuserpass = ChangeUsernamePassword(request.POST) context = {} if 'edit-info' in request.POST: if info_form.is_valid(): updateinfo = CustomerInfo() u = User() updateinfo.customer_id = user_id updateinfo.home_phone = info_form.cleaned_data['home_phone'] updateinfo.work_phone = info_form.cleaned_data['work_phone'] u.first_name = info_form.cleaned_data['first_name'] u.last_name = info_form.cleaned_data['last_name'] u.email = info_form.cleaned_data['email'] updateinfo.set_user(u) self.cdao.update(updateinfo) context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] return redirect(reverse('customeraccount')) elif 'changeusernamepassword' in request.POST: if Cuserpass.is_valid(): u = self.udao.get_byid(user_id) u.id = user_id u.username = Cuserpass.cleaned_data['username'] current_password = Cuserpass.cleaned_data['password'] if check_password(current_password, u.password): x = Cuserpass.cleaned_data['password2'] u.password = make_password(x,salt=None,hasher='default') self.udao.updateUserPass(u) context['user_id'] = request.session['user_id'], context['username'] = request.session['username'] return redirect(reverse('customeraccount')) elif 'deactivate' in request.POST: self.udao.deactivateUser(user_id) return redirect(reverse('login')) else: return redirect(reverse('customeraccount'))
def get_all(self): try: db_config = read_db_config() conn = MySQLConnection(**db_config) cursor = conn.cursor() cursor.callproc('getAllCustomerUserInfo') all_customer_info = [] for result in cursor.stored_results(): customers = result.fetchall() for x in customers: currentinfo = CustomerInfo() currentinfo.customer_id = x[0] currentinfo.work_phone = x[1] currentinfo.home_phone = x[2] u = User() u.id = x[3] u.password = x[4] u.last_login = x[5] u.is_superuser = x[6] u.username = x[7] u.first_name = x[8] u.last_name = x[9] u.email = x[10] u.is_staff = x[11] u.is_active = x[12] u.date_joined = x[13] currentinfo.set_user(u) all_customer_info.append(currentinfo) cursor.close() conn.close() except Error as error: print(error) except Exception as e: print(e) return all_customer_info
def post(self, request): #settings.configure(DEBUG=True) loginform = LoginForm(request.POST) registerform = RegisterUserForm(request.POST) context = { 'user_id': None } if 'login-user' in request.POST: if loginform.is_valid(): # Get user data from database user = User() user.username = loginform.cleaned_data['username'] user = self.udao.get_byusername(user.username) input_password = loginform.cleaned_data['password'] # Validate login if check_password(input_password, user.password): # Store session data request.session['user_id'] = user.id request.session['username'] = user.username request.session['user_is_staff'] = user.is_staff context['text'] = 'Yay password' context['user_id'] = request.session['user_id'] context['username'] = request.session['username'] self.udao.updateLastLogin(user.id) if user.is_staff == 0 and user.is_active == 1: return redirect(reverse('home')) elif user.is_active == 0 and user.is_staff == 0: self.udao.activateUser(user.id) return redirect(reverse('home')) else: return redirect(reverse('adminindex')) # Handle if password is bad else: loginform = LoginForm() registerform = RegisterUserForm() context = { 'loginform': loginform, 'registerform': registerform, 'login_error': 'Either username or password is incorrect' } return render(request, self.template_name, context) else: context['text'] = 'try again' return render(request, self.template_name, context) if 'create-user' in request.POST: if registerform.is_valid(): u = User() u.first_name = registerform.cleaned_data['first_name'] u.last_name = registerform.cleaned_data['last_name'] u.email = registerform.cleaned_data['email'] u.username = registerform.cleaned_data['username'] x = registerform.cleaned_data['password'] u.is_superuser = 0 u.is_active = 1 u.is_staff = 0 u.password = make_password(x,salt=None,hasher='default') self.udao.create(u) user = self.udao.get_byusername(u.username) customer = CustomerInfo() customer.customer_id = user.id customer.work_phone = registerform.cleaned_data['work_phone'] customer.home_phone = registerform.cleaned_data['home_phone'] self.cdao.create(customer) loginform = LoginForm() registerform = RegisterUserForm() context = { 'loginform': loginform, 'registerform': registerform } return render(request, self.template_name, context)