# Path: test_project/test_app/models.py
# class Post(BaseSocialModel):
#     user = models.ForeignKey(User, related_name='posts', on_delete=models.CASCADE)
#     title = models.CharField(max_length=100, null=True, blank=True)
#     description = models.TextField(blank=True, null=True)
#     thumbnail = models.ImageField(upload_to="post_photos/thumbnail/", blank=True, null=True)
# 
#     TAG_FIELD = 'description'
# 
#     likes = GenericRelation(Like)
#     flags = GenericRelation(Flag)
#     shares = GenericRelation(Share)
#     related_tags = models.ManyToManyField(Tag, blank=True)
#     comments = GenericRelation(Comment)
#     notifications = GenericRelation(Notification)
# 
#     def likes_count(self):
#         return self.likes.count()
# 
#     def comments_count(self):
#         return self.comments.count()
# 
#     def identifier(self):
#         return "{}".format(self.title)
# 
#     def __unicode__(self):
#         return "{}".format(self.title) if self.title else "Untitled"
# 
#     def url(self):
#         current_site = Site.objects.get_current()
#         return "http://{0}/{1}/{2}/".format(current_site.domain, "post", base62.encode(self.pk))
# 
#     def facebook_og_info(self):
#         return {'action': 'post', 'object': 'cut', 'url': self.url()}
# 
#     def create_social_message(self, provider):
#         message = "{} published by {} on Test Project".format(self.title, self.user.username)
# 
#         # TODO: Sending of messages to post on social media is broken and convoluted at this point, need to refactor
#         if provider == "twitter":
#             return "{}".format(message.encode('utf-8'))
#         else:
#             return "{} {}".format(message.encode('utf-8'), self.url())
# 
# Path: yak/rest_core/serializers.py
# class YAKModelSerializer(serializers.ModelSerializer):
# 
#     def __init__(self, *args, **kwargs):
#         super(YAKModelSerializer, self).__init__(*args, **kwargs)
#         self.fields['content_type'] = serializers.SerializerMethodField()
# 
#     def get_content_type(self, obj):
#         return ContentType.objects.get_for_model(obj).pk
# 
# Path: yak/rest_social_network/serializers.py
# class LikedMixin(object):
#     def get_liked_id(self, obj):
#         request = self.context['request']
#         if request.user.is_authenticated:
#             try:
#                 content_type = self.get_content_type(obj)
#                 return Like.objects.get(content_type=content_type, user=request.user, object_id=obj.pk).pk
#             except Like.DoesNotExist:
#                 pass
#         return None
# 
# class FollowedMixin(object):
#     def get_follow_id(self, obj):
#         # Indicate whether or not the logged in user is following a given object (e.g., another user)
#         # Provide the id of the follow object so it can be deleted to unfollow the object
#         if self.context['request'].user.is_authenticated:
#             try:
#                 content_type = self.get_content_type(obj)
#                 return self.context['request'].user.following.get(content_type=content_type, object_id=obj.pk).pk
#             except Follow.DoesNotExist:
#                 pass
#         return None
# 
# Path: yak/rest_user/serializers.py
# class AuthSerializerMixin(object):
#     def create(self, validated_data):
#         if validated_data.get("username", None):
#             validated_data["username"] = validated_data["username"].lower()
#         if validated_data.get("email", None):
#             validated_data["email"] = validated_data["email"].lower()
#         if validated_data.get("password", None):
#             validated_data["password"] = make_password(validated_data["password"])
# 
#         return super(AuthSerializerMixin, self).create(validated_data)
# 
#     def update(self, instance, validated_data):
#         if validated_data.get("username", None):
#             validated_data["username"] = validated_data["username"].lower()
#         if validated_data.get("email", None):
#             validated_data["email"] = validated_data["email"].lower()
#         if validated_data.get("password", None):
#             validated_data["password"] = make_password(validated_data["password"])
# 
#         return super(AuthSerializerMixin, self).update(instance, validated_data)
# 
#     def validate_username(self, value):
#         username = self.context['request'].user.username if self.context['request'].user.is_authenticated else None
#         if value and value != username and User.objects.filter(username__iexact=value).exists():
#             raise serializers.ValidationError("That username is taken")
#         return value
# 
#     def validate_email(self, value):
#         email = self.context['request'].user.email if self.context['request'].user.is_authenticated else None
#         if value and value != email and User.objects.filter(email__iexact=value).exists():
#             raise serializers.ValidationError("An account already exists with that email")
#         return value
# 
#     def validate_password(self, value):
#         value = base64.decodebytes(bytes(value, 'utf8')).decode()
#         if len(value) < 6:
#             raise serializers.ValidationError("Password must be at least 6 characters")
#         return value


from django.contrib.auth import get_user_model
from rest_framework import serializers
from test_project.test_app.models import Post
from yak.rest_core.serializers import YAKModelSerializer
from yak.rest_social_network.serializers import LikedMixin, FollowedMixin
from yak.rest_user.serializers import AuthSerializerMixin


User = get_user_model()


class ProjectUserSerializer(FollowedMixin, AuthSerializerMixin, YAKModelSerializer):
    class Meta:
        model = User
        fields = ('email', 'id', 'username', 'fullname', 'thumbnail', 'original_photo', 'small_photo', 'large_photo',
                  'about', 'user_following_count', 'user_followers_count', 'follow_id')

    follow_id = serializers.SerializerMethodField()



# Path: test_project/test_app/models.py
# class Post(BaseSocialModel):
#     user = models.ForeignKey(User, related_name='posts', on_delete=models.CASCADE)
#     title = models.CharField(max_length=100, null=True, blank=True)
#     description = models.TextField(blank=True, null=True)
#     thumbnail = models.ImageField(upload_to="post_photos/thumbnail/", blank=True, null=True)
# 
#     TAG_FIELD = 'description'
# 
#     likes = GenericRelation(Like)
#     flags = GenericRelation(Flag)
#     shares = GenericRelation(Share)
#     related_tags = models.ManyToManyField(Tag, blank=True)
#     comments = GenericRelation(Comment)
#     notifications = GenericRelation(Notification)
# 
#     def likes_count(self):
#         return self.likes.count()
# 
#     def comments_count(self):
#         return self.comments.count()
# 
#     def identifier(self):
#         return "{}".format(self.title)
# 
#     def __unicode__(self):
#         return "{}".format(self.title) if self.title else "Untitled"
# 
#     def url(self):
#         current_site = Site.objects.get_current()
#         return "http://{0}/{1}/{2}/".format(current_site.domain, "post", base62.encode(self.pk))
# 
#     def facebook_og_info(self):
#         return {'action': 'post', 'object': 'cut', 'url': self.url()}
# 
#     def create_social_message(self, provider):
#         message = "{} published by {} on Test Project".format(self.title, self.user.username)
# 
#         # TODO: Sending of messages to post on social media is broken and convoluted at this point, need to refactor
#         if provider == "twitter":
#             return "{}".format(message.encode('utf-8'))
#         else:
#             return "{} {}".format(message.encode('utf-8'), self.url())
# 
# Path: yak/rest_core/serializers.py
# class YAKModelSerializer(serializers.ModelSerializer):
# 
#     def __init__(self, *args, **kwargs):
#         super(YAKModelSerializer, self).__init__(*args, **kwargs)
#         self.fields['content_type'] = serializers.SerializerMethodField()
# 
#     def get_content_type(self, obj):
#         return ContentType.objects.get_for_model(obj).pk
# 
# Path: yak/rest_social_network/serializers.py
# class LikedMixin(object):
#     def get_liked_id(self, obj):
#         request = self.context['request']
#         if request.user.is_authenticated:
#             try:
#                 content_type = self.get_content_type(obj)
#                 return Like.objects.get(content_type=content_type, user=request.user, object_id=obj.pk).pk
#             except Like.DoesNotExist:
#                 pass
#         return None
# 
# class FollowedMixin(object):
#     def get_follow_id(self, obj):
#         # Indicate whether or not the logged in user is following a given object (e.g., another user)
#         # Provide the id of the follow object so it can be deleted to unfollow the object
#         if self.context['request'].user.is_authenticated:
#             try:
#                 content_type = self.get_content_type(obj)
#                 return self.context['request'].user.following.get(content_type=content_type, object_id=obj.pk).pk
#             except Follow.DoesNotExist:
#                 pass
#         return None
# 
# Path: yak/rest_user/serializers.py
# class AuthSerializerMixin(object):
#     def create(self, validated_data):
#         if validated_data.get("username", None):
#             validated_data["username"] = validated_data["username"].lower()
#         if validated_data.get("email", None):
#             validated_data["email"] = validated_data["email"].lower()
#         if validated_data.get("password", None):
#             validated_data["password"] = make_password(validated_data["password"])
# 
#         return super(AuthSerializerMixin, self).create(validated_data)
# 
#     def update(self, instance, validated_data):
#         if validated_data.get("username", None):
#             validated_data["username"] = validated_data["username"].lower()
#         if validated_data.get("email", None):
#             validated_data["email"] = validated_data["email"].lower()
#         if validated_data.get("password", None):
#             validated_data["password"] = make_password(validated_data["password"])
# 
#         return super(AuthSerializerMixin, self).update(instance, validated_data)
# 
#     def validate_username(self, value):
#         username = self.context['request'].user.username if self.context['request'].user.is_authenticated else None
#         if value and value != username and User.objects.filter(username__iexact=value).exists():
#             raise serializers.ValidationError("That username is taken")
#         return value
# 
#     def validate_email(self, value):
#         email = self.context['request'].user.email if self.context['request'].user.is_authenticated else None
#         if value and value != email and User.objects.filter(email__iexact=value).exists():
#             raise serializers.ValidationError("An account already exists with that email")
#         return value
# 
#     def validate_password(self, value):
#         value = base64.decodebytes(bytes(value, 'utf8')).decode()
#         if len(value) < 6:
#             raise serializers.ValidationError("Password must be at least 6 characters")
#         return value


# Path: test_project/test_app/api/serializers.py
from django.contrib.auth import get_user_model
from rest_framework import serializers
from test_project.test_app.models import Post
from yak.rest_core.serializers import YAKModelSerializer
from yak.rest_social_network.serializers import LikedMixin, FollowedMixin
from yak.rest_user.serializers import AuthSerializerMixin



User = get_user_model()


class ProjectUserSerializer(FollowedMixin, AuthSerializerMixin, YAKModelSerializer):
    class Meta:
        model = User
        fields = ('email', 'id', 'username', 'fullname', 'thumbnail', 'original_photo', 'small_photo', 'large_photo',
                  'about', 'user_following_count', 'user_followers_count', 'follow_id')

    follow_id = serializers.SerializerMethodField()



class PostSerializer(YAKModelSerializer, LikedMixin):