def accept_task(request): result = {} if request.method == "POST": task_id = request.POST.get('task_id', None) user_id = get_userid(request) try: task = models.TaskInfo.objects.get(task_id=task_id) # 用户领取验证,不是本人发起的任务、任务状态待领取、任务性别要求符合 if task.user_id != user_id and task.task_status == 0 and ( task.task_sex_preference == 0 or task.task_sex_preference == get_user(request).user_sex): # 生成id accept_id = time.strftime('%Y%m%d', time.localtime( time.time())) + "00" while True: if len( models.TaskAcceptInfo.objects.filter( accept_id=accept_id)) == 0: break accept_id = str(int(accept_id) + 1) # 领取任务 if models.TaskAcceptInfo.objects.create(accept_id=accept_id, task_id=task_id, user_id=user_id): task.task_status = 1 # 更改任务状态 task.save() # 更新数据库 result = {'state': '0', 'info': '任务领取成功!'} except Exception as e: result = {'state': '-2', 'info': '服务器错误请稍后尝试!', 'error': e} return JsonResponse(result, safe=False)
def set_task(request): result = {} if request.method == "POST": task_id = request.POST.get('task_id', None) title = request.POST.get('title', None) detail = request.POST.get('detail', None) price = request.POST.get('price', None) sex_preference = request.POST.get('sex_preference', None) deadline = request.POST.get('deadline', None) user_id = get_userid(request) try: task = models.TaskInfo.objects.get(task_id=task_id) # 用户验证,是本人发起的任务、任务状态待领取 if task.user_id == user_id and task.task_status == 0: user = get_user(request) if user.user_price > int(price) - task.task_price: user.user_price -= int(price) - task.task_price user.save() task.task_title = title task.task_detail = detail task.task_price = float(price) task.task_sex_preference = sex_preference task.deadline = deadline task.save() result = {'state': '0', 'info': '任务信息更改成功!'} else: result = {'state': '-9', 'info': '您的余额不足,请充值后再更改任务金额!'} except Exception as e: result = {'state': '-2', 'info': '服务器错误请稍后尝试!', 'error': e} return JsonResponse(result, safe=False)
def task_info(request): if request.method == "POST": task_id = request.POST.get('task_id', None) user_id = get_userid(request) try: task = models.TaskInfo.objects.get(task_id=task_id) except Exception: result = [{'state': '-1', 'info': '服务器错误请稍后尝试!'}] else: data = task.__dict__ del data['_state'] data['create_time'] = data['create_time'].strftime( "%Y-%m-%d %H:%M") data['is_collected'] = len( models.TaskCollectInfo.objects.filter(user_id=user_id, task_id=task_id)) # 任务领取状态码生成 # 0表示可领取; # 1表示用户自己发布的任务他人已领取,可以确认结束; # 2表示该任务已完成; # -1表示用户自己发布的任务,还未被领取,不能被用户自己领取; # -2表示用户不符合任务性别要求,不能被用户领取; # -3表示该任务已被领取,不能被用户领取 # -4表示该任务已被用户自己领取,不能被用户重复领取 task_can_accept = '0' accept_info = models.TaskAcceptInfo.objects.filter(task_id=task_id) if task.task_status == 2: task_can_accept = '3' if models.TaskAcceptInfo.objects.get( task_id=task_id).user_id == user_id: task_can_accept = '2' elif task.user_id == user_id: task_can_accept = '-1' if accept_info: task_can_accept = '1' elif task.task_sex_preference != 0 and task.task_sex_preference != get_user( request).user_sex: task_can_accept = '-2' elif accept_info: task_can_accept = '-3' if models.TaskAcceptInfo.objects.get( task_id=task_id).user_id == user_id: task_can_accept = '-4' data['task_can_accept'] = task_can_accept result = [{'state': '0', 'info': '查询成功!'}, data] return JsonResponse(result, safe=False) else: return render(request, "task_info.html")
def remove_task(request): result = {} if request.method == "POST": task_id = request.POST.get('task_id', None) user_id = get_userid(request) try: task = models.TaskInfo.objects.get(task_id=task_id) # 用户验证,是本人发起的任务、任务状态待领取 if task.user_id == user_id and task.task_status == 0: user = get_user(request) user.user_price += task.task_price user.save() task.delete() result = {'state': '0', 'info': '任务删除成功!'} except Exception as e: result = {'state': '-2', 'info': '服务器错误请稍后尝试!', 'error': e} return JsonResponse(result, safe=False)
def create_task(request): if request.method == "POST": result = {'state': '0', 'info': '任务创建成功!'} title = request.POST.get('title', None) detail = request.POST.get('detail', None) price = request.POST.get('price', None) sex_preference = request.POST.get('sex_preference', None) deadline = request.POST.get('deadline', None) user_id = get_userid(request) if title and price: task_id = time.strftime('%Y%m%d', time.localtime( time.time())) + "00" while True: if len(models.TaskInfo.objects.filter(task_id=task_id)) == 0: break task_id = str(int(task_id) + 1) print("新建任务:", task_id, title, detail, price, user_id, deadline, 0) try: user = get_user(request) if user.user_price >= int(price): models.TaskInfo.objects.create( task_id=task_id, task_title=title, task_detail=detail, task_price=float(price), task_sex_preference=sex_preference, user_id=user_id, deadline=deadline, task_status=0) user.user_price -= int(price) user.save() else: result = {'state': '-9', 'info': '您的余额不足,请充值后再发布任务!'} except Exception: result = {'state': '-2', 'info': '服务器错误请稍后尝试!'} else: result = {'state': '-1', 'info': '任务标题不能为空!'} return JsonResponse(result, safe=False) else: return render(request, "add_task.html")