def put(self, request, name, format=None): group = self.get_object(name) serializer = GroupSerializer(group, data=request.data) if serializer.is_valid(): serializer.save() return Response( {'data': serializer.data}, status=status.HTTP_202_ACCEPTED ) return Response(status=status.HTTP_406_NOT_ACCEPTABLE)
def put(self, request, pk): group = self.get_object(pk) data = request.data['params'] # data = request.data # print(request.data['extraParams']) serializer = GroupSerializer(group, data=data) if serializer.is_valid(): serializer.save() if 'extraParams' in request.data: extraData = request.data['extraParams'] menuKey = None if 'menuKey' in extraData: menuKey = extraData['menuKey'] departKey = None if 'departKey' in extraData: departKey = extraData['departKey'] permissionsKey = None if 'permissionsKey' in extraData: permissionsKey = extraData['permissionsKey'] if not departKey is None: rd = GroupDepart.objects.filter(group_id=group.id).first() if not rd is None: GroupDepart.objects.filter(id=rd.id).update(group_id=group.id, depart_id=departKey) else: GroupDepart.objects.create(group_id=group.id, depart_id=departKey) if menuKey: GroupMenu.objects.filter(group=group).delete() querysetlist = [] for i in menuKey: querysetlist.append(GroupMenu(group_id=group.id, menu_id=i)) GroupMenu.objects.bulk_create(querysetlist) else: GroupMenu.objects.filter(group=group).delete() if permissionsKey: g = Group.objects.get(id=group.id) g.permissions.clear() for i in permissionsKey: p = Permission.objects.get(id=i) g.permissions.add(p) g.save() else: g = Group.objects.get(id=group.id) g.permissions.clear() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class SubstituteSerializer(ModelSerializer): ScheduleKey = ScheduleSerializer(read_only=True) GroupKey = GroupSerializer(read_only=True) class Meta: model = Substitute fields = '__all__'
def get(_, lang): if lang == 'ja': cached_groups = Cache.get('api_groups') if cached_groups is None: res = GroupSerializer(Group.get_all(), many=True).data Cache.set('api_groups', res) else: res = cached_groups elif lang == 'en': try: cached_groups_en = Cache.get('api_groups_en') except: cached_groups_en = None if cached_groups_en is None: res = GroupEnSerializer(Group.get_all(), many=True).data Cache.set('api_groups_en', res) else: res = cached_groups_en else: return JsonResponse({'message': 'Not Found'}, status=404) return JsonResponse(res, safe=False)
class ScheduleSerializer(ModelSerializer): GroupKey = GroupSerializer(read_only=True) ParticipateKey = ParticipateSerializer(read_only=True) MemberKey = MemberSerializer(read_only=True) class Meta: model = Schedule fields = '__all__'
def get(self, request): name = request.GET.get("roleName") if not name is None: group = Group.objects.filter(name__icontains=name).order_by('id') else: group = Group.objects.all().order_by('id') pg = StandardPageNumberPagination() page_roles = pg.paginate_queryset(queryset=group, request=request, view=self) serializer = GroupSerializer(instance=page_roles, many=True) return pg.get_paginated_response(serializer.data)#Response(serializer.data)
class UserSerializer(DynamicFieldsMixin, serializers.HyperlinkedModelSerializer, UUIDSerializer, ModificationSerializer, CreationSerializer): groups = GroupSerializer(many=True) class Meta(UUIDSerializer.Meta, ModificationSerializer.Meta, CreationSerializer.Meta): model = User fields = UUIDSerializer.Meta.fields + ModificationSerializer.Meta.fields + CreationSerializer.Meta.fields + [ 'email', 'first_name', 'last_name', 'groups' ]
def _get_group_detail(myself, group_id): """ 获取某群组详细信息 """ group = check_exist_group(group_id) check_user_in_group(myself, group) memberships = group.membership_set group_serializer = GroupSerializer(group) membership_serializer = GroupMemberSerializer(memberships, many=True) rtn = dict(group_serializer.data) rtn['members'] = membership_serializer.data return Response(rtn)
def post(request, group_id=None): """ 新建群组 """ from group.models import Group, MembershipTmp check_is_none(group_id) myself = request.user.user name = request.data.get('name') my_group = Group.objects.create(name=name) MembershipTmp.objects.create(group=my_group, user=myself, permission='master') serializer = GroupSerializer(my_group) return Response(serializer.data)
def post(self, request): data = request.data['params'] # data = request.data # print(request.data['extraParams']) serializer = GroupSerializer(data=data) if serializer.is_valid(): serializer.save() groupId = serializer.data['id'] if 'extraParams' in request.data: extraData = request.data['extraParams'] menuKey = None if 'menuKey' in extraData: menuKey = extraData['menuKey'] departKey = None if 'departKey' in extraData: departKey = extraData['departKey'] permissionsKey = None if 'permissionsKey' in extraData: permissionsKey = extraData['permissionsKey'] if not departKey is None: GroupDepart.objects.create(group_id=groupId, depart_id=departKey) if menuKey: querysetlist = [] for i in menuKey: querysetlist.append(GroupMenu(group_id=groupId, menu_id=i)) GroupMenu.objects.bulk_create(querysetlist) if permissionsKey: g = Group.objects.get(id=groupId) for i in permissionsKey: p = Permission.objects.get(id=i) g.permissions.add(p) g.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class PostSerializer(serializers.ModelSerializer): author = UserSerializer(read_only=True) group = GroupSerializer(read_only=True) class Meta: model = Post fields = ('pk', 'author', 'group', 'content', 'image', 'video', 'like_count', 'comment_count', 'created_date') read_only_fields = ( 'author', 'group', 'like_count', 'comment_count', ) def to_representation(self, obj): ret = super().to_representation(obj) ret['is_like'] = self.context['request'].user in obj.like_users.all() return ret
class APSerializer(serializers.ModelSerializer): """ AP序列化 """ apType = APTypeSerializer(many=False, read_only=True) group = GroupSerializer(many=False, read_only=True) class Meta: model = AP fields = ('apIndex', 'basemac', 'radio_basemac_offset', 'radio_basemac2', 'radio_basemac5',\ 'apname', 'profileversion', 'apversion', 'bootversion', 'group', 'vlanId',\ 'vlanId1', 'vlanId2', 'vlanId3', 'vlanId4', 'lan_disable1', 'lan_disable2',\ 'lan_disable3', 'lan_disable4', 'forwardmode1', 'forwardmode2', 'forwardmode3',\ 'forwardmode4', 'portal_enable1', 'portal_enable2', 'portal_enable3', 'portal_enable4',\ 'apMemo', 'location', 'city', 'binIndex', 'apType', 'apOnline', 'apStatus', 'apPreConf',\ 'apUptime', 'rogue_ap_detect', 'balance_enable', 'balance_mode', 'users_threshold', 'users_offset',\ 'flow_threshold', 'flow_offset', 'dband_balance', 'user_maxretries', 'log_level', 'local_file_level',\ 'remote_server_level', 'remote_server_addr', 'remote_server_port', 'log_cloud', 'log_cloud_url',\ 'check_addr', 'check_switch', 'apLocalIp', 'apGwIp', 'acIp', 'ap_flag', 'portal_audit_enable', 'portal_audit_ip',\ 'portal_audit_port', 'portal_audit_type', 'portal_audit_key', 'rtls_enable', 'rtls_ip', 'rtls_port', 'rtls_token',\ 'rtls_appkey', 'rtls_servers', 'rtls_engine', 'rtls_timeout', 'capwap_data_sport', 'capwap_data_dport',\ 'capwap_data_local_sport', 'capwap_ac_local_ip', 'binUrl', 'saveConfig', 'binMd5', 'updateState',\ 'uplink_detect_switch', 'uplink_detect_action', 'uplink_detect_ip')
def get(self, request, pk): # print(request.user) group = self.get_object(pk) serializer = GroupSerializer(group) return Response(serializer.data)
class UserSerializer(serializers.ModelSerializer): """Serializer with current match.""" net_id = serializers.CharField(source="person.net_id") majors = MajorSerializer(source="person.majors", many=True) hometown = serializers.CharField(source="person.hometown") profile_pic_url = serializers.CharField(source="person.profile_pic_url") facebook_url = serializers.CharField(source="person.facebook_url") instagram_username = serializers.CharField( source="person.instagram_username") graduation_year = serializers.CharField(source="person.graduation_year") pronouns = serializers.CharField(source="person.pronouns") purposes = PurposeSerializer(source="person.purposes", many=True) availability = SerializerMethodField("get_availability") locations = LocationSerializer(source="person.locations", many=True) interests = InterestSerializer(source="person.interests", many=True) groups = GroupSerializer(source="person.groups", many=True) prompts = SerializerMethodField("get_prompts") has_onboarded = serializers.BooleanField(source="person.has_onboarded") pending_feedback = serializers.BooleanField( source="person.pending_feedback") current_match = serializers.SerializerMethodField("get_current_match") deleted = serializers.BooleanField(source="person.soft_deleted") blocked_users = SerializerMethodField("get_blocked_users") is_paused = serializers.BooleanField(source="person.is_paused") pause_expiration = serializers.DateTimeField( source="person.pause_expiration") last_active = serializers.DateTimeField(source="person.last_active") def get_availability(self, user): if user.person.availability is None: return [] availability = json.loads(user.person.availability) return availability def get_current_match(self, user): blocked_ids = user.person.blocked_users.values_list("id", flat=True) matches = Match.objects.filter(Q(user_1=user) | Q( user_2=user)).order_by("-created_date") if (len(matches) == 0 or matches[0].user_1.id in blocked_ids or matches[0].user_2.id in blocked_ids): return None return MatchSerializer(matches.first(), user=user).data def get_prompts(self, user): prompt_questions = user.person.prompt_questions.all() prompt_answers = user.person.prompt_answers if prompt_answers is None: return [] prompt_answers = json.loads(prompt_answers) prompts = [] for question_index in range(len(prompt_questions)): prompts.append({ "id": prompt_questions[question_index].id, "question_name": prompt_questions[question_index].question_name, "question_placeholder": prompt_questions[question_index].question_placeholder, "answer": prompt_answers[question_index], }) return prompts def get_blocked_users(self, user): return map(lambda u: u.id, user.person.blocked_users.all()) class Meta: model = User fields = ( "id", "net_id", "first_name", "last_name", "majors", "hometown", "profile_pic_url", "facebook_url", "instagram_username", "graduation_year", "pronouns", "purposes", "availability", "locations", "interests", "groups", "prompts", "has_onboarded", "deleted", "pending_feedback", "current_match", "blocked_users", "is_paused", "pause_expiration", "last_active", ) read_only_fields = fields
class SimpleUserSerializer(serializers.ModelSerializer): """Serializer for all users view.""" net_id = serializers.CharField(source="person.net_id") profile_pic_url = serializers.CharField(source="person.profile_pic_url") majors = MajorSerializer(source="person.majors", many=True) hometown = serializers.CharField(source="person.hometown") graduation_year = serializers.CharField(source="person.graduation_year") pronouns = serializers.CharField(source="person.pronouns") interests = InterestSerializer(source="person.interests", many=True) groups = GroupSerializer(source="person.groups", many=True) prompts = serializers.SerializerMethodField("get_prompts") pending_feedback = serializers.BooleanField(source="person.pending_feedback") is_blocked = serializers.SerializerMethodField("get_blocked") def get_blocked(self, user): request_user = self.context.get("request_user") if request_user is not None and hasattr(request_user, "person"): return user.id in request_user.person.blocked_users.values_list( "id", flat=True ) return None def get_prompts(self, user): prompt_questions = user.person.prompt_questions.all() prompt_answers = user.person.prompt_answers if prompt_answers is None: return [] prompt_answers = json.loads(prompt_answers) prompts = [] for question_index in range(len(prompt_questions)): prompts.append( { "id": prompt_questions[question_index].id, "question_name": prompt_questions[question_index].question_name, "question_placeholder": prompt_questions[ question_index ].question_placeholder, "answer": prompt_answers[question_index], } ) return prompts class Meta: model = User fields = ( "id", "net_id", "first_name", "last_name", "profile_pic_url", "majors", "hometown", "graduation_year", "pronouns", "interests", "groups", "prompts", "pending_feedback", "is_blocked", ) read_only_fields = fields