def search_room_by_condition(request): """the function of searching eligible rooms of the teachers and students""" if request.method == "POST": query_condition = request.POST.get('query_condition') page = int(request.POST.get('page')) limit = int(request.POST.get('limit')) start_position = page * limit if not query_condition: room = Room.objects.all()[start_position:start_position+limit] else: room = search_by_diff_condition(query_condition, start_position, limit) if not room: return HttpResponse(json.dumps({'code': '0001', 'msg': '查询结果为空', 'data': ''})) rooms = JsonUtil.json_util(room) for room in rooms: college = College.objects.get(id=int(room['college'])) teacher = Teacher.objects.get(email=room['host_email']) room['college'] = college.name room['teacher_name'] = teacher.real_name if rooms: data = {'code': '0000', 'msg': '查询成功', 'data': rooms} else: data = {'code': '0001', 'msg': '查询结果为空', 'data': ''} return HttpResponse(json.dumps(data)) return None
def get_all_calendars(request): """the function of getting all room's calendar""" room_id = int(request.POST.get('room_id')) room_calendar = RoomCalendar.objects.filter(room_id_id=room_id) room_calendar = JsonUtil.json_util(room_calendar) room_calendars = { 'timetable': room_calendar, 'max_week': MAX_WEEK } data = {'code': '0000', 'msg': '获取成功', 'data': room_calendars} return HttpResponse(json.dumps(data))
def list_room_calendar(request): """the function of students and teachers listing room calendar""" room_id = request.POST.get('room_id') week = request.POST.get("week") rooms = RoomCalendar.objects.filter(room_id_id=room_id, week=week) if rooms.count(): rooms = JsonUtil.json_util(rooms) data = {'code': '0000', 'msg': '查询成功', 'data': rooms} else: data = {'code': '0001', 'msg': '该房间暂无日历信息', 'data': None} return HttpResponse(json.dumps(data))
def list_own_rooms(request): """list all rooms which are created by the current teacher""" user_id = request.POST.get('user_id') user = User.objects.get(id=user_id) teacher = Teacher.objects.get(user=user) host_email = teacher.email teachers_rooms = Room.objects.filter( host_email_id=host_email) rooms = JsonUtil.json_util(teachers_rooms) for room in rooms: college = College.objects.get(id=int(room['college'])) room['college'] = college.name data = {'code': '0000', 'msg': '查询成功', 'data': rooms} return HttpResponse(json.dumps(data))
def atomic_create_college(table, rows): """the function of admin atomic_create_college""" try: with transaction.atomic(): for i in range(1, rows): row_values = table.row_values(i) college = College.objects.create(name=row_values[0]) college.save() except Exception: data = {'code': '0003', 'msg': '导入专业信息失败'} else: colleges = College.objects.all() colleges = JsonUtil.json_util(colleges) data = {'code': '0000', 'msg': '导入专业信息成功', 'data': colleges} return data
def list_resolved_rooms(request): """list all rooms which are resolved with the current teacher""" user_id = request.POST.get('user_id') user = User.objects.get(id=user_id) teacher = Teacher.objects.get(user=user) host_email = teacher.email teachers_rooms = RoomTeacher.objects.filter(teacher_email_id=host_email) host_rooms = Room.objects.values('host_email_id').filter(host_email_id=host_email) rooms = JsonUtil.json_util(teachers_rooms) data = [] for room in rooms: room_object = Room.objects.filter(id=room['room_id']) data_json = json.loads(serializers.serialize("json", room_object)) college = College.objects.get(id=int(data_json[0]['fields']['college'])) data_json[0]['fields']['college'] = college.name if not is_host(host_rooms, data_json[0]['fields']['host_email']): data.append(data_json[0]['fields']) data = {'code': '0000', 'msg': '查询成功', 'data': data} return HttpResponse(json.dumps(data))
def list_colleges(request): """the function of getting all colleges""" colleges = College.objects.all() college_list = JsonUtil.json_util(colleges) data = {'code': '0000', 'msg': '查询成功', 'data': college_list} return HttpResponse(json.dumps(data))