def get(self, request, format=None): """ The default get method, i.e on page load """ resource = Resource.objects.all() serializer = ResourceSerializer(resource, many=True) return Response(serializer.data)
def put(self, request, pk, format=None): resource = self.get_object(pk) serializer = ResourceSerializer(resource, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, pk, format=None): """ We are going to add the Certification content along with this pull request """ resource = self.get_object(pk) serializer = ResourceSerializer(resource) return Response(serializer.data)
def post(self, pk): serializer = ResourceSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, pk): resource = self.get_object(self, pk) serializer = ResourceSerializer(resource, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, format=None): """ The default post method. """ serializer = ResourceSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, pk, format=None): """ When requested update the corresponding entry of the table """ resource = self.get_object(pk) serializer = ResourceSerializer(resource, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get_cart_list(self,request): ''' 获取购物车列表 :param request: :return: ''' car_list = request.user.cart resources = Resource.objects.filter(id__in = car_list) serializer = ResourceSerializer(resources,many=True,context={'request': request}) return JsonResponse('ok',data=serializer.data,status=1)
def post(self, request, id): to_do = get_object_or_404(ToDoUser, user=request.user, id=id) to_do.form = request.data['data'] to_do.save() data = to_do.mark_completed() data['to_do'] = ToDoSerializer(data['to_do'], many=True).data data['resources'] = ResourceSerializer(data['resources'], many=True).data data['badges'] = BadgeSerializer(data['badges'], many=True).data return Response(data)
def get_username(self,request): user = request.user a = cache.get(str(user.username)) print a if user.is_anonymous(): return JsonResponse('error', status=0) name = request.user.real_name serializer = self.get_serializer(user).data focus = user.focus.all() res = ResourceSerializer(focus,many=True,context={'request': request}).data return JsonResponse(name,status=1,data=res)
class ConditionSerializer(serializers.ModelSerializer): condition_to_do = ToDoSerializer(many=True, read_only=True) to_do = ToDoSerializer(many=True) badges = BadgeSerializer(many=True) resources = ResourceSerializer(many=True) admin_tasks = PendingAdminTaskSerializer(many=True) external_messages = ExternalMessageSerializer(many=True) class Meta: model = Condition fields = '__all__'
class TaskSerializer(serializers.ModelSerializer): resources = ResourceSerializer(many=True, read_only=True) to_do = ToDoSerializer(many=True, read_only=True) introductions = IntroductionSerializer(many=True, read_only=True) appointments = AppointmentSerializer(many=True, read_only=True) preboarding = PreboardingSerializer(many=True, read_only=True) class Meta: model = get_user_model() fields = ('preboarding', 'to_do', 'resources', 'introductions', 'appointments')
class SequenceSerializer(serializers.ModelSerializer): search_type = serializers.CharField(default='sequence', read_only=True) preboarding = PreboardingSerializer(many=True) to_do = ToDoSerializer(many=True) resources = ResourceSerializer(many=True) appointments = AppointmentSerializer(many=True) conditions = ConditionSerializer(many=True) class Meta: model = Sequence fields = '__all__'
def get_resources(self, obj): try: resources = Resource.objects.filter( companyhasresource__company=obj) serialized_resources = ResourceSerializer(resources, many=True).data for resource in serialized_resources: company_has_resource = CompanyHasResource.objects.get( company=obj, resource_id=resource.get('id')) resource['order'] = company_has_resource.order resource['company_resource_id'] = company_has_resource.id from operator import itemgetter sorted_list = sorted(serialized_resources, key=itemgetter('order')) return sorted_list except Exception as e: return []
class UserSerializer(serializers.ModelSerializer): password = serializers.CharField(write_only=True) password_confirmation = serializers.CharField(write_only=True) resources = ResourceSerializer(many=True, required=False) comments = CommentSerializer(many=True, required=False) def validate(self, data): password = data.pop('password') password_confirmation = data.pop('password_confirmation') if password != password_confirmation: raise ValidationError({'password_confirmation': 'does not match'}) data['password'] = make_password( password) # this is hashing the users password return data class Meta: model = User fields = '__all__'
def get(self, request, company_id): """ Get all client resources :return: {message: string, apps: AppSerializer} """ try: company_resources = ResourceSerializer( App.objects.filter(id__in=CompanyHasResource.objects.filter( company_id=company_id).values_list('id', flat=True)), many=True).data for c_app in company_resources: c_app['order'] = CompanyHasResource.objects.get( company_id=company_id, resource_id=c_app.get('id')).order return Response({'resources': company_resources}, status=status.HTTP_200_OK) except Exception as e: return Response({'message': 'Error retrieving apps'}, status=status.HTTP_400_BAD_REQUEST)
def add_cart(self,request,resource_id): ''' 加入购物车 :param request: :return: ''' user = request.user cart = user.cart is_collect = request.query_params.get('is_collect',None) try: res = Resource.objects.get(id=resource_id) except Resource.DoesNotExist: return JsonResponse('没有这个资源',status = 0) if int(resource_id) in cart: return JsonResponse('物品已在购物车',status=0) user.cart.append(int(resource_id)) user.save() if is_collect == '1': user.focus.remove(res) user.save() collections = user.focus.all() ser = ResourceSerializer(collections, many=True,context={'request': request}).data return JsonResponse('ok', status=1, data=ser) return JsonResponse('ok',status=1)
def get(self, request, id): resources = ResourceSerializer(request.user.resources.get(id=id)) return Response(resources.data)
def get(self, pk): resources = Resources.objects.all().filter(profile__isnull=True) serializer = ResourceSerializer(resources, many=True) return Response(serializer.data, status=status.HTTP_200_OK)
class PopulatedUserSerializer(UserSerializer): created_resources = ResourceSerializer(many=True) created_tasks = TaskSerializer(many=True) created_contacts = ContactSerializer(many=True) created_jobs = JobSerializer(many=True)
def get(self, request, pk): resource = Resources.objects.all().filter(profile=pk) serializer = ResourceSerializer(resource, many=True) return Response(serializer.data)
def get(self, request, pk, format=None): resource = self.get_object(pk) serializer = ResourceSerializer(resource, ) return Response(serializer.data)