# Path: fossevents/events/decorators.py
# def match_token(function):
#     """
#     Decorator for views that checks that the user is authorized using the token.
#     """
# 
#     def wrapper(request, *args, **kwargs):
#         token = kwargs.get('token', '')
#         event_id = kwargs.get('pk', '')
#         event = models.Event.objects.filter(id=event_id, auth_token=token)
#         if not event.exists():
#             return HttpResponseRedirect(reverse('home'))
#         return function(request, *args, **kwargs)
#     return wrapper
# 
# Path: fossevents/events/forms.py
# class EventForm(forms.ModelForm):
#     description = forms.CharField(widget=forms.Textarea(attrs={'placeholder': 'Enter description...'},),
#                                   help_text='<a href="https://guides.github.com/features/mastering-markdown/">'
#                                             'Markdown supported</a>')
#     start_date = forms.DateTimeField(input_formats=["%Y-%m-%d", "%Y-%m-%d %H:%M"],
#                                      widget=forms.DateTimeInput(attrs={'placeholder': 'YYYY-MM-DD HH:MM',
#                                                                        'type': 'datetime'},
#                                                                 format='%Y-%m-%d %H:%M'))
#     end_date = forms.DateTimeField(input_formats=["%Y-%m-%d", "%Y-%m-%d %H:%M"],
#                                    widget=forms.DateTimeInput(attrs={'placeholder': 'YYYY-MM-DD HH:MM',
#                                                                      'type': 'datetime'},
#                                                               format='%Y-%m-%d %H:%M'))
#     homepage = forms.URLField(widget=forms.URLInput(attrs={'placeholder': 'http://example.com', 'type': 'text'}))
# 
#     class Meta:
#         model = Event
#         exclude = ('auth_token', 'is_published')
#         widgets = {
#             'name': forms.TextInput(attrs={'placeholder': 'Title'}),
#             'owner_email': forms.EmailInput(attrs={'placeholder': 'user@example.com'}),
#         }
# 
#     def clean(self):
#         cleaned_data = super().clean()
#         start_date = cleaned_data.get('start_date')
#         end_date = cleaned_data.get('end_date')
#         if end_date and start_date:
#             if end_date < start_date:
#                 self.add_error('end_date',
#                                forms.ValidationError('End date should be greater than or equal to start date.'))
#         return cleaned_data
# 
# class EventReviewForm(forms.ModelForm):
#     comment = forms.CharField(widget=forms.Textarea(attrs={'placeholder': 'Enter notes or comments...', 'rows': 2},),
#                               help_text='<a href="https://guides.github.com/features/mastering-markdown/">'
#                                         'Markdown supported</a>',
#                               required=False)
# 
#     class Meta:
#         model = EventReview
#         exclude = ('event', 'moderator')
#         widgets = {'is_approved': forms.HiddenInput()}
# 
#     def clean(self):
#         cleaned_data = super().clean()
#         is_approved = cleaned_data.get('is_approved')
#         comment = cleaned_data.get('comment')
#         if not is_approved and not comment:
#             self.add_error('comment', forms.ValidationError('Reject reason is required.'))
#         return cleaned_data


from django.contrib.auth.decorators import login_required, user_passes_test
from django.core.urlresolvers import reverse
from django.views.decorators.http import require_http_methods
from django.http.response import HttpResponseRedirect
from django.utils.decorators import method_decorator
from django.views.generic import CreateView, UpdateView
from django.shortcuts import get_object_or_404, redirect, render
from . import models
from .decorators import match_token
from .forms import EventForm, EventReviewForm
from . import services
# -*- coding: utf-8 -*-




def event_detail(request, pk, slug=None, template_name='events/event_detail.html'):
    event = get_object_or_404(models.Event, pk=pk)
    if event.slug != slug:
        return redirect(event.get_absolute_url())
    ctx = {
        'event': event,
    }
    if not request.user.is_anonymous() and request.user.is_moderator:
        ctx['review_url'] = services.get_event_review_url(event)
        ctx['form'] = EventReviewForm()

    return render(request, template_name, ctx)


@user_passes_test(lambda u: u.is_moderator)
@login_required()
@require_http_methods(['POST'])
def event_review(request, pk, token, slug=None, event_detail_template='events/event_detail.html'):
    # TODO: Add reference in email for analytics
    # TODO: Add history of reviews in review page
    event = get_object_or_404(models.Event, pk=pk)
    if event.slug != slug:
        return redirect(event.get_absolute_url())


# Path: fossevents/events/decorators.py
# def match_token(function):
#     """
#     Decorator for views that checks that the user is authorized using the token.
#     """
# 
#     def wrapper(request, *args, **kwargs):
#         token = kwargs.get('token', '')
#         event_id = kwargs.get('pk', '')
#         event = models.Event.objects.filter(id=event_id, auth_token=token)
#         if not event.exists():
#             return HttpResponseRedirect(reverse('home'))
#         return function(request, *args, **kwargs)
#     return wrapper
# 
# Path: fossevents/events/forms.py
# class EventForm(forms.ModelForm):
#     description = forms.CharField(widget=forms.Textarea(attrs={'placeholder': 'Enter description...'},),
#                                   help_text='<a href="https://guides.github.com/features/mastering-markdown/">'
#                                             'Markdown supported</a>')
#     start_date = forms.DateTimeField(input_formats=["%Y-%m-%d", "%Y-%m-%d %H:%M"],
#                                      widget=forms.DateTimeInput(attrs={'placeholder': 'YYYY-MM-DD HH:MM',
#                                                                        'type': 'datetime'},
#                                                                 format='%Y-%m-%d %H:%M'))
#     end_date = forms.DateTimeField(input_formats=["%Y-%m-%d", "%Y-%m-%d %H:%M"],
#                                    widget=forms.DateTimeInput(attrs={'placeholder': 'YYYY-MM-DD HH:MM',
#                                                                      'type': 'datetime'},
#                                                               format='%Y-%m-%d %H:%M'))
#     homepage = forms.URLField(widget=forms.URLInput(attrs={'placeholder': 'http://example.com', 'type': 'text'}))
# 
#     class Meta:
#         model = Event
#         exclude = ('auth_token', 'is_published')
#         widgets = {
#             'name': forms.TextInput(attrs={'placeholder': 'Title'}),
#             'owner_email': forms.EmailInput(attrs={'placeholder': 'user@example.com'}),
#         }
# 
#     def clean(self):
#         cleaned_data = super().clean()
#         start_date = cleaned_data.get('start_date')
#         end_date = cleaned_data.get('end_date')
#         if end_date and start_date:
#             if end_date < start_date:
#                 self.add_error('end_date',
#                                forms.ValidationError('End date should be greater than or equal to start date.'))
#         return cleaned_data
# 
# class EventReviewForm(forms.ModelForm):
#     comment = forms.CharField(widget=forms.Textarea(attrs={'placeholder': 'Enter notes or comments...', 'rows': 2},),
#                               help_text='<a href="https://guides.github.com/features/mastering-markdown/">'
#                                         'Markdown supported</a>',
#                               required=False)
# 
#     class Meta:
#         model = EventReview
#         exclude = ('event', 'moderator')
#         widgets = {'is_approved': forms.HiddenInput()}
# 
#     def clean(self):
#         cleaned_data = super().clean()
#         is_approved = cleaned_data.get('is_approved')
#         comment = cleaned_data.get('comment')
#         if not is_approved and not comment:
#             self.add_error('comment', forms.ValidationError('Reject reason is required.'))
#         return cleaned_data


# Path: fossevents/events/views.py
from django.contrib.auth.decorators import login_required, user_passes_test
from django.core.urlresolvers import reverse
from django.views.decorators.http import require_http_methods
from django.http.response import HttpResponseRedirect
from django.utils.decorators import method_decorator
from django.views.generic import CreateView, UpdateView
from django.shortcuts import get_object_or_404, redirect, render
from . import models
from .decorators import match_token
from .forms import EventForm, EventReviewForm
from . import services

# -*- coding: utf-8 -*-




def event_detail(request, pk, slug=None, template_name='events/event_detail.html'):
    event = get_object_or_404(models.Event, pk=pk)
    if event.slug != slug:
        return redirect(event.get_absolute_url())
    ctx = {
        'event': event,
    }
    if not request.user.is_anonymous() and request.user.is_moderator:
        ctx['review_url'] = services.get_event_review_url(event)
        ctx['form'] = EventReviewForm()

    return render(request, template_name, ctx)


@user_passes_test(lambda u: u.is_moderator)
@login_required()
@require_http_methods(['POST'])
def event_review(request, pk, token, slug=None, event_detail_template='events/event_detail.html'):
    # TODO: Add reference in email for analytics
    # TODO: Add history of reviews in review page
    event = get_object_or_404(models.Event, pk=pk)
    if event.slug != slug:
        return redirect(event.get_absolute_url())


    form = EventReviewForm(data=request.POST)