# Path: warp/users/models.py
# class User(AbstractUser):
# 
#     # First Name and Last Name do not cover name patterns
#     # around the globe.
#     name = models.CharField(_('Name of User'), blank=True, max_length=255)
# 
#     def __str__(self):
#         return self.username
# 
#     def get_profile_image_url(self):
#         social_image = None
#         if self.socialaccount_set.all():
#             social_image = self.socialaccount_set.all()[0].get_avatar_url()
#         if social_image:
#             return social_image
#         return '/static/images/default_profile.svg'
# 
#     def get_absolute_url(self):
#         return reverse('users:detail', kwargs={'username': self.username})
# 
# Path: presentation/forms.py
# class PresentationCreateForm(PresentationBaseForm):
#     def save(self, commit=True):
#         instance = super(PresentationCreateForm, self).save(commit=False)
#         instance.author = self.author
#         instance.save()
# 
#         self.add_slide_list(instance)
#         return instance
# 
# class PresentationUpdateForm(PresentationBaseForm):
#     def save(self, commit=True):
#         instance = super(PresentationUpdateForm, self).save(commit=False)
#         instance.subject = self.cleaned_data.get('subject')
#         instance.is_public = self.cleaned_data.get('is_public')
#         instance.author = self.author
#         instance.save()
# 
#         self.add_slide_list(instance)
#         return instance
# 
# 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/behaviors.py
# class AuthorRequiredMixin(object):
#     def dispatch(self, request, *args, **kwargs):
#         if self.get_object().author != self.request.user:
#             return HttpResponseForbidden()
#         return super(AuthorRequiredMixin, self).dispatch(request, *args, **kwargs)


from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponse
from django.http import HttpResponseNotAllowed
from django.core.urlresolvers import reverse, reverse_lazy
from django.shortcuts import render
from django.views.generic import DeleteView
from django.views.generic import UpdateView
from django.views.generic import CreateView
from django.views.generic import DetailView
from django.views.generic import ListView
from pure_pagination import PaginationMixin
from warp.users.models import User
from .forms import PresentationCreateForm, PresentationUpdateForm
from .models import Presentation, Slide
from .behaviors import AuthorRequiredMixin
class PresentationDetail(DetailView):
    model = Presentation
    context_object_name = 'presentation'

    def get_context_data(self, **kwargs):
        context = super(PresentationDetail, self).get_context_data()
        context['is_user_liked'] = self.get_object().is_user_like_presentation(self.request.user)
        self.object.view()
        return context


class PresentationCreate(LoginRequiredMixin, CreateView):
    model = Presentation
    form_class = PresentationCreateForm

    def get_context_data(self, **kwargs):
        context = super(PresentationCreate, self).get_context_data()
        context['action_url'] = reverse('presentation:create')
        context['submit_button_text'] = 'Create'
        return context

    def form_valid(self, form):
        form.author = self.request.user
        form.cleaned_data['author'] = User.objects.get(username=form.author.username)
        form.save()
        form.save_m2m()
        return super(PresentationCreate, self).form_valid(form)


class PresentationUpdate(AuthorRequiredMixin, LoginRequiredMixin, UpdateView):

# Path: warp/users/models.py
# class User(AbstractUser):
# 
#     # First Name and Last Name do not cover name patterns
#     # around the globe.
#     name = models.CharField(_('Name of User'), blank=True, max_length=255)
# 
#     def __str__(self):
#         return self.username
# 
#     def get_profile_image_url(self):
#         social_image = None
#         if self.socialaccount_set.all():
#             social_image = self.socialaccount_set.all()[0].get_avatar_url()
#         if social_image:
#             return social_image
#         return '/static/images/default_profile.svg'
# 
#     def get_absolute_url(self):
#         return reverse('users:detail', kwargs={'username': self.username})
# 
# Path: presentation/forms.py
# class PresentationCreateForm(PresentationBaseForm):
#     def save(self, commit=True):
#         instance = super(PresentationCreateForm, self).save(commit=False)
#         instance.author = self.author
#         instance.save()
# 
#         self.add_slide_list(instance)
#         return instance
# 
# class PresentationUpdateForm(PresentationBaseForm):
#     def save(self, commit=True):
#         instance = super(PresentationUpdateForm, self).save(commit=False)
#         instance.subject = self.cleaned_data.get('subject')
#         instance.is_public = self.cleaned_data.get('is_public')
#         instance.author = self.author
#         instance.save()
# 
#         self.add_slide_list(instance)
#         return instance
# 
# 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/behaviors.py
# class AuthorRequiredMixin(object):
#     def dispatch(self, request, *args, **kwargs):
#         if self.get_object().author != self.request.user:
#             return HttpResponseForbidden()
#         return super(AuthorRequiredMixin, self).dispatch(request, *args, **kwargs)


# Path: presentation/views.py
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponse
from django.http import HttpResponseNotAllowed
from django.core.urlresolvers import reverse, reverse_lazy
from django.shortcuts import render
from django.views.generic import DeleteView
from django.views.generic import UpdateView
from django.views.generic import CreateView
from django.views.generic import DetailView
from django.views.generic import ListView
from pure_pagination import PaginationMixin
from warp.users.models import User
from .forms import PresentationCreateForm, PresentationUpdateForm
from .models import Presentation, Slide
from .behaviors import AuthorRequiredMixin

class PresentationDetail(DetailView):
    model = Presentation
    context_object_name = 'presentation'

    def get_context_data(self, **kwargs):
        context = super(PresentationDetail, self).get_context_data()
        context['is_user_liked'] = self.get_object().is_user_like_presentation(self.request.user)
        self.object.view()
        return context


class PresentationCreate(LoginRequiredMixin, CreateView):
    model = Presentation
    form_class = PresentationCreateForm

    def get_context_data(self, **kwargs):
        context = super(PresentationCreate, self).get_context_data()
        context['action_url'] = reverse('presentation:create')
        context['submit_button_text'] = 'Create'
        return context

    def form_valid(self, form):
        form.author = self.request.user
        form.cleaned_data['author'] = User.objects.get(username=form.author.username)
        form.save()
        form.save_m2m()
        return super(PresentationCreate, self).form_valid(form)


class PresentationUpdate(AuthorRequiredMixin, LoginRequiredMixin, UpdateView):

    model = Presentation