예제 #1
0
 def test_message_creation_from_json(self):
     json = {
         "title": "Arco do Cego",
         "text": "Arco do Cego, os parque!!",
         "fromDate": "2017-05-01T18:42:15.703112Z",
         "toDate": "2017-06-01T18:42:15.703122Z",
         "location": {
             "id": 1
         },
         "whitelist": [{
             "key": "clube",
             "value": "benfica"
         }],
         "blacklist": []
     }
     message_serializer = MessageSerializer(data=json)
     if message_serializer.is_valid():
         m = message_serializer.save(user=self.u)
         self.assertEqual(m.title, "Arco do Cego", "Title matches!")
         self.assertEqual(m.text, "Arco do Cego, os parque!!")
         self.assertEqual(m.author.username, "luissantos")
         for item in m.whitelist.all():
             self.assertEqual(item.key.id, 1)
     else:
         self.fail(message_serializer.errors)
예제 #2
0
 def test_user_get_message_no_filter(self):
     json = [
         {
             "title": "CasaLuis",
             "text": "Arco do Cego, os parque!!",
             "fromDate": "2017-05-01T18:42:15.703112Z",
             "toDate": "2017-06-01T18:42:15.703122Z",
             "location": {
                 "id": 1
             },
             "whitelist": [],
             "blacklist": []
         },
     ]
     message_serializer = MessageSerializer(data=json, many=True)
     if message_serializer.is_valid():
         m = message_serializer.save(user=self.u)
         messages = UserLocation(
             self.u, {
                 "gps": {
                     "radius": self.location_gps[2],
                     "longitude": self.location_gps[1],
                     "latitude": self.location_gps[0]
                 },
                 "wifi": [{
                     "ssid": "edurom"
                 }],
                 "date": "2017-05-02T18:42:15.703122Z"
             }).getMessages()
         self.assertEqual(len(messages), 1)
         self.assertEqual(messages[0].title, "CasaLuis")
     else:
         self.fail(message_serializer.errors)
예제 #3
0
    def create(self, request, **kwargs):
        thread = self._get_message_thread(request, **kwargs)
        logger.debug("Preparing to create a message in thread %r", thread)

        sender = get_user_from_request(request)
        context = {
            'request': request,
            'user': sender,
        }

        data = dict(request.data)

        if thread is not None:
            data['thread'] = thread.pk

        message_serializer = MessageSerializer(data=data, context=context)
        message_serializer.is_valid(raise_exception=True)
        sent_message = message_serializer.save()

        return Response(MessageSerializer(sent_message, context=context).data)
예제 #4
0
def create_message(request):
    if request.data["data"]:
        message_serializer = MessageSerializer(data=request.data["data"])
        if message_serializer.is_valid():
            m = message_serializer.save(user=request.user)
            return Response(JSONResponse().addData(
                "Message",
                message_serializer.data).addData("status",
                                                 "Message created!").send(),
                            status=status.HTTP_201_CREATED)
        else:
            return Response(JSONResponse().addError(
                0, "Message could not be created").addError(
                    1, message_serializer.errors).send(),
                            status=status.HTTP_400_BAD_REQUEST)
    else:
        return Response(JSONResponse().addError(0,
                                                "No data in request").send(),
                        status=status.HTTP_400_BAD_REQUEST)