def create(self, request, **kwargs): serializer = BranchSerializer(data=request.data) if serializer.is_valid(): user = save_user(request.data, UserPermissions.IS_BRANCH) company = Company(**request.data["company"]) city = City(**request.data["city"]) branch = Branch( nickName=request.data["nickName"], type=UserType.BRANCH, address=request.data["address"], phone=request.data["phone"], company=company, city=city, user=user, ) branch.save() return Response( { "status": "SUCCESS", "msg_status": "Sucursal creada satisfactoriamente. Puede ingresar con su nueva contrasena.", } ) else: messages = [] make_error(serializer.errors.values(), messages) return Response({"status": "FAILURE", "msg_status": messages})
def create_branches(self): """ Cadastra os Ramos """ #print("CADASTRAR RAMOS:") for branch in BRANCHS: branch_new = Branch() branch_new.id = branch['id'] branch_new.name = branch['name'] branch_new.description = branch['description'] branch_new.active = branch['active'] if branch.get('group'): branch_new.group = Group.objects.get(pk=branch['group']['id']) branch_new.save()
def _branchAdd(requestData, user): ''' 增加一个选项的具体处理过程 ''' # 检查是否有提供Question keys = requestData.keys() if 'question' not in keys: return packageResult(RESULT_CODE.ERROR, RESULT_MESSAGE.NO_ID) questionIdSigned = requestData['question'] # 对id进行数字签名的检查 try: signer = Signer() questionId = signer.unsign(questionIdSigned) except BadSignature: # 篡改发现处理 return packageResult(RESULT_CODE.ERROR, RESULT_MESSAGE.BAD_SAGNATURE) # 尝试读取question信息 questionList = Question.objects.filter(id=questionId) if len(questionList) == 0: return packageResult(RESULT_CODE.ERROR, RESULT_MESSAGE.OBJECT_NOT_EXIST) question = questionList[0] # 检查是否有权限做新增 if question.createBy.id != user.id: return packageResult(RESULT_CODE.ERROR, RESULT_MESSAGE.NO_PRIVILEGE) # 遍历Branch的所有Field,并尝试在request中寻找是否提供了对应的数据 data = {} for field in getModelFields(Branch): # 跳过系统自动增加的字段 if field.auto_created: continue # 读取request数据 value = requestData.get(field.name, None) # 特殊处理json的Boolean型的变量 if type(field) == BooleanField: value = jsonBoolean2Python(value) # 对创建人和修改人的信息进行特殊处理 if field.name in [USER_CREATE_BY_FIELD_NAME, USER_MODIFY_BY_FIELD_NAME]: value = user # 对question字段进行特殊处理,提交的数据是id转化为对象 if field.name == 'question': value = question # 对ord 字段进行特殊处理,取当前的问题数量加1 if field.name == 'ord': # 这里锁定了question所有branch对象 value = question.branch_set.select_for_update().count() # 如果调用者没有显示执行字段值为空,则不增加到data中去,让模型的默认值发挥作用 # 字段代码不能早于对createBy和modifyBy的处理 if value is None and field.name not in keys: continue # 将校验的数据添加到data,准备为创建数据库用 data[field.name] = value branch = Branch(**data) # 进行数据校验 try: branch.full_clean() except ValidationError as exception: return packageResult( RESULT_CODE.ERROR, RESULT_MESSAGE.VALIDATION_ERROR, {'validationMessage': exception.message_dict}) # 写到数据库 branch.save() # 返回成功 return packageResult(RESULT_CODE.SUCCESS, RESULT_MESSAGE.SUCCESS, {'branchId': branch.id})
def index(request): if request.method != 'POST': return render(request, 'index.html') else: if request.is_ajax(): if request.POST.get('userName'): user_name = request.POST.get('userName') user = User.objects.get(user_name=user_name) else: user = None if request.POST.get("mode") == "grow": parent = request.POST.get('parent') child = request.POST.get('child') # case 1: parent already trunk -- create branch from child try: trunk = Trunk.objects.get(text=parent) drawing = trunk.drawing if trunk: try: branch = Branch.objects.get(text=child) data = { "already": True, "parent": parent, "child": child } return JsonResponse(data) except Branch.DoesNotExist: branch = Branch(text=child, trunk=trunk) branch.save() branch.get_tips() data = drawing.get_data() if trunk.branch_set.count() > 1: data["tree?"] = trunk.text branch.stage = "filled" branch.save() return JsonResponse(data) # display message saying that this combo already exists. except Trunk.DoesNotExist: # case 2: parent already branch -- create twig from child print("trying case 2") try: branch = Branch.objects.get(text=parent) drawing = branch.trunk.drawing if branch: try: twig = Twig.objects.get(text=child) data = { "already": True, "parent": parent, "child": child } return JsonResponse(data) except Twig.DoesNotExist: twig = Twig(text=child, branch=branch) twig.save() twig.get_tips() data = drawing.get_data() if branch.trunk.branch_set.count() > 1: data["tree?"] = branch.trunk.text twig.stage = "filled" twig.save() return JsonResponse(data) except Branch.DoesNotExist: # case 3: parent already twig -- create leaf from child print("trying case 3") try: twig = Twig.objects.get(text=parent) drawing = twig.branch.trunk.drawing if twig: try: leaf = Leaf.objects.get(text=child) leaf.twigs.add(twig) leaf.save() leaf.get_tips(parent) data = drawing.get_data() return JsonResponse(data) except Leaf.DoesNotExist: leaf = Leaf(text=child) leaf.save() leaf.twigs.add(twig) leaf.save() leaf.get_tips(parent) data = drawing.get_data() if twig.branch.trunk.branch_set.count( ) > 1: data["tree?"] = twig.branch.trunk.text leaf.stage = "filled" leaf.save() return JsonResponse(data) except Twig.DoesNotExist: # case 4: neither child nor parent saved to tree -- # create branch from child and trunk from parent print("trying case 4") trunk = Trunk.objects.create(text=parent) branch = Branch.objects.create(text=child) branch.get_tips() branch.trunk = trunk branch.save() drawing = Drawing.objects.create(text=parent, trunk=trunk) data = drawing.get_data() trunk.stage = "filled" trunk.save() branch.stage = "filled" branch.save() return JsonResponse(data) elif request.POST.get("mode") == "prompt": trunk_name = request.POST.get("trunkName") trunk = Trunk.objects.get(text=trunk_name) try: tree = Tree.objects.get(name=trunk_name) tree.rank_twigs() tree.save() sorted_twigs = Twig.objects.order_by('-rank').filter( tree=tree) current_twig = sorted_twigs[tree.twigs_learned] print("current twig is " + current_twig.text) data = {"twig": current_twig.text} return JsonResponse(data) except Tree.DoesNotExist: tree = Tree(name=trunk_name, trunk=trunk) tree.rank_twigs() tree.save() sorted_twigs = Twig.objects.order_by('-rank').filter( tree=tree) current_twig = sorted_twigs[tree.twigs_learned] data = {"twig": current_twig.text} return JsonResponse(data) elif request.POST.get("mode") == "remind": trunk_name = request.POST.get("trunkName") tree = Tree.objects.get(name=trunk_name) sorted_twigs = Twig.objects.order_by('-rank').filter(tree=tree) current_twig = sorted_twigs[tree.twigs_learned] tries = request.POST.get("tries").strip(',') for leaf in current_twig.leaf_set.all(): if leaf.text in tries: leaf.learned = True leaf.save() total_count = current_twig.leaf_set.count() learned_count = current_twig.leaf_set.all().filter( learned=True).count() if total_count == learned_count: tree.twigs_learned += 1 tree.save() current_twig = sorted_twigs[tree.twigs_learned] data = {"twig": current_twig.text} return JsonResponse(data) else: missed_leaf = current_twig.leaf_set.all().filter( learned=False)[0] print("missed leaf is: " + missed_leaf.text) next_twigs_list = [ i.text for i in missed_leaf.twigs.all() [1:missed_leaf.reminders + 1] ] print(next_twigs_list) next_twigs = map(lambda x: " AND " + x, next_twigs_list) data = { "miss": current_twig.text, "next_twigs": next_twigs } missed_leaf.reminders += 1 missed_leaf.save() return JsonResponse(data) elif request.POST.get("mode") == "check": to_check = request.POST.get("toCheck") try: matching_leaf = current_twig.leaf_set.get(text=to_check) next_leaf = current_twig.misses[indexofmissedleaf + 1] other_twigs = request.POST.get(nextTwigs) next_leaf_next_twigs = [ i.text for i in next_leaf.twigs[0:missed_leaf.reminders] ] if other_twigs in [twig.text for i in matching_leaf.twigs]: # user successfully remembered missed leaf she was reminded of. go on # to next leaf... matching_leaf.learned = True matching_leaf.save() current_twig.misses.remove(matching_leaf) data = { "miss": next_leaf.text, "next_twigs": next_leaf_next_twigs } return JsonResponse(data) except matching_leaf.DoesNotExist: # user failed to remember missed leaf she was reminded of. # give another reminder, unless she has exhausted them all... missed_leaf = current_twig.misses[0] next_twigs = [ i.text for i in missed_leaf.twigs[0:missed_leaf.reminders] ] data = {"miss": current_twig, "next_twigs": [i.text]} return JsonResponse(data) else: print("request wasn't ajax") return render(request, 'index.html')
def index(request): if request.method != 'POST': return render(request, 'index.html') else: if request.is_ajax(): if request.POST.get('userName'): user_name=request.POST.get('userName') user=User.objects.get(user_name=user_name) else: user=None if request.POST.get("mode") == "grow": parent = request.POST.get('parent') child = request.POST.get('child') # case 1: parent already trunk -- create branch from child try: trunk = Trunk.objects.get(text=parent) drawing = trunk.drawing if trunk: try: branch = Branch.objects.get(text=child) data = {"already" : True, "parent" : parent, "child" : child } return JsonResponse(data) except Branch.DoesNotExist: branch = Branch(text=child, trunk=trunk) branch.save() branch.get_tips() data = drawing.get_data() if trunk.branch_set.count() > 1: data["tree?"] = trunk.text branch.stage = "filled" branch.save() return JsonResponse(data) # display message saying that this combo already exists. except Trunk.DoesNotExist: # case 2: parent already branch -- create twig from child print("trying case 2") try: branch = Branch.objects.get(text=parent) drawing = branch.trunk.drawing if branch: try: twig = Twig.objects.get(text=child) data = {"already" : True, "parent" : parent, "child" : child } return JsonResponse(data) except Twig.DoesNotExist: twig = Twig(text=child, branch=branch) twig.save() twig.get_tips() data = drawing.get_data() if branch.trunk.branch_set.count() > 1: data["tree?"] = branch.trunk.text twig.stage = "filled" twig.save() return JsonResponse(data) except Branch.DoesNotExist: # case 3: parent already twig -- create leaf from child print("trying case 3") try: twig = Twig.objects.get(text=parent) drawing = twig.branch.trunk.drawing if twig: try: leaf = Leaf.objects.get(text=child) leaf.twigs.add(twig) leaf.save() leaf.get_tips(parent) data = drawing.get_data() return JsonResponse(data) except Leaf.DoesNotExist: leaf = Leaf(text=child) leaf.save() leaf.twigs.add(twig) leaf.save() leaf.get_tips(parent) data = drawing.get_data() if twig.branch.trunk.branch_set.count() > 1: data["tree?"] = twig.branch.trunk.text leaf.stage = "filled" leaf.save() return JsonResponse(data) except Twig.DoesNotExist: # case 4: neither child nor parent saved to tree -- # create branch from child and trunk from parent print("trying case 4") trunk = Trunk.objects.create(text=parent) branch = Branch.objects.create(text=child) branch.get_tips() branch.trunk = trunk branch.save() drawing = Drawing.objects.create(text=parent, trunk=trunk) data = drawing.get_data() trunk.stage = "filled" trunk.save() branch.stage = "filled" branch.save() return JsonResponse(data) elif request.POST.get("mode") == "prompt": trunk_name = request.POST.get("trunkName") trunk = Trunk.objects.get(text=trunk_name) try: tree = Tree.objects.get(name=trunk_name) tree.rank_twigs() tree.save() sorted_twigs = Twig.objects.order_by('-rank').filter(tree=tree) current_twig = sorted_twigs[tree.twigs_learned] print("current twig is " + current_twig.text) data = { "twig" : current_twig.text } return JsonResponse(data) except Tree.DoesNotExist: tree = Tree(name=trunk_name, trunk=trunk) tree.rank_twigs() tree.save() sorted_twigs = Twig.objects.order_by('-rank').filter(tree=tree) current_twig = sorted_twigs[tree.twigs_learned] data = { "twig" : current_twig.text } return JsonResponse(data) elif request.POST.get("mode") == "remind": trunk_name = request.POST.get("trunkName") tree = Tree.objects.get(name=trunk_name) sorted_twigs = Twig.objects.order_by('-rank').filter(tree=tree) current_twig = sorted_twigs[tree.twigs_learned] tries = request.POST.get("tries").strip(',') for leaf in current_twig.leaf_set.all(): if leaf.text in tries: leaf.learned = True leaf.save() total_count = current_twig.leaf_set.count() learned_count = current_twig.leaf_set.all().filter(learned=True).count() if total_count == learned_count: tree.twigs_learned += 1 tree.save() current_twig = sorted_twigs[tree.twigs_learned] data = {"twig" : current_twig.text} return JsonResponse(data) else: missed_leaf = current_twig.leaf_set.all().filter(learned=False)[0] print("missed leaf is: " + missed_leaf.text) next_twigs_list = [i.text for i in missed_leaf.twigs.all()[1:missed_leaf.reminders+1]] print(next_twigs_list) next_twigs = map(lambda x: " AND " + x, next_twigs_list) data = {"miss" : current_twig.text, "next_twigs" : next_twigs} missed_leaf.reminders += 1 missed_leaf.save() return JsonResponse(data) elif request.POST.get("mode") == "check": to_check = request.POST.get("toCheck") try: matching_leaf = current_twig.leaf_set.get(text=to_check) next_leaf = current_twig.misses[indexofmissedleaf+1] other_twigs = request.POST.get(nextTwigs) next_leaf_next_twigs = [i.text for i in next_leaf.twigs[0:missed_leaf.reminders]] if other_twigs in [twig.text for i in matching_leaf.twigs]: # user successfully remembered missed leaf she was reminded of. go on # to next leaf... matching_leaf.learned = True matching_leaf.save() current_twig.misses.remove(matching_leaf) data = { "miss" : next_leaf.text, "next_twigs" : next_leaf_next_twigs } return JsonResponse(data) except matching_leaf.DoesNotExist: # user failed to remember missed leaf she was reminded of. # give another reminder, unless she has exhausted them all... missed_leaf = current_twig.misses[0] next_twigs = [i.text for i in missed_leaf.twigs[0:missed_leaf.reminders]] data = { "miss" : current_twig, "next_twigs" : [i.text] } return JsonResponse(data) else: print("request wasn't ajax") return render(request, 'index.html')