Skip to content

Commit

Permalink
Merged with latest changes from main
Browse files Browse the repository at this point in the history
  • Loading branch information
nluu175 committed Oct 23, 2023
2 parents 35acd49 + 0f91ddb commit bc149cb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 22 deletions.
30 changes: 8 additions & 22 deletions server/socialdistribution/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .models import Author, Post
from django.contrib.auth.models import User

from .utils import *


class AuthorSerializer(serializers.ModelSerializer):
Expand All @@ -26,36 +27,21 @@ class Meta:
"origin", "published", "updatedAt", "visibility", "unlisted")

def get_id_url(self, obj):
# id field needs to be a uri of the post
uri = self.context["request"].build_absolute_uri("/")
return f"{uri}author/{obj.author.id}/posts/{obj.id}"
"""id field needs to be a uri of the post"""
return build_default_post_uri(obj=obj, request=self.context["request"])

def get_content(self, obj):
# decode content as it's a binary field
"""decode content as it's a binary field"""
if obj.contentType == Post.ContentType.PLAIN and obj.content:
return obj.content.decode("utf-8")

def get_origin_url(self, obj):
# if source is given, pass in the origin, otherwise, build it using current request uri
if obj.origin:
return obj.origin
else:
uri = self.context["request"].build_absolute_uri("/")
return f"{uri}author/{obj.author.id}/posts/{obj.id}"
"""if source is given, pass in the origin, otherwise, build it using current request uri"""
return obj.origin if obj.origin else build_default_post_uri(obj=obj, request=self.context["request"])

def get_type(self, obj):
return "post"

def get_source_url(self, obj):
# if source is given, pass in the source, otherwise, build it using current request uri
if obj.source:
return obj.source
else:
uri = self.context["request"].build_absolute_uri("/")
return f"{uri}author/{obj.author.id}/posts/{obj.id}"


class LoginSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ("username", "email", "password")
"""if source is given, pass in the source, otherwise, build it using current request uri"""
return obj.source if obj.source else build_default_post_uri(obj=obj, request=self.context["request"])
1 change: 1 addition & 0 deletions server/socialdistribution/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

from .constants import *
from .views_utils import *
from .serializers_utils import *
4 changes: 4 additions & 0 deletions server/socialdistribution/utils/serializers_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

def build_default_post_uri(obj, request):
uri = request.build_absolute_uri("/")
return f"{uri}author/{obj.author.id}/posts/{obj.id}"

0 comments on commit bc149cb

Please sign in to comment.