# Path: twobuntu/images/forms.py
# class ImageUploadForm(forms.ModelForm):
#     """
#     Form for uploading images.
#     """
# 
#     class Meta:
#         model = Image
#         fields = ('caption', 'image')
# 
# Path: twobuntu/images/models.py
# class Image(models.Model):
#     """
#     A web-accessible image file.
#     """
# 
#     caption = models.CharField(
#         max_length=100,
#         help_text="A brief description of the image.",
#     )
#     image = models.ImageField(
#         upload_to='images',
#         help_text="The image file.",
#     )
# 
#     def __str__(self):
#         return self.caption
# 
#     @models.permalink
#     def get_absolute_url(self):
#         return ('images:view', (), {
#             'id': self.id,
#         })


from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect, render
from twobuntu.images.forms import ImageUploadForm
from twobuntu.images.models import Image



@login_required
def view(request, id):
    """
    View uploaded image.
    """
    image = get_object_or_404(Image, pk=id)
    return render(request, 'images/view.html', {
        'title': image.caption,
        'image': image,
    })


@login_required
def upload(request):
    """
    Upload images for embedding in articles.
    """
    js = 'js' in request.GET
    if request.method == 'POST':
        form = ImageUploadForm(data=request.POST, files=request.FILES)
        if form.is_valid():
            image = form.save()
            if js:
                return HttpResponse('<script>window.opener.editor.insertImage("[image:%d]");window.close();</script>' % image.id)
            else:
                return redirect(image)
    else:

# Path: twobuntu/images/forms.py
# class ImageUploadForm(forms.ModelForm):
#     """
#     Form for uploading images.
#     """
# 
#     class Meta:
#         model = Image
#         fields = ('caption', 'image')
# 
# Path: twobuntu/images/models.py
# class Image(models.Model):
#     """
#     A web-accessible image file.
#     """
# 
#     caption = models.CharField(
#         max_length=100,
#         help_text="A brief description of the image.",
#     )
#     image = models.ImageField(
#         upload_to='images',
#         help_text="The image file.",
#     )
# 
#     def __str__(self):
#         return self.caption
# 
#     @models.permalink
#     def get_absolute_url(self):
#         return ('images:view', (), {
#             'id': self.id,
#         })


# Path: twobuntu/images/views.py
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect, render
from twobuntu.images.forms import ImageUploadForm
from twobuntu.images.models import Image




@login_required
def view(request, id):
    """
    View uploaded image.
    """
    image = get_object_or_404(Image, pk=id)
    return render(request, 'images/view.html', {
        'title': image.caption,
        'image': image,
    })


@login_required
def upload(request):
    """
    Upload images for embedding in articles.
    """
    js = 'js' in request.GET
    if request.method == 'POST':
        form = ImageUploadForm(data=request.POST, files=request.FILES)
        if form.is_valid():
            image = form.save()
            if js:
                return HttpResponse('<script>window.opener.editor.insertImage("[image:%d]");window.close();</script>' % image.id)
            else:
                return redirect(image)
    else:

        form = ImageUploadForm()