# Path: presentation/models.py
# class Presentation(TimeStampedModel):
#     subject = models.CharField(max_length=50)
#     author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='author')
#     views = models.IntegerField(default=0)
#     likes = models.IntegerField(default=0)
#     is_public = models.BooleanField(default=True)
#     tags = TaggableManager()
# 
#     like_users = models.ManyToManyField(User, related_name='like_users')
# 
#     def like_toggle(self, user):
#         is_like = self.is_user_like_presentation(user)
#         if is_like:
#             self.like_users.remove(user)
#         else:
#             self.like_users.add(user)
# 
#         result_like = not is_like
#         self.likes = self.like_users.count()
#         self.save()
#         return result_like
# 
#     def is_user_like_presentation(self, user):
#         is_like = False
#         try:
#             self.like_users.get(id=user.id)
#             is_like = True
#         except User.DoesNotExist:
#             pass
#         return is_like
# 
#     objects = PresentationManager()
# 
#     def view(self):
#         self.views += 1
#         self.save()
# 
#     def __str__(self):
#         return self.subject
# 
#     def get_absolute_url(self, *args, **kwargs):
#         return reverse('presentation:detail', kwargs={'pk': self.pk})
# 
# class Slide(TimeStampedModel):
#     presentation = models.ForeignKey(Presentation, on_delete=models.CASCADE)
#     slide_order = models.PositiveSmallIntegerField()
#     markdown = models.TextField()
# 
#     def __str__(self):
#         return self.markdown


import re
from django import forms
from django.core.exceptions import ValidationError
from django.forms import RadioSelect
from .models import Presentation, Slide




class PresentationBaseForm(forms.ModelForm):
    subject = forms.CharField(max_length=50)
    markdown = forms.CharField(widget=forms.HiddenInput(), required=True)
    is_public = forms.BooleanField(initial=True,
                                   required=False,
                                   widget=RadioSelect(choices=[
                                       (True, 'public'), (False, 'private')]))

    def clean_tags(self):
        tags = self.cleaned_data['tags']
        if len(tags) > 20:
            raise ValidationError("Too much tags")
        for tag in tags:
            if len(tag) > 16:
                raise ValidationError("Too long tag")

        return tags

    def add_slide_list(self, presentation):
        markdown = self.cleaned_data.get('markdown')
        slide_list = re.split("={5,}", markdown)
        Slide.objects.filter(presentation=presentation).delete()

        for order_num, slide in enumerate(slide_list):

# Path: presentation/models.py
# class Presentation(TimeStampedModel):
#     subject = models.CharField(max_length=50)
#     author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='author')
#     views = models.IntegerField(default=0)
#     likes = models.IntegerField(default=0)
#     is_public = models.BooleanField(default=True)
#     tags = TaggableManager()
# 
#     like_users = models.ManyToManyField(User, related_name='like_users')
# 
#     def like_toggle(self, user):
#         is_like = self.is_user_like_presentation(user)
#         if is_like:
#             self.like_users.remove(user)
#         else:
#             self.like_users.add(user)
# 
#         result_like = not is_like
#         self.likes = self.like_users.count()
#         self.save()
#         return result_like
# 
#     def is_user_like_presentation(self, user):
#         is_like = False
#         try:
#             self.like_users.get(id=user.id)
#             is_like = True
#         except User.DoesNotExist:
#             pass
#         return is_like
# 
#     objects = PresentationManager()
# 
#     def view(self):
#         self.views += 1
#         self.save()
# 
#     def __str__(self):
#         return self.subject
# 
#     def get_absolute_url(self, *args, **kwargs):
#         return reverse('presentation:detail', kwargs={'pk': self.pk})
# 
# class Slide(TimeStampedModel):
#     presentation = models.ForeignKey(Presentation, on_delete=models.CASCADE)
#     slide_order = models.PositiveSmallIntegerField()
#     markdown = models.TextField()
# 
#     def __str__(self):
#         return self.markdown


# Path: presentation/forms.py
import re
from django import forms
from django.core.exceptions import ValidationError
from django.forms import RadioSelect
from .models import Presentation, Slide





class PresentationBaseForm(forms.ModelForm):
    subject = forms.CharField(max_length=50)
    markdown = forms.CharField(widget=forms.HiddenInput(), required=True)
    is_public = forms.BooleanField(initial=True,
                                   required=False,
                                   widget=RadioSelect(choices=[
                                       (True, 'public'), (False, 'private')]))

    def clean_tags(self):
        tags = self.cleaned_data['tags']
        if len(tags) > 20:
            raise ValidationError("Too much tags")
        for tag in tags:
            if len(tag) > 16:
                raise ValidationError("Too long tag")

        return tags

    def add_slide_list(self, presentation):
        markdown = self.cleaned_data.get('markdown')
        slide_list = re.split("={5,}", markdown)
        Slide.objects.filter(presentation=presentation).delete()

        for order_num, slide in enumerate(slide_list):

            Slide.objects.create(