Exemplo n.º 1
0
def list_project(request):
    """展示项目和添加项目功能"""
    if request.method == 'GET':
        user_obj = request.tracer.user
        user_project_dict = {"star": [], "join": [], "my": []}
        project_list = Project.objects.filter(creator=user_obj)
        for item in project_list:
            if item.star:
                user_project_dict['star'].append({'value': item, 'type': 'my'})
            else:
                user_project_dict['my'].append(item)
        join_project_list = ProjectUser.objects.filter(user=user_obj)

        for join_project in join_project_list:
            if join_project.star:
                user_project_dict["star"].append({"value": join_project.project, "type": "join"})
            else:
                user_project_dict["join"].append(join_project.project)

        form = ProjectModelForm(request)
        return render(request, 'project_list.html', {'form': form, 'user_project_dict': user_project_dict})

    form = ProjectModelForm(request, data=request.POST)
    if form.is_valid():
        bucket = '{}-1300310288'.format(request.tracer.user.mobile_phone)
        region = 'ap-guangzhou'
        create_bucket(bucket, region) # 为每个项目创建存储桶
        project_obj = form.save(commit=False)
        project_obj.creator = request.tracer.user
        project_obj.bucket =bucket
        project_obj.region =region
        project_obj.save()
        return JsonResponse({"status": True})
    return JsonResponse({"status": False, "error": form.errors})
Exemplo n.º 2
0
def project_list(request):
    """项目列表"""
    if request.method == 'GET':
        """
        1. 从数据库中获取两部分数据
            我创建的所有项目:已星标、未星标
            我参与的所有项目:已星标、未星标
        2. 提取已星标
            列表 = 循环 [我创建的所有项目] + [我参与的所有项目] 把已星标的数据提取

        得到三个列表:星标、创建、参与
        """
        project_dict = {'star': [], 'join': [], 'my': []}

        my_project_list = Project.objects.filter(creator=request.tracer.user).order_by('id')
        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 = ProjectUser.objects.filter(user=request.tracer.user).order_by('id')
        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, 'web/project_list.html', {'form': form, 'project_dict': project_dict})

    if request.method == 'POST':
        '''前端发送Ajax的post请求创建项目'''

        form = ProjectModelForm(request, data=request.POST)

        if form.is_valid():
            # 1、保存项目之前创建cos桶
            time_str = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))   # 时间戳格式化
            bucket_name = '{0}-{1}-1300263909'.format(request.tracer.user.mobile_phone, time_str)
            region = 'ap-beijing'
            creat_bucket(bucket_name, region=region)
            # 保存项目的腾讯云cos桶名称和区域
            form.instance.bucket = bucket_name
            form.instance.region = region

            # 2、验证通过,获取当前登录用户之后, 将项目保存到数据库
            form.instance.creator = request.tracer.user
            instance = form.save()

            # 3、给项目默认添加问题类型
            issues_type_list = []
            for issues_type in IssuesType.PROJECT_INIT_LIST:  # ["任务", '功能', 'Bug']
                issues_type_list.append(IssuesType(title=issues_type, project=instance))
            # 批量加入到数据库中  bulk_create
            IssuesType.objects.bulk_create(issues_type_list)

            return JsonResponse({'status': True})
        # 验证失败
        return JsonResponse({'status': False, 'error': form.errors})
Exemplo n.º 3
0
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})
Exemplo n.º 4
0
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})
Exemplo n.º 5
0
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, 'web/project_list.html', {
            'form': form,
            'project_dict': project_dict
        })

    form = ProjectModelForm(request, data=request.POST)
    if form.is_valid():
        name = form.cleaned_data['name']
        # 1. 为项目创建一个桶
        bucket = "{}-{}-1302470725".format(request.tracer.user.phone,
                                           str(int(time.time())))
        region = 'ap-nanjing'
        create_bucket(bucket, region)
        # 2.创建项目
        # 验证通过:项目名、颜色、描述 + creator谁创建的项目?
        form.instance.bucket = bucket
        form.instance.region = region
        form.instance.creator = request.tracer.user
        instance = form.save()

        return JsonResponse({'status': True})
    return JsonResponse({'status': False, 'error': form.errors})
Exemplo n.º 6
0
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})
Exemplo n.º 7
0
def project_edit(request, pk):
    project_object = models.Project.objects.filter(id=pk).first()
    if request.method == 'GET':
        form = ProjectModelForm(instance=project_object)
        return render(request, 'form.html', {'form': form})
    form = ProjectModelForm(data=request.POST, instance=project_object)
    if form.is_valid():
        form.save()
        return redirect('project_list')
    else:
        return render(request, 'form.html', {'form': form})
Exemplo n.º 8
0
def project_add(request):
    if request.method == 'GET':
        form = ProjectModelForm()
        return render(request, 'form.html', {'form': form})
    # 接收用户提交的数据并进行表单验证
    form = ProjectModelForm(data=request.POST)
    if form.is_valid():
        form.save()
        return redirect('project_list')
    else:
        return render(request, 'form.html', {'form': form})
Exemplo n.º 9
0
def project_list(request):
    """ 项目列表 """
    if request.method == "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(item.project)
            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():
        # 为项目创建桶,名字不能重复
        # 手机号+时间戳+后缀名

        buckname = create_bucket_name(request, form.instance.name,
                                      settings.BUCKET_REGION)

        create_bucket(buckname)
        form.cleaned_data['name'] = buckname
        form.instance.region = settings.BUCKET_REGION

        form.instance.bucket = buckname

        # 验证通过:项目名、颜色、描述 + creator谁创建的项目?
        form.instance.creator = request.tracer.user
        # 创建项目

        form.save()

        return JsonResponse({'status': True})

    return JsonResponse({'status': False, 'error': form.errors})
Exemplo n.º 10
0
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})
Exemplo n.º 11
0
def project_add(request):
    """
    添加项目
    :param request:
    :return:
    """
    if request.method == 'GET':
        form = ProjectModelForm()
        return render(request, 'form.html', {'form': form})
    form = ProjectModelForm(data=request.POST)
    # 对用户提交的数据进行校验
    if form.is_valid():
        form.save()
        return redirect(memory_reverse(request, 'project_list'))
    return render(request, 'form.html', {'form': form})
Exemplo n.º 12
0
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})
Exemplo n.º 13
0
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})
Exemplo n.º 14
0
def project_edit(request, nid):
    """
    编辑项目
    :param request:
    :param nid: 当前要编辑的部门ID
    :return:
    """
    obj = models.Project.objects.filter(id=nid).first()  # 包含此行的所有数据
    if request.method == "GET":
        # 生成HTML标签 + 携带默认值
        form = ProjectModelForm(instance=obj)
        return render(request, 'form.html', {'form': form})  # 带默认值
    form = ProjectModelForm(data=request.POST, instance=obj)
    if form.is_valid():
        form.save()
        return redirect(memory_reverse(request, 'project_list'))
    return render(request, 'form.html', {'form': form})
Exemplo n.º 15
0
 def post(self, request, *args, **kwargs):
     form = ProjectModelForm(request, data=request.POST)
     if form.is_valid():
         #验证通过
         form.instance.creator = request.tracer.user
         # 1.为项目创建一个桶
         bucket = "{}-{}-1258040712".format(
             request.tracer.user.mobile_phone, int(time.time()))
         region = 'ap-nanjing'
         cos.create_bucket(bucket, region)
         # 2. 把桶和区域写进数据库
         form.instance.bucket = bucket
         form.instance.region = region
         #讲项目保存到数据库
         form.save()
         return JsonResponse({'status': True})
     else:
         return JsonResponse({'status': False, 'error': form.errors})
Exemplo n.º 16
0
def project_list(request):
    if request.method == 'GET':
        form = ProjectModelForm(request)

        project_dict = {'star': [], 'mine': [], 'joined': []}
        raw_project_list = models.Project.objects.filter(
            creator=request.auth.user)
        for item in raw_project_list:
            if item.star:
                project_dict['star'].append({'value': item, 'type': 'mine'})
            else:
                project_dict['mine'].append(item)
        raw_join_list = models.ProjectUser.objects.filter(
            user=request.auth.user)
        for item in raw_join_list:
            if item.star:
                project_dict['star'].append({
                    'value': item.project,
                    'type': 'joined'
                })
            else:
                project_dict['joined'].append(item.project)

        return render(request, 'web/project_list.html', {
            'form': form,
            'project_dict': project_dict
        })

    form = ProjectModelForm(request, data=request.POST)
    if form.is_valid():
        # 创建桶与区域
        bucket = '{}-{}-1302697284'.format(request.auth.user.phone_num,
                                           str(int(time.time() * 1000)))
        region = 'ap-nanjing'
        create_bucket(bucket, region)
        form.instance.region = region
        form.instance.bucket = bucket
        form.instance.creator = request.auth.user
        form.save()
        return JsonResponse({'status': True})

    return JsonResponse({'status': False, 'error': form.errors})
Exemplo n.º 17
0
def project_list(request):
    print(request.tracker.user)
    if request.method == "GET":
        project_dict = {"star": [], "my": [], "join": []}
        # 我创建的所有项目
        my_project_list = models.Project.objects.filter(
            creator=request.tracker.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.tracker.user
        )
        for item in join_project_list:
            if item.star:
                project_dict["star"].append(
                    {"value": item.project, "type": "join"}
                )
            else:
                project_dict["my"].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():
        # 将项目制定为当前登录的用户
        form.instance.creator = request.tracker.user
        # 保存项目
        form.save()
        return JsonResponse({"status": True})
    return JsonResponse({"status": False, "error": form.errors})
Exemplo n.º 18
0
def project_list(request):
    """ 项目列表 """
    if request.method == "GET":
        # GET请求查看项目列表
        project_dict = {'star': [], 'my': []}
        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)

        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():
        form.instance.creator = request.tracer.user
        form.save()
        create_folder.addAlbum(user_id=str(form.instance.creator_id), filename=form.instance.name)
        return JsonResponse({'status': True})
    return JsonResponse({'status': False, 'error': form.errors})
Exemplo n.º 19
0
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})
Exemplo n.º 20
0
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})