# 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

    form = EventReviewForm(data=request.POST)
    if form.is_valid():
        review = services.create_event_review(request.user, event, form.cleaned_data)
        services.update_event_visibility(review)
        services.send_event_review_mail(review, request.user)
        return HttpResponseRedirect(reverse('home'))

    ctx = {
        'event': event,
        'review_url': services.get_event_review_url(event),
        'form': form
    }
    return render(request, event_detail_template, ctx)


class EventCreateView(CreateView):
    form_class = EventForm
    template_name = 'events/event_form.html'

    def form_valid(self, form):
        """
        If the form is valid, save the associated model.
        """
        response = super().form_valid(form)
        services.send_confirmation_mail(self.object, False)
        return response


class EventUpdateView(UpdateView):

# 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


    form = EventReviewForm(data=request.POST)
    if form.is_valid():
        review = services.create_event_review(request.user, event, form.cleaned_data)
        services.update_event_visibility(review)
        services.send_event_review_mail(review, request.user)
        return HttpResponseRedirect(reverse('home'))

    ctx = {
        'event': event,
        'review_url': services.get_event_review_url(event),
        'form': form
    }
    return render(request, event_detail_template, ctx)


class EventCreateView(CreateView):
    form_class = EventForm
    template_name = 'events/event_form.html'

    def form_valid(self, form):
        """
        If the form is valid, save the associated model.
        """
        response = super().form_valid(form)
        services.send_confirmation_mail(self.object, False)
        return response


class EventUpdateView(UpdateView):

    form_class = EventForm