Professional Documents
Culture Documents
1
1. Use case tổng quan hệ thống
2
3.1. Chức năng Đăng nhập :
3
3.5. Chức năng Quản lý bán hàng
4
3.7. Chức năng Đề xuất sản phẩm
5
3.9. năng quản lý khách hàng và nhân viên
6
4.2. Product Service:
4.3. CommentService:
4.4. ImageService
4.5. CartService
7
4.6. OrderService
8
5.3. Comment Service :
9
5.7. Payment Service :
def __str__(self):
@csrf_exempt
10
def user_login(request):
uname = request.POST.get("User Name")
password = request.POST.get("Password")
resp = {}
if uname and password:
respdata = user_validation(uname, password)
# If a user is valid then it gives success as a response.
if respdata == "Valid User":
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['message'] = 'Welcome to Ecommerce website......'
elif respdata == "Invalid User":
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'Invalid credentials.'
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'All fields are mandatory.'
return HttpResponse(json.dumps(resp), content_type='application/json')
@csrf_exempt
def registration_req(request):
### The Following are the input fields.
fname = request.POST.get("First Name")
lname = request.POST.get("Last Name")
email = request.POST.get("Email Id")
mobile = request.POST.get("Mobile Number")
password = request.POST.get("Password")
cnf_password = request.POST.get("Confirm Password")
address = request.POST.get("Address")
resp = {}
#### In this if statement, checking that all fields are available.
if fname and lname and email and mobile and password and cnf_password and
address:
if len(str(mobile)) == 10:
if password == cnf_password:
respdata = data_insert(fname, lname, email, mobile,
password, address)
### If it returns value then will show success.
if respdata:
11
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['message'] = 'User is registered Successfully.'
### If it is not returning any value then the show will fail.
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'Unable to register user, Please try again.'
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'Password and Confirm Password should be same.'
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'Mobile Number should be 10 digit.'
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'All fields are mandatory.'
return HttpResponse(json.dumps(resp), content_type='application/json')
def getAllUser(request) :
resp = {}
data = []
if request.method == 'GET':
users = user_registration.objects.all()
for user in users.values():
data.append(user)
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['data'] = data
return HttpResponse(json.dumps(resp), content_type='application/json')
6.2. Product service
class product_details(models.Model):
product_id = models.CharField(max_length=10)
product_category = models.CharField(max_length=50)
product_name = models.CharField(max_length=100)
availability = models.CharField(max_length=15)
price = models.CharField(max_length=10)
12
def __str__(self):
return '%s %s %s %s %s' % (self.product_id, self.product_category,
self.product_name, self.availability, self.price)
13
resp['message'] = 'Data is not available.'
return HttpResponse(json.dumps(resp), content_type='application/json')
@csrf_exempt
def add_comment(request):
uname = request.POST.get("User Name")
proid = request.POST.get("Product id")
comment = request.POST.get("Comment")
rating = request.POST.get("Rating") # one to five star
# user_id = models.IntegerField()
# product_id = models.IntegerField()
url = "http://127.0.0.1:8001/get_product_data/{}".format(proid)
d1 = {}
# d1["User Name"] = uname
# d1['Product Id'] = proid
# data = json.dumps(d1)
headers = {'Content-Type': 'application/json'}
response = requests.get(url, headers=headers)
val1 = json.loads(response.content.decode('utf-8'))
product_name = val1.get("data").get("product_name")
respdata =comment_data_insert(comment=comment, product_id= proid, rating=rating,
uname=uname)
resp = {}
if respdata:
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['message'] = 'Product is ready to dispatch.'
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'Failed to update shipment details.'
return HttpResponse(json.dumps(resp), content_type='application/json')
14
# if var1
#
@csrf_exempt
def show_product_comment(request):
uname = request.POST.get("User Name")
proid = request.POST.get("Product id")
comments = product_comment.objects.filter(product_id=proid, uname=uname)
data = []
resp = {}
# This will fetch the data from the database.
for tbl_value in comments.values():
data.append(tbl_value)
# If data is available then it returns the data.
if data:
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['data'] = data
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'Data is not available.'
@csrf_exempt
def add_image(request, product_id):
prod_id = product_id
url = request.POST.get('URL')
des = request.POST.get('Description')
resp = {}
if url and des:
image = Image(product_id=prod_id, url=url, description=des)
if image:
image.save()
resp['status'] = 'Success'
15
resp['status_code'] = '200'
resp['message'] = f"Add image to product {prod_id} successfully"
else :
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = f"Data is invalid"
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = f"All fields are mandatory"
return HttpResponse(json.dumps(resp), content_type='application/json')
@csrf_exempt
def delete_image(request, product_id):
resp= {}
if request.method == 'DELETE':
images = Image.objects.filter(product_id=product_id)
for image in images:
image.delete()
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['message'] = f"Delete image successfully"
else :
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = f"Failed method"
return HttpResponse(json.dumps(resp), content_type='application/json')
16
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['data'] = data
return HttpResponse(json.dumps(resp), content_type='application/json')
#lấy thông tin các sản phẩm trong giỏ hàng theo người dùng
def getCartItemByUser(request,user_id):
resp = {}
data = []
list_cart_items = Cart.objects.filter(user_id=user_id)
if len(list_cart_items) == 0 :
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['message'] = f"User {user_id} haven't add any product yet"
else :
for cart_item in list_cart_items.values():
data.append(cart_item)
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['data'] = data
return HttpResponse(json.dumps(resp), content_type='application/json')
17
product_id = request.POST.get("Product Id")
quantity = request.POST.get("Quantity")
resp = {}
if user_id and product_id and quantity:
cart = Cart(user_id=user_id, product_id=product_id, quantity=quantity)
cart.save()
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['message'] = 'Add to cart successfully'
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = "All fields are mandatory"
return HttpResponse(json.dumps(resp), content_type='application/json')
#cập nhật giỏ hàng
@csrf_exempt
def update_cart(request, cart_id):
if request.method == 'POST':
quantity = request.POST.get("Quantity")
resp = {}
list_cart = Cart.objects.filter(pk=cart_id).values()
if len(list_cart) > 0:
cart = list_cart[0]
cart_update = Cart(pk=cart_id, user_id=cart['user_id'],
product_id=cart['product_id'],
quantity=quantity)
cart = Cart.objects.get(pk=cart_id)
cart.delete()
cart_update.save()
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['message'] = 'Update to cart successfully'
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = f"Not found cart {cart_id}"
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = "Failed"
return HttpResponse(json.dumps(resp), content_type='application/json')
18
@csrf_exempt
def delete_cart(request, cart_id):
resp = {}
if request.method == 'DELETE':
cart = Cart.objects.get(pk=cart_id)
if cart:
print(cart)
cart.delete()
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['message'] = 'Delete cart successfully'
else :
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = f"Not found {cart_id}"
else :
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'Delete cart unsuccessfully'
return HttpResponse(json.dumps(resp), content_type='application/json')
19
#Tạo một order mới với input là User Id và Cart Id
@csrf_exempt
def create_order(request):
user_id = request.POST.get("User Id")
cart_id = request.POST.get("Cart Id")
print(cart_id)
total = getTotal(cart_id)
print(total)
shipment_id = request.POST.get("Shipment Id")
resp = {}
if user_id and cart_id and total and shipment_id:
order = Order(user_id=user_id, cart_id=cart_id, total=total,
shipment_id=shipment_id)
if order:
order.save()
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['message'] = 'Create a new order successfully'
else:
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'Data is invalid'
else :
resp['status'] = 'Failed'
resp['status_code'] = '400'
resp['message'] = 'All fields are mandatory'
return HttpResponse(json.dumps(resp, default=str), content_type='application/json')
20
def checkProduct(id):
url = 'http://127.0.0.1:8001/get_product_data/'
listProduct = requests.get(url).json()
for product in listProduct['data']:
if product['product_id'] == id:
return True
return False
21
6.7. Shipment service :
class shipment(models.Model):
### The following are the fields of our table.
fname = models.CharField(max_length=50)
lname = models.CharField(max_length=50)
email = models.CharField(max_length=50)
mobile = models.CharField(max_length=12)
address = models.CharField(max_length=200)
shipment_status = models.CharField(max_length=20)
order_id = models.IntegerField(max_length=10, default=1)
product_id=models.CharField(max_length=10, default="")
total = models.FloatField(default=1)
resp = {}
23
6.8. Payment service
class payment_status(models.Model):
### The following are the fields of our table.
username = models.CharField(max_length=50)
order_id = models.IntegerField(default=1)
status = models.CharField(max_length=15)
#cập nhật danh sách order sau khi thanh toán thành công
def order_update(orderid):
url = "http://127.0.0.1:8005/update_order/{}".format(orderid)
response = requests.delete(url)
def getAllPayment(request) :
resp = {}
data = []
if request.method == 'GET':
list_pay = paystat.objects.all()
for pay in list_pay.values():
data.append(pay)
resp['status'] = 'Success'
resp['status_code'] = '200'
resp['data'] = data
return HttpResponse(json.dumps(resp), content_type='application/json')
25
7. Sử dụng kỹ thuật học máy để phân loại khách hàng hay tư vấn sản phẩm
7.1. Dữ liệu
Nguồn dữ liệu : Customer Segment Classification | Kaggle
Giải thích : Data trên từ thông tin của khách hàng để đưa ra các phân loại khách hàng
theo 4 phân loại :
Segment A : Khách hàng khá giả, tự chủ, có nguồn thu nhập cao và thời gian mua sẵm
cao (độ tuổi từ khoảng 40-50, có gia đình, công việc
Segment B : Khách hàng cũng có mức thu nhập ổn định hoặc cao, nhưng lại đang tập
trung vào sự nghiệp, điểm chi tiêu thấp ➔ mua hàng và ưu tiên công việc hơn các hoạt
động giải trí : 20-30t, kinh nghiệm 0-1 năm
Segment C : KH có mức thu nhập và chi tiêu trung bình, có xu hướng thận trọng và thực
tế khi mua hàng, học ưu tiên giá trị và chất lượng. : Người đã có gia đình, mức thu nhập
vừa phải, …
Segment D : KH có thu nhập và điểm chi tiêu thấp hơn, có xu hướng ít quan tâm đến mua
sắm và giải trí, ưu tiên các nhu yếu phẩm cơ bản và có thu nhập hạn chế : công nhân cao
tuổi, đã nghỉ hưu, có thu nhập ổn định…
7.2. Loading data
26
7.3. Data preliminary exploration
27
7.5. Features selection
28
29
7.6. Modelling data
30
7.7. Machine learning
31
7.8. Deep learning
32
33