2018-08-20 08:21:25 +00:00
|
|
|
# coding=utf-8
|
|
|
|
from django.shortcuts import render, HttpResponseRedirect, get_object_or_404, HttpResponse
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from metadata.models import LineItem, Order, Favorite, Cart
|
|
|
|
from django.contrib import messages
|
|
|
|
from django.contrib.auth import authenticate, login, logout
|
|
|
|
|
|
|
|
|
|
|
|
# Create your views here.
|
|
|
|
|
|
|
|
|
|
|
|
def user_login(request):
|
|
|
|
username = None
|
|
|
|
password = None
|
|
|
|
if request.method == 'POST':
|
|
|
|
if not request.POST.get('username'):
|
|
|
|
messages.error(request, u'请输入用户名')
|
|
|
|
else:
|
|
|
|
username = request.POST.get('username')
|
|
|
|
if not request.POST.get('password'):
|
|
|
|
messages.error(request, u'请输入密码')
|
|
|
|
else:
|
|
|
|
password = request.POST.get('password')
|
|
|
|
if username is not None and password is not None:
|
|
|
|
user = authenticate(username=username, password=password)
|
|
|
|
if user is not None:
|
|
|
|
if user.is_active:
|
|
|
|
login(request, user)
|
|
|
|
cart = Cart.objects.filter(user_id=user.id)
|
|
|
|
if not cart:
|
|
|
|
cart = Cart()
|
|
|
|
cart.user_id = user.id
|
|
|
|
cart.save()
|
|
|
|
count = LineItem.objects.filter(cart=user.cart).count()
|
|
|
|
request.session['count'] = count
|
|
|
|
return HttpResponseRedirect('/')
|
|
|
|
else:
|
|
|
|
messages.error(request, 'disabled username')
|
|
|
|
else:
|
|
|
|
messages.error(request, 'invalid user')
|
|
|
|
return render(request, 'account/login.html')
|
|
|
|
|
|
|
|
|
|
|
|
def user_register(request):
|
|
|
|
username = None
|
|
|
|
email = None
|
|
|
|
password = None
|
|
|
|
confirm_password = None
|
|
|
|
flag = False
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
if not request.POST.get('username'):
|
|
|
|
messages.error(request, u'请输入用户名')
|
|
|
|
else:
|
|
|
|
username = request.POST.get('username')
|
|
|
|
if not request.POST.get('email'):
|
|
|
|
messages.error(request, u'请输入邮箱')
|
|
|
|
else:
|
|
|
|
email = request.POST.get('email')
|
|
|
|
if not request.POST.get('password'):
|
|
|
|
messages.error(request, u'请输入密码')
|
|
|
|
else:
|
|
|
|
password = request.POST.get('password')
|
|
|
|
if not request.POST.get('confirm_password'):
|
|
|
|
messages.error(request, u'请再次输入密码')
|
|
|
|
else:
|
|
|
|
confirm_password = request.POST.get('confirm_password')
|
|
|
|
|
|
|
|
if password is not None and confirm_password is not None:
|
|
|
|
if password == confirm_password:
|
|
|
|
flag = True
|
|
|
|
else:
|
|
|
|
messages.error(request, u'两次输入的密码不一致,请重新输入')
|
|
|
|
|
|
|
|
filter_result = User.objects.filter(username=username)
|
|
|
|
if len(filter_result) > 0:
|
|
|
|
messages.error(request, u"对不起,您输入的用户名已被注册")
|
|
|
|
return render(request, "account/register.html")
|
|
|
|
|
|
|
|
if username is not None and password is not None and confirm_password is not None and email is not None and flag:
|
|
|
|
user = User.objects.create_user(username, email, password)
|
|
|
|
user.is_active = True
|
|
|
|
user.save()
|
|
|
|
messages.success(request, u'注册成功')
|
|
|
|
return HttpResponseRedirect('/account/login')
|
|
|
|
|
|
|
|
return render(request, 'account/register.html')
|
|
|
|
|
|
|
|
|
|
|
|
def user_logout(request):
|
|
|
|
logout(request)
|
|
|
|
messages.error(request, u'您已经成功退出系统')
|
|
|
|
return HttpResponseRedirect('/')
|
|
|
|
|
|
|
|
|
|
|
|
def cart(request):
|
|
|
|
user = request.user
|
|
|
|
current_cart = user.cart
|
|
|
|
cart_id = current_cart.id
|
|
|
|
items = LineItem.objects.filter(cart_id=cart_id).all()
|
|
|
|
|
|
|
|
return render(request, 'account/my_cart.html', {"items": items})
|
|
|
|
|
|
|
|
|
|
|
|
def favorite(request):
|
|
|
|
user = request.user
|
|
|
|
favorites = Favorite.objects.filter(user=user).all()
|
|
|
|
return render(request, 'account/my_favorite.html', {"favorites": favorites})
|
|
|
|
|
|
|
|
|
|
|
|
def delete_cart_data(request, id):
|
|
|
|
line_item = get_object_or_404(LineItem, pk=id)
|
|
|
|
line_item.delete()
|
|
|
|
messages.success(request, u'您购物车中的数据已被成功删除!')
|
|
|
|
|
|
|
|
return HttpResponseRedirect('/account/cart')
|
|
|
|
|
|
|
|
|
|
|
|
def delete_favorite(request, id):
|
|
|
|
favorite = get_object_or_404(Favorite, pk=id)
|
|
|
|
favorite.delete()
|
|
|
|
messages.success(request, u'您收藏的数据已被成功删除!')
|
|
|
|
|
|
|
|
return HttpResponseRedirect('/account/favorite')
|
|
|
|
|
|
|
|
|
|
|
|
def add_to_order(request):
|
|
|
|
if request.method == 'POST':
|
|
|
|
item_ids = request.POST.getlist('item_ids')
|
|
|
|
items = LineItem.objects.filter(id__in=item_ids).all()
|
|
|
|
|
|
|
|
return render(request, 'account/new_order.html', {"items": items})
|
|
|
|
|
|
|
|
|
|
|
|
def save_to_order(request):
|
|
|
|
if request.method == 'POST':
|
|
|
|
real_name = request.POST.get('real_name')
|
|
|
|
email = request.POST.get('email')
|
|
|
|
phone = request.POST.get('phone')
|
|
|
|
post_code = request.POST.get('post_code')
|
|
|
|
unit = request.POST.get('unit')
|
|
|
|
address = request.POST.get('address')
|
|
|
|
purpose = request.POST.get('purpose')
|
|
|
|
item_ids = request.POST.getlist('item_ids')
|
|
|
|
user = request.user
|
|
|
|
order = Order(real_name=real_name, email=email, phone=phone, post_code=post_code,
|
|
|
|
unit=unit, address=address, purpose=purpose, user=user)
|
|
|
|
order.save()
|
|
|
|
for item_id in item_ids:
|
|
|
|
item = get_object_or_404(LineItem, pk=item_id)
|
|
|
|
item.cart_id = None
|
|
|
|
item.save()
|
|
|
|
order.lineitem_set.add(item)
|
|
|
|
|
|
|
|
messages.success(request, u'您的数据申请已经提交')
|
|
|
|
return HttpResponseRedirect('/account/order')
|
|
|
|
|
|
|
|
|
|
|
|
def my_order(request):
|
|
|
|
user = request.user
|
|
|
|
orders = Order.objects.filter(user=user).exclude(lineitem=None)
|
|
|
|
return render(request, 'account/my_order.html', {"orders": orders})
|
2018-10-21 12:18:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def order_audits(request):
|
|
|
|
orders = Order.objects.exclude(lineitem=None)
|
|
|
|
return render(request, 'account/order_audits.html', {"orders": orders})
|