Skip to content
This repository has been archived by the owner on Sep 27, 2022. It is now read-only.

# 7 solved #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions conduit/apps/articles/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.contrib import admin
from .models import Article, Comment, Tag

admin.site.register(Article)
admin.site.register(Comment)
admin.site.register(Tag)
4 changes: 4 additions & 0 deletions conduit/apps/authentication/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from django.contrib import admin
from .models import User

admin.site.register(User)
10 changes: 5 additions & 5 deletions conduit/apps/authentication/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def validate(self, data):
class UserSerializer(serializers.ModelSerializer):
"""Handles serialization and deserialization of User objects."""

# Passwords must be at least 8 characters, but no more than 128
# Passwords must be at least 8 characters, but no more than 128
# characters. These values are the default provided by Django. We could
# change them, but that would create extra work while introducing no real
# benefit, so let's just stick with the defaults.
Expand All @@ -113,7 +113,7 @@ class UserSerializer(serializers.ModelSerializer):
# so. Moreover, `UserSerializer` should never expose profile information,
# so we set `write_only=True`.
profile = ProfileSerializer(write_only=True)

# We want to get the `bio` and `image` fields from the related Profile
# model.
bio = serializers.CharField(source='profile.bio', read_only=True)
Expand All @@ -122,15 +122,15 @@ class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = (
'email', 'username', 'password', 'token', 'profile', 'bio',
'email', 'username', 'password', 'token', 'profile', 'bio',
'image',
)

# The `read_only_fields` option is an alternative for explicitly
# specifying the field with `read_only=True` like we did for password
# above. The reason we want to use `read_only_fields` here is because
# we don't need to specify anything else about the field. For the
# password field, we needed to specify the `min_length` and
# password field, we needed to specify the `min_length` and
# `max_length` properties too, but that isn't the case for the token
# field.
read_only_fields = ('token',)
Expand Down Expand Up @@ -169,7 +169,7 @@ def update(self, instance, validated_data):
# We're doing the same thing as above, but this time we're making
# changes to the Profile model.
setattr(instance.profile, key, value)

# Save the profile just like we saved the user.
instance.profile.save()

Expand Down
1 change: 0 additions & 1 deletion conduit/apps/authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,3 @@ def update(self, request, *args, **kwargs):
serializer.save()

return Response(serializer.data, status=status.HTTP_200_OK)

4 changes: 4 additions & 0 deletions conduit/apps/core/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from django.contrib import admin
from .models import TimestampedModel

# admin.site.register(TimestampedModel)
4 changes: 4 additions & 0 deletions conduit/apps/profiles/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from django.contrib import admin
from .models import Profile

admin.site.register(Profile)
20 changes: 20 additions & 0 deletions conduit/apps/profiles/migrations/0004_auto_20190930_2347.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2019-09-30 18:17
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('profiles', '0003_profile_favorites'),
]

operations = [
migrations.AlterField(
model_name='profile',
name='image',
field=models.FileField(blank=True, upload_to='profile_pictures'),
),
]
25 changes: 25 additions & 0 deletions conduit/apps/profiles/migrations/0005_auto_20190930_2355.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2019-09-30 18:25
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('profiles', '0004_auto_20190930_2347'),
]

operations = [
migrations.AlterField(
model_name='profile',
name='favorites',
field=models.ManyToManyField(blank=True, related_name='favorited_by', to='articles.Article'),
),
migrations.AlterField(
model_name='profile',
name='follows',
field=models.ManyToManyField(blank=True, related_name='followed_by', to='profiles.Profile'),
),
]
20 changes: 20 additions & 0 deletions conduit/apps/profiles/migrations/0006_auto_20191001_0059.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2019-09-30 19:29
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('profiles', '0005_auto_20190930_2355'),
]

operations = [
migrations.AlterField(
model_name='profile',
name='image',
field=models.URLField(blank=True),
),
]
20 changes: 20 additions & 0 deletions conduit/apps/profiles/migrations/0007_auto_20191001_0105.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2019-09-30 19:35
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('profiles', '0006_auto_20191001_0059'),
]

operations = [
migrations.AlterField(
model_name='profile',
name='image',
field=models.FileField(blank=True, upload_to='profile_pictures'),
),
]
20 changes: 20 additions & 0 deletions conduit/apps/profiles/migrations/0008_auto_20191001_0108.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2019-09-30 19:38
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('profiles', '0007_auto_20191001_0105'),
]

operations = [
migrations.AlterField(
model_name='profile',
name='image',
field=models.URLField(blank=True),
),
]
7 changes: 5 additions & 2 deletions conduit/apps/profiles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Profile(TimestampedModel):
# In addition to the `bio` field, each user may have a profile image or
# avatar. Similar to `bio`, this field is not required. It may be blank.
image = models.URLField(blank=True)
#image=models.FileField(upload_to='profile_pictures', blank=True)

# This is an example of a Many-To-Many relationship where both sides of the
# relationship are of the same model. In this case, the model is `Profile`.
Expand All @@ -29,12 +30,14 @@ class Profile(TimestampedModel):
follows = models.ManyToManyField(
'self',
related_name='followed_by',
symmetrical=False
symmetrical=False,
blank=True
)

favorites = models.ManyToManyField(
'articles.Article',
related_name='favorited_by'
related_name='favorited_by',
blank=True
)


Expand Down
7 changes: 0 additions & 7 deletions conduit/apps/profiles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,13 @@
class ProfileSerializer(serializers.ModelSerializer):
username = serializers.CharField(source='user.username')
bio = serializers.CharField(allow_blank=True, required=False)
image = serializers.SerializerMethodField()
following = serializers.SerializerMethodField()

class Meta:
model = Profile
fields = ('username', 'bio', 'image', 'following',)
read_only_fields = ('username',)

def get_image(self, obj):
if obj.image:
return obj.image

return 'https://static.productionready.io/images/smiley-cyrus.jpg'

def get_following(self, instance):
request = self.context.get('request', None)

Expand Down
5 changes: 3 additions & 2 deletions conduit/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
SECRET_KEY = '2^f+3@v7$v1f8yt0!s)3-1t$)tlp+xm17=*g))_xoi&&9m#2a&'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
DEBUG = False

ALLOWED_HOSTS = []
ALLOWED_HOSTS = ['*']


# Application definition
Expand Down Expand Up @@ -129,6 +129,7 @@

STATIC_URL = '/static/'


CORS_ORIGIN_WHITELIST = (
'0.0.0.0:4000',
'localhost:4000',
Expand Down