# 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())
# 
# class Article(CoreModel):
#     title = models.CharField(max_length=60)
#     body = models.TextField()
#     thumbnail = models.ImageField(upload_to="article_photos/thumbnail/", blank=True, null=True)
#     likes = GenericRelation(Like)
#     notifications = GenericRelation(Notification)
# 
#     def __unicode__(self):
#         return "{}".format(self.title) if self.title else "Untitled"
# 
#     def identifier(self):
#         return "{}".format(self.title)
# 
# Path: yak/rest_social_auth/views.py
# class SocialShareMixin(object):
# 
#     @detail_route(methods=['post'], permission_classes=[IsAuthenticated])
#     def social_share(self, request, pk):
#         try:
#             user_social_auth = UserSocialAuth.objects.get(user=request.user, provider=request.data['provider'])
#             social_obj = self.get_object()
#             post_social_media(user_social_auth, social_obj)
#             return Response({'status': 'success'})
#         except UserSocialAuth.DoesNotExist:
#             raise AuthenticationFailed("User is not authenticated with {}".format(request.data['provider']))
# 
# Path: yak/rest_social_network/views.py
# class SocialUserViewSet(UserViewSet):
#     serializer_class = UserSerializer
# 
#     @detail_route(methods=['get'])
#     def following(self, request, pk):
#         requested_user = User.objects.get(pk=pk)
#         following = requested_user.user_following()
# 
#         if drf_version[0] >= 3 and drf_version[1] < 1:
#             result_page = self.paginate_queryset(following)
#         else:
#             paginator = FollowViewSet().paginator
#             result_page = paginator.paginate_queryset(following, request)
# 
#         serializer = FollowSerializer(instance=result_page, many=True, context={'request': request})
#         return Response(serializer.data)
# 
#     @detail_route(methods=['get'])
#     def followers(self, request, pk):
#         requested_user = User.objects.get(pk=pk)
#         followers = requested_user.user_followers()
# 
#         if drf_version[0] >= 3 and drf_version[1] < 1:
#             result_page = self.paginate_queryset(followers)
#         else:
#             paginator = FollowViewSet().paginator
#             result_page = paginator.paginate_queryset(followers, request)
# 
#         serializer = FollowSerializer(instance=result_page, many=True, context={'request': request})
#         return Response(serializer.data)
# 
# Path: test_project/test_app/api/serializers.py
# 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):
#     user = ProjectUserSerializer(read_only=True, default=serializers.CurrentUserDefault())
#     liked_id = serializers.SerializerMethodField()
# 
#     class Meta:
#         model = Post
#         fields = ('id', 'user', 'title', 'description', 'thumbnail', 'liked_id')
# 
# class ArticleSerializer(YAKModelSerializer, LikedMixin):
#     liked_id = serializers.SerializerMethodField()
# 
#     class Meta:
#         model = Post
#         fields = ('id', 'title', 'thumbnail', 'liked_id')


from rest_framework import viewsets
from test_project.test_app.models import Post, Article
from yak.rest_social_auth.views import SocialShareMixin
from yak.rest_social_network.views import SocialUserViewSet
from test_project.test_app.api.serializers import ProjectUserSerializer, PostSerializer, ArticleSerializer


class ProjectUserViewSet(SocialUserViewSet):
    serializer_class = ProjectUserSerializer


class PostViewSet(viewsets.ModelViewSet, SocialShareMixin):
    serializer_class = PostSerializer
    queryset = Post.objects.all()

    def perform_create(self, serializer):
        serializer.save(user=self.request.user)


class ArticleViewSet(viewsets.ModelViewSet):
    serializer_class = ArticleSerializer
    queryset = Article.objects.all()


# 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())
# 
# class Article(CoreModel):
#     title = models.CharField(max_length=60)
#     body = models.TextField()
#     thumbnail = models.ImageField(upload_to="article_photos/thumbnail/", blank=True, null=True)
#     likes = GenericRelation(Like)
#     notifications = GenericRelation(Notification)
# 
#     def __unicode__(self):
#         return "{}".format(self.title) if self.title else "Untitled"
# 
#     def identifier(self):
#         return "{}".format(self.title)
# 
# Path: yak/rest_social_auth/views.py
# class SocialShareMixin(object):
# 
#     @detail_route(methods=['post'], permission_classes=[IsAuthenticated])
#     def social_share(self, request, pk):
#         try:
#             user_social_auth = UserSocialAuth.objects.get(user=request.user, provider=request.data['provider'])
#             social_obj = self.get_object()
#             post_social_media(user_social_auth, social_obj)
#             return Response({'status': 'success'})
#         except UserSocialAuth.DoesNotExist:
#             raise AuthenticationFailed("User is not authenticated with {}".format(request.data['provider']))
# 
# Path: yak/rest_social_network/views.py
# class SocialUserViewSet(UserViewSet):
#     serializer_class = UserSerializer
# 
#     @detail_route(methods=['get'])
#     def following(self, request, pk):
#         requested_user = User.objects.get(pk=pk)
#         following = requested_user.user_following()
# 
#         if drf_version[0] >= 3 and drf_version[1] < 1:
#             result_page = self.paginate_queryset(following)
#         else:
#             paginator = FollowViewSet().paginator
#             result_page = paginator.paginate_queryset(following, request)
# 
#         serializer = FollowSerializer(instance=result_page, many=True, context={'request': request})
#         return Response(serializer.data)
# 
#     @detail_route(methods=['get'])
#     def followers(self, request, pk):
#         requested_user = User.objects.get(pk=pk)
#         followers = requested_user.user_followers()
# 
#         if drf_version[0] >= 3 and drf_version[1] < 1:
#             result_page = self.paginate_queryset(followers)
#         else:
#             paginator = FollowViewSet().paginator
#             result_page = paginator.paginate_queryset(followers, request)
# 
#         serializer = FollowSerializer(instance=result_page, many=True, context={'request': request})
#         return Response(serializer.data)
# 
# Path: test_project/test_app/api/serializers.py
# 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):
#     user = ProjectUserSerializer(read_only=True, default=serializers.CurrentUserDefault())
#     liked_id = serializers.SerializerMethodField()
# 
#     class Meta:
#         model = Post
#         fields = ('id', 'user', 'title', 'description', 'thumbnail', 'liked_id')
# 
# class ArticleSerializer(YAKModelSerializer, LikedMixin):
#     liked_id = serializers.SerializerMethodField()
# 
#     class Meta:
#         model = Post
#         fields = ('id', 'title', 'thumbnail', 'liked_id')


# Path: test_project/test_app/api/views.py
from rest_framework import viewsets
from test_project.test_app.models import Post, Article
from yak.rest_social_auth.views import SocialShareMixin
from yak.rest_social_network.views import SocialUserViewSet
from test_project.test_app.api.serializers import ProjectUserSerializer, PostSerializer, ArticleSerializer



class ProjectUserViewSet(SocialUserViewSet):
    serializer_class = ProjectUserSerializer


class PostViewSet(viewsets.ModelViewSet, SocialShareMixin):
    serializer_class = PostSerializer
    queryset = Post.objects.all()

    def perform_create(self, serializer):
        serializer.save(user=self.request.user)


class ArticleViewSet(viewsets.ModelViewSet):
    serializer_class = ArticleSerializer
    queryset = Article.objects.all()


    def perform_create(self, serializer):