纪念一下第一次写的django代码

@csrf_exempt
def new_project_detail(request):
    if 'project_name' not in request.POST or 'project_position' not in request.POST or 'interest_rate' not in \
            request.POST or 'financing_amount' not in request.POST or 'deadline' not in request.POST or \
                    'repayment_time' not in request.POST or 'project_detail' not in request.POST or 'project_type' \
            not in request.POST or 'bonding_company' not in request.POST:
        return json_response(False, '050002', 'lack of parameter')
    try:
        if store_data(request):
            return json_response(True)
        else:
            return json_response(False, '020005', '数据库错误')
    except ValueError:
        return json_response(False, '020002', '数据字段错误')
    except ProjectDetail.DoesNotExist:
        return json_response(False, '020003', '信息不存在')
    except Exception, e:
        print e
        return json_response(False, '020004', e)


@transaction.commit_manually
def store_data(request):
    try:
        project = ProjectDetail(project_name=request.POST.get("project_name"),
                                project_position=request.POST.get("project_position"),
                                interest_rate=request.POST.get("interest_rate"),
                                financing_amount=request.POST.get("financing_amount"),
                                deadline=request.POST.get("deadline"),
                                repayment_time=request.POST.get("repayment_time"),
                                project_detail=request.POST.get("project_detail"),
                                project_type=request.POST.get("project_type"))
        project.save()
        project.project_NO = request.POST.get("project_NO", datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S"))
        project.project_purpose = request.POST.get("project_purpose", None)
        if 'company_id' in request.POST:
            company = Company.objects.get(id=request.POST.get("company_id"))
            financial_status = CompanyFinancialStatus.objects.get(company=company)
            project.company = company
            project.financial_status = financial_status

        bonding_company = BondingCompany.objects.get(id=request.POST.get("bonding_company"))
        project.bonding_company.add(bonding_company)
        project.status = PROJECT_STATUS_INIT
        project.project_end_type = request.POST.get("project_end_type", PROJECT_END_TYPE_BY_AMOUNT)
        project.save()
        transaction.commit()
        return True
    except Company.DoesNotExist:
        transaction.rollback()
        return False
    except CompanyFinancialStatus.DoesNotExist:
        transaction.rollback()
        return False
    except BondingCompany.DoesNotExist:
        transaction.rollback()
        return False
    except Exception, e:
        print e
        transaction.rollback()
        return False

posted on 2014-05-12 20:40  颓废的悠然  阅读(222)  评论(0编辑  收藏  举报

导航