def delete_employee(request): employee = Employee.objects.get(id=request.POST['employee']) user = User.objects.get(employee=employee) store = Store.objects.get(id=request.POST['store']) boss = get_boss(request.user) employee.delete() user.delete() employees = Employee.objects.filter(boss=boss, store=store).order_by('-type') employees_dict = {} store = model_to_dict(store) for current_employee in employees: employee_user = User.objects.get(employee_id=current_employee.id) employee_id = current_employee.id employees_dict[employee_id] = { 'first_name': employee_user.first_name, 'last_name': employee_user.last_name, 'type': current_employee.type, 'username': employee_user.username, 'email': employee_user.email, 'id': employee_id } store['employees'] = employees_dict return render_json({'store': store, 'success': True})
def user_login(request): helper.check_req_data(['username', 'password'], request.POST) username = request.POST['username'].strip().lower() password = request.POST['password'].strip().lower() if '@' in username: # Check Email if not re.match(r"[^@]+@[^@]+\.[^@]+", username): data = {'success': False, 'error_msg': 'Invalid email'} return HttpResponseBadRequest(json.dumps(data), 'application/json') # Check if the user exist first if not User.objects.filter(email=username).exists(): data = {'success': False, 'error_msg': 'User does not exists.'} return HttpResponseBadRequest(json.dumps(data), 'application/json') # Validate password user = authenticate(email=username, password=password) else: # Check if username is over 15 characters if len(username) > 15: data = {'success': False, 'error_msg': 'Username to long.'} return HttpResponseBadRequest(json.dumps(data), 'application/json') # Check if the user exist first if not User.objects.filter(username=username).exists(): data = {'success': False, 'error_msg': 'User does not exists.'} return HttpResponseBadRequest(json.dumps(data), 'application/json') # Validate password user = authenticate(username=username, password=password) login(request, user) return render_json({'success': True})
def edit_employee(request): helper.check_req_data([ 'username', 'email', 'password', 'first_name', 'last_name', 'type', 'store', 'employee' ], request.POST) username = request.POST['username'].strip().lower() email = request.POST['email'].strip().lower() password = request.POST['password'] first_name = request.POST['first_name'] last_name = request.POST['last_name'] user_type = request.POST['type'] store = Store.objects.get(id=request.POST['store']) employee = Employee.objects.get(id=request.POST['employee']) boss = get_boss(request.user) # Check first name if not len(first_name): print username data = {'success': False, 'error_msg': 'Must have a first name.'} return HttpResponseBadRequest(json.dumps(data), 'application/json') # Check last name if not len(last_name): print username data = {'success': False, 'error_msg': 'Must have a last name.'} return HttpResponseBadRequest(json.dumps(data), 'application/json') # Check username if len(username) <= 2 or len(username) >= 16: print username data = { 'success': False, 'error_msg': 'Username must be between 3 to 15 characters.' } return HttpResponseBadRequest(json.dumps(data), 'application/json') # Check Email if not re.match(r"[^@]+@[^@]+\.[^@]+", email): data = {'success': False, 'error_msg': 'Invalid email.'} return HttpResponseBadRequest(json.dumps(data), 'application/json') # Check if valid password: Must be 8 or more characters and contain a combo of letters and numbers if not len(password) >= 8: data = { 'success': False, 'error_msg': 'Password must be 8 characters or more.' } return HttpResponseBadRequest(json.dumps(data), 'application/json') if not bool(re.search(r'\d', password)) or not bool( re.search(r'[a-zA-Z]', password)): data = {'success': False, 'error_msg': 'Invalid password.'} return HttpResponseBadRequest(json.dumps(data), 'application/json') # Check if email exist in the database if User.objects.filter(username=username).exists(): data = {'success': False, 'error_msg': 'Username exists.'} return HttpResponseBadRequest(json.dumps(data), 'application/json') # Check if email exist in the database if User.objects.filter(email=email).exists(): data = {'success': False, 'error_msg': 'Email exists.'} return HttpResponseBadRequest(json.dumps(data), 'application/json') employee.type = user_type employee.save() user = User.objects.get(employee=employee) user.username = username user.email = email user.password = helper.create_password(password) user.first_name = first_name user.last_name = last_name user.save() employees = Employee.objects.filter(boss=boss, store=store).order_by('-type') employees_dict = {} store = model_to_dict(store) for current_employee in employees: employee_user = User.objects.get(employee_id=current_employee.id) employee_id = current_employee.id employees_dict[employee_id] = { 'first_name': employee_user.first_name, 'last_name': employee_user.last_name, 'type': current_employee.type, 'username': employee_user.username, 'email': employee_user.email, 'id': employee_id } store['employees'] = employees_dict return render_json({'store': store, 'success': True})
def register(request): helper.check_req_data([ 'username', 'email', 'password', 'first_name', 'last_name', 'business_name' ], request.POST) username = request.POST['username'].strip().lower() email = request.POST['email'].strip().lower() password = request.POST['password'] first_name = request.POST['first_name'] last_name = request.POST['last_name'] business_name = request.POST['business_name'] # Check first name if not len(first_name): print username data = {'success': False, 'error_msg': 'Must have a first name.'} return HttpResponseBadRequest(json.dumps(data), 'application/json') # Check last name if not len(last_name): print username data = {'success': False, 'error_msg': 'Must have a last name.'} return HttpResponseBadRequest(json.dumps(data), 'application/json') # Check business name if not len(business_name): print username data = {'success': False, 'error_msg': 'Must have a business name.'} return HttpResponseBadRequest(json.dumps(data), 'application/json') # Check username if len(username) <= 2 or len(username) >= 16: print username data = { 'success': False, 'error_msg': 'Username must be between 3 to 15 characters.' } return HttpResponseBadRequest(json.dumps(data), 'application/json') # Check Email if not re.match(r"[^@]+@[^@]+\.[^@]+", email): data = {'success': False, 'error_msg': 'Invalid email.'} return HttpResponseBadRequest(json.dumps(data), 'application/json') # Check if valid password: Must be 8 or more characters and contain a combo of letters and numbers if not len(password) >= 8: data = { 'success': False, 'error_msg': 'Password must be 8 characters or more.' } return HttpResponseBadRequest(json.dumps(data), 'application/json') if not bool(re.search(r'\d', password)) or not bool( re.search(r'[a-zA-Z]', password)): data = {'success': False, 'error_msg': 'Invalid password.'} return HttpResponseBadRequest(json.dumps(data), 'application/json') # Check if email exist in the database if User.objects.filter(username=username).exists(): data = {'success': False, 'error_msg': 'Username exists.'} return HttpResponseBadRequest(json.dumps(data), 'application/json') # Check if email exist in the database if User.objects.filter(email=email).exists(): data = {'success': False, 'error_msg': 'Email exists.'} return HttpResponseBadRequest(json.dumps(data), 'application/json') user_settings = Settings.objects.create() business = Business.objects.create(name=business_name) boss = Boss.objects.create(settings=user_settings, business=business) User.objects.create(username=username, email=email, password=helper.create_password(password), first_name=first_name, last_name=last_name, boss=boss) # Validate password auth_user = authenticate(email=email, password=password) # Login user login(request, auth_user) return render_json({'success': True})