Esempio n. 1
0
    def patch(self, request, uuid):
        try:
            person = Person.objects.get(uuid=uuid)
        except ValidationError:
            return Response({
                "status": "error",
                "reason": "Invalid UUID"
            },
                            status=status.HTTP_400_BAD_REQUEST)

        except Person.DoesNotExist:
            return Response(
                {
                    "status": "error",
                    "reason": "UUID does not exist"
                },
                status=status.HTTP_404_NOT_FOUND)

        serializer = PersonSerializer(person, data=request.data, partial=True)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_200_OK)
        return Response({
            'status': 'error',
            'reason': serializer.errors
        }, status.HTTP_400_BAD_REQUEST)
Esempio n. 2
0
    def post(self, request):
        """POST handler."""
        # person = Person.get(self.database_manager, id=1)
        # processor = PersonSerializer(person)
        # output_json = processor.to_json()
        print("POST")
        input_processor = PersonSerializer(self.database_manager, request.body_json)
        errors = input_processor.check_json()
        print("JSON Errors:")
        print(errors)
        if len(errors) == 0:
            # new_person = input_processor.to_object()
            new_person = input_processor.save_object()
            print(new_person)
        else:
            output = dict([(error, "This field is required") for error in errors])
            return Response(output, status=Response.STATUS_400_BAD_REQUEST, headers=self.headers)
        # print("PERSON as JSON:")
        # print(json.dumps(output_json, indent=4))
        print("POST")
        print(request)
        print(self.headers)
        body = request.body
        print("BODY:")
        print(body)
        print(json.dumps(request.body_json, indent=4))
        # input_processor = PersonSerializer(request.body_json)

        response = Response(input_processor.to_json(), headers=self.headers)
        return response
Esempio n. 3
0
 def get(self, request, format=None):
     """
     Return a list of all users.
     """
     people = [person for person in Person.objects.all()]
     serializer = PersonSerializer(people, many=True)
     s = PersonSerializer()
     return Response({"columns": s.get_slick_columns(), "rows": serializer.data})
Esempio n. 4
0
 def post(self, request):
     serializer = PersonSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     else:
         print serializer.error_messages
         print serializer.errors
         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 5
0
 def put(self, request):
     person = Person.objects.get(id=request.data["id"])
     serializer = PersonSerializer(person, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     else:
         print serializer.error_messages
         print serializer.errors
         return Response(serializer.error_messages, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 6
0
 def put(self, request):
     serializer = PersonSerializer(data=request.data)
     if serializer.is_valid():
         obj = serializer.save()
         return Response({
             'status': 'ok',
             'uuid': obj.uuid
         },
                         status=status.HTTP_201_CREATED)
     return Response({
         'status': 'error',
         'reason': serializer.errors
     }, status.HTTP_400_BAD_REQUEST)
Esempio n. 7
0
def person_list_view(request):
    people = Person.objects.order_by("full_name").all()
    if is_react_api_call(request):
        return Response(data=PersonSerializer(people, many=True).data)
    else:
        context = {"people": people}
        return render(request, "case/person/list.html", context)
    def get(self, *args, **kwargs):
        """."""

        id = kwargs.get("id")
        queryset: Person = Person.query.get(id)
        serializer: PersonSerializer = PersonSerializer()
        return jsonify(serializer.dump(queryset)), 200
Esempio n. 9
0
 def get(self, id):
     '''Get a person.'''
     try:
         person = models.person.query().get(id=id)
     except Person.DoesNotExist:
         abort(404)
     return jsonify(PersonSerializer(person).data)
Esempio n. 10
0
    def create(self, request, *args, **kwargs):

        name = request.POST.get('name', None)

        #Ughh so hacky, need to fix this. Damn serializer read_only_fields seem buggy, they claim to be required for POSTs.
        #Overriding perform_create worked in django 1.8 but seems to fail in 1.9 claiming read only fields are required for writes.
        if (name in [None, 'undefined', ''] or Person.objects.filter(
                name=name, user=request.user).exists()):
            response = Response(status=status.HTTP_400_BAD_REQUEST)
        else:
            new_person = Person.objects.create(name=name, user=request.user)

            for p2 in Person.objects.all():
                PersonSimilarity.objects.get_or_create(person1=new_person,
                                                       person2=p2,
                                                       similarity=0)
                PersonSimilarity.objects.get_or_create(person1=p2,
                                                       person2=new_person,
                                                       similarity=0)

            for product in [
                    p for p in Product.objects.filter(
                        ASIN__in=settings.DEFAULT_SUGGESTIONS)
            ]:
                Suggestion.objects.get_or_create(person=new_person,
                                                 product=product,
                                                 relevance=.99)

            serializer = PersonSerializer(new_person)
            response = JsonResponse(serializer.data)

        return response
Esempio n. 11
0
 def get(self, id):
     '''Get a person.'''
     try:
         person = Person[id]
     except orm.ObjectNotFound:
         abort(404)
     return jsonify(PersonSerializer(person).data)
Esempio n. 12
0
 def index(self):
     '''Get all people, ordered by creation date.'''
     all_people = orm.select(p for p in Person).order_by(
         orm.desc(Person.created))[:]
     data = PersonSerializer(all_people, many=True,
                             exclude=('created', )).data
     return jsonify({"people": data})
Esempio n. 13
0
 def get(self, id):
     '''Get a person.'''
     # Could also use flask_peewee.utils.get_object_or_404
     try:
         person = Person.get(Person.id == int(id))
     except Person.DoesNotExist:
         abort(404)
     return jsonify(PersonSerializer(person).data)
Esempio n. 14
0
def person_all(request, format=None):
    """
    Get all persons
    """
    if request.method == 'GET':
        creators = Person.objects.all()
        serializer = PersonSerializer(creators, many=True)
        return Response(serializer.data)
    def delete(self, *args, **kwargs):
        """."""

        id = kwargs.get("id")
        serializer: PersonSerializer = PersonSerializer()
        queryset: Person = Person.query.get(id)
        Person.query.filter_by(id=id).delete()
        db.session.commit()

        return jsonify(serializer.dump(queryset)), 200
Esempio n. 16
0
 def post(self):
     '''Insert a new person.'''
     data = request.json
     firstname = data.get("firstname")
     lastname = data.get("lastname")
     if not firstname or not lastname:
         abort(400)  # Must specify both first and last name
     person = Person(firstname=firstname, lastname=lastname)
     db.session.add(person)
     db.session.commit()
     return jsonify({"message": "Successfully added new person.",
                     "person": PersonSerializer(person).data}), 201
    def post(self, *args, **kwargs):
        """."""

        try:
            serializer: PersonSerializer = PersonSerializer()
            queryset: Person = serializer.load(request.json)
            db.session.add(queryset)
            db.session.commit()
        except ValidationError as e:
            return jsonify(e.messages), 400
        else:
            return jsonify(serializer.dump(queryset)), 200
    def put(self, *args, **kwargs):
        """."""

        try:
            id = kwargs.get("id")
            queryset: Person = Person.query.get(id)
            serializer: PersonSerializer = PersonSerializer()
            serializer.load(request.json, instance=queryset, partial=True)
            db.session.commit()
        except ValidationError as e:
            return jsonify(e.messages), 400
        else:
            return jsonify(serializer.dump(queryset)), 200
Esempio n. 19
0
    def get(self, request, uuid):
        try:
            person = Person.objects.get(uuid=uuid)
        except ValidationError:
            return Response({
                "status": "error",
                "reason": "Invalid UUID"
            },
                            status=status.HTTP_400_BAD_REQUEST)

        except Person.DoesNotExist:
            return Response(
                {
                    "status": "error",
                    "reason": "UUID does not exist"
                },
                status=status.HTTP_404_NOT_FOUND)

        serializer = PersonSerializer(person)
        return Response(serializer.data, status=status.HTTP_200_OK)
Esempio n. 20
0
 def index(self):
     '''Get all people, ordered by creation date.'''
     all_people = models.person.query().sort_by("-created")
     data = PersonSerializer(all_people, exclude=('created', ),
                             many=True).data
     return jsonify({"people": data})
    def get(self, *args, **kwargs):
        """."""

        queryset: Person = Person.query.all()
        serializer: list = PersonSerializer(many=True)
        return jsonify(serializer.dump(queryset)), 200
Esempio n. 22
0
 def get(self, id):
     '''Get a person.'''
     person = Person.query.get_or_404(int(id))
     return jsonify(PersonSerializer(person).data)
Esempio n. 23
0
 def index(self):
     '''Get all people, ordered by creation date.'''
     all_items = Person.select().order_by(Person.created.desc())
     data = PersonSerializer(all_items, exclude=('created', ),
                             many=True).data
     return jsonify({"people": data})
Esempio n. 24
0
	def get(self,request,pk):
		person = Person.objects.get(pk=request.user.pk)
		serializer = PersonSerializer(person)
		return Response(data=serializer.data,status=status.HTTP_200_OK)