# Path: flowcelltool/threads/models.py
# class Message(UuidStampedMixin, TimeStampedModel):
#     """A message that is written by a user"""
# 
#     # Messages related to a "thread" items on
#     content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
#     object_id = models.PositiveIntegerField()
#     thread_object = GenericForeignKey('content_type', 'object_id')
# 
#     author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
# 
#     title = models.CharField(
#         blank=True, null=True, max_length=200,
#         help_text='Optional title for the message')
#     body = models.TextField(blank=True, null=True, help_text='Your message')
# 
#     mime_type = models.CharField(max_length=50, default='text/plain',
#                                  choices=FORMAT_CHOICES)
# 
#     # Permissions -------------------------------------------------------------
# 
#     # The boilerplate below ("DRY permissions") hooks up the DRY REST permission system into our
#     # django-rules based system.
# 
#     @staticmethod
#     def has_None_permission(request):
#         # TODO: why do we need this? Only for the automatically generated UI?
#         return False
# 
#     @staticmethod
#     def has_read_permission(request):
#         return True
# 
#     @staticmethod
#     def has_write_permission(request):
#         return True
# 
#     @staticmethod
#     def has_list_permission(request):
#         return request.user.has_perm('threads.list_message')
# 
#     @staticmethod
#     def has_create_permission(request):
#         return request.user.has_perm('threads.add_message')
# 
#     def has_object_retrieve_permission(self, request):
#         return request.user.has_perm('threads.view_message', self)
# 
#     def has_object_update_permission(self, request):
#         return request.user.has_perm('threads.change_message', self)
# 
#     def has_object_destroy_permission(self, request):
#         return request.user.has_perm('threads.delete_message', self)
# 
#     # Boilerplate str/repr ----------------------------------------------------
# 
#     def __str__(self):
#         return 'Message({}, {}, {})'.format(self.title, self.author.username,
#                                             self.body)


from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.shortcuts import redirect, get_object_or_404
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Field
from .models import Message
from . import forms
    """CBV for updating messages"""

    #: The Model type to handle
    model = Message

    #: The fields to show in the form
    fields = ['title', 'body']

    def get_success_url(self):
        """Return absolute URL of the related object"""
        return self.object.thread_object.get_absolute_url()

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        # Setup djangocrispy-forms helper
        context = super().get_context_data(**kwargs)
        context['related_object'] = self.object.thread_object
        context['helper'] = FormHelper()
        context['helper'].form_tag = False
        context['helper'].layout = Layout(
            Field('title'),
            Field('body'),
        )
        return context


class MessageDeleteView(UuidViewMixin, DeleteView):
    """CBV for deleting messages"""

    #: The Model type to handle

# Path: flowcelltool/threads/models.py
# class Message(UuidStampedMixin, TimeStampedModel):
#     """A message that is written by a user"""
# 
#     # Messages related to a "thread" items on
#     content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
#     object_id = models.PositiveIntegerField()
#     thread_object = GenericForeignKey('content_type', 'object_id')
# 
#     author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
# 
#     title = models.CharField(
#         blank=True, null=True, max_length=200,
#         help_text='Optional title for the message')
#     body = models.TextField(blank=True, null=True, help_text='Your message')
# 
#     mime_type = models.CharField(max_length=50, default='text/plain',
#                                  choices=FORMAT_CHOICES)
# 
#     # Permissions -------------------------------------------------------------
# 
#     # The boilerplate below ("DRY permissions") hooks up the DRY REST permission system into our
#     # django-rules based system.
# 
#     @staticmethod
#     def has_None_permission(request):
#         # TODO: why do we need this? Only for the automatically generated UI?
#         return False
# 
#     @staticmethod
#     def has_read_permission(request):
#         return True
# 
#     @staticmethod
#     def has_write_permission(request):
#         return True
# 
#     @staticmethod
#     def has_list_permission(request):
#         return request.user.has_perm('threads.list_message')
# 
#     @staticmethod
#     def has_create_permission(request):
#         return request.user.has_perm('threads.add_message')
# 
#     def has_object_retrieve_permission(self, request):
#         return request.user.has_perm('threads.view_message', self)
# 
#     def has_object_update_permission(self, request):
#         return request.user.has_perm('threads.change_message', self)
# 
#     def has_object_destroy_permission(self, request):
#         return request.user.has_perm('threads.delete_message', self)
# 
#     # Boilerplate str/repr ----------------------------------------------------
# 
#     def __str__(self):
#         return 'Message({}, {}, {})'.format(self.title, self.author.username,
#                                             self.body)


# Path: flowcelltool/threads/views.py
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.shortcuts import redirect, get_object_or_404
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Field
from .models import Message
from . import forms

    """CBV for updating messages"""

    #: The Model type to handle
    model = Message

    #: The fields to show in the form
    fields = ['title', 'body']

    def get_success_url(self):
        """Return absolute URL of the related object"""
        return self.object.thread_object.get_absolute_url()

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        # Setup djangocrispy-forms helper
        context = super().get_context_data(**kwargs)
        context['related_object'] = self.object.thread_object
        context['helper'] = FormHelper()
        context['helper'].form_tag = False
        context['helper'].layout = Layout(
            Field('title'),
            Field('body'),
        )
        return context


class MessageDeleteView(UuidViewMixin, DeleteView):
    """CBV for deleting messages"""

    #: The Model type to handle

    model = Message