# Path: della/gallery/models.py
# class Image(TimeStampMixin):
#     file = models.ImageField(upload_to='images/%Y/%m/%d')
#     title = models.CharField(max_length=300)
#     description = models.TextField(null=True)
#     added_by = models.ForeignKey(User, on_delete=models.CASCADE)
# 
# Path: della/gallery/forms.py
# class ImageUploadForm(ModelForm):
# 
#     def __init__(self, *args, **kwargs):
#         super().__init__(*args, **kwargs)
# 
#         # make description optional field
#         self.fields['description'].required = False
# 
#         self.helper = FormHelper()
#         self.helper.layout = Layout(
#             Fieldset(
#                 'Upload Image',
#                 'title',
#                 'file',
#                 'description'
#             )
#         )
#         self.helper.form_class = 'form-horizontal'
#         self.helper.form_method = 'post'
#         self.helper.form_action = 'gallery:upload'
#         self.helper.label_class = 'col-lg-2'
#         self.helper.field_class = 'col-lg-10'
#         self.helper.add_input(Reset('reset', 'Cancel'))
#         self.helper.add_input(Submit('submit', 'Submit'))
# 
#     class Meta:
#         model = Image
#         fields = ['title', 'file', 'description']


from django.views.generic.edit import CreateView
from django.views.generic import DetailView, ListView
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.shortcuts import reverse
from .models import Image
from .forms import ImageUploadForm



@method_decorator(login_required, name='dispatch')
class ImageUploadView(CreateView):
    model = Image
    form_class = ImageUploadForm
    template_name = 'generic_crispy_form_template.html'

    def form_valid(self, form):
        image = form.save(commit=False)
        image.added_by = self.request.user
        return super().form_valid(form)

    def get_success_url(self):
        return reverse('gallery:image-detail', args=(self.object.id,))


class ImageDetailView(DetailView):
    model = Image


class ImageListView(ListView):

# Path: della/gallery/models.py
# class Image(TimeStampMixin):
#     file = models.ImageField(upload_to='images/%Y/%m/%d')
#     title = models.CharField(max_length=300)
#     description = models.TextField(null=True)
#     added_by = models.ForeignKey(User, on_delete=models.CASCADE)
# 
# Path: della/gallery/forms.py
# class ImageUploadForm(ModelForm):
# 
#     def __init__(self, *args, **kwargs):
#         super().__init__(*args, **kwargs)
# 
#         # make description optional field
#         self.fields['description'].required = False
# 
#         self.helper = FormHelper()
#         self.helper.layout = Layout(
#             Fieldset(
#                 'Upload Image',
#                 'title',
#                 'file',
#                 'description'
#             )
#         )
#         self.helper.form_class = 'form-horizontal'
#         self.helper.form_method = 'post'
#         self.helper.form_action = 'gallery:upload'
#         self.helper.label_class = 'col-lg-2'
#         self.helper.field_class = 'col-lg-10'
#         self.helper.add_input(Reset('reset', 'Cancel'))
#         self.helper.add_input(Submit('submit', 'Submit'))
# 
#     class Meta:
#         model = Image
#         fields = ['title', 'file', 'description']


# Path: della/gallery/views.py
from django.views.generic.edit import CreateView
from django.views.generic import DetailView, ListView
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.shortcuts import reverse
from .models import Image
from .forms import ImageUploadForm




@method_decorator(login_required, name='dispatch')
class ImageUploadView(CreateView):
    model = Image
    form_class = ImageUploadForm
    template_name = 'generic_crispy_form_template.html'

    def form_valid(self, form):
        image = form.save(commit=False)
        image.added_by = self.request.user
        return super().form_valid(form)

    def get_success_url(self):
        return reverse('gallery:image-detail', args=(self.object.id,))


class ImageDetailView(DetailView):
    model = Image


class ImageListView(ListView):

    model = Image