-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
625 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
errors = { | ||
"profile_missing": {"errors": "profile with this username does not exist", | ||
"status": 404}, | ||
"bad_image": {"errors": "Ensure that the file is an image", | ||
"status": 400} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class ProfilesConfig(AppConfig): | ||
name = 'profiles' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Generated by Django 2.1 on 2019-04-25 10:04 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='UserProfile', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('username', models.CharField(max_length=30)), | ||
('bio', models.TextField(blank=True, max_length=250)), | ||
('image', models.URLField(null=True)), | ||
('following', models.BooleanField(default=False)), | ||
('created_at', models.DateTimeField(auto_now_add=True)), | ||
('updated_at', models.DateTimeField(auto_now=True)), | ||
], | ||
), | ||
] |
40 changes: 40 additions & 0 deletions
40
authors/apps/profiles/migrations/0002_auto_20190426_0237.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Generated by Django 2.1 on 2019-04-26 02:37 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('profiles', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name='userprofile', | ||
name='following', | ||
), | ||
migrations.RemoveField( | ||
model_name='userprofile', | ||
name='username', | ||
), | ||
migrations.AddField( | ||
model_name='userprofile', | ||
name='first_name', | ||
field=models.CharField(blank=True, max_length=50), | ||
), | ||
migrations.AddField( | ||
model_name='userprofile', | ||
name='last_name', | ||
field=models.CharField(blank=True, max_length=50), | ||
), | ||
migrations.AddField( | ||
model_name='userprofile', | ||
name='user', | ||
field=models.OneToOneField(default=0, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), | ||
preserve_default=False, | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from django.db import models | ||
from ..authentication.models import User | ||
from django.db.models.signals import post_save | ||
|
||
|
||
# Sample request data | ||
# { | ||
# "profile": { | ||
# "username": "jake", | ||
# "bio": "I work at statefarm", | ||
# "image": "image-link", | ||
# "following": false, -for observer | ||
# "email": "this@gmail.com", | ||
# bookmarked articles | ||
# favorite articles | ||
# user_articles | ||
# } | ||
|
||
# ACCEPTANCE CRITERIA | ||
# As a User | ||
# I want to get my profile created once I register to the application. | ||
# I want my profile to display my bio, image/avatar and any other fields that "we" the dev team deem necessary. Let's get creative! | ||
# I want to be able to View other users profiles, Edit my profile but I should NOT be able to edit another user's profile. | ||
|
||
# GET userprofile | ||
|
||
# Acceptance Criteria | ||
# Scenario: | ||
# Given a User registers to be a member of Authors Haven. | ||
|
||
# Registration is done. | ||
# User Profile is created. | ||
# A timestamp is associated with the profile. | ||
|
||
# Register listener | ||
|
||
|
||
class UserProfile(models.Model): | ||
""" | ||
The User Profile Model | ||
""" | ||
|
||
user = models.OneToOneField(User, on_delete=models.CASCADE) | ||
first_name = models.CharField(max_length=50, blank=True) | ||
last_name = models.CharField(max_length=50, blank=True) | ||
bio = models.TextField(max_length=250, blank=True) | ||
image = models.URLField(null=True) | ||
created_at = models.DateTimeField(auto_now_add=True, editable=False) | ||
updated_at = models.DateTimeField(auto_now=True) | ||
|
||
def __str__(self): | ||
return self.user.username | ||
|
||
|
||
def create_profile(sender, **kwargs): | ||
if kwargs['created']: | ||
user_profile = UserProfile.objects.create(user=kwargs["instance"]) | ||
|
||
|
||
post_save.connect(create_profile, sender=User) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from rest_framework import permissions | ||
|
||
|
||
class IsGetOrIsAuthenticated(permissions.BasePermission): | ||
|
||
def has_permission(self, request, view): | ||
|
||
if request.method == 'GET': | ||
return True | ||
|
||
return request.user and request.user.is_authenticated | ||
|
||
def has_object_permission(self, request, view, obj): | ||
|
||
if request.method in permissions.SAFE_METHODS: | ||
return True | ||
|
||
return obj.user == request.user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import json | ||
|
||
from rest_framework.renderers import JSONRenderer | ||
|
||
|
||
class ProfileJSONRenderer(JSONRenderer): | ||
charset = 'utf-8' | ||
|
||
def render(self, data, media_type=None, renderer_context=None): | ||
# If the view throws an error (such as the user can't be authenticated | ||
# or something similar), `data` will contain an `errors` key. We want | ||
# the default JSONRenderer to handle rendering errors, so we need to | ||
# check for this case. | ||
errors = data.get('errors', None) | ||
|
||
if errors is not None: | ||
# As mentioned about, we will let the default JSONRenderer handle | ||
# rendering errors. | ||
return super(ProfileJSONRenderer, self).render(data) | ||
|
||
# Finally, we can render our data under the "user" namespace. | ||
return json.dumps({ | ||
'profile': data | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from .models import UserProfile | ||
from rest_framework import serializers | ||
|
||
|
||
class ProfileSerializer(serializers.ModelSerializer): | ||
""" | ||
Handles serialization and deserialization | ||
of User Profile objects. | ||
""" | ||
|
||
username = serializers.CharField(source="user.username", read_only=True) | ||
email = serializers.CharField(source="user.email", read_only=True) | ||
created_at = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S") | ||
updated_at = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S") | ||
|
||
bio = serializers.CharField( | ||
max_length=250, | ||
min_length=8 | ||
) | ||
|
||
class Meta: | ||
model = UserProfile | ||
fields = ('first_name', "last_name", "username", | ||
"email", "bio", "image", | ||
"created_at", "updated_at") | ||
|
||
def update(self, instance, validated_data): | ||
"""Performs an update on a User Profile.""" | ||
|
||
for (key, value) in validated_data.items(): | ||
|
||
setattr(instance, key, value) | ||
|
||
instance.save() | ||
|
||
return instance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from django.urls import path | ||
|
||
from .views import ( | ||
ProfileRetreiveUpdateAPIView | ||
) | ||
|
||
urlpatterns = [ | ||
path("profiles/<username>", | ||
ProfileRetreiveUpdateAPIView.as_view()), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import datetime | ||
import cloudinary.uploader | ||
from django.http import JsonResponse | ||
from rest_framework.exceptions import ParseError | ||
|
||
from ..authentication.messages import errors | ||
|
||
|
||
def ImageUploader(image): | ||
""" | ||
Upload image to cloudinary | ||
:param image: FILE from post request | ||
:return: uploaded image data if image was uploaded successfully else None | ||
""" | ||
|
||
if not str(image.name).endswith(('.png', '.jpg', '.jpeg')): | ||
raise ParseError(errors["bad_image"]) | ||
|
||
try: | ||
image_data = cloudinary.uploader.upload( | ||
image, | ||
public_id=str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') | ||
.replace("-", "") | ||
.replace(":", "") | ||
.replace(" ", "")), | ||
crop='limit', | ||
width=2000, | ||
height=2000, | ||
eager=[ | ||
{'width': 200, 'height': 200, | ||
'crop': 'thumb', 'gravity': 'face', | ||
'radius': 20, 'effect': 'sepia'}, | ||
{'width': 100, 'height': 150, | ||
'crop': 'fit', 'format': 'png'} | ||
] | ||
) | ||
return image_data | ||
|
||
except Exception as e: | ||
raise ParseError({"error": e.__dict__}) | ||
|
||
|
||
def validate_image_upload(request): | ||
""" | ||
Check if image is provided and uploaded | ||
:param request: put request | ||
:return: uploaded image data if successful else error raised | ||
""" | ||
|
||
if request.FILES.get("image", False): | ||
|
||
image = ImageUploader(request.FILES["image"]) | ||
|
||
request.data["image"] = image.get( | ||
"secure_url", request.FILES["image"]) |
Oops, something went wrong.