def post(self, request): response_data = {'retCode': error_constants.ERR_STATUS_SUCCESS[0], 'retMsg': error_constants.ERR_STATUS_SUCCESS[1]} try: section_id = int(request.POST.get('sectionId')) # faculty_id = int(request.POST.get('facultyId')) name = request.POST.get('name', '') mobile = request.POST.get('mobile', '') duty = request.POST.get('duty', '') channel = request.POST.get('channel', '') call_sign = request.POST.get('callSign', '') district_id = int(request.POST.get('districtId', 0)) # faculty_type 1 段长;2 执行段长 分局;3: 执行段长 交通;4:执行段长 武警 faculty_type = int(request.POST.get('facultyType')) except Exception as ex: print 'function name: ', __name__ print Exception, ":", ex return generate_error_response(error_constants.ERR_INVALID_PARAMETER, status.HTTP_400_BAD_REQUEST) cur_section = Section.objects.get(id=section_id) cur_faculty = Faculty.objects.filter(name=name, mobile=mobile) if cur_faculty.exists(): cur_faculty.update(enabled=True) cur_faculty = cur_faculty.first() else: cur_faculty = Faculty(name=name, mobile=mobile, duty=duty, main_name=cur_section.name, level=2, role=faculty_type, main_id=section_id, district_id=cur_section.district_id, channel=cur_section.channel, call_sign=cur_section.call_sign) # 岗位人数限制 count = check_faculty_count_particular_role(cur_faculty) if faculty_type == 1: count1 = cur_section.chief.filter(enabled=True).count() if faculty_type == 2: count1 = cur_section.exec_chief_sub_bureau.filter(enabled=True).count() if faculty_type == 3: count1 = cur_section.exec_chief_trans.filter(enabled=True).count() if faculty_type == 4: count1 = cur_section.exec_chief_armed_poli.filter(enabled=True).count() print 'section people_count:', count1 if count >= 4 or count1 >= 4: return generate_error_response(error_constants.ERR_FACULTY_EXCEED_COUNT, status.HTTP_400_BAD_REQUEST) else: try: with transaction.atomic(): cur_faculty.save() except Exception as ex: print 'function name: ', __name__ print Exception, ":", ex return generate_error_response(error_constants.ERR_SAVE_INFO_FAIL, status.HTTP_500_INTERNAL_SERVER_ERROR) if faculty_type == 1: cur_section.chief.add(cur_faculty) if faculty_type == 2: cur_section.exec_chief_sub_bureau.add(cur_faculty) if faculty_type == 3: cur_section.exec_chief_trans.add(cur_faculty) if faculty_type == 4: cur_section.exec_chief_armed_poli.add(cur_faculty) return Response(response_data, status.HTTP_200_OK)
def FacultyAdd(request): user = request.user if user.is_authenticated(): facultyForm = FacultyForm(request.POST) # Проверяем - если браузер присылает данные (запрос) по методу Post if request.method == 'POST': # Создем экземпляр модели данных факультета (новый факультет) faculty = Faculty() #создаем экземпляр отображаемой формы (forms.py) #Возвращаем пользователю страничку в ответ на запрос (шаблон, form подменяем на созданную выше форму, остальное просто надо) if facultyForm.is_valid(): # Заполним содержимое факультета и сохраним его faculty.name = facultyForm.cleaned_data['name'] faculty.save() #lesson.lessonID=date+room #lesson.user = user return HttpResponseRedirect('/admin') return render_to_response('dokladedit.html', {'form': facultyForm}, context_instance=RequestContext(request)) return HttpResponseRedirect('/admin')
def post(self, request): response_data = { 'retCode': error_constants.ERR_STATUS_SUCCESS[0], 'retMsg': error_constants.ERR_STATUS_SUCCESS[1] } try: name = request.POST.get('name', '') mobile = request.POST.get('mobile', '') duty = request.POST.get('duty', '') district_id = int(request.POST.get('districtId', 0)) channel = request.POST.get('channel', '') call_sign = request.POST.get('callSign', '') level = int(request.POST.get('level', 0)) role = int(request.POST.get('role', 0)) road_section_station = int( request.POST.get('road_section_station', 0)) except Exception as ex: print 'function name: ', __name__ print Exception, ":", ex return generate_error_response( error_constants.ERR_INVALID_PARAMETER, status.HTTP_400_BAD_REQUEST) if not name or not mobile: return generate_error_response(error_constants.ERR_NO_NAME_MOBILE, status.HTTP_400_BAD_REQUEST) # cur_faculty = Faculty.objects.filter(name=name, mobile=mobile) # if cur_faculty.exists(): # cur_faculty.update(enabled=True) else: main_name = '' if level and road_section_station: if level == 1: main_name = Road.objects.get(id=road_section_station).name if level == 2: main_name = Section.objects.get( id=road_section_station).name if level == 3: main_name = Station.objects.get( id=road_section_station).name cur_faculty = Faculty(district_id=district_id, name=name, mobile=mobile, duty=duty, channel=channel, call_sign=call_sign, level=level, role=role, main_id=road_section_station, main_name=main_name) # 岗位人数限制 count = check_faculty_count_particular_role(cur_faculty) if count >= 4: return generate_error_response( error_constants.ERR_FACULTY_EXCEED_COUNT, status.HTTP_400_BAD_REQUEST) else: try: with transaction.atomic(): cur_faculty.save() except Exception as ex: print 'function name: ', __name__ print Exception, ":", ex return generate_error_response( error_constants.ERR_SAVE_INFO_FAIL, status.HTTP_500_INTERNAL_SERVER_ERROR) return Response(response_data, status=status.HTTP_200_OK)