Skip to content

Commit c665afc

Browse files
committed
added tests, serializers to include profile urls in profile info
1 parent dcbea4d commit c665afc

File tree

5 files changed

+76
-12
lines changed

5 files changed

+76
-12
lines changed

profile/migrations/0001_initial.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Generated by Django 2.1.2 on 2018-10-06 17:06
1+
# Generated by Django 2.1.2 on 2018-10-06 22:16
22

33
from django.conf import settings
44
from django.db import migrations, models
@@ -30,6 +30,10 @@ class Migration(migrations.Migration):
3030
('blog', models.CharField(blank=True, max_length=255)),
3131
('public_repos', models.CharField(blank=True, max_length=255)),
3232
('hireable', models.BooleanField(default=False)),
33+
('name', models.CharField(blank=True, max_length=255)),
34+
('avatar_url', models.TextField(null=True)),
35+
('hero_image_url', models.TextField(null=True)),
36+
('private', models.BooleanField(default=False)),
3337
('user', models.OneToOneField(on_delete=django.db.models.deletion.DO_NOTHING, related_name='profile', to=settings.AUTH_USER_MODEL, verbose_name='user')),
3438
],
3539
options={
@@ -46,9 +50,12 @@ class Migration(migrations.Migration):
4650
('updated_at', models.DateTimeField(auto_now=True)),
4751
('name', models.CharField(max_length=250)),
4852
('description', models.TextField(blank=True)),
49-
('description', models.TextField(blank=True)),
5053
('url', models.TextField(null=True)),
51-
('profile', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='profile.Profile')),
54+
('profile', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, related_name='urls', to='profile.Profile')),
5255
],
56+
options={
57+
'verbose_name': 'Profile URLs',
58+
'verbose_name_plural': 'Profile URLs',
59+
},
5360
),
5461
]

profile/models.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ class Profile(models.Model):
2222
blog = models.CharField(max_length=255, blank=True)
2323
public_repos = models.CharField(max_length=255, blank=True)
2424
hireable = models.BooleanField(default=False)
25+
name = models.CharField(max_length=255, blank=True)
26+
avatar_url = models.TextField(null=True)
27+
hero_image_url = models.TextField(null=True)
28+
private = models.BooleanField(default=False)
2529

2630
class Meta:
2731
verbose_name = "Profile"

profile/serializers.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
11
from rest_framework_json_api import serializers
2-
from .models import Profile
2+
from .models import Profile, ProfileURL
33
from accounts.serializers import UserSerializer
44
from accounts.models import User
55
from rest_framework_json_api.relations import ResourceRelatedField
66

7+
class ProfileURLSerializer(serializers.ModelSerializer):
8+
class Meta:
9+
model = ProfileURL
10+
fields = ('name', 'description', 'url',)
11+
712
class ProfileSerializer(serializers.ModelSerializer):
813
class Meta:
914
model = Profile
10-
fields = ('bio', 'user',)
15+
fields = ('user', 'urls', 'bio', 'tagline', '___location', 'linkedin_url', 'hero_image_url',
16+
'name', 'avatar_url', 'twitter_url', 'github_url', 'company', 'blog', 'public_repos', 'hireable')
1117

12-
included_serializers = { 'user': UserSerializer }
18+
included_serializers = {
19+
'user': UserSerializer,
20+
'urls': ProfileURLSerializer,
21+
}
1322

1423
user = ResourceRelatedField(
15-
queryset=User.objects # queryset argument is required
16-
) # except when read_only=True
24+
queryset=User.objects
25+
)
26+
27+
urls = ResourceRelatedField(
28+
queryset=ProfileURL.objects,
29+
many=True,
30+
)
1731

1832
class JSONAPIMeta:
19-
included_resources = ['user',]
33+
included_resources = ['user', 'urls']
2034

profile/tests/test_requests/test_profile.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django.test import TestCase
22
from rest_framework.test import APIClient, APITestCase
33
from accounts.models import User
4-
from profile.models import Profile
4+
from profile.models import Profile, ProfileURL
55
import sure
66
import json
77

@@ -28,3 +28,29 @@ def test_profile_create_with_user(self):
2828
relationships = response_data['data'][0]['relationships']
2929
attributes['bio'].should.equal(profile.bio)
3030
relationships['user']['data']['id'].should.equal(str(user.id))
31+
32+
def test_profile_create_with_urls(self):
33+
"""
34+
Ensure we get the correct profile and project urls
35+
"""
36+
user = User.objects.create(username="poodle")
37+
38+
profile = Profile.objects.create(user=user, bio="Woof! in French")
39+
40+
url1 = ProfileURL.objects.create(profile=profile, name='codecorgi', url='https://codecorgi.co')
41+
url2 = ProfileURL.objects.create(profile=profile, name='codecorgi gh', url=None)
42+
43+
response = self.client.get("/services/api/profiles",
44+
content_type=self.content_type)
45+
46+
response.status_code.should.equal(200)
47+
48+
response_data = json.loads(response.content)
49+
attributes = response_data['data'][0]['attributes']
50+
relationships = response_data['data'][0]['relationships']
51+
52+
attributes['bio'].should.equal(profile.bio)
53+
54+
relationships['urls']['meta']['count'].should.equal(2)
55+
relationships['urls']['data'][0]['id'].should.equal(str(url2.id))
56+
relationships['urls']['data'][1]['id'].should.equal(str(url1.id))

profile/views.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
from rest_framework import viewsets
2-
from .models import Profile
3-
from .serializers import ProfileSerializer
2+
from .models import Profile, ProfileURL
3+
from .serializers import ProfileSerializer, ProfileURLSerializer
44

55
class ProfileViewSet(viewsets.ModelViewSet):
66
queryset = Profile.objects.all()
77
serializer_class = ProfileSerializer
8+
9+
class ProfileURLsViewSet(viewsets.ModelViewSet):
10+
queryset = ProfileURL.objects
11+
serializer_class = ProfileURLSerializer
12+
13+
def get_queryset(self):
14+
queryset = super(ProfileURL, self).get_queryset()
15+
16+
if 'profile_pk' in self.kwargs:
17+
order_pk = self.kwargs['profile_pk']
18+
queryset = queryset.filter(profile__pk=order_pk)
19+
20+
return queryset

0 commit comments

Comments
 (0)