# Path: events/forms.py
# class EventCreateForm(forms.ModelForm):
#     name = forms.CharField(label="Name", help_text="Enter name of event",
#                            widget=forms.TextInput(attrs={'placeholder': "Name"}))
#     start_date = forms.DateTimeField(label="Enter start date and time",
#                                     widget=forms.DateTimeInput())
#     end_date = forms.DateTimeField(label="Enter end date and time",
#                                   widget=forms.DateTimeInput())
#     description = forms.CharField(label="Description", help_text="Description",
#                                  widget=forms.Textarea(attrs={'placeholder': "Description"}))
#     venue = forms.CharField(label="Venue", help_text="Venue",
#                             widget=forms.TextInput(attrs={'placeholder': 'Venue'}))
#     trainer_bio = forms.CharField(label="Trainer bio", help_text="Enter trainer bio")
#     no_of_participants = forms.IntegerField(label="No of participants", help_text="Enter no of participants")
#     level = forms.ChoiceField(choices=LEVEL_CHOICES, label="Level")
#     prerequisite = forms.CharField(label="Prerequisite")
#     travel = forms.CharField(label="Travel info", help_text="Enter travel data")
#     accommodation = forms.CharField(label="Accomodation")
#     expense = forms.FloatField(label="Expenses")
#     lab_requirements = forms.CharField(label='Lab Requirements')
#     icts_support = forms.CharField(label="ICTS support")
# 
#     def __init__(self, *args, **kwargs):
#         super(EventCreateForm, self).__init__(*args, **kwargs)
#         for field in iter(self.fields):
#             self.fields[field].widget.attrs.update({'class': 'form-control'})
#     class Meta:
#         model = Event
#         fields = ['name', 'start_date', 'end_date', 'description', 'venue', 'trainer_bio', 'no_of_participants',
#                   'level', 'prerequisite', 'travel', 'accommodation', 'expense', 'lab_requirements', 'icts_support']
# 
# class EventImageForm(forms.ModelForm):
#     event = forms.ModelChoiceField(queryset=Event.objects.all(), label='Event', help_text='Select event')
#     image = forms.ImageField(label='images',
#                              widget=forms.FileInput())
# 
#     def __init__(self, *args, **kwargs):
#         super(EventImageForm, self).__init__(*args, **kwargs)
#         for field in iter(self.fields):
#             self.fields[field].widget.attrs.update({'class': 'form-control'})
# 
#     class Meta:
#         model = EventImage
#         fields = ['event', 'image']
# 
# Path: events/models.py
# class Event(models.Model):
#     created_by = models.ForeignKey(User, on_delete=models.CASCADE)
#     name = models.CharField(max_length=300)
#     start_date = models.DateField(blank=True)
#     end_date = models.DateField(blank=True)
#     description = models.TextField(blank=True)
#     venue = models.CharField(max_length=300, blank=True)
# 
#     trainer_bio = models.TextField(blank=True)
#     no_of_participants = models.IntegerField(blank=True, default=0)
#     level = models.CharField(choices=LEVEL_CHOICES, max_length=100)
#     prerequisite = models.TextField(blank=True)
# 
#     travel = models.TextField(blank=True)
#     accommodation = models.TextField(blank=True)
#     expense = models.FloatField(blank=True, default=0)
#     lab_requirements = models.TextField(blank=True)
#     icts_support = models.TextField(blank=True)
# 
#     permissions = models.TextField(blank=True)
#     date_added = models.DateField(auto_now_add=True)
# 
#     is_approved = models.BooleanField(default=False)
#     is_published = models.BooleanField(default=False)
# 
#     def __str__(self):
#         return self.name
# 
# class EventImage(models.Model):
#     event = models.ForeignKey(Event, on_delete=models.CASCADE)
#     image = models.ImageField(upload_to='events/images/')
# 
#     def __str__(self):
#         return self.event.name


from django.shortcuts import render, redirect
from django.urls import reverse_lazy
from django.views.generic import CreateView, UpdateView, DeleteView, ListView, DetailView
from events.forms import EventCreateForm, EventImageForm
from events.models import Event, EventImage
    def form_valid(self, form):
        form.instance.user = self.request.user
        return super(EventCreateView, self).form_valid(form)

class EventUpdateView(UpdateView):
    form_class = EventCreateForm
    template_name = 'base/form.html'
    model = Event

    def get(self, request, *args, **kwargs):
        if not (request.user.is_superuser or request.user == self.get_object().user):
            redirect('permission_denied')
        return super(EventUpdateView, self).get(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(EventUpdateView, self).get_context_data(**kwargs)
        context['heading'] = 'Update Event'
        context['title'] = 'Events'
        return context

    def form_valid(self, form):
        form.instance.user = self.request.user
        return super(EventUpdateView, self).form_valid(form)

    def post(self, request, *args, **kwargs):
        if not (request.user.is_superuser or request.user == self.get_object().user):
            redirect('permission_denied')
        return super(EventUpdateView, self).post(request, *args, **kwargs)

class EventDeleteView(DeleteView):

# Path: events/forms.py
# class EventCreateForm(forms.ModelForm):
#     name = forms.CharField(label="Name", help_text="Enter name of event",
#                            widget=forms.TextInput(attrs={'placeholder': "Name"}))
#     start_date = forms.DateTimeField(label="Enter start date and time",
#                                     widget=forms.DateTimeInput())
#     end_date = forms.DateTimeField(label="Enter end date and time",
#                                   widget=forms.DateTimeInput())
#     description = forms.CharField(label="Description", help_text="Description",
#                                  widget=forms.Textarea(attrs={'placeholder': "Description"}))
#     venue = forms.CharField(label="Venue", help_text="Venue",
#                             widget=forms.TextInput(attrs={'placeholder': 'Venue'}))
#     trainer_bio = forms.CharField(label="Trainer bio", help_text="Enter trainer bio")
#     no_of_participants = forms.IntegerField(label="No of participants", help_text="Enter no of participants")
#     level = forms.ChoiceField(choices=LEVEL_CHOICES, label="Level")
#     prerequisite = forms.CharField(label="Prerequisite")
#     travel = forms.CharField(label="Travel info", help_text="Enter travel data")
#     accommodation = forms.CharField(label="Accomodation")
#     expense = forms.FloatField(label="Expenses")
#     lab_requirements = forms.CharField(label='Lab Requirements')
#     icts_support = forms.CharField(label="ICTS support")
# 
#     def __init__(self, *args, **kwargs):
#         super(EventCreateForm, self).__init__(*args, **kwargs)
#         for field in iter(self.fields):
#             self.fields[field].widget.attrs.update({'class': 'form-control'})
#     class Meta:
#         model = Event
#         fields = ['name', 'start_date', 'end_date', 'description', 'venue', 'trainer_bio', 'no_of_participants',
#                   'level', 'prerequisite', 'travel', 'accommodation', 'expense', 'lab_requirements', 'icts_support']
# 
# class EventImageForm(forms.ModelForm):
#     event = forms.ModelChoiceField(queryset=Event.objects.all(), label='Event', help_text='Select event')
#     image = forms.ImageField(label='images',
#                              widget=forms.FileInput())
# 
#     def __init__(self, *args, **kwargs):
#         super(EventImageForm, self).__init__(*args, **kwargs)
#         for field in iter(self.fields):
#             self.fields[field].widget.attrs.update({'class': 'form-control'})
# 
#     class Meta:
#         model = EventImage
#         fields = ['event', 'image']
# 
# Path: events/models.py
# class Event(models.Model):
#     created_by = models.ForeignKey(User, on_delete=models.CASCADE)
#     name = models.CharField(max_length=300)
#     start_date = models.DateField(blank=True)
#     end_date = models.DateField(blank=True)
#     description = models.TextField(blank=True)
#     venue = models.CharField(max_length=300, blank=True)
# 
#     trainer_bio = models.TextField(blank=True)
#     no_of_participants = models.IntegerField(blank=True, default=0)
#     level = models.CharField(choices=LEVEL_CHOICES, max_length=100)
#     prerequisite = models.TextField(blank=True)
# 
#     travel = models.TextField(blank=True)
#     accommodation = models.TextField(blank=True)
#     expense = models.FloatField(blank=True, default=0)
#     lab_requirements = models.TextField(blank=True)
#     icts_support = models.TextField(blank=True)
# 
#     permissions = models.TextField(blank=True)
#     date_added = models.DateField(auto_now_add=True)
# 
#     is_approved = models.BooleanField(default=False)
#     is_published = models.BooleanField(default=False)
# 
#     def __str__(self):
#         return self.name
# 
# class EventImage(models.Model):
#     event = models.ForeignKey(Event, on_delete=models.CASCADE)
#     image = models.ImageField(upload_to='events/images/')
# 
#     def __str__(self):
#         return self.event.name


# Path: events/views.py
from django.shortcuts import render, redirect
from django.urls import reverse_lazy
from django.views.generic import CreateView, UpdateView, DeleteView, ListView, DetailView
from events.forms import EventCreateForm, EventImageForm
from events.models import Event, EventImage

    def form_valid(self, form):
        form.instance.user = self.request.user
        return super(EventCreateView, self).form_valid(form)

class EventUpdateView(UpdateView):
    form_class = EventCreateForm
    template_name = 'base/form.html'
    model = Event

    def get(self, request, *args, **kwargs):
        if not (request.user.is_superuser or request.user == self.get_object().user):
            redirect('permission_denied')
        return super(EventUpdateView, self).get(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(EventUpdateView, self).get_context_data(**kwargs)
        context['heading'] = 'Update Event'
        context['title'] = 'Events'
        return context

    def form_valid(self, form):
        form.instance.user = self.request.user
        return super(EventUpdateView, self).form_valid(form)

    def post(self, request, *args, **kwargs):
        if not (request.user.is_superuser or request.user == self.get_object().user):
            redirect('permission_denied')
        return super(EventUpdateView, self).post(request, *args, **kwargs)

class EventDeleteView(DeleteView):

    model = Event