Example #1
0
 def put(self, request, pk, format=None):
     location = self.get_object(pk)
     serializer = LocationSerializer(location, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #2
0
    def test_wifi_location_from_json(self):
        """Test that a wifi location can be deserialized from json"""
        location_name = "Arco do CegoS"
        request = {
            "name": location_name,
            "creation_date": "2017-04-30T14:19:35.505954Z",
            "coordinate": {
                "wifiSSIDs": [{
                    "name": "edurom"
                }, {
                    "name": "thompson"
                }, {
                    "name": "house"
                }],
                "type":
                "WIFI"
            }
        }
        #data = JSONParser().parse(json)
        serializer = LocationSerializer(data=request)
        if serializer.is_valid():
            u1 = User.objects.get(username="******")
            serializer.save(user=u1)
            l = Location.objects.get(name=location_name)
            self.assertEqual(l.name, location_name, "Correct location created")
            self.assertEqual(l.coordinate.type, "WIFI",
                             "Correct coordinate added")
            for ssid in l.coordinate.wificoordinate.ssid_set.all():
                self.assertIn(ssid.name, self.ssids_arco,
                              "The ssids are persisted")

        else:
            self.fail(serializer.errors)
Example #3
0
    def post(self, request):

        serializer = LocationSerializer(data=request.data, many=True)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.error_messages,
                        status=status.HTTP_400_BAD_REQUEST)
Example #4
0
 def post(self, request, format=None):
     ##############JSON_GEO Post data######################
     new_location = GeojsonParser(request.data)
     new_data = new_location.set()
     ######################################################
     serializer = LocationSerializer(data=new_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)
Example #5
0
def create_location(request):
    serializer = LocationSerializer(data=request.data["data"])
    if serializer.is_valid():
        serializer.save(user=request.user)
        return Response(JSONResponse().addData("Location",
                                               serializer.data).addData(
                                                   "status",
                                                   "Location created!").send(),
                        status=status.HTTP_201_CREATED)
    else:
        return Response(JSONResponse().addError(
            0, "Location could not be created").addError(
                1, serializer.errors).send(),
                        status=status.HTTP_400_BAD_REQUEST)
Example #6
0
    def post(self, request, pk):
        # Add location Data
        location_data = {
            "location": request.data['location'],
            "address": request.data['address'],
            "x_coord": request.data['x_coord'],
            "y_coord": request.data['y_coord'],
        }
        location_serializer = LocationSerializer(data=location_data)
        location_in_db = Location.objects.filter(
            location=request.data['location'])

        if not location_in_db:
            if location_serializer.is_valid():
                location_serializer.save()
                location_id = location_serializer.data['id']
            else:
                return Response(location_serializer.errors,
                                status=status.HTTP_400_BAD_REQUEST)
        else:
            location_id = location_in_db.get().id

        # Add Case location visit history Data
        case_locations_data = {
            'case': pk,
            'location': location_id,
            'date_from': request.data['date_from'],
            'date_to': request.data['date_to'],
            'category': request.data['category']
        }
        case_locations_serializer = CaseLocationSerializer(
            data=case_locations_data)

        if case_locations_serializer.is_valid():
            case_locations_serializer.save()
            return Response(case_locations_serializer.data,
                            status=status.HTTP_201_CREATED)

        else:
            return Response(case_locations_serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
Example #7
0
    def post(self, request, pk):
        # Add location Data
        location_data = {
            "location": request.data['location'],
            "address": request.data['address'],
            "x_coord": request.data['x_coord'],
            "y_coord": request.data['y_coord'],
        }

        location_serializer = LocationSerializer(data=location_data)

        if location_serializer.is_valid():
            location_serializer.save()
        else:
            return Response(location_serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

        # Add Case location visit history Data
        location_history_data = {
            'case': pk,
            'location': location_serializer.data['id'],
            'date_from': request.data['date_from'],
            'date_to': request.data['date_to'],
            'category': request.data['category']
        }
        location_history_serializer = LocationVisitHistorySerializer(
            data=location_history_data)

        if location_history_serializer.is_valid():
            location_history_serializer.save()
            return Response(location_history_serializer.data,
                            status=status.HTTP_201_CREATED)

        else:
            return Response(location_history_serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
Example #8
0
    def post(self, request):
        user_field = [
            'id', 'username', 'password', 'email', 'first_name', 'last_name',
            'date_joined', 'last_login', 'user_type', 'bazaar_point', 'company'
        ]
        user_dict = {}
        location_dict = {}
        for fields in request.data:
            if fields in user_field:
                user_dict[fields] = request.data[fields]
            else:
                location_dict[fields] = request.data[fields]
        serializer = UserSerializer(data=user_dict)

        if serializer.is_valid():
            # There error handling part might not be required, additional test is needed
            if not "user_type" in serializer.validated_data.keys():
                return Response({"user_type": ["This field is required."]},
                                status=status.HTTP_400_BAD_REQUEST)
            if not "username" in serializer.validated_data.keys():
                return Response({"username": ["This field is required."]},
                                status=status.HTTP_400_BAD_REQUEST)
            if not "password" in serializer.validated_data.keys():
                return Response({"password": ["This field is required."]},
                                status=status.HTTP_400_BAD_REQUEST)
            email = serializer.validated_data['username']

            serializer.save()
            user = User.objects.get(username=email)

            if request.data['user_type'] == 2 or request.data[
                    'user_type'] == '2':
                if not "company" in request.data:
                    user.delete()
                    return Response({"company": ["This field is required."]},
                                    status=status.HTTP_400_BAD_REQUEST)
                location_dict['user'] = user.id
                serializer2 = LocationSerializer(data=location_dict)
                if serializer2.is_valid():
                    serializer2.save()
                else:
                    user.delete()
                    return Response({"location": ["bad location request."]},
                                    status=status.HTTP_400_BAD_REQUEST)

            user.is_active = False
            user.save()

            uidb64 = urlsafe_base64_encode(force_bytes(user.pk))

            domain = get_current_site(request).domain
            # link = reverse('activate', kwargs={'uidb64': uidb64})

            activate_url = 'http://' + "3.121.223.52:3000" + "/activate=" + str(
                uidb64)

            email_subject = 'Activate'
            email_body = 'Hi,\nPlease use this link to verify your account:\n' + activate_url
            email = EmailMessage(
                email_subject,
                email_body,
                '*****@*****.**',
                [email],
            )
            try:
                email.send(fail_silently=False)
                return Response(
                    {
                        "message":
                        "An mail has been sent to your email, please check it"
                    },
                    status=status.HTTP_201_CREATED)
            except:
                user.delete()
                return Response({"email": ["Couldn't send email"]},
                                status=status.HTTP_400_BAD_REQUEST)

        else:
            return Response(serializer._errors,
                            status=status.HTTP_400_BAD_REQUEST)
 def post(self, request, format=None):
     serializer = LocationSerializer(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)
Example #10
0
def ws_message(message):
    ls = LocationSerializer(data=json.loads(message['text']))
    if ls.is_valid(raise_exception=True):
        ls.save(owner=message.user)
    else:
        pass # TODO Send error message