# Path: wagtailsocialfeed/models.py
# class ModeratedItem(models.Model):
#     config = models.ForeignKey(SocialFeedConfiguration,
#                                related_name='moderated_items',
#                                on_delete=models.CASCADE)
#     moderated = models.DateTimeField(auto_now_add=True)
#     posted = models.DateTimeField(blank=False, null=False)
# 
#     external_id = models.CharField(max_length=255,
#                                    blank=False)
#     content = models.TextField(blank=False)
# 
#     objects = ModeratedItemManager()
# 
#     class Meta:
#         ordering = ['-posted', ]
# 
#     def __str__(self):
#         return "{}<{}> ({} posted {})".format(
#             self.__class__.__name__,
#             self.type,
#             self.external_id,
#             self.posted
#         )
# 
#     def get_content(self):
#         if not hasattr(self, '_feeditem'):
#             item_cls = FeedItemFactory.get_class(self.config.source)
#             self._feeditem = item_cls.from_moderated(self)
#         return self._feeditem
# 
#     @cached_property
#     def type(self):
#         return self.config.source
# 
# class SocialFeedConfiguration(models.Model):
#     FEED_CHOICES = (
#         ('twitter', _('Twitter')),
#         ('instagram', _('Instagram')),
#         ('facebook', _('Facebook'))
#     )
# 
#     source = models.CharField(_('Feed source'),
#                               max_length=100,
#                               choices=FEED_CHOICES,
#                               blank=False)
#     username = models.CharField(_('User to track'),
#                                 max_length=255,
#                                 blank=False)
#     moderated = models.BooleanField(default=False)
# 
#     def __str__(self):
#         name = self.username
#         if self.source == 'twitter':
#             name = "@{}".format(self.username)
#         return "{} ({})".format(self.source, name)
# 
# Path: wagtailsocialfeed/utils/feed/factory.py
# class FeedFactory(object):
#     @classmethod
#     def create(cls, source):
#         try:
#             return FEED_CONFIG[source].feed()
#         except KeyError:
#             raise NotImplementedError(
#                 "Feed class for type '{}' not available".format(source))


from django.http import JsonResponse
from django.utils import six
from django.utils.translation import ugettext_lazy as _
from django.views.decorators.csrf import csrf_exempt
from django.views.generic.base import View
from django.views.generic.detail import DetailView
from wagtail.wagtailadmin.forms import SearchForm
from .models import ModeratedItem, SocialFeedConfiguration
from .utils.feed.factory import FeedFactory


class ModerateAllowView(View):
    @csrf_exempt
    def dispatch(self, *args, **kwargs):
        return super(ModerateAllowView, self).dispatch(*args, **kwargs)

    def post(self, request, pk, post_id):
        config = SocialFeedConfiguration.objects.get(pk=pk)

        if 'original' not in request.POST:
            err = {'message': six.text_type(error_messages['no_original'])}
            return JsonResponse(err, status=400)

        original = request.POST['original']
        item, created = config.moderated_items.get_or_create_for(original)

        return JsonResponse({
            'message': 'The post is now allowed on the feed',
            'post_id': post_id,
            'allowed': True
        })


class ModerateRemoveView(View):
    @csrf_exempt
    def dispatch(self, *args, **kwargs):
        return super(ModerateRemoveView, self).dispatch(*args, **kwargs)

    def post(self, request, pk, post_id):

# Path: wagtailsocialfeed/models.py
# class ModeratedItem(models.Model):
#     config = models.ForeignKey(SocialFeedConfiguration,
#                                related_name='moderated_items',
#                                on_delete=models.CASCADE)
#     moderated = models.DateTimeField(auto_now_add=True)
#     posted = models.DateTimeField(blank=False, null=False)
# 
#     external_id = models.CharField(max_length=255,
#                                    blank=False)
#     content = models.TextField(blank=False)
# 
#     objects = ModeratedItemManager()
# 
#     class Meta:
#         ordering = ['-posted', ]
# 
#     def __str__(self):
#         return "{}<{}> ({} posted {})".format(
#             self.__class__.__name__,
#             self.type,
#             self.external_id,
#             self.posted
#         )
# 
#     def get_content(self):
#         if not hasattr(self, '_feeditem'):
#             item_cls = FeedItemFactory.get_class(self.config.source)
#             self._feeditem = item_cls.from_moderated(self)
#         return self._feeditem
# 
#     @cached_property
#     def type(self):
#         return self.config.source
# 
# class SocialFeedConfiguration(models.Model):
#     FEED_CHOICES = (
#         ('twitter', _('Twitter')),
#         ('instagram', _('Instagram')),
#         ('facebook', _('Facebook'))
#     )
# 
#     source = models.CharField(_('Feed source'),
#                               max_length=100,
#                               choices=FEED_CHOICES,
#                               blank=False)
#     username = models.CharField(_('User to track'),
#                                 max_length=255,
#                                 blank=False)
#     moderated = models.BooleanField(default=False)
# 
#     def __str__(self):
#         name = self.username
#         if self.source == 'twitter':
#             name = "@{}".format(self.username)
#         return "{} ({})".format(self.source, name)
# 
# Path: wagtailsocialfeed/utils/feed/factory.py
# class FeedFactory(object):
#     @classmethod
#     def create(cls, source):
#         try:
#             return FEED_CONFIG[source].feed()
#         except KeyError:
#             raise NotImplementedError(
#                 "Feed class for type '{}' not available".format(source))


# Path: wagtailsocialfeed/views.py
from django.http import JsonResponse
from django.utils import six
from django.utils.translation import ugettext_lazy as _
from django.views.decorators.csrf import csrf_exempt
from django.views.generic.base import View
from django.views.generic.detail import DetailView
from wagtail.wagtailadmin.forms import SearchForm
from .models import ModeratedItem, SocialFeedConfiguration
from .utils.feed.factory import FeedFactory



class ModerateAllowView(View):
    @csrf_exempt
    def dispatch(self, *args, **kwargs):
        return super(ModerateAllowView, self).dispatch(*args, **kwargs)

    def post(self, request, pk, post_id):
        config = SocialFeedConfiguration.objects.get(pk=pk)

        if 'original' not in request.POST:
            err = {'message': six.text_type(error_messages['no_original'])}
            return JsonResponse(err, status=400)

        original = request.POST['original']
        item, created = config.moderated_items.get_or_create_for(original)

        return JsonResponse({
            'message': 'The post is now allowed on the feed',
            'post_id': post_id,
            'allowed': True
        })


class ModerateRemoveView(View):
    @csrf_exempt
    def dispatch(self, *args, **kwargs):
        return super(ModerateRemoveView, self).dispatch(*args, **kwargs)

    def post(self, request, pk, post_id):

        config = SocialFeedConfiguration.objects.get(pk=pk)