Beispiel #1
0
 def put(self, request, *args, **kwargs):
     data = json.loads(request.body)
     user = UserSerializer(data=data)
     current_user = CustomUser.objects.get(pk=int(data['id']))
     if user.is_valid():
         user.update(current_user, data)
         return JSONResponse(
             {'session_user': UserSerializer(current_user).data})
     return JSONResponse(user.errors, status=400)
class UpdateUserTest(APITestCase):
    def setUp(self):
        self.user = User.objects.create(username="******", first_name="Jake")
        self.data = UserSerializer(self.user).data
        self.data.update({'first_name': 'Changed'})

    def test_can_update_user(self):
        response = self.client.put(reverse('user-detail', args=[self.user.id]), self.data)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
Beispiel #3
0
class UpdateUserTest(APITestCase):
    def setUp(self):
        self.user = User.objects.create(username="******", first_name="Jake")
        self.data = UserSerializer(self.user).data
        self.data.update({'first_name': 'Changed'})

    def test_can_update_user(self):
        response = self.client.put(reverse('user-detail', args=[self.user.id]),
                                   self.data)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
Beispiel #4
0
    def post(self, request, *args, **kwargs):
        data = {"id": request.user.id, **request.data}
        serializer = UserSerializer(data=data)
        serializer.is_valid(raise_exception=True)
        user = User.objects.get(id=request.user.id)
        serializer.update(instance=user,
                          validated_data=serializer.validated_data)

        response = UserSerializer(user).data

        return JsonResponse(response)
Beispiel #5
0
def test_make_contact_sync_message(user):
    """Test make_contact_sync_message serializes a user and returns a properly formatted sync message"""
    contact_sync_message = api.make_contact_sync_message(user.id)

    serialized_user = UserSerializer(user).data
    serialized_user.update(serialized_user.pop("legal_address") or {})
    serialized_user.update(serialized_user.pop("profile") or {})
    serialized_user["street_address"] = "\n".join(serialized_user.pop("street_address"))
    serialized_user.pop("unused_coupons")
    assert contact_sync_message == [
        {
            "integratorObjectId": "{}-{}".format(settings.HUBSPOT_ID_PREFIX, user.id),
            "action": "UPSERT",
            "changeOccurredTimestamp": any_instance_of(int),
            "propertyNameToValues": api.sanitize_properties(serialized_user),
        }
    ]
Beispiel #6
0
def make_contact_sync_message(user_id):
    """
    Create the body of a sync message for a contact. This will flatten the contained LegalAddress and Profile
    serialized data into one larger serializable dict

    Args:
        user_id (int): User id

    Returns:
        list: dict containing serializable sync-message data
    """
    from users.models import User
    from users.serializers import UserSerializer

    user = User.objects.get(id=user_id)
    properties = UserSerializer(user).data
    properties.update(properties.pop("legal_address") or {})
    properties.update(properties.pop("profile") or {})
    properties.pop("unused_coupons")
    if "street_address" in properties:
        properties["street_address"] = "\n".join(
            properties.pop("street_address"))
    return [make_sync_message(user.id, properties)]