def like(req): item = Item.objects.get_by_id(req.REQUEST.get('id')) if not item: return HttpResponse(json.dumps({'status': 0, 'msg': u'该产品不存在'})) LikeRecord.objects.get_or_create(ip=get_ip(req), item=item) item.like_count += 1 item.save() return HttpResponse(json.dumps({'status': 1, 'msg': u'喜欢成功'}))
def detail(req, id): item = Item.objects.get_by_id(id) cate = Category.objects.get_by_id(req.REQUEST.get('cate', '1')) if not item or not item.is_active or not cate or not cate in item.cate.all(): raise Http404 liked = LikeRecord.objects.filter(ip=get_ip(req)).values_list('item', flat=True) liked = Item.objects.filter(id__in=liked) return render_and_response(req, 'product/detail.html', {'item': item, 'cate': cate, 'liked': liked})
def pager(req, items): paginator = Paginator(items, settings.PAGE_SIZE) page_num = req.REQUEST.get('p', '1') if not page_num.isdigit: page_num = int(page_num) try: result = paginator.page(page_num) except InvalidPage: result = paginator.page(1) liked = LikeRecord.objects.filter(ip=get_ip(req)).values_list('item', flat=True) liked = Item.objects.filter(id__in=liked) return {'result': result, 'liked': liked}
def send_message(request): """发送消息 """ logger.info("send message[ip=%s]" % tools.get_ip(request)) account_id = request.session.get('aid', None) if account_id is None: return HttpResponse(packer.pack_logout_response()) ret = packer.Ret() try: another_account_id = int(request.POST['fid']) content = request.POST['c'] except Exception as e: logger.warning("Invalid request:%s" % e) ret.ret = ret.RET_REQUEST_ERROR ret.reason = "Invalid request[%s]" % e return HttpResponse(packer.pack_error_response(ret)) #只能给自己的联系人发消息 friend = friend_manager.query(account_id, another_account_id, ret) if friend is None: logger.warning("Friend not exist[own_id=%d][other_id=%d]" % (account_id, another_account_id)) return HttpResponse(packer.pack_error_response(ret)) #如果还不是对方的联系人,自动添加 account = account_manager.get_account_by_id(account_id, ret) assert account is not None another_account = account_manager.get_account_by_id(another_account_id, ret) if another_account is None: return HttpResponse(packer.pack_error_response(ret)) friend = friend_manager.query_or_create(another_account_id, account_id, account.name, ret) if friend is None: return HttpResponse(packer.pack_error_response(ret)) #更新联系人信息 new_message_num = 1 #一条新消息 friend = friend_manager.update_by_new_message( another_account_id, account_id, new_message_num, ret) if friend is None: return HttpResponse(packer.pack_error_response(ret)) #发送消息 message = message_manager.add(account_id, another_account_id, content, ret) if message is None: return HttpResponse(packer.pack_error_response(ret)) else: return HttpResponse(packer.pack_response(message))
def auth(request): """自动登录 验证 """ logger.info("auth[ip=%s]" % tools.get_ip(request)) #验证 session ret = packer.Ret() account_id = request.session.get('aid', None) if account_id is None: return HttpResponse(packer.pack_logout_response()) account = account_manager.get_account_by_id(account_id, ret) if account is not None: return HttpResponse(packer.pack_response(account)) else: return HttpResponse(packer.pack_error_response(ret))
def query_message(request): """查询 """ logger.info("query message[ip=%s]" % tools.get_ip(request)) account_id = request.session.get('aid', None) if account_id is None: return HttpResponse(packer.pack_logout_response()) ret = packer.Ret() try: another_account_id = int(request.GET['fid']) offset = None limit = None if 'offset' in request.GET: offset = int(request.GET['offset']) if 'limit' in request.GET: limit = int(request.GET['limit']) except Exception as e: logger.warning("Invalid request:%s" % e) ret.ret = ret.RET_REQUEST_ERROR ret.reason = "Invalid request[%s]" % e return HttpResponse(packer.pack_error_response(ret)) friend = friend_manager.query(account_id, another_account_id, ret) if friend is None: return HttpResponse(packer.pack_error_response(ret)) #确保获取所有的未读消息 limit = friend.unread_msg_num if friend.unread_msg_num > limit else limit (messages, count) = message_manager.query_all( account_id, another_account_id, offset, limit) #更新未读消息数量 read_num = friend.unread_msg_num friend = friend_manager.update_by_new_message( account_id, another_account_id, -read_num, ret) if friend is None: return HttpResponse(packer.pack_error_response(ret)) response = packer.pack_list_response(messages, count) logger.info("Query message succeed[res=%s]" % response) return HttpResponse(response)
def register(request): """创建账户 """ logger.info("create account[ip=%s]" % tools.get_ip(request)) ret = packer.Ret() try: username = request.POST['username'] password = request.POST['password'] except Exception as e: logger.warning("Invalid request:%s" % e) ret.ret = ret.RET_REQUEST_ERROR ret.reason = "Invalid request[%s]" % e return HttpResponse(packer.pack_error_response(ret)) #注册 account = account_manager.register(username, password, ret) if account is None: return HttpResponse(packer.pack_error_response(ret)) else: _set_online(request, account) return HttpResponse(packer.pack_response(account))
def login(request): """登录 """ logger.info("login[ip=%s]" % tools.get_ip(request)) ret = packer.Ret() try: username = request.POST['username'] password = request.POST['password'] except Exception as e: logger.warning("Invalid request:%s" % e) ret.ret = ret.RET_REQUEST_ERROR ret.reason = "Invalid request[%s]" % e return HttpResponse(packer.pack_error_response(ret)) #登录验证 account = account_manager.login(username, password, ret) if account is not None: _set_online(request, account) response = HttpResponse(packer.pack_response(account)) return response else: return HttpResponse(packer.pack_error_response(ret))
def query_new_message(request): """查询新消息 """ logger.info("query new message[ip=%s]" % tools.get_ip(request)) account_id = request.session.get('aid', None) if account_id is None: return HttpResponse(packer.pack_logout_response()) ret = packer.Ret() try: another_account_id = int(request.GET['fid']) except Exception as e: logger.warning("Invalid request:%s" % e) ret.ret = ret.RET_REQUEST_ERROR ret.reason = "Invalid request[%s]" % e return HttpResponse(packer.pack_error_response(ret)) friend = friend_manager.query(account_id, another_account_id, ret) if friend is None: return HttpResponse(packer.pack_error_response(ret)) if friend.unread_msg_num <= 0: return HttpResponse(packer.pack_list_response([])) #获取未读的新消息 (messages, count) = message_manager.query_all( account_id, another_account_id, 0, friend.unread_msg_num) #更新未读消息数量 read_num = friend.unread_msg_num friend = friend_manager.update_by_new_message( account_id, another_account_id, -read_num, ret) if friend is None: return HttpResponse(packer.pack_error_response(ret)) response = packer.pack_list_response(messages, count) return HttpResponse(response)
def delete_message(request): """删除消息 """ logger.info("delete message[ip=%s]" % tools.get_ip(request)) account_id = request.session.get('aid', None) if account_id is None: return HttpResponse(packer.pack_logout_response()) ret = packer.Ret() try: mid = int(request.POST['mid']) except Exception as e: logger.warning("Invalid request:%s" % e) ret.ret = ret.RET_REQUEST_ERROR ret.reason = "Invalid request[%s]" % e return HttpResponse(packer.pack_error_response(ret)) #TODO:并没有更新联系人信息中的未读消息数量 #删除自己发送的消息 if message_manager.delete(mid, account_id, ret): return HttpResponse(packer.pack_response()) else: return HttpResponse(packer.pack_error_response(ret))
def landing(request): """登录页 """ logger.info("landing[ip=%s]" % tools.get_ip(request)) return render(request, 'home.html', {})