def invite_url(request, project_id): """ 生成邀请码 """ form = InviteModelForm(data=request.POST) if form.is_valid(): """ 1. 创建随机的邀请码 2. 验证码保存到数据库 3. 限制:只有创建者才能邀请 """ if request.tracer.user != request.tracer.project.creator: form.add_error('period', "无权创建邀请码") return JsonResponse({'status': False, 'error': form.errors}) random_invite_code = uid(request.tracer.user.mobile_phone) form.instance.project = request.tracer.project form.instance.code = random_invite_code form.instance.creator = request.tracer.user form.save() # 将验邀请码返回给前端,前端页面上展示出来。 url = "{scheme}://{host}{path}".format( scheme=request.scheme, host=request.get_host(), path=reverse('invite_join', kwargs={'code': random_invite_code})) return JsonResponse({'status': True, 'data': url}) return JsonResponse({'status': False, 'error': form.errors})
def invite_url(request, project_id): """ 生成邀请码 :param request: :param project_id: :return: """ form = InviteModelForm(data=request.POST) if form.is_valid(): """ 1、创建随机的邀请码 2、验证码保存到数据库 3、限制:只有创建者才能邀请 """ if request.tracer.user != request.tracer.project.creator: # 当前用户不是项目的创建者不能进行邀请 form.add_error("period", "无权创建邀请码") # 有效期下面提示 return JsonResponse({"status": False, "error": form.errors}) random_invite_code = uid(request.tracer.user.mobile_phone) form.instance.project = request.tracer.project form.instance.code = random_invite_code form.instance.creator = request.tracer.user form.save() # 将验证码返回给前段,前段页面上展示出来。 url = "{scheme}://{host}{path}".format( scheme=request.scheme, # 获取http或者https host=request.get_host(), # 主机IP和端口 path=reverse("invite_join", kwargs={"code": random_invite_code}) # 反向生成url格式 /invite/join/asdfadiqr-9asudfapsef/ ) return JsonResponse({"status": True, "data": url}) return JsonResponse({"status": False, "error": form.errors})
def issues_invite(request, project_id): """创建邀请码并返回给前端""" form = InviteModelForm(request.POST) if form.is_valid(): # todo 校验用户选中的邀请人数是否在额度范围内 # 1、只有项目的创建者才能发送邀请链接 if not request.tracer.project.creator == request.tracer.user: form.add_error('period', '无权创建邀请码') return JsonResponse({'status': False, 'error': form.errors}) # 2、设置验证码 random_invite_code = uid( request.tracer.user.mobile_phone) # 使用用户手机号创建随机码 form.instance.creator = request.tracer.user form.instance.project = request.tracer.project form.instance.code = random_invite_code form.save() # 3、返回邀请码链接给前端,便于前端页面展示 url = "{scheme}://{host}{path}".format( scheme=request.scheme, host=request.get_host(), path=reverse('web:invite_join', kwargs={'code': random_invite_code})) return JsonResponse({'status': True, 'url': url}) return JsonResponse({'status': False, 'error': form.errors})
def invite_url(request, project_id): """生成邀请码""" form = InviteModelForm(data=request.POST) if form.is_valid(): """ 表单验证通过后: 1、只有创建者才能邀请 的限制 2、创建随机邀请码 3、验证码保存到数据库 """ if request.lxyker.user != request.lxyker.project.creator: form.add_error('period', '没有权限') return JsonResponse({'status': False, 'errors': form.errors}) random_invite_code = uid(request.lxyker.user.mobile_phone) form.instance.project = request.lxyker.project form.instance.code = random_invite_code form.instance.creator = request.lxyker.user form.save() # 邀请码保存到数据库 # 邀请码返回给前端 url = '{scheme}://{host}{path}'.format( scheme=request.scheme, host=request.get_host(), path=reverse('invite_join', kwargs={'code': random_invite_code}), ) return JsonResponse({'status': True, 'data': url}) return JsonResponse({'status': False, 'error': form.errors})
def invite_url(request, project_id): """ 生成邀请码 """ form = InviteModelForm(data=request.POST) if form.is_valid(): """ 1. 创建一个随机的邀请码 2. 验证码保存到数据库 3. 只有创建者才能邀请,其他成员无法创建 """ if request.tracer.user != request.tracer.project.creator: form.add_error('period', '没有权限创建邀请码') return JsonResponse({'status': False, 'error': form.errors}) random_invite_code = uid(request.tracer.user.mobile_phone) form.instance.project = request.tracer.project form.instance.code = random_invite_code form.instance.creator = request.tracer.user form.save() # 邀请码返回前端,前端展示验证码 url = '{scheme}://{host}{path}'.format( scheme=request.scheme, # 访问的方法 host=request.get_host(), # 返回域名和端口 path=reverse('invite_join', kwargs={'code': random_invite_code})) return JsonResponse({'status': True, 'data': url}) return JsonResponse({'status': False, 'error': form.errors})
def invite_url(request, project_id): form = InviteModelForm(data=request.POST) if form.is_valid(): if request.tracer.user != request.tracer.project.creator: form.add_error('period', '无权创建邀请码') return JsonResponse({'status': False, 'error': form.errors}) random_invite_code = uid(request.tracer.user.phone) form.instance.project = request.tracer.project form.instance.code = random_invite_code form.instance.creator = request.tracer.user form.save() url_path = reverse('web:invite_join', kwargs={'code': random_invite_code}) url = f'{request.scheme}://{request.get_host()}{url_path}' return JsonResponse({'status': True, 'data': url}) return JsonResponse({'status': False, 'error': form.errors})