def project_list(request): """ 项目列表 """ if request.method == "GET": form = ProjectModelForm(request) # 查询我创建的项目 my_projects = models.Project.objects.filter(creator_id=request.session.get('user_id')) # 查询我参与的项目 part = models.ProjectUser.objects.filter(user_id=request.session.get('user_id')).values_list('project') projects = models.Project.objects.filter(pk__in=part) return render(request, 'project_list.html', {'form': form, 'my_projects': my_projects, 'projects':projects}) form = ProjectModelForm(request, data=request.POST) if form.is_valid(): name = form.cleaned_data['name'] # bucket = "{}-{}-{}-{}".format(name, request.tracer.user.mobile_phone, str(int(time.time())), settings.TENCENT_COS_APPID) bucket = "{}-{}-1251317460".format(request.tracer.user.mobile_phone, str(int(time.time()))) create_bucket(bucket, settings.TENCENT_COS_REGION) # 验证通过:项目名、颜色、描述 + creator谁创建的项目? form.instance.bucket = bucket form.instance.region = "ap-beijing" form.instance.creator = request.tracer.user # 创建项目 instance = form.save() # 3.项目初始化问题类型 issues_type_object_list = [] for item in models.IssuesType.PROJECT_INIT_LIST: # ["任务", '功能', 'Bug'] issues_type_object_list.append(models.IssuesType(project=instance, title=item)) models.IssuesType.objects.bulk_create(issues_type_object_list) return JsonResponse({'status': True}) return JsonResponse({'status': False, 'error': form.errors})
def project_list(request): """项目列表""" if request.method == "GET": #GET請求查看項目列表 """ 从数据库获取数据: 我创建的项目:已星标、未星标 我参与的项目:已星标、未星标 """ project_dict = {'star': [], 'my': [], 'join': []} my_project_list = models.Project.objects.filter( creator=request.tracer.user) for row in my_project_list: if row.star: project_dict['star'].append({'value': row, 'type': 'my'}) else: project_dict['my'].append(row) join_project_list = models.ProjectUser.objects.filter( user=request.tracer.user) for item in join_project_list: if item.star: project_dict['star'].append({ 'vaule': item.project, 'type': 'join' }) else: project_dict['join'].append(item.project) form = ProjectModelForm(request) return render(request, 'project_list.html', { 'form': form, 'project_dict': project_dict }) #POST,通过ajax添加项目 form = ProjectModelForm(request, data=request.POST) if form.is_valid(): #为项目创建桶 name = form.cleaned_data['name'] bucket = "{}-{}-1301841574".format(request.tracer.user.mobile_phone, str(int(time.time()))) region = 'ap-guangzhou' create_bucket(bucket, region) #验证通过:项目名、颜色、描述+creator form.instance.creator = request.tracer.user form.instance.region = region form.instance.bucket = bucket #创建项目 instance = form.save() #项目初始化问题类型 issues_type_object_list = [] for item in models.IssuesType.PROJECT_INIT_LIST: issues_type_object_list.append( models.IssuesType(project=instance, title=item)) models.IssuesType.objects.bulk_create(issues_type_object_list) return JsonResponse({'status': True}) return JsonResponse({'status': False, 'error': form.errors})
def project_list(request): """ 项目列表 """ if request.method == "GET": # GET请求查看项目列表 """ 1. 从数据库中获取两部分数据 我创建的所有项目:已星标、未星标 我参与的所有项目:已星标、未星标 2. 提取已星标 列表 = 循环 [我创建的所有项目] + [我参与的所有项目] 把已星标的数据提取 得到三个列表:星标、创建、参与 """ project_dict = {'star': [], 'my': [], 'join': []} my_project_list = models.Project.objects.filter(creator=request.tracer.user) for row in my_project_list: if row.star: project_dict['star'].append({"value": row, 'type': 'my'}) else: project_dict['my'].append(row) join_project_list = models.ProjectUser.objects.filter(user=request.tracer.user) for item in join_project_list: if item.star: project_dict['star'].append({"value": item.project, 'type': 'join'}) else: project_dict['join'].append(item.project) form = ProjectModelForm(request) return render(request, 'project_list.html', {'form': form, 'project_dict': project_dict}) # POST,对话框的ajax添加项目。 form = ProjectModelForm(request, data=request.POST) if form.is_valid(): name = form.cleaned_data['name'] # 1. 为项目创建一个桶 bucket = "{}-{}-1301488155".format(request.tracer.user.mobile_phone, str(int(time.time()))) region = 'ap-chengdu' create_bucket(bucket, region) # 2.创建项目 # 验证通过:项目名、颜色、描述 + creator谁创建的项目? form.instance.bucket = bucket form.instance.region = region form.instance.creator = request.tracer.user instance = form.save() # 3.项目初始化问题类型 issues_type_object_list = [] for item in models.IssuesType.PROJECT_INIT_LIST: # ["任务", '功能', 'Bug'] issues_type_object_list.append(models.IssuesType(project=instance, title=item)) models.IssuesType.objects.bulk_create(issues_type_object_list) return JsonResponse({'status': True}) return JsonResponse({'status': False, 'error': form.errors})
def project_list(request): """ 后台管理展示 """ if request.method == "GET": """ 得到三个列表 """ project_dic = {"star": [], "my": [], "join": []} # 获取展示的项目 # 1. 获取我创建的项目 my_project_list = models.Project.objects.filter( creator=request.tracer.user) for item in my_project_list: if item.star: project_dic["star"].append({"value": item, "type": "my"}) else: project_dic["my"].append(item) # 2. 获取参与的项目 join_project_list = models.ProjectUser.objects.filter( user=request.tracer.user) for item in join_project_list: if item.star: project_dic["star"].append({ "value": item.project, "type": "join" }) else: project_dic["join"].append(item.project) form = ProjectForm(request) return render(request, "web/project_list.html", { "form": form, "project_dic": project_dic }) form = ProjectForm(request, data=request.POST) if form.is_valid(): # 创建项目同时创建桶 & 区域 bucket = "{}-{}-1300113042".format(request.tracer.user.mobile_phone, str(int(time.time()))) region = "ap-beijing" create_bucket(bucket, region) # 保存到数据库 form.instance.bucket = bucket form.instance.region = region # 校验通过,保存到数据库之前,还有一个必填字段,在form表单校验时没有 form.instance.creator = request.tracer.user instance = form.save() # 生成默认问题类型 issuse_type_list = [] for item in models.IssuesType.PROJECT_INIT_LIST: issuse_type_list.append( models.IssuesType(project=instance, title=item)) models.IssuesType.objects.bulk_create(issuse_type_list) return JsonResponse({"status": True}) return JsonResponse({"status": False, "error": form.errors})
def project_list(request): """展示项目列表""" if request.method == 'GET': # 将 星标的、我创建的、我加入的 项目作为三个列表,整体存入字典中传给前端 project_dict = {'star': list(), 'mine': list(), 'join': list()} create_project_list = models.Project.objects.filter( creator=request.lxyker.user) join_project_list = models.ProjectUser.objects.filter( user=request.lxyker.user) for row in create_project_list: if row.is_star: project_dict['star'].append({'type': 'created', 'value': row}) else: project_dict['mine'].append(row) for row in join_project_list: if row.is_star: project_dict['star'].append({ 'type': 'joined', 'value': row.project }) else: project_dict['join'].append(row) form = ProjectModelForm(request) return render(request, 'web/project_list.html', locals()) form = ProjectModelForm(request, data=request.POST) if form.is_valid(): # 填写的创建项目数据没有问题,应该:数据库创建项目、配套存储桶 # 为项目创建一个桶: bucket = '{}-{}-1258158324'.format(request.lxyker.user.mobile_phone, int(time.time())) region = 'ap-beijing' create_bucket(bucket=bucket, region=region) # 项目信息存入数据库 form.instance.bucket = bucket form.instance.region = region form.instance.creator = request.lxyker.user instance = form.save() # 给新创建的项目初始化问题的类型,便于创建问题时有默认的问题类型可选择 issues_type_object_list = list() for item in models.IssuesType.PROJECT_INIT_LIST: issues_type_object_list.append( models.IssuesType(project=instance, title=item)) models.IssuesType.objects.bulk_create(issues_type_object_list) return JsonResponse({'status': True}) else: return JsonResponse({'status': False, 'error': form.errors})
def project_list(request): if request.method=='GET': """ 从数据库获取两部分数据 我创建的所有项目:已星标,未星标 我参与的所有项目:已星标,未星标 提取已星标 将得到三个列表 """ project_dict={'star':[],'my':[],'join':[]} my_project_list=models.Project.objects.filter(creator=request.blog.user) for row in my_project_list: if row.star: project_dict['star'].append({'value':row,'type':'my'}) else: project_dict['my'].append(row) join_project_list=models.ProjectUser.objects.filter(user=request.blog.user) for item in join_project_list: if item.star: project_dict['star'].append({'value':item.project,'type':'join'}) else: project_dict['join'].append(item.project) form=ProjectMdoelForm(request) return render(request,'project_list.html',{'form':form,'project_dict':project_dict}) form=ProjectMdoelForm(request,data=request.POST) if form.is_valid(): #为项目创建cos桶 #构建唯一桶名称手机号,时间 bucket="{}-{}-1303841926".format(request.blog.user.mobile_phone,str(int(time.time()))) region="ap-chengdu" create_bucket(bucket,region) #将桶和区域存储 form.instance.bucket=bucket form.instance.region=region #验证通过 用户仅提供博客名,风格,描述,还有一些字段尚未给出 form.instance.creator=request.blog.user #创建项目 instance=form.save() # 3.项目初始化问题类型 issues_type_object_list = [] for item in models.IssuesType.PROJECT_INIT_LIST: # ["任务", '功能', 'Bug'] issues_type_object_list.append(models.IssuesType(project=instance, title=item)) models.IssuesType.objects.bulk_create(issues_type_object_list) return JsonResponse({'status': True}) return JsonResponse({'status':False,'errors':form.errors})
def project_list(request): if request.method == 'GET': user = request.tracer.user project_dict = {'star': [], 'my': [], 'join': []} my_project = models.Project.objects.filter(creator=user) join_project = models.ProjectUser.objects.filter(user=user) for row in my_project: if row.star: project_dict['star'].append({'value': row, 'type': 'my'}) else: project_dict['my'].append(row) for item in join_project: if item.star: project_dict['star'].append({ 'value': item.project, 'type': 'join' }) else: project_dict['join'].append(item.project) form = ProjectModelForm(request) return render(request, 'project_list.html', { 'form': form, 'project_dict': project_dict }) form = ProjectModelForm(request, request.POST) if form.is_valid(): # 为项目创建桶 bucket = "{}-{}-1252704499".format(request.tracer.user.phone, str(int(time.time()))) region = 'ap-chengdu' create_bucket(bucket, region) form.instance.bucket = bucket form.instance.region = region form.instance.creator = request.tracer.user instance = form.save() issues_type_object_list = [] for item in models.IssuesType.PROJECT_INIT_LIST: issues_type_object_list.append( models.IssuesType(project=instance, title=item)) models.IssuesType.objects.bulk_create(issues_type_object_list) return JsonResponse({'status': True}) return JsonResponse({'status': False, 'error': form.errors})
def project_list(request): """项目列表""" if request.method == 'GET': # get 请求查看项目列表 """ 1.从数据库中获取三部分的数据 我创建的项目:已星标、未星标 我参与的所有项目:已星标、未星标 2.提取已星标 列表= 循环我创建的+我参与的,把所有星标的项目提取出来 得到三个列表:星标,创建,参与 """ project_dict = {'star': [], 'my': [], 'join': []} my_project_list = models.ProjectInfo.objects.filter(creator=request.tracer.user) for row in my_project_list: if row.star: project_dict['star'].append({'value': row, 'type': 'my'}) else: project_dict['my'].append(row) join_project_list = models.ProjectUser.objects.filter(user=request.tracer.user) for item in join_project_list: if item.star: project_dict['star'].append({'value': item.project, 'type': 'join'}) else: project_dict['join'].append(item.project) form = ProjectModelForm(request) return render(request, 'project_list.html', {'form': form, 'project_dict': project_dict}) form = ProjectModelForm(request, data=request.POST) if form.is_valid(): # 为项目创建一个桶,名称唯一 bucket = '{}-{}-1302167637'.format(request.tracer.user.mobile_phone, str(int(time.time()))) region = 'ap-nanjing' create_bucket(bucket, region) # 验证通过:项目名、颜色、描述 form.instance.creator = request.tracer.user form.instance.bucket = bucket form.instance.region = region # 创建项目 instance = form.save() # 初始化项目问题类型 issues_type_object_list = [] for item in models.IssuesType.PROJECT_INIT_LIST: issues_type_object_list.append(models.IssuesType(project=instance, title=item)) models.IssuesType.objects.bulk_create(issues_type_object_list) return JsonResponse({'status': True, }) return JsonResponse({'status': False, 'error': form.errors})
def project_list(request): """ 项目列表 """ if request.method == "GET": # GET请求查看项目列表 """ 1. 从数据库中获取两部分数据 我创建的所有项目:已星标、未星标 我参与的所有项目:已星标、未星标 2. 提取已星标 列表 = 循环 [我创建的所有项目] + [我参与的所有项目] 把已星标的数据提取 得到三个列表:星标、创建、参与 """ project_dict = {'star': [], 'my': [], 'join': []} my_project_list = models.Project.objects.filter(creator=request.tracer.user) for row in my_project_list: if row.star: #获取星标项目到底是我创建的还是参与的,使取消星标回到正确的位置 project_dict['star'].append({"value": row, 'type': 'my'}) else: project_dict['my'].append(row) join_project_list = models.ProjectUser.objects.filter(user=request.tracer.user) for item in join_project_list: if item.star: project_dict['star'].append({"value": item.project, 'type': 'join'}) else: project_dict['join'].append(item.project) form = ProjectModelForm(request) return render(request, 'project_list.html', {'form': form, 'project_dict': project_dict}) # POST,对话框的ajax添加项目。 form = ProjectModelForm(request, data=request.POST) if form.is_valid(): name = form.cleaned_data['name'] p = Pinyin() name_pinyin = p.get_pinyin(name) # 发现一个bug,如果名字中含有大写字母,上传文件会失败,wiki上传图片能成功(文件用的js临时凭证上传,wiki用的python上传 # 将大写字母转换成小写 name_pinyin = name_pinyin.lower() # 1. 为项目创建一个桶 bucket = "{}-{}-{}-1302500805".format(name_pinyin, request.tracer.user.mobile_phone, str(int(time.time()))) region = 'ap-nanjing' try: create_bucket(bucket, region) except CosClientError as e: # form.errors是一个对象:{ "name" : [ "字段不能为空", ] },所以给它构造成这样就可以模拟raise ValidationError form.errors['name'] = [" 项目名只能是数字字母和“-” "] return JsonResponse({'status': False, 'error': form.errors}) # 验证通过:项目名、颜色、描述 + creator谁创建的项目? form.instance.bucket = bucket form.instance.region = region form.instance.creator = request.tracer.user # 2.创建项目 instance = form.save() # 返回值是这个对象 # 3.项目初始化问题类型 issues_type_object_list = [] for item in models.IssuesType.PROJECT_INIT_LIST: # 类实例代表一行数据 issues_type_object_list.append(models.IssuesType(project=instance, title=item)) models.IssuesType.objects.bulk_create(issues_type_object_list) return JsonResponse({'status': True}) # form.errors传给前端是对象,但输出的时候是<ul>标签字符串,因为内部的方法将它转化了 return JsonResponse({'status': False, 'error': form.errors})
def project_list(request): """项目列表""" if request.method == "GET": # get请求查看项目列表 """ 1.从数据库中获取两部分数据 我创建的所有项目:已星标、未星标 我参与的所有项目:已星标、未星标 2.提取已星标 列表 = 循环[我创建的所有项目] + [我参与的所有项目] 把已星标的数据提取 得到三个列表:星标、创建、参与 """ project_dict = {'star': [], 'my': [], 'join': []} my_project_list = models.Project.objects.filter( creator=request.bug_mgt.user) for row in my_project_list: if row.star: project_dict['star'].append({'value': row, 'type': 'my'}) else: project_dict['my'].append({'value': row, 'type': 'my'}) join_project_list = models.ProjectUser.objects.filter( user=request.bug_mgt.user) for item in join_project_list: if item.star: project_dict['star'].append({ 'value': item.project, 'type': 'join' }) else: project_dict['join'].append({ 'value': item.project, 'type': 'join' }) form = ProjectModelForm(request) return render(request, 'web/project_list.html', { 'form': form, 'project_dict': project_dict }) form = ProjectModelForm(request, data=request.POST) if form.is_valid(): # 只有当表单验证的数据都合法(例如手机号码验证等),is_valid()才等于True name = form.cleaned_data['name'] # 为项目创建桶 bucket = "{}-{}-{}-1259386016".format( name, request.bug_mgt.user.mobile_phone, str(int(time.time()))) region = 'ap-nanjing' cos.create_bucket(bucket=bucket, region=region) # 验证通过后,则需要向数据库写入数据,此时project表中所必须填入的字段有 # 项目名,颜色,描述,(项目使用空间,星标,项目参与人数,创建时间这些都有默认值,不需要赋值),创建者 form.instance.creator = request.bug_mgt.user form.instance.bucket = bucket form.instance.region = region # 保存项目后返回一个对象 instance = form.save() # 项目初始化问题类型 issues_type_list = [] for item in models.IssuesType.PROJECT_INIT_LIST: issues_type_list.append( models.IssuesType(project=instance, title=item)) models.IssuesType.objects.bulk_create(issues_type_list) return JsonResponse({'status': True}) return JsonResponse({'status': False, 'error': form.errors})
def project_list(request): """ 项目列表 :param request: :return: """ if request.method == "GET": # GET请求查看项目列表 """ 1、从数据库中获取两部分数据: 我创建的所有项目:已星标、未星标 我参与的所有项目:已星标、未星标 2、提取已星标 列表 = 循环【我创建的所有项目】 + [我参与的所有项目] 把已星标的数据提取处理 得到三个列表:星标、创建、参与 """ # 区分:用户创建项目 和 用户参与项目 # 存放创建项目和参与项目的对象,(当前用户创建项目、当前用户参与者的星标对象放到star,(当前用户创建项目)无星标对象放到my,join无星标存放项目参与者 project_dict = {"star": [], "my": [], "join": []} # 查询当前用户创建的所有项目 my_project_list = models.Project.objects.filter( creator=request.tracer.user) for row in my_project_list: if row.star: # 星标为true放到star project_dict['star'].append({"value": row, "type": "my"}) else: project_dict['my'].append(row) # 当前用户参与的所有项目 join_project_list = models.ProjectUser.objects.filter( user=request.tracer.user) for item in join_project_list: if item.start: project_dict['star'].append({ "values": item.project, "type": "join" }) # project_dict字典中star已添加了项目的对象,这里在添加对象以后取值就会混乱,所有添加项目参与者项目 else: project_dict['join'].append(item.project) # 用户参与项目无星标放到join中 form = ProjectModeForm(request) return render(request, "project_list.html", { "form": form, "project_dict": project_dict }) # POST,对话框的ajax添加项目 form = ProjectModeForm(request, data=request.POST) if form.is_valid(): # name = form.cleaned_data["name"] # 1.为项目创建一个桶 桶名字格式:"{}-{}-{}-1253858492" bucket = "{}-{}-1253858492".format(request.tracer.user.mobile_phone, str(int(time.time()))) region = "ap-guangzhou" create_bucket(bucket, region) # 2.创建项目,把桶和区域写入到数据库 form.instance.bucket = bucket form.instance.region = region # 验证通过:用户提交了项目名、颜色、描述,还需要谁创建的项目 form.instance.creator = request.tracer.user # 当前用户创建的项目 instance = form.save() # 创建项目成功后返回当前创建项目的对象 # 3、项目初始化问题类型 issues_type_object_list = [] for item in models.IssuesType.PROJECT_INIT_LIST: # ["任务", '功能', 'Bug'] issues_type_object_list.append( models.IssuesType(project=instance, title=item)) models.IssuesType.objects.bulk_create(issues_type_object_list) # 批量添加 return JsonResponse({"status": True}) return JsonResponse({"status": False, "error": form.errors})